github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/specs/filesets_test.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package specs_test 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/juju/caas/specs" 11 ) 12 13 type validatorFileSet interface { 14 Validate() error 15 } 16 17 type validateFileSetTc struct { 18 spec validatorFileSet 19 errStr string 20 } 21 22 func (s *typesSuite) TestValidateFileSetV2(c *gc.C) { 23 for i, tc := range []validateTc{ 24 { 25 spec: &specs.FileSetV2{ 26 Name: "file1", 27 }, 28 errStr: `mount path is missing for file set "file1"`, 29 }, 30 { 31 spec: &specs.FileSetV2{ 32 MountPath: "/foo/bar", 33 }, 34 errStr: `file set name is missing`, 35 }, 36 } { 37 c.Logf("#%d: testing FileSetV2.Validate", i) 38 c.Check(tc.spec.Validate(), gc.ErrorMatches, tc.errStr) 39 } 40 } 41 42 func (s *typesSuite) TestValidateFileSet(c *gc.C) { 43 badMultiSource := &specs.FileSet{ 44 Name: "file1", 45 MountPath: "/foo/bar", 46 } 47 badMultiSource.HostPath = &specs.HostPathVol{ 48 Path: "/foo/bar", 49 } 50 badMultiSource.EmptyDir = &specs.EmptyDirVol{} 51 for i, tc := range []validateFileSetTc{ 52 { 53 spec: &specs.FileSet{ 54 Name: "file1", 55 }, 56 errStr: `mount path is missing for file set "file1"`, 57 }, 58 { 59 spec: &specs.FileSet{ 60 MountPath: "/foo/bar", 61 }, 62 errStr: `file set name is missing`, 63 }, 64 { 65 spec: &specs.FileSet{ 66 Name: "file1", 67 MountPath: "/foo/bar", 68 }, 69 errStr: `file set "file1" requires volume source`, 70 }, 71 { 72 spec: badMultiSource, 73 errStr: `file set "file1" can only have one volume source`, 74 }, 75 } { 76 c.Logf("#%d: testing FileSet.Validate", i) 77 c.Check(tc.spec.Validate(), gc.ErrorMatches, tc.errStr) 78 } 79 } 80 81 type comparerFileSet interface { 82 Equal(specs.FileSet) bool 83 } 84 85 type comparerFileSetTc struct { 86 f1 comparerFileSet 87 f2 specs.FileSet 88 equal bool 89 } 90 91 func (s *typesSuite) TestCompareFileSet(c *gc.C) { 92 for i, tc := range []comparerFileSetTc{ 93 { 94 f1: specs.FileSet{ 95 Name: "file1", 96 MountPath: "/foo/bar", 97 VolumeSource: specs.VolumeSource{ 98 HostPath: &specs.HostPathVol{ 99 Path: "/foo/bar", 100 }, 101 }, 102 }, 103 f2: specs.FileSet{ 104 Name: "file1", 105 MountPath: "/foo/bar", 106 VolumeSource: specs.VolumeSource{ 107 HostPath: &specs.HostPathVol{ 108 Path: "/foo/bar", 109 }, 110 }, 111 }, 112 equal: true, 113 }, 114 { 115 f1: specs.FileSet{ 116 Name: "file1", 117 MountPath: "/foo/bar", 118 VolumeSource: specs.VolumeSource{ 119 HostPath: &specs.HostPathVol{ 120 Path: "/foo/bar", 121 }, 122 }, 123 }, 124 f2: specs.FileSet{ 125 Name: "file1", 126 MountPath: "/foo/bar", 127 VolumeSource: specs.VolumeSource{ 128 HostPath: &specs.HostPathVol{ 129 Path: "/foo/foo", // different path. 130 }, 131 }, 132 }, 133 equal: false, 134 }, 135 { 136 f1: specs.FileSet{ 137 Name: "file1", 138 MountPath: "/foo/bar", 139 VolumeSource: specs.VolumeSource{ 140 HostPath: &specs.HostPathVol{ 141 Path: "/foo/bar", 142 }, 143 }, 144 }, 145 f2: specs.FileSet{ 146 Name: "file1", 147 MountPath: "/foo/bar", 148 VolumeSource: specs.VolumeSource{ // different VolumeSource. 149 EmptyDir: &specs.EmptyDirVol{}, 150 }, 151 }, 152 equal: false, 153 }, 154 } { 155 c.Logf("#%d: testing FileSet.Equal", i) 156 c.Check(tc.f1.Equal(tc.f2), gc.DeepEquals, tc.equal) 157 } 158 } 159 160 type comparerFileSetVol interface { 161 EqualVolume(specs.FileSet) bool 162 } 163 164 type comparerFileSetVolTc struct { 165 f1 comparerFileSetVol 166 f2 specs.FileSet 167 equal bool 168 } 169 170 func (s *typesSuite) TestCompareFileSetVolume(c *gc.C) { 171 for i, tc := range []comparerFileSetVolTc{ 172 { 173 // exactly same. 174 f1: specs.FileSet{ 175 Name: "file1", 176 MountPath: "/foo/bar", 177 VolumeSource: specs.VolumeSource{ 178 HostPath: &specs.HostPathVol{ 179 Path: "/foo/bar", 180 }, 181 }, 182 }, 183 f2: specs.FileSet{ 184 Name: "file1", 185 MountPath: "/foo/bar", 186 VolumeSource: specs.VolumeSource{ 187 HostPath: &specs.HostPathVol{ 188 Path: "/foo/bar", 189 }, 190 }, 191 }, 192 equal: true, 193 }, 194 { 195 f1: specs.FileSet{ 196 Name: "file1", 197 MountPath: "/foo/bar", 198 VolumeSource: specs.VolumeSource{ 199 HostPath: &specs.HostPathVol{ 200 Path: "/foo/bar", 201 }, 202 }, 203 }, 204 f2: specs.FileSet{ 205 Name: "file1", 206 MountPath: "/foo/bla", // different mount path. 207 VolumeSource: specs.VolumeSource{ 208 HostPath: &specs.HostPathVol{ 209 Path: "/foo/bar", 210 }, 211 }, 212 }, 213 equal: true, 214 }, 215 { 216 // different name. 217 f1: specs.FileSet{ 218 Name: "file1", 219 MountPath: "/foo/bar", 220 VolumeSource: specs.VolumeSource{ 221 HostPath: &specs.HostPathVol{ 222 Path: "/foo/bar", 223 }, 224 }, 225 }, 226 f2: specs.FileSet{ 227 Name: "file2", 228 MountPath: "/foo/bar", 229 VolumeSource: specs.VolumeSource{ 230 HostPath: &specs.HostPathVol{ 231 Path: "/foo/bar", 232 }, 233 }, 234 }, 235 equal: false, 236 }, 237 { 238 f1: specs.FileSet{ 239 Name: "file1", 240 MountPath: "/foo/bar", 241 VolumeSource: specs.VolumeSource{ 242 HostPath: &specs.HostPathVol{ 243 Path: "/foo/bar", 244 }, 245 }, 246 }, 247 f2: specs.FileSet{ 248 Name: "file1", 249 MountPath: "/foo/bar", 250 VolumeSource: specs.VolumeSource{ // different VolumeSource. 251 EmptyDir: &specs.EmptyDirVol{}, 252 }, 253 }, 254 equal: false, 255 }, 256 } { 257 c.Logf("#%d: testing FileSet.EqualVolume", i) 258 c.Check(tc.f1.EqualVolume(tc.f2), gc.DeepEquals, tc.equal) 259 } 260 } 261 262 type validatorVolumeSource interface { 263 Validate(string) error 264 } 265 266 type validateVolumeSourceTc struct { 267 spec validatorVolumeSource 268 errStr string 269 } 270 271 func (s *typesSuite) TestValidateFileSetVolumeSource(c *gc.C) { 272 for i, tc := range []validateVolumeSourceTc{ 273 { 274 spec: &specs.VolumeSource{ 275 HostPath: &specs.HostPathVol{}, 276 }, 277 errStr: `Path is missing for "fakeFileSet"`, 278 }, 279 { 280 spec: &specs.VolumeSource{ 281 Secret: &specs.ResourceRefVol{}, 282 }, 283 errStr: `Name is missing for "fakeFileSet"`, 284 }, 285 { 286 spec: &specs.VolumeSource{ 287 ConfigMap: &specs.ResourceRefVol{}, 288 }, 289 errStr: `Name is missing for "fakeFileSet"`, 290 }, 291 { 292 spec: &specs.File{}, 293 errStr: `Path is missing for "fakeFileSet"`, 294 }, 295 { 296 spec: &specs.File{ 297 Path: "foo/key1", 298 }, 299 errStr: `Content is missing for "fakeFileSet"`, 300 }, 301 { 302 spec: &specs.FileRef{}, 303 errStr: `Key is missing for "fakeFileSet"`, 304 }, 305 { 306 spec: &specs.FileRef{ 307 Key: "key1", 308 }, 309 errStr: `Path is missing for "fakeFileSet"`, 310 }, 311 } { 312 c.Logf("#%d: testing VolumeSource.Validate", i) 313 c.Check(tc.spec.Validate("fakeFileSet"), gc.ErrorMatches, tc.errStr) 314 } 315 } 316 317 func (s *typesSuite) TestSortKeysForFiles(c *gc.C) { 318 tests := []struct { 319 Files map[string]string 320 ExpectedKeys []string 321 }{ 322 { 323 Files: map[string]string{ 324 "foo": "bar", 325 "tt": "ff", 326 }, 327 ExpectedKeys: []string{"foo", "tt"}, 328 }, 329 { 330 Files: map[string]string{ 331 "tt": "ff", 332 "foo": "bar", 333 }, 334 ExpectedKeys: []string{"foo", "tt"}, 335 }, 336 } 337 338 for _, test := range tests { 339 keys := specs.SortKeysForFiles(test.Files) 340 c.Assert(keys, jc.DeepEquals, test.ExpectedKeys) 341 } 342 }