mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-20 18:37:01 +00:00
fail early when cache folder does not exist
This commit is contained in:
parent
56a281afdb
commit
128fcf993b
4 changed files with 29 additions and 6 deletions
|
|
@ -202,3 +202,17 @@
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
[ "$output" = "Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0, Skipped: 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" ]
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yannh/kubeconform/pkg/cache"
|
"github.com/yannh/kubeconform/pkg/cache"
|
||||||
|
|
@ -22,7 +23,7 @@ type SchemaRegistry struct {
|
||||||
strict bool
|
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{
|
reghttp := &http.Transport{
|
||||||
MaxIdleConns: 100,
|
MaxIdleConns: 100,
|
||||||
IdleConnTimeout: 3 * time.Second,
|
IdleConnTimeout: 3 * time.Second,
|
||||||
|
|
@ -35,6 +36,14 @@ func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool,
|
||||||
|
|
||||||
var filecache cache.Cache = nil
|
var filecache cache.Cache = nil
|
||||||
if cacheFolder != "" {
|
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)
|
filecache = cache.NewOnDiskCache(cacheFolder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +52,7 @@ func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool,
|
||||||
schemaPathTemplate: schemaPathTemplate,
|
schemaPathTemplate: schemaPathTemplate,
|
||||||
cache: filecache,
|
cache: filecache,
|
||||||
strict: strict,
|
strict: strict,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DownloadSchema downloads the schema for a particular resource from an HTTP server
|
// DownloadSchema downloads the schema for a particular resource from an HTTP server
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,11 @@ type LocalRegistry struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLocalSchemas creates a new "registry", that will serve schemas from files, given a list of schema filenames
|
// 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{
|
return &LocalRegistry{
|
||||||
pathTemplate,
|
pathTemplate,
|
||||||
strict,
|
strict,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DownloadSchema retrieves the schema from a file for the resource
|
// DownloadSchema retrieves the schema from a file for the resource
|
||||||
|
|
|
||||||
|
|
@ -90,8 +90,8 @@ func New(schemaLocation string, cache string, strict bool, skipTLS bool) (Regist
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(schemaLocation, "http") {
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue