mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-20 18:37:01 +00:00
add test for missing apiVersion - better error message
This commit is contained in:
parent
031f83cbde
commit
509ad8e997
5 changed files with 57 additions and 8 deletions
|
|
@ -85,13 +85,19 @@
|
||||||
@test "Fail when parsing a config that is missing a Kind" {
|
@test "Fail when parsing a config that is missing a Kind" {
|
||||||
run bin/kubeconform -summary fixtures/missing_kind.yaml
|
run bin/kubeconform -summary fixtures/missing_kind.yaml
|
||||||
[ "$status" -eq 1 ]
|
[ "$status" -eq 1 ]
|
||||||
[[ "$output" == *"resource missing a Kind"* ]]
|
[[ "$output" == *"missing 'kind' key"* ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "Fail when parsing a config that is missing an apiVersion" {
|
||||||
|
run bin/kubeconform -summary fixtures/missing_apiversion.yaml
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
[[ "$output" == *"missing 'apiVersion' key"* ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Fail when parsing a config that is missing a Kind value" {
|
@test "Fail when parsing a config that is missing a Kind value" {
|
||||||
run bin/kubeconform -summary fixtures/missing_kind_value.yaml
|
run bin/kubeconform -summary fixtures/missing_kind_value.yaml
|
||||||
[ "$status" -eq 1 ]
|
[ "$status" -eq 1 ]
|
||||||
[[ "$output" == *"resource missing a Kind"* ]]
|
[[ "$output" == *"missing 'kind' key"* ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Fail when parsing a config with CRD" {
|
@test "Fail when parsing a config with CRD" {
|
||||||
|
|
|
||||||
18
fixtures/missing_apiversion.yaml
Normal file
18
fixtures/missing_apiversion.yaml
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
kind: ReplicationController
|
||||||
|
metadata:
|
||||||
|
name: "bob"
|
||||||
|
spec:
|
||||||
|
replicas: 2
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
name: nginx
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx
|
||||||
|
ports:
|
||||||
|
- containerPort: 80
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -40,6 +42,19 @@ func (res *Resource) Signature() (*Signature, error) {
|
||||||
|
|
||||||
// We cache the result to not unmarshall every time we want to access the signature
|
// We cache the result to not unmarshall every time we want to access the signature
|
||||||
res.sig = &Signature{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace, Name: name}
|
res.sig = &Signature{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace, Name: name}
|
||||||
|
|
||||||
|
if err != nil { // Exit if there was an error unmarshalling
|
||||||
|
return res.sig, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resource.Kind == "" {
|
||||||
|
return res.sig, fmt.Errorf("missing 'kind' key")
|
||||||
|
}
|
||||||
|
|
||||||
|
if resource.APIVersion == "" {
|
||||||
|
return res.sig, fmt.Errorf("missing 'apiVersion' key")
|
||||||
|
}
|
||||||
|
|
||||||
return res.sig, err
|
return res.sig, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,8 +63,15 @@ func (res *Resource) SignatureFromMap(m map[string]interface{}) (*Signature, err
|
||||||
return res.sig, nil
|
return res.sig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
APIVersion, _ := m["apiVersion"].(string)
|
Kind, ok := m["kind"].(string)
|
||||||
Kind, _ := m["kind"].(string)
|
if !ok {
|
||||||
|
return res.sig, fmt.Errorf("missing 'kind' key")
|
||||||
|
}
|
||||||
|
|
||||||
|
APIVersion, ok := m["apiVersion"].(string)
|
||||||
|
if !ok {
|
||||||
|
return res.sig, fmt.Errorf("missing 'apiVersion' key")
|
||||||
|
}
|
||||||
|
|
||||||
var name, ns string
|
var name, ns string
|
||||||
Metadata, ok := m["metadata"].(map[string]interface{})
|
Metadata, ok := m["metadata"].(map[string]interface{})
|
||||||
|
|
|
||||||
|
|
@ -123,10 +123,6 @@ func (val *v) ValidateResource(res resource.Resource) Result {
|
||||||
return Result{Resource: res, Err: fmt.Errorf("error while parsing: %s", err), Status: Error}
|
return Result{Resource: res, Err: fmt.Errorf("error while parsing: %s", err), Status: Error}
|
||||||
}
|
}
|
||||||
|
|
||||||
if sig.Kind == "" { // Resource contains key/values but no Kind
|
|
||||||
return Result{Resource: res, Err: fmt.Errorf("resource missing a Kind"), Status: Error}
|
|
||||||
}
|
|
||||||
|
|
||||||
if skip(*sig) {
|
if skip(*sig) {
|
||||||
return Result{Resource: res, Err: nil, Status: Skipped}
|
return Result{Resource: res, Err: nil, Status: Skipped}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ func TestValidate(t *testing.T) {
|
||||||
"valid resource",
|
"valid resource",
|
||||||
[]byte(`
|
[]byte(`
|
||||||
kind: name
|
kind: name
|
||||||
|
apiVersion: v1
|
||||||
firstName: foo
|
firstName: foo
|
||||||
lastName: bar
|
lastName: bar
|
||||||
`),
|
`),
|
||||||
|
|
@ -49,6 +50,7 @@ lastName: bar
|
||||||
"invalid resource",
|
"invalid resource",
|
||||||
[]byte(`
|
[]byte(`
|
||||||
kind: name
|
kind: name
|
||||||
|
apiVersion: v1
|
||||||
firstName: foo
|
firstName: foo
|
||||||
lastName: bar
|
lastName: bar
|
||||||
`),
|
`),
|
||||||
|
|
@ -79,6 +81,7 @@ lastName: bar
|
||||||
"missing required field",
|
"missing required field",
|
||||||
[]byte(`
|
[]byte(`
|
||||||
kind: name
|
kind: name
|
||||||
|
apiVersion: v1
|
||||||
firstName: foo
|
firstName: foo
|
||||||
`),
|
`),
|
||||||
[]byte(`{
|
[]byte(`{
|
||||||
|
|
@ -108,6 +111,7 @@ firstName: foo
|
||||||
"resource has invalid yaml",
|
"resource has invalid yaml",
|
||||||
[]byte(`
|
[]byte(`
|
||||||
kind: name
|
kind: name
|
||||||
|
apiVersion: v1
|
||||||
firstName foo
|
firstName foo
|
||||||
lastName: bar
|
lastName: bar
|
||||||
`),
|
`),
|
||||||
|
|
@ -118,6 +122,9 @@ lastName: bar
|
||||||
"kind": {
|
"kind": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"apiVersion": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
"firstName": {
|
"firstName": {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue