fail early when cache folder does not exist

This commit is contained in:
Yann Hamon 2021-01-02 11:57:52 +01:00
parent 56a281afdb
commit 128fcf993b
4 changed files with 29 additions and 6 deletions

View file

@ -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" ]
}

View file

@ -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

View file

@ -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

View file

@ -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)
}