Skip to content

Commit 6e2b24b

Browse files
Add validation tests
1 parent 0e2d922 commit 6e2b24b

File tree

8 files changed

+238
-5
lines changed

8 files changed

+238
-5
lines changed

cmd/kubeapply/subcmd/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func execValidation(ctx context.Context, clusterConfig *config.ClusterConfig) er
9494
}
9595

9696
log.Infof("Running kubeconform on configs in %+v", clusterConfig.AbsSubpaths())
97-
results, err := kubeValidator.RunKubeconform(ctx, clusterConfig.AbsSubpaths()[0])
97+
results, err := kubeValidator.RunSchemaValidation(ctx, clusterConfig.AbsSubpaths()[0])
9898
if err != nil {
9999
return err
100100
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
not valid YAML
2+
3+
xxx
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: nginx-deployment
6+
namespace: test1
7+
labels:
8+
app: nginx
9+
spec:
10+
replicas: 3
11+
selector:
12+
matchLabels:
13+
app: nginx
14+
template:
15+
metadata:
16+
labels:
17+
app: nginx
18+
spec:
19+
containers:
20+
- name: nginx
21+
image: nginx:1.14.2
22+
ports:
23+
- containerPort: 80
24+
---
25+
apiVersion: apps/v1
26+
kind: Deployment
27+
metadata:
28+
name: nginx-deployment2
29+
namespace: test1
30+
labels:
31+
app: nginx
32+
spec:
33+
replicas: 3
34+
notAValidKey: value
35+
selector:
36+
matchLabels:
37+
app: nginx
38+
template:
39+
metadata:
40+
labels:
41+
app: nginx
42+
spec:
43+
containers:
44+
- name: nginx
45+
image: nginx:1.14.2
46+
ports:
47+
- containerPort: 80
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: my-service
5+
namespace: test1
6+
spec:
7+
selector:
8+
app: MyApp
9+
ports:
10+
- protocol: TCP
11+
port: 80
12+
targetPort: 9376
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: nginx-deployment
6+
namespace: test1
7+
labels:
8+
app: nginx
9+
spec:
10+
replicas: 3
11+
selector:
12+
matchLabels:
13+
app: nginx
14+
template:
15+
metadata:
16+
labels:
17+
app: nginx
18+
spec:
19+
containers:
20+
- name: nginx
21+
image: nginx:1.14.2
22+
ports:
23+
- containerPort: 80
24+
---
25+
apiVersion: apps/v1
26+
kind: Deployment
27+
metadata:
28+
name: nginx-deployment2
29+
namespace: test1
30+
labels:
31+
app: nginx
32+
spec:
33+
replicas: 3
34+
selector:
35+
matchLabels:
36+
app: nginx
37+
template:
38+
metadata:
39+
labels:
40+
app: nginx
41+
spec:
42+
containers:
43+
- name: nginx
44+
image: nginx:1.14.2
45+
ports:
46+
- containerPort: 80
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: my-service
5+
namespace: test1
6+
spec:
7+
selector:
8+
app: MyApp
9+
ports:
10+
- protocol: TCP
11+
port: 80
12+
targetPort: 9376

pkg/validation/validation.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/yannh/kubeconform/pkg/validator"
1111
)
1212

13-
// KubeValidator is a struct that validates the kube configs associated with a cell config.
13+
// KubeValidator is a struct that validates the kube configs associated with a cluster.
1414
type KubeValidator struct {
1515
validatorObj validator.Validator
1616
}
@@ -69,8 +69,8 @@ func NewKubeValidator() (*KubeValidator, error) {
6969
}, nil
7070
}
7171

72-
// RunKubeconform runs kubeconform over all files in the provided path.
73-
func (k *KubeValidator) RunKubeconform(
72+
// RunSchemaValidation runs kubeconform over all files in the provided path and returns the result.
73+
func (k *KubeValidator) RunSchemaValidation(
7474
ctx context.Context,
7575
path string,
7676
) ([]ValidationResult, error) {
@@ -92,7 +92,12 @@ func (k *KubeValidator) RunKubeconform(
9292
return err
9393
}
9494

95-
for _, kResult := range k.validatorObj.Validate(subPath, file) {
95+
for _, kResult := range k.validatorObj.ValidateWithContext(ctx, subPath, file) {
96+
if kResult.Status == validator.Empty {
97+
// Skip over empty results
98+
continue
99+
}
100+
96101
result := ValidationResult{
97102
Filename: kResult.Resource.Path,
98103
Status: kubeconformStatusToStatus(kResult.Status),

pkg/validation/validation_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package validation
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestKubeValidator(t *testing.T) {
12+
ctx := context.Background()
13+
14+
type testCase struct {
15+
path string
16+
expected []ValidationResult
17+
}
18+
19+
testCases := []testCase{
20+
{
21+
path: "testdata/error",
22+
expected: []ValidationResult{
23+
{
24+
Filename: "testdata/error/deployment.yaml",
25+
Kind: "",
26+
Name: "",
27+
Namespace: "",
28+
Version: "",
29+
Status: "error",
30+
Message: "error unmarshalling resource: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type map[string]interface {}",
31+
},
32+
},
33+
},
34+
{
35+
path: "testdata/invalid",
36+
expected: []ValidationResult{
37+
{
38+
Filename: "testdata/invalid/deployment.yaml",
39+
Kind: "Deployment",
40+
Name: "nginx-deployment",
41+
Namespace: "test1",
42+
Version: "apps/v1",
43+
Status: "valid",
44+
Message: "",
45+
},
46+
{
47+
Filename: "testdata/invalid/deployment.yaml",
48+
Kind: "Deployment",
49+
Name: "nginx-deployment2",
50+
Namespace: "test1",
51+
Version: "apps/v1",
52+
Status: "invalid",
53+
Message: "For field spec: Additional property notAValidKey is not allowed",
54+
},
55+
{
56+
Filename: "testdata/invalid/service.yaml",
57+
Kind: "Service",
58+
Name: "my-service",
59+
Namespace: "test1",
60+
Version: "v1",
61+
Status: "valid",
62+
Message: "",
63+
},
64+
},
65+
},
66+
{
67+
path: "testdata/valid",
68+
expected: []ValidationResult{
69+
{
70+
Filename: "testdata/valid/deployment.yaml",
71+
Kind: "Deployment",
72+
Name: "nginx-deployment",
73+
Namespace: "test1",
74+
Version: "apps/v1",
75+
Status: "valid",
76+
Message: "",
77+
},
78+
{
79+
Filename: "testdata/valid/deployment.yaml",
80+
Kind: "Deployment",
81+
Name: "nginx-deployment2",
82+
Namespace: "test1",
83+
Version: "apps/v1",
84+
Status: "valid",
85+
Message: "",
86+
},
87+
{
88+
Filename: "testdata/valid/service.yaml",
89+
Kind: "Service",
90+
Name: "my-service",
91+
Namespace: "test1",
92+
Version: "v1",
93+
Status: "valid",
94+
Message: "",
95+
},
96+
},
97+
},
98+
}
99+
100+
validator, err := NewKubeValidator()
101+
require.NoError(t, err)
102+
103+
for _, testCase := range testCases {
104+
results, err := validator.RunSchemaValidation(ctx, testCase.path)
105+
require.NoError(t, err)
106+
assert.Equal(t, testCase.expected, results)
107+
}
108+
}

0 commit comments

Comments
 (0)