Skip to content

Commit

Permalink
check: Verify size of documentation files for Terraform Registry stor…
Browse files Browse the repository at this point in the history
…age limits

Closes #20
  • Loading branch information
bflad committed Dec 17, 2019
1 parent 91eff80 commit 5ddf392
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ENHANCEMENTS

* check: Verify number of documentation files for Terraform Registry storage limits
* check: Verify size of documentation files for Terraform Registry storage limits

# v0.1.2

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The `tfproviderdocs check` command verifies the Terraform Provider documentation
The validity of files is checked with the following rules:

- Proper file extensions are used (e.g. `.md` for Terraform Registry).
- Verifies size of file is below Terraform Registry storage limits.
- YAML frontmatter can be parsed and matches expectations.

The YAML frontmatter checks include some defaults (e.g. no `layout` field for Terraform Registry), but there are some useful flags that can be passed to the command to tune the behavior, especially for larger Terraform Providers.
Expand Down
1 change: 1 addition & 0 deletions check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
// Terraform Registry Storage Limits
// https://www.terraform.io/docs/registry/providers/docs.html#storage-limits
RegistryMaximumNumberOfFiles = 1000
RegistryMaximumSizeOfFile = 500000 // 500KB
)

type Check struct {
Expand Down
19 changes: 19 additions & 0 deletions check/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package check

import (
"fmt"
"log"
"os"
"path/filepath"
)

Expand All @@ -20,3 +23,19 @@ func (opts *FileOptions) FullPath(path string) string {

return path
}

// FileSizeCheck verifies that documentation file is below the Terraform Registry storage limit.
func FileSizeCheck(fullpath string) error {
fi, err := os.Stat(fullpath)

if err != nil {
return err
}

log.Printf("[DEBUG] File %s size: %d (limit: %d)", fullpath, fi.Size(), RegistryMaximumSizeOfFile)
if fi.Size() >= int64(RegistryMaximumSizeOfFile) {
return fmt.Errorf("exceeded maximum (%d) size of documentation file for Terraform Registry: %d", RegistryMaximumSizeOfFile, fi.Size())
}

return nil
}
59 changes: 55 additions & 4 deletions check/file_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
package check

import (
"io/ioutil"
"os"
"testing"
)

func TestFileSizeCheck(t *testing.T) {
testCases := []struct {
Name string
Size int64
ExpectError bool
}{
{
Name: "under limit",
Size: RegistryMaximumSizeOfFile - 1,
},
{
Name: "on limit",
Size: RegistryMaximumSizeOfFile,
ExpectError: true,
},
{
Name: "over limit",
Size: RegistryMaximumSizeOfFile + 1,
ExpectError: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "TestFileSizeCheck")

if err != nil {
t.Fatalf("error creating temporary file: %s", err)
}

defer os.Remove(file.Name())

if err := file.Truncate(testCase.Size); err != nil {
t.Fatalf("error writing temporary file: %s", err)
}

got := FileSizeCheck(file.Name())

if got == nil && testCase.ExpectError {
t.Errorf("expected error, got no error")
}

if got != nil && !testCase.ExpectError {
t.Errorf("expected no error, got error: %s", got)
}
})
}
}

func TestFullPath(t *testing.T) {
testCases := []struct {
Name string
Expand All @@ -14,16 +65,16 @@ func TestFullPath(t *testing.T) {
{
Name: "without base path",
FileOptions: &FileOptions{},
Path: "docs/resource/thing.md",
Expect: "docs/resource/thing.md",
Path: "docs/resources/thing.md",
Expect: "docs/resources/thing.md",
},
{
Name: "without base path",
FileOptions: &FileOptions{
BasePath: "/full/path/to",
},
Path: "docs/resource/thing.md",
Expect: "/full/path/to/docs/resource/thing.md",
Path: "docs/resources/thing.md",
Expect: "/full/path/to/docs/resources/thing.md",
},
}

Expand Down
6 changes: 5 additions & 1 deletion check/legacy_data_source_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func (check *LegacyDataSourceFileCheck) Run(path string) error {
log.Printf("[DEBUG] Checking file: %s", fullpath)

if err := LegacyFileExtensionCheck(path); err != nil {
return fmt.Errorf("error checking file (%s) extension: %w", path, err)
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}

if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}

content, err := ioutil.ReadFile(fullpath)
Expand Down
6 changes: 5 additions & 1 deletion check/legacy_guide_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func (check *LegacyGuideFileCheck) Run(path string) error {
log.Printf("[DEBUG] Checking file: %s", fullpath)

if err := LegacyFileExtensionCheck(path); err != nil {
return fmt.Errorf("error checking file (%s) extension: %w", path, err)
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}

if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}

content, err := ioutil.ReadFile(fullpath)
Expand Down
6 changes: 5 additions & 1 deletion check/legacy_index_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func (check *LegacyIndexFileCheck) Run(path string) error {
log.Printf("[DEBUG] Checking file: %s", fullpath)

if err := LegacyFileExtensionCheck(path); err != nil {
return fmt.Errorf("error checking file (%s) extension: %w", path, err)
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}

if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}

content, err := ioutil.ReadFile(fullpath)
Expand Down
6 changes: 5 additions & 1 deletion check/legacy_resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func (check *LegacyResourceFileCheck) Run(path string) error {
log.Printf("[DEBUG] Checking file: %s", fullpath)

if err := LegacyFileExtensionCheck(path); err != nil {
return fmt.Errorf("error checking file (%s) extension: %w", path, err)
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}

if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}

content, err := ioutil.ReadFile(fullpath)
Expand Down
6 changes: 5 additions & 1 deletion check/registry_data_source_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func (check *RegistryDataSourceFileCheck) Run(path string) error {
log.Printf("[DEBUG] Checking file: %s", fullpath)

if err := RegistryFileExtensionCheck(path); err != nil {
return fmt.Errorf("error checking file (%s) extension: %w", path, err)
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}

if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}

content, err := ioutil.ReadFile(fullpath)
Expand Down
6 changes: 5 additions & 1 deletion check/registry_guide_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func (check *RegistryGuideFileCheck) Run(path string) error {
log.Printf("[DEBUG] Checking file: %s", fullpath)

if err := RegistryFileExtensionCheck(path); err != nil {
return fmt.Errorf("error checking file (%s) extension: %w", path, err)
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}

if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}

content, err := ioutil.ReadFile(fullpath)
Expand Down
6 changes: 5 additions & 1 deletion check/registry_index_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func (check *RegistryIndexFileCheck) Run(path string) error {
log.Printf("[DEBUG] Checking file: %s", fullpath)

if err := RegistryFileExtensionCheck(path); err != nil {
return fmt.Errorf("error checking file (%s) extension: %w", path, err)
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}

if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}

content, err := ioutil.ReadFile(fullpath)
Expand Down
6 changes: 5 additions & 1 deletion check/registry_resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func (check *RegistryResourceFileCheck) Run(path string) error {
log.Printf("[DEBUG] Checking file: %s", fullpath)

if err := RegistryFileExtensionCheck(path); err != nil {
return fmt.Errorf("error checking file (%s) extension: %w", path, err)
return fmt.Errorf("%s: error checking file extension: %w", path, err)
}

if err := FileSizeCheck(fullpath); err != nil {
return fmt.Errorf("%s: error checking file size: %w", path, err)
}

content, err := ioutil.ReadFile(fullpath)
Expand Down

0 comments on commit 5ddf392

Please sign in to comment.