github.com/sap/cf-mta-plugin@v2.6.3+incompatible/util/archive_builder_test.go (about) 1 package util_test 2 3 import ( 4 "archive/zip" 5 "github.com/cloudfoundry-incubator/multiapps-cli-plugin/testutil" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "strings" 10 11 "github.com/cloudfoundry-incubator/multiapps-cli-plugin/util" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 "gopkg.in/yaml.v2" 15 ) 16 17 var _ = Describe("ArchiveBuilder", func() { 18 Describe("Build", func() { 19 var tempDirLocation string 20 BeforeEach(func() { 21 tempDirLocation, _ = ioutil.TempDir("", "archive-builder") 22 }) 23 Context("With not existing resources", func() { 24 It("should try to find the directory and fail with error", func() { 25 _, err := util.NewMtaArchiveBuilder([]string{}, []string{}).Build("not-existing-location") 26 Expect(err).To(MatchError("Deployment descriptor location does not exist not-existing-location")) 27 }) 28 It("should try to find the deployment descriptor in the provided location and fail with error", func() { 29 _, err := util.NewMtaArchiveBuilder([]string{}, []string{}).Build(tempDirLocation) 30 Expect(err).To(MatchError("No deployment descriptor with name mtad.yaml was found in location " + tempDirLocation)) 31 }) 32 }) 33 34 Context("With different paths relative to the deployment descriptor", func() { 35 var ( 36 currentWorkingDirectory string 37 err error 38 mtaArchiveLocation string 39 ) 40 const requiredDependencyContent = "test-module-content" 41 const testDeploymentDescriptor = "mtad.yaml" 42 const moduleName = "TestModule" 43 44 BeforeEach(func() { 45 // need to cd into the tempDir in order to simulate the relative path 46 currentWorkingDirectory, err = os.Getwd() 47 Expect(err).To(BeNil()) 48 err = os.Chdir(tempDirLocation) 49 Expect(err).To(BeNil()) 50 51 os.Create(requiredDependencyContent) 52 ioutil.WriteFile(requiredDependencyContent, []byte("this is a test module content"), os.ModePerm) 53 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 54 util.Module{Name: moduleName, Path: requiredDependencyContent}, 55 }} 56 generatedYamlBytes, _ := yaml.Marshal(descriptor) 57 58 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 59 }) 60 61 It("Should find deployment descriptor with \".\" baseDirectory path", func() { 62 mtaArchiveLocation, err = util.NewMtaArchiveBuilder([]string{moduleName}, []string{}).Build(".") 63 }) 64 65 It("Should find deployment descriptor with \"./\" baseDirectory path", func() { 66 mtaArchiveLocation, err = util.NewMtaArchiveBuilder([]string{moduleName}, []string{}).Build("./") 67 }) 68 69 It("Should find deployment descriptor with \"../\" baseDirectory path", func() { 70 // create and cd into new dir 71 err = os.Mkdir("test", 0700) 72 Expect(err).To(BeNil()) 73 err = os.Chdir("test") 74 Expect(err).To(BeNil()) 75 mtaArchiveLocation, err = util.NewMtaArchiveBuilder([]string{moduleName}, []string{}).Build("../") 76 }) 77 78 AfterEach(func() { 79 expectedModulePath := filepathUnixJoin(moduleName, requiredDependencyContent) 80 Expect(err).To(BeNil()) 81 _, err = os.Stat(mtaArchiveLocation) 82 Expect(err).To(BeNil()) 83 Expect(isInArchive(expectedModulePath, mtaArchiveLocation)).To(BeTrue()) 84 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 85 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 86 Expect(isManifestValid("META-INF/MANIFEST.MF", map[string]string{"MTA-Module": "TestModule", "Name": expectedModulePath}, mtaArchiveLocation)).To(Equal(map[string]string{"MTA-Module": "TestModule", "Name": expectedModulePath})) 87 defer os.Remove(mtaArchiveLocation) 88 defer os.Chdir(currentWorkingDirectory) 89 }) 90 }) 91 92 Context("With deployment descriptor which contains some modules and resources", func() { 93 It("Try to parse the specified modules and fail as the paths are not existing", func() { 94 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 95 util.Module{Name: "TestModule", Path: "not-existing-path"}, 96 }} 97 generatedYamlBytes, _ := yaml.Marshal(descriptor) 98 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 99 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 100 _, err := util.NewMtaArchiveBuilder([]string{"TestModule"}, []string{}).Build(tempDirLocation) 101 Expect(err.Error()).To(MatchRegexp("Error building MTA Archive: file path .*?not-existing-path not found")) 102 }) 103 104 It("Try to parse the specified resources and fail as the paths are not existing", func() { 105 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{}, Resources: []util.Resource{ 106 util.Resource{Name: "foo", Type: "Some type", Parameters: map[string]interface{}{ 107 "path": "not-existing-resource-path", 108 }}, 109 }} 110 generatedYamlBytes, _ := yaml.Marshal(descriptor) 111 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 112 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 113 _, err := util.NewMtaArchiveBuilder([]string{}, []string{"foo"}).Build(tempDirLocation) 114 Expect(err.Error()).To(MatchRegexp("Error building MTA Archive: file path .*?not-existing-resource-path not found")) 115 }) 116 117 It("Try to parse the specified required dependencies config paths and fail as the paths are not existing", func() { 118 requiredDependencyContent := filepath.Join(tempDirLocation, "test-module-1-content") 119 os.Create(requiredDependencyContent) 120 ioutil.WriteFile(requiredDependencyContent, []byte("this is a test module content"), os.ModePerm) 121 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 122 util.Module{Name: "TestModule", Path: "test-module-1-content", RequiredDependencies: []util.RequiredDependency{ 123 util.RequiredDependency{Name: "foo", Parameters: map[string]interface{}{ 124 "path": "not-existing-required-dependency-path", 125 }}, 126 }}, 127 }} 128 generatedYamlBytes, _ := yaml.Marshal(descriptor) 129 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 130 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 131 _, err := util.NewMtaArchiveBuilder([]string{"TestModule"}, []string{}).Build(tempDirLocation) 132 Expect(err.Error()).To(MatchRegexp("Error building MTA Archive: file path .*?not-existing-required-dependency-path not found")) 133 }) 134 }) 135 136 Context("With deployment descriptor which contains some modules and resources and not valid modules or resources", func() { 137 It("Try to parse the specified modules and fail as the modules are not presented in the descriptor", func() { 138 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 139 util.Module{Name: "foo", Path: "not-existing-path"}, 140 util.Module{Name: "bar", Path: "not-existing-path"}, 141 util.Module{Name: "baz", Path: "not-existing-path"}, 142 util.Module{Name: "baz-foo", Path: "not-existing-path"}, 143 }} 144 generatedYamlBytes, _ := yaml.Marshal(descriptor) 145 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 146 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 147 _, err := util.NewMtaArchiveBuilder([]string{"foo", "bar", "test-1", "test-2"}, []string{}).Build(tempDirLocation) 148 Expect(err.Error()).To(MatchRegexp("Error building MTA Archive: Modules test-1, test-2 are specified for deployment but are not part of deployment descriptor modules")) 149 }) 150 151 It("Try to parse the specified resources and fail as the resources are not part of deployment descriptor", func() { 152 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{}, Resources: []util.Resource{ 153 util.Resource{Name: "foo", Type: "Some type", Parameters: map[string]interface{}{ 154 "path": "not-existing-resource-path", 155 }}, 156 util.Resource{Name: "bar", Type: "Some type", Parameters: map[string]interface{}{ 157 "path": "not-existing-resource-path", 158 }}, 159 util.Resource{Name: "baz", Type: "Some type", Parameters: map[string]interface{}{ 160 "path": "not-existing-resource-path", 161 }}, 162 util.Resource{Name: "baz-foo", Type: "Some type", Parameters: map[string]interface{}{ 163 "path": "not-existing-resource-path", 164 }}, 165 }} 166 generatedYamlBytes, _ := yaml.Marshal(descriptor) 167 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 168 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 169 _, err := util.NewMtaArchiveBuilder([]string{}, []string{"foo", "bar", "testing", "not-existing"}).Build(tempDirLocation) 170 Expect(err.Error()).To(MatchRegexp("Error building MTA Archive: Resources testing, not-existing are specified for deployment but are not part of deployment descriptor resources")) 171 }) 172 }) 173 174 Context("With deployment descriptor which does not contain any path path param", func() { 175 var oc = testutil.NewUIOutputCapturer() 176 It("Should try to resolve the modules and report that they do not have path params.", func() { 177 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 178 util.Module{Name: "TestModule"}, 179 util.Module{Name: "TestModule1"}, 180 }} 181 182 generatedYamlBytes, _ := yaml.Marshal(descriptor) 183 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 184 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 185 output := oc.CaptureOutput(func() { 186 util.NewMtaArchiveBuilder([]string{"TestModule", "TestModule1"}, []string{}).Build(tempDirLocation) 187 }) 188 Expect(output[0]).To(Equal("Modules TestModule, TestModule1 do not have a path, specified for their binaries and will be ignored\n")) 189 }) 190 191 }) 192 193 Context("With deployment descriptor which contains only valid modules", func() { 194 It("Should build the MTA Archive containing the valid modules", func() { 195 requiredDependencyContent := filepath.Join(tempDirLocation, "test-module-1-content") 196 os.Create(requiredDependencyContent) 197 ioutil.WriteFile(requiredDependencyContent, []byte("this is a test module content"), os.ModePerm) 198 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 199 util.Module{Name: "TestModule", Path: "test-module-1-content"}, 200 }} 201 generatedYamlBytes, _ := yaml.Marshal(descriptor) 202 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 203 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 204 mtaArchiveLocation, err := util.NewMtaArchiveBuilder([]string{"TestModule"}, []string{}).Build(tempDirLocation) 205 defer os.Remove(mtaArchiveLocation) 206 Expect(err).To(BeNil()) 207 _, err = os.Stat(mtaArchiveLocation) 208 Expect(err).To(BeNil()) 209 expectedModulePath := filepathUnixJoin("TestModule", "test-module-1-content") 210 Expect(isInArchive(expectedModulePath, mtaArchiveLocation)).To(BeTrue()) 211 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 212 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 213 Expect(isManifestValid("META-INF/MANIFEST.MF", map[string]string{"MTA-Module": "TestModule", "Name": expectedModulePath}, mtaArchiveLocation)).To(Equal(map[string]string{"MTA-Module": "TestModule", "Name": expectedModulePath})) 214 }) 215 }) 216 217 Context("With deployment descriptor which contains valid modules with non-normalized paths", func() { 218 It("Should build the MTA Archive containing the valid modules", func() { 219 requiredDependencyContent := filepath.Join(tempDirLocation, "test-module-1-content") 220 os.Create(requiredDependencyContent) 221 ioutil.WriteFile(requiredDependencyContent, []byte("this is a test module content"), os.ModePerm) 222 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 223 util.Module{Name: "TestModule", Path: "../test-module-1-content"}, 224 }} 225 generatedYamlBytes, _ := yaml.Marshal(descriptor) 226 mtadDirectory := filepath.Join(tempDirLocation, "test") 227 os.MkdirAll(mtadDirectory, os.ModePerm) 228 testDeploymentDescriptor := filepath.Join(mtadDirectory, "mtad.yaml") 229 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 230 mtaArchiveLocation, err := util.NewMtaArchiveBuilder([]string{"TestModule"}, []string{}).Build(mtadDirectory) 231 defer os.Remove(mtaArchiveLocation) 232 Expect(err).To(BeNil()) 233 _, err = os.Stat(mtaArchiveLocation) 234 Expect(err).To(BeNil()) 235 expectedModulePath := filepathUnixJoin("TestModule", "test-module-1-content") 236 Expect(isInArchive(expectedModulePath, mtaArchiveLocation)).To(BeTrue()) 237 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 238 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 239 Expect(isManifestValid("META-INF/MANIFEST.MF", map[string]string{"MTA-Module": "TestModule", "Name": expectedModulePath}, mtaArchiveLocation)).To(Equal(map[string]string{"MTA-Module": "TestModule", "Name": expectedModulePath})) 240 }) 241 }) 242 243 Context("With deployment descriptor which contains only valid modules with same paths", func() { 244 It("should build the MTA Archive containing the valid modules", func() { 245 requiredDependencyContent := filepath.Join(tempDirLocation, "test-module-1-content") 246 os.Create(requiredDependencyContent) 247 ioutil.WriteFile(requiredDependencyContent, []byte("this is a test module content"), os.ModePerm) 248 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 249 util.Module{Name: "TestModule", Path: "test-module-1-content"}, 250 util.Module{Name: "TestModule1", Path: "test-module-1-content"}, 251 }} 252 generatedYamlBytes, _ := yaml.Marshal(descriptor) 253 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 254 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 255 mtaArchiveLocation, err := util.NewMtaArchiveBuilder([]string{"TestModule", "TestModule1"}, []string{}).Build(tempDirLocation) 256 defer os.Remove(mtaArchiveLocation) 257 Expect(err).To(BeNil()) 258 _, err = os.Stat(mtaArchiveLocation) 259 Expect(err).To(BeNil()) 260 Expect(isInArchive("TestModule/test-module-1-content", mtaArchiveLocation)).To(BeTrue()) 261 Expect(isInArchive("TestModule1/test-module-1-content", mtaArchiveLocation)).To(BeTrue()) 262 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 263 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 264 }) 265 }) 266 267 Context("With deployment descriptor which contains only valid resources", func() { 268 It("Should build the MTA Archive containing the valid resources", func() { 269 resourceContent := filepath.Join(tempDirLocation, "test-resource-1-content") 270 os.Create(resourceContent) 271 ioutil.WriteFile(resourceContent, []byte("this is a test resource content"), os.ModePerm) 272 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Resources: []util.Resource{ 273 util.Resource{Name: "TestResource", Parameters: map[string]interface{}{"path": "test-resource-1-content"}}, 274 }} 275 generatedYamlBytes, _ := yaml.Marshal(descriptor) 276 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 277 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 278 mtaArchiveLocation, err := util.NewMtaArchiveBuilder([]string{}, []string{"TestResource"}).Build(tempDirLocation) 279 Expect(err).To(BeNil()) 280 _, err = os.Stat(mtaArchiveLocation) 281 Expect(err).To(BeNil()) 282 expectedResourcePath := filepathUnixJoin("TestResource", "test-resource-1-content") 283 Expect(isInArchive(expectedResourcePath, mtaArchiveLocation)).To(BeTrue()) 284 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 285 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 286 Expect(isManifestValid("META-INF/MANIFEST.MF", map[string]string{"MTA-Resource": "TestResource", "Name": expectedResourcePath}, mtaArchiveLocation)).To(Equal(map[string]string{"MTA-Resource": "TestResource", "Name": expectedResourcePath})) 287 defer os.Remove(mtaArchiveLocation) 288 }) 289 290 It("Should build the MTA Archive containing the valid resources with non-normalized paths", func() { 291 resourceContent := filepath.Join(tempDirLocation, "test-resource-1-content") 292 os.Create(resourceContent) 293 ioutil.WriteFile(resourceContent, []byte("this is a test resource content"), os.ModePerm) 294 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Resources: []util.Resource{ 295 util.Resource{Name: "TestResource", Parameters: map[string]interface{}{"path": "../test-resource-1-content"}}, 296 }} 297 generatedYamlBytes, _ := yaml.Marshal(descriptor) 298 mtadDirectory := filepath.Join(tempDirLocation, "test") 299 os.MkdirAll(mtadDirectory, os.ModePerm) 300 testDeploymentDescriptor := filepath.Join(mtadDirectory, "mtad.yaml") 301 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 302 mtaArchiveLocation, err := util.NewMtaArchiveBuilder([]string{}, []string{"TestResource"}).Build(mtadDirectory) 303 Expect(err).To(BeNil()) 304 _, err = os.Stat(mtaArchiveLocation) 305 Expect(err).To(BeNil()) 306 expectedResourcePath := filepathUnixJoin("TestResource", "test-resource-1-content") 307 Expect(isInArchive(expectedResourcePath, mtaArchiveLocation)).To(BeTrue()) 308 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 309 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 310 Expect(isManifestValid("META-INF/MANIFEST.MF", map[string]string{"MTA-Resource": "TestResource", "Name": expectedResourcePath}, mtaArchiveLocation)).To(Equal(map[string]string{"MTA-Resource": "TestResource", "Name": expectedResourcePath})) 311 defer os.Remove(mtaArchiveLocation) 312 }) 313 314 It("Should build the MTA Archive containing the resources and add them in the MANIFEST.MF only", func() { 315 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Resources: []util.Resource{ 316 util.Resource{Name: "TestResource"}, 317 }} 318 generatedYamlBytes, _ := yaml.Marshal(descriptor) 319 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 320 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 321 mtaArchiveLocation, err := util.NewMtaArchiveBuilder([]string{}, []string{"TestResource"}).Build(tempDirLocation) 322 Expect(err).To(BeNil()) 323 _, err = os.Stat(mtaArchiveLocation) 324 Expect(err).To(BeNil()) 325 Expect(isInArchive("test-resource-1-content", mtaArchiveLocation)).To(BeFalse()) 326 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 327 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 328 Expect(isManifestValid("META-INF/MANIFEST.MF", map[string]string{"MTA-Resource": "TestResource"}, mtaArchiveLocation)).To(Equal(map[string]string{})) 329 defer os.Remove(mtaArchiveLocation) 330 }) 331 332 }) 333 334 Context("With deployment descriptor which contains only valid modules with required dependencies", func() { 335 It("Should build the MTA Archive containing the valid modules and required dependencies configuration", func() { 336 requiredDependencyContent := filepath.Join(tempDirLocation, "test-required-dep-1-content") 337 os.Create(requiredDependencyContent) 338 ioutil.WriteFile(requiredDependencyContent, []byte("this is a test module content"), os.ModePerm) 339 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 340 util.Module{Name: "TestModule", RequiredDependencies: []util.RequiredDependency{ 341 util.RequiredDependency{ 342 Name: "TestRequired", 343 Parameters: map[string]interface{}{ 344 "path": "test-required-dep-1-content", 345 }, 346 }, 347 }}, 348 }} 349 generatedYamlBytes, _ := yaml.Marshal(descriptor) 350 testDeploymentDescriptor := tempDirLocation + string(os.PathSeparator) + "mtad.yaml" 351 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 352 mtaArchiveLocation, err := util.NewMtaArchiveBuilder([]string{"TestModule"}, []string{}).Build(tempDirLocation) 353 defer os.Remove(mtaArchiveLocation) 354 Expect(err).To(BeNil()) 355 _, err = os.Stat(mtaArchiveLocation) 356 Expect(err).To(BeNil()) 357 expectedRequiredDependenciesPath := filepathUnixJoin("TestModule", "TestRequired", "test-required-dep-1-content") 358 Expect(isInArchive(expectedRequiredDependenciesPath, mtaArchiveLocation)).To(BeTrue()) 359 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 360 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 361 Expect(isManifestValid("META-INF/MANIFEST.MF", map[string]string{"MTA-Requires": "TestModule/TestRequired", "Name": expectedRequiredDependenciesPath}, mtaArchiveLocation)).To(Equal(map[string]string{"MTA-Requires": "TestModule/TestRequired", "Name": expectedRequiredDependenciesPath})) 362 }) 363 364 It("Should build the MTA Archive containing the valid modules and required dependencies configuration with non-normalized paths", func() { 365 requiredDependencyContent := filepath.Join(tempDirLocation, "test-required-dep-1-content") 366 os.Create(requiredDependencyContent) 367 ioutil.WriteFile(requiredDependencyContent, []byte("this is a test module content"), os.ModePerm) 368 descriptor := util.MtaDeploymentDescriptor{SchemaVersion: "100", ID: "test", Modules: []util.Module{ 369 util.Module{Name: "TestModule", RequiredDependencies: []util.RequiredDependency{ 370 util.RequiredDependency{ 371 Name: "TestRequired", 372 Parameters: map[string]interface{}{ 373 "path": "../test-required-dep-1-content", 374 }, 375 }, 376 }}, 377 }} 378 generatedYamlBytes, _ := yaml.Marshal(descriptor) 379 mtadDirectory := filepath.Join(tempDirLocation, "test") 380 os.MkdirAll(mtadDirectory, os.ModePerm) 381 testDeploymentDescriptor := filepath.Join(mtadDirectory, "mtad.yaml") 382 ioutil.WriteFile(testDeploymentDescriptor, generatedYamlBytes, os.ModePerm) 383 mtaArchiveLocation, err := util.NewMtaArchiveBuilder([]string{"TestModule"}, []string{}).Build(mtadDirectory) 384 defer os.Remove(mtaArchiveLocation) 385 Expect(err).To(BeNil()) 386 _, err = os.Stat(mtaArchiveLocation) 387 Expect(err).To(BeNil()) 388 expectedRequiredDependenciesPath := filepathUnixJoin("TestModule", "TestRequired", "test-required-dep-1-content") 389 Expect(isInArchive(expectedRequiredDependenciesPath, mtaArchiveLocation)).To(BeTrue()) 390 Expect(isInArchive("META-INF/MANIFEST.MF", mtaArchiveLocation)).To(BeTrue()) 391 Expect(isInArchive("META-INF/mtad.yaml", mtaArchiveLocation)).To(BeTrue()) 392 Expect(isManifestValid("META-INF/MANIFEST.MF", map[string]string{"MTA-Requires": "TestModule/TestRequired", "Name": expectedRequiredDependenciesPath}, mtaArchiveLocation)).To(Equal(map[string]string{"MTA-Requires": "TestModule/TestRequired", "Name": expectedRequiredDependenciesPath})) 393 }) 394 }) 395 396 AfterEach(func() { 397 os.RemoveAll(tempDirLocation) 398 }) 399 }) 400 }) 401 402 func isInArchive(fileName, archiveLocation string) bool { 403 mtaArchiveReader, err := zip.OpenReader(archiveLocation) 404 if err != nil { 405 return false 406 } 407 defer mtaArchiveReader.Close() 408 for _, file := range mtaArchiveReader.File { 409 if file.Name == fileName { 410 return true 411 } 412 } 413 return false 414 } 415 416 func isManifestValid(manifestLocation string, searchCriteria map[string]string, archiveLocation string) map[string]string { 417 mtaArchiveReader, err := zip.OpenReader(archiveLocation) 418 if err != nil { 419 return map[string]string{} 420 } 421 defer mtaArchiveReader.Close() 422 searchCriteriaResult := make(map[string]string) 423 for _, file := range mtaArchiveReader.File { 424 if file.Name == manifestLocation { 425 reader, err := file.Open() 426 if err != nil { 427 return map[string]string{} 428 } 429 defer reader.Close() 430 manifestBytes, _ := ioutil.ReadAll(reader) 431 manifestSplittedByNewLine := strings.Split(string(manifestBytes), "\n") 432 for _, manifestSectionElement := range manifestSplittedByNewLine { 433 if strings.Trim(manifestSectionElement, " ") == "" { 434 continue 435 } 436 separatorIndex := strings.Index(manifestSectionElement, ":") 437 key := manifestSectionElement[:separatorIndex] 438 value := manifestSectionElement[separatorIndex+1:] 439 if searchCriteria[key] != "" { 440 delete(searchCriteria, key) 441 searchCriteriaResult[key] = strings.Trim(value, " ") 442 } 443 } 444 break 445 } 446 } 447 return searchCriteriaResult 448 } 449 450 func filepathUnixJoin(elements ...string) string { 451 return strings.Join(elements, "/") 452 }