|
| 1 | +// Copyright 2019 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package watches |
| 16 | + |
| 17 | +import ( |
| 18 | + "io/ioutil" |
| 19 | + "os" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +type testCase struct { |
| 24 | + name string |
| 25 | + data string |
| 26 | + expectErr bool |
| 27 | +} |
| 28 | + |
| 29 | +func TestLoadWatches(t *testing.T) { |
| 30 | + testCases := []testCase{ |
| 31 | + { |
| 32 | + name: "valid", |
| 33 | + data: `--- |
| 34 | +- group: mygroup |
| 35 | + version: v1alpha1 |
| 36 | + kind: MyKind |
| 37 | + chart: ../../../internal/scaffold/helm/testdata/testcharts/test-chart |
| 38 | +`, |
| 39 | + expectErr: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "duplicate gvk", |
| 43 | + data: `--- |
| 44 | +- group: mygroup |
| 45 | + version: v1alpha1 |
| 46 | + kind: MyKind |
| 47 | + chart: ../../../internal/scaffold/helm/testdata/testcharts/test-chart |
| 48 | +- group: mygroup |
| 49 | + version: v1alpha1 |
| 50 | + kind: MyKind |
| 51 | + chart: ../../../internal/scaffold/helm/testdata/testcharts/test-chart |
| 52 | +`, |
| 53 | + expectErr: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "no version", |
| 57 | + data: `--- |
| 58 | +- group: mygroup |
| 59 | + kind: MyKind |
| 60 | + chart: ../../../internal/scaffold/helm/testdata/testcharts/test-chart |
| 61 | +`, |
| 62 | + expectErr: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "no kind", |
| 66 | + data: `--- |
| 67 | +- group: mygroup |
| 68 | + version: v1alpha1 |
| 69 | + chart: ../../../internal/scaffold/helm/testdata/testcharts/test-chart |
| 70 | +`, |
| 71 | + expectErr: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "bad chart path", |
| 75 | + data: `--- |
| 76 | +- group: mygroup |
| 77 | + version: v1alpha1 |
| 78 | + kind: MyKind |
| 79 | + chart: nonexistent/path/to/chart |
| 80 | +`, |
| 81 | + expectErr: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "invalid yaml", |
| 85 | + data: `--- |
| 86 | +foo: bar |
| 87 | +`, |
| 88 | + expectErr: true, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tc := range testCases { |
| 93 | + t.Run(tc.name, func(t *testing.T) { |
| 94 | + tmp, err := ioutil.TempFile("", "watches.yaml") |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("Failed to create temporary watches.yaml file: %w", err) |
| 97 | + } |
| 98 | + defer func() { _ = os.Remove(tmp.Name()) }() |
| 99 | + if _, err := tmp.WriteString(tc.data); err != nil { |
| 100 | + t.Fatalf("Failed to write data to temporary watches.yaml file: %w", err) |
| 101 | + } |
| 102 | + if err := tmp.Close(); err != nil { |
| 103 | + t.Fatalf("Failed to close temporary watches.yaml file: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + _, err = Load(tmp.Name()) |
| 107 | + if !tc.expectErr && err != nil { |
| 108 | + t.Fatalf("Expected no error; got error: %w", err) |
| 109 | + } else if tc.expectErr && err == nil { |
| 110 | + t.Fatalf("Expected error; got no error") |
| 111 | + } |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
0 commit comments