github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/openshift/template_test.go (about) 1 package openshift 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/opendevstack/tailor/internal/test/helper" 8 "github.com/opendevstack/tailor/pkg/cli" 9 "github.com/opendevstack/tailor/pkg/utils" 10 ) 11 12 func TestTemplateContainsTailorNamespaceParam(t *testing.T) { 13 tests := map[string]struct { 14 filename string 15 wantContains bool 16 wantError string 17 }{ 18 "contains param": { 19 filename: "with-tailor-namespace-param.yml", 20 wantContains: true, 21 wantError: "", 22 }, 23 "without param": { 24 filename: "without-tailor-namespace-param.yml", 25 wantContains: false, 26 wantError: "", 27 }, 28 "invalid template": { 29 filename: "invalid-template.yml", 30 wantContains: false, 31 wantError: "Not a valid template. Did you forget to add the template header?\n\napiVersion: v1\nkind: Template\nobjects: [...]", 32 }, 33 "template with blank parameters": { 34 filename: "template-blank-parameters.yml", 35 wantContains: false, 36 wantError: "", 37 }, 38 "garbage": { 39 filename: "garbage.yml", 40 wantContains: false, 41 wantError: "Not a valid template. Please see https://github.com/opendevstack/tailor#template-authoring", 42 }, 43 } 44 for name, tc := range tests { 45 t.Run(name, func(t *testing.T) { 46 contains, err := templateContainsTailorNamespaceParam( 47 "../../internal/test/fixtures/template-param-detection/" + tc.filename, 48 ) 49 if len(tc.wantError) == 0 { 50 if err != nil { 51 t.Fatalf("Could not determine if the template contains the param: %s", err) 52 } 53 } else { 54 if err == nil { 55 t.Fatalf("Want error '%s', but no error occured", tc.wantError) 56 } 57 if tc.wantError != err.Error() { 58 t.Fatalf("Want error '%s', got '%s'", tc.wantError, err) 59 } 60 } 61 if tc.wantContains != contains { 62 t.Fatalf("Want template containing param '%t', got '%t'", tc.wantContains, contains) 63 } 64 }) 65 } 66 } 67 68 func TestCalculateParamFiles(t *testing.T) { 69 tests := map[string]struct { 70 namespace string 71 templateName string 72 paramDir string 73 paramFileFlag []string 74 fs utils.FileStater 75 expected []string 76 }{ 77 "template is foo.yml and corresponding param file exists": { 78 namespace: "foo", 79 templateName: "bar.yml", 80 paramDir: ".", // default 81 paramFileFlag: []string{}, 82 fs: &helper.SomeFilesExistFS{Existing: []string{"bar.env", "foo.env"}}, 83 expected: []string{"bar.env", "foo.env"}, 84 }, 85 "template is bar.yml but corresponding param file does not exist": { 86 namespace: "foo", 87 templateName: "bar.yml", 88 paramDir: ".", // default 89 paramFileFlag: []string{}, 90 fs: &helper.SomeFilesExistFS{Existing: []string{"foo.env"}}, 91 expected: []string{"foo.env"}, 92 }, 93 "template is bar.yml and no files exist": { 94 namespace: "foo", 95 templateName: "bar.yml", 96 paramDir: ".", // default 97 paramFileFlag: []string{}, 98 fs: &helper.SomeFilesExistFS{}, 99 expected: []string{}, 100 }, 101 "template is foo.yml and corresponding param file exists in param dir": { 102 namespace: "foo", 103 templateName: "bar.yml", 104 paramDir: "foo", // default 105 paramFileFlag: []string{}, 106 fs: &helper.SomeFilesExistFS{Existing: []string{"foo/bar.env", "foo.env"}}, 107 expected: []string{"foo/bar.env", "foo.env"}, 108 }, 109 "template is foo.yml but corresponding param file does not exist in param dir": { 110 namespace: "foo", 111 templateName: "bar.yml", 112 paramDir: "foo", // default 113 paramFileFlag: []string{}, 114 fs: &helper.SomeFilesExistFS{Existing: []string{"foo", "foo.env"}}, 115 expected: []string{"foo.env"}, 116 }, 117 "param env file is given explicitly": { 118 namespace: "foo", 119 templateName: "bar.yml", 120 paramDir: ".", // default 121 paramFileFlag: []string{"foo.env"}, 122 fs: &helper.SomeFilesExistFS{Existing: []string{"foo.env"}}, 123 expected: []string{"foo.env"}, 124 }, 125 } 126 for name, tc := range tests { 127 t.Run(name, func(t *testing.T) { 128 globalOptions := cli.InitGlobalOptions(tc.fs) 129 compareOptions := &cli.CompareOptions{ 130 GlobalOptions: globalOptions, 131 NamespaceOptions: &cli.NamespaceOptions{Namespace: tc.namespace}, 132 ParamFiles: tc.paramFileFlag, 133 } 134 135 actual := calculateParamFiles(tc.templateName, tc.paramDir, compareOptions) 136 if diff := cmp.Diff(tc.expected, actual); diff != "" { 137 t.Fatalf("Desired state mismatch (-want +got):\n%s", diff) 138 } 139 }) 140 } 141 } 142 143 func TestReadParamFileBytes(t *testing.T) { 144 tests := map[string]struct { 145 paramFiles []string 146 expected string 147 }{ 148 "multiple files get concatenated": { 149 paramFiles: []string{"foo.env", "bar.env"}, 150 expected: "FOO=foo\nBAR=bar\n", 151 }, 152 "missing EOL is handled in concatenation": { 153 paramFiles: []string{"baz-without-eol.env", "bar.env"}, 154 expected: "BAZ=baz\nBAR=bar\n", 155 }, 156 } 157 for name, tc := range tests { 158 t.Run(name, func(t *testing.T) { 159 actualParamFiles := []string{} 160 for _, f := range tc.paramFiles { 161 actualParamFiles = append(actualParamFiles, "../../internal/test/fixtures/param-files/"+f) 162 } 163 b, err := readParamFileBytes(actualParamFiles, "", "") 164 if err != nil { 165 t.Fatal(err) 166 } 167 got := string(b) 168 if diff := cmp.Diff(tc.expected, got); diff != "" { 169 t.Fatalf("Result is not expected (-want +got):\n%s", diff) 170 } 171 }) 172 } 173 }