|
| 1 | +package limatmpl |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gotest.tools/v3/assert" |
| 8 | +) |
| 9 | + |
| 10 | +type useAbsLocatorsTestCase struct { |
| 11 | + description string |
| 12 | + locator string |
| 13 | + template string |
| 14 | + expected string |
| 15 | +} |
| 16 | + |
| 17 | +var useAbsLocatorsTestCases = []useAbsLocatorsTestCase{ |
| 18 | + { |
| 19 | + "Template without basedOn or script file", |
| 20 | + "template://foo", |
| 21 | + `foo: bar`, |
| 22 | + `foo: bar`, |
| 23 | + }, |
| 24 | + { |
| 25 | + "Single string base template", |
| 26 | + "template://foo", |
| 27 | + `basedOn: bar.yaml`, |
| 28 | + `basedOn: template://bar.yaml`, |
| 29 | + }, |
| 30 | + { |
| 31 | + "Flow style array of one base template", |
| 32 | + "template://foo", |
| 33 | + `basedOn: [bar.yaml]`, |
| 34 | + `basedOn: ['template://bar.yaml']`, |
| 35 | + }, |
| 36 | + { |
| 37 | + "Block style array of one base template", |
| 38 | + "template://foo", |
| 39 | + ` |
| 40 | +basedOn: |
| 41 | +- bar.yaml |
| 42 | +`, |
| 43 | + ` |
| 44 | +basedOn: |
| 45 | +- template://bar.yaml`, |
| 46 | + }, |
| 47 | + { |
| 48 | + "Block style of four base templates", |
| 49 | + "template://foo", |
| 50 | + ` |
| 51 | +basedOn: |
| 52 | +- bar.yaml |
| 53 | +- template://my |
| 54 | +- https://example.com/my.yaml |
| 55 | +- baz.yaml |
| 56 | +`, |
| 57 | + ` |
| 58 | +basedOn: |
| 59 | +- template://bar.yaml |
| 60 | +- template://my |
| 61 | +- https://example.com/my.yaml |
| 62 | +- template://baz.yaml |
| 63 | +`, |
| 64 | + }, |
| 65 | + { |
| 66 | + "Provisioning and probe scripts", |
| 67 | + "template://experimental/foo", |
| 68 | + ` |
| 69 | +provision: |
| 70 | +- mode: user |
| 71 | + file: script.sh |
| 72 | +probes: |
| 73 | +- file: probe.sh |
| 74 | +`, |
| 75 | + ` |
| 76 | +provision: |
| 77 | +- mode: user |
| 78 | + file: template://experimental/script.sh |
| 79 | +probes: |
| 80 | +- file: template://experimental/probe.sh |
| 81 | +`, |
| 82 | + }, |
| 83 | +} |
| 84 | + |
| 85 | +func TestUseAbsLocators(t *testing.T) { |
| 86 | + for _, tc := range useAbsLocatorsTestCases { |
| 87 | + t.Run(tc.description, func(t *testing.T) { RunUseAbsLocatorTest(t, tc) }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func RunUseAbsLocatorTest(t *testing.T, tc useAbsLocatorsTestCase) { |
| 92 | + tmpl := &Template{ |
| 93 | + Bytes: []byte(strings.TrimSpace(tc.template)), |
| 94 | + Locator: tc.locator, |
| 95 | + } |
| 96 | + err := tmpl.UseAbsLocators() |
| 97 | + assert.NilError(t, err, tc.description) |
| 98 | + |
| 99 | + actual := strings.TrimSpace(string(tmpl.Bytes)) |
| 100 | + expected := strings.TrimSpace(tc.expected) |
| 101 | + assert.Equal(t, actual, expected, tc.description) |
| 102 | +} |
| 103 | + |
| 104 | +func TestBasePath(t *testing.T) { |
| 105 | + actual, err := basePath("/foo") |
| 106 | + assert.NilError(t, err) |
| 107 | + assert.Equal(t, actual, "/") |
| 108 | + |
| 109 | + actual, err = basePath("/foo/bar") |
| 110 | + assert.NilError(t, err) |
| 111 | + assert.Equal(t, actual, "/foo") |
| 112 | + |
| 113 | + actual, err = basePath("template://foo") |
| 114 | + assert.NilError(t, err) |
| 115 | + assert.Equal(t, actual, "template://") |
| 116 | + |
| 117 | + actual, err = basePath("template://foo/bar") |
| 118 | + assert.NilError(t, err) |
| 119 | + assert.Equal(t, actual, "template://foo") |
| 120 | + |
| 121 | + actual, err = basePath("http://host/foo") |
| 122 | + assert.NilError(t, err) |
| 123 | + assert.Equal(t, actual, "http://host") |
| 124 | + |
| 125 | + actual, err = basePath("http://host/foo/bar") |
| 126 | + assert.NilError(t, err) |
| 127 | + assert.Equal(t, actual, "http://host/foo") |
| 128 | + |
| 129 | + actual, err = basePath("file:///foo") |
| 130 | + assert.NilError(t, err) |
| 131 | + assert.Equal(t, actual, "file:///") |
| 132 | + |
| 133 | + actual, err = basePath("file:///foo/bar") |
| 134 | + assert.NilError(t, err) |
| 135 | + assert.Equal(t, actual, "file:///foo") |
| 136 | +} |
| 137 | + |
| 138 | +func TestAbsPath(t *testing.T) { |
| 139 | + // If the locator is already an absolute path, it is returned unchanged (no extension appended either) |
| 140 | + actual, err := absPath("/foo", "/root") |
| 141 | + assert.NilError(t, err) |
| 142 | + assert.Equal(t, actual, "/foo") |
| 143 | + |
| 144 | + actual, err = absPath("template://foo", "/root") |
| 145 | + assert.NilError(t, err) |
| 146 | + assert.Equal(t, actual, "template://foo") |
| 147 | + |
| 148 | + actual, err = absPath("http://host/foo", "/root") |
| 149 | + assert.NilError(t, err) |
| 150 | + assert.Equal(t, actual, "http://host/foo") |
| 151 | + |
| 152 | + actual, err = absPath("file:///foo", "/root") |
| 153 | + assert.NilError(t, err) |
| 154 | + assert.Equal(t, actual, "file:///foo") |
| 155 | + |
| 156 | + // Can't have relative path when reading from STDIN |
| 157 | + _, err = absPath("foo", "-") |
| 158 | + assert.ErrorContains(t, err, "STDIN") |
| 159 | + |
| 160 | + // Relative paths must be underneath the basePath |
| 161 | + _, err = absPath("../foo", "/root") |
| 162 | + assert.ErrorContains(t, err, "'../'") |
| 163 | + |
| 164 | + // basePath must not be empty |
| 165 | + _, err = absPath("foo", "") |
| 166 | + assert.ErrorContains(t, err, "empty") |
| 167 | + |
| 168 | + _, err = absPath("./foo", "") |
| 169 | + assert.ErrorContains(t, err, "empty") |
| 170 | + |
| 171 | + // Check relative paths with all the supported schemes |
| 172 | + actual, err = absPath("./foo", "/root") |
| 173 | + assert.NilError(t, err) |
| 174 | + assert.Equal(t, actual, "/root/foo") |
| 175 | + |
| 176 | + actual, err = absPath("foo", "template://") |
| 177 | + assert.NilError(t, err) |
| 178 | + assert.Equal(t, actual, "template://foo") |
| 179 | + |
| 180 | + actual, err = absPath("bar", "template://foo") |
| 181 | + assert.NilError(t, err) |
| 182 | + assert.Equal(t, actual, "template://foo/bar") |
| 183 | + |
| 184 | + actual, err = absPath("foo", "http://host") |
| 185 | + assert.NilError(t, err) |
| 186 | + assert.Equal(t, actual, "http://host/foo") |
| 187 | + |
| 188 | + actual, err = absPath("bar", "http://host/foo") |
| 189 | + assert.NilError(t, err) |
| 190 | + assert.Equal(t, actual, "http://host/foo/bar") |
| 191 | + |
| 192 | + actual, err = absPath("foo", "file:///") |
| 193 | + assert.NilError(t, err) |
| 194 | + assert.Equal(t, actual, "file:///foo") |
| 195 | + |
| 196 | + actual, err = absPath("bar", "file:///foo") |
| 197 | + assert.NilError(t, err) |
| 198 | + assert.Equal(t, actual, "file:///foo/bar") |
| 199 | +} |
0 commit comments