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