config/validate: add rule for file encoding
This commit is contained in:
parent
3c93938f8a
commit
551cbb1e5d
@ -31,6 +31,7 @@ type rule func(config node, report *Report)
|
|||||||
// Rules contains all of the validation rules.
|
// Rules contains all of the validation rules.
|
||||||
var Rules []rule = []rule{
|
var Rules []rule = []rule{
|
||||||
checkDiscoveryUrl,
|
checkDiscoveryUrl,
|
||||||
|
checkEncoding,
|
||||||
checkStructure,
|
checkStructure,
|
||||||
checkValidity,
|
checkValidity,
|
||||||
checkWriteFiles,
|
checkWriteFiles,
|
||||||
@ -49,6 +50,22 @@ func checkDiscoveryUrl(cfg node, report *Report) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkEncoding validates that, for each file under 'write_files', the
|
||||||
|
// content can be decoded given the specified encoding.
|
||||||
|
func checkEncoding(cfg node, report *Report) {
|
||||||
|
for _, f := range cfg.Child("write_files").children {
|
||||||
|
e := f.Child("encoding")
|
||||||
|
if !e.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
c := f.Child("contents")
|
||||||
|
if _, err := config.DecodeContent(c.String(), e.String()); err != nil {
|
||||||
|
report.Error(c.line, fmt.Sprintf("contents cannot be decoded as %q", e.String()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// checkStructure compares the provided config to the empty config.CloudConfig
|
// checkStructure compares the provided config to the empty config.CloudConfig
|
||||||
// structure. Each node is checked to make sure that it exists in the known
|
// structure. Each node is checked to make sure that it exists in the known
|
||||||
// structure and that its type is compatible.
|
// structure and that its type is compatible.
|
||||||
|
@ -54,6 +54,52 @@ func TestCheckDiscoveryUrl(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCheckEncoding(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
config string
|
||||||
|
|
||||||
|
entries []Entry
|
||||||
|
}{
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
config: "write_files:\n - encoding: base64\n contents: aGVsbG8K",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
config: "write_files:\n - contents: !!binary aGVsbG8K",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
config: "write_files:\n - encoding: base64\n contents: !!binary aGVsbG8K",
|
||||||
|
entries: []Entry{{entryError, `contents cannot be decoded as "base64"`, 3}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
config: "write_files:\n - encoding: base64\n contents: !!binary YUdWc2JHOEsK",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
config: "write_files:\n - encoding: gzip\n contents: !!binary H4sIAOC3tVQAA8tIzcnJ5wIAIDA6NgYAAAA=",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
config: "write_files:\n - encoding: gzip+base64\n contents: H4sIAOC3tVQAA8tIzcnJ5wIAIDA6NgYAAAA=",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
config: "write_files:\n - encoding: custom\n contents: hello",
|
||||||
|
entries: []Entry{{entryError, `contents cannot be decoded as "custom"`, 3}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
r := Report{}
|
||||||
|
n, err := parseCloudConfig([]byte(tt.config), &r)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
checkEncoding(n, &r)
|
||||||
|
|
||||||
|
if e := r.Entries(); !reflect.DeepEqual(tt.entries, e) {
|
||||||
|
t.Errorf("bad report (%d, %q): want %#v, got %#v", i, tt.config, tt.entries, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCheckStructure(t *testing.T) {
|
func TestCheckStructure(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
config string
|
config string
|
||||||
|
Loading…
Reference in New Issue
Block a user