From 128fcf993b2164c0d8429627b183fd21cd9ca1f2 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 2 Jan 2021 11:57:52 +0100 Subject: [PATCH] fail early when cache folder does not exist --- acceptance.bats | 14 ++++++++++++++ pkg/registry/http.go | 13 +++++++++++-- pkg/registry/local.go | 4 ++-- pkg/registry/registry.go | 4 ++-- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/acceptance.bats b/acceptance.bats index 37b6e1b..d995fe4 100755 --- a/acceptance.bats +++ b/acceptance.bats @@ -202,3 +202,17 @@ [ "$status" -eq 0 ] [ "$output" = "Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ] } + +@test "Pass when parsing a valid Kubernetes config YAML file and store cache" { + run mkdir cache + run bin/kubeconform -cache cache -summary fixtures/valid.yaml + [ "$status" -eq 0 ] + [ "$output" = "Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ] + [ "`ls cache/ | wc -l`" -eq 1 ] +} + +@test "Fail when cache folder does not exist" { + run bin/kubeconform -cache cache_does_not_exist -summary fixtures/valid.yaml + [ "$status" -eq 1 ] + [ "$output" = "failed opening cache folder cache_does_not_exist: stat cache_does_not_exist: no such file or directory" ] +} diff --git a/pkg/registry/http.go b/pkg/registry/http.go index 5e1f878..73bcbc8 100644 --- a/pkg/registry/http.go +++ b/pkg/registry/http.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "time" "github.com/yannh/kubeconform/pkg/cache" @@ -22,7 +23,7 @@ type SchemaRegistry struct { strict bool } -func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool, skipTLS bool) *SchemaRegistry { +func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool, skipTLS bool) (*SchemaRegistry, error) { reghttp := &http.Transport{ MaxIdleConns: 100, IdleConnTimeout: 3 * time.Second, @@ -35,6 +36,14 @@ func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool, var filecache cache.Cache = nil if cacheFolder != "" { + fi, err := os.Stat(cacheFolder) + if err != nil { + return nil, fmt.Errorf("failed opening cache folder %s: %s", cacheFolder, err) + } + if !fi.IsDir() { + return nil, fmt.Errorf("cache folder %s is not a directory", err) + } + filecache = cache.NewOnDiskCache(cacheFolder) } @@ -43,7 +52,7 @@ func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool, schemaPathTemplate: schemaPathTemplate, cache: filecache, strict: strict, - } + }, nil } // DownloadSchema downloads the schema for a particular resource from an HTTP server diff --git a/pkg/registry/local.go b/pkg/registry/local.go index afb5b0c..abae6e8 100644 --- a/pkg/registry/local.go +++ b/pkg/registry/local.go @@ -12,11 +12,11 @@ type LocalRegistry struct { } // NewLocalSchemas creates a new "registry", that will serve schemas from files, given a list of schema filenames -func newLocalRegistry(pathTemplate string, strict bool) *LocalRegistry { +func newLocalRegistry(pathTemplate string, strict bool) (*LocalRegistry, error) { return &LocalRegistry{ pathTemplate, strict, - } + }, nil } // DownloadSchema retrieves the schema from a file for the resource diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 3b1cb51..a231b2b 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -90,8 +90,8 @@ func New(schemaLocation string, cache string, strict bool, skipTLS bool) (Regist } if strings.HasPrefix(schemaLocation, "http") { - return newHTTPRegistry(schemaLocation, cache, strict, skipTLS), nil + return newHTTPRegistry(schemaLocation, cache, strict, skipTLS) } - return newLocalRegistry(schemaLocation, strict), nil + return newLocalRegistry(schemaLocation, strict) }