github.com/SAP/cloud-mta-build-tool@v1.2.27/internal/artifacts/meta_test.go (about)

     1  package artifacts
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/SAP/cloud-mta-build-tool/internal/archive"
    14  	"github.com/SAP/cloud-mta-build-tool/internal/platform"
    15  	"github.com/SAP/cloud-mta-build-tool/internal/version"
    16  	"github.com/SAP/cloud-mta/mta"
    17  )
    18  
    19  var _ = Describe("Meta", func() {
    20  
    21  	AfterEach(func() {
    22  		Ω(os.RemoveAll(getResultPath())).Should(Succeed())
    23  	})
    24  
    25  	var _ = Describe("ExecuteGenMeta", func() {
    26  
    27  		It("Sanity", func() {
    28  			createMtahtml5TmpFolder()
    29  			Ω(ExecuteGenMeta(getTestPath("mtahtml5"), getResultPath(), "dev", nil, "CF", os.Getwd)).Should(Succeed())
    30  			Ω(getFullPathInTmpFolder("mtahtml5", "META-INF", "MANIFEST.MF")).Should(BeAnExistingFile())
    31  			Ω(getFullPathInTmpFolder("mtahtml5", "META-INF", "mtad.yaml")).Should(BeAnExistingFile())
    32  		})
    33  
    34  		It("Fails on META-INF folder creation", func() {
    35  			createDirInTmpFolder("mtahtml5")
    36  			createFileInTmpFolder("mtahtml5", "META-INF")
    37  			err := ExecuteGenMeta(getTestPath("mtahtml5"), getResultPath(), "dev", nil, "CF", os.Getwd)
    38  			checkError(err, dir.FolderCreationFailedMsg, getFullPathInTmpFolder("mtahtml5", "META-INF"))
    39  		})
    40  
    41  		It("Wrong location - fails on Working directory get", func() {
    42  			err := ExecuteGenMeta("", "", "dev", nil, "cf", func() (string, error) {
    43  				return "", errors.New("error of working dir get")
    44  			})
    45  			checkError(err, "error of working dir get")
    46  		})
    47  		It("Wrong platform", func() {
    48  			createDirInTmpFolder("mtahtml5", "ui5app2")
    49  			createDirInTmpFolder("mtahtml5", "testapp")
    50  			err := ExecuteGenMeta(getTestPath("mtahtml5"), getResultPath(), "dev", nil, "xx", os.Getwd)
    51  			checkError(err, invalidPlatformMsg, "xx")
    52  		})
    53  		It("generateMeta fails on wrong source path - parse mta fails", func() {
    54  			err := ExecuteGenMeta(getTestPath("mtahtml6"), getResultPath(), "dev", nil, "cf", os.Getwd)
    55  			checkError(err, getTestPath("mtahtml6", "mta.yaml"))
    56  		})
    57  	})
    58  
    59  	var _ = Describe("GenMetaInfo", func() {
    60  		ep := dir.Loc{SourcePath: getTestPath("testproject"), TargetPath: getResultPath()}
    61  		var mtaSingleModule = []byte(`
    62  _schema-version: "2.0.0"
    63  ID: mta_proj
    64  version: 1.0.0
    65  
    66  modules:
    67    - name: htmlapp
    68      type: html5
    69      path: app
    70  `)
    71  
    72  		It("Sanity", func() {
    73  			m, err := mta.Unmarshal(mtaSingleModule)
    74  			Ω(err).Should(Succeed())
    75  			createDirInTmpFolder("testproject", "htmlapp")
    76  			createFileInTmpFolder("testproject", "htmlapp", "data.zip")
    77  			createDirInTmpFolder("testproject", "META-INF")
    78  			Ω(genMetaInfo(&ep, &ep, &ep, ep.IsDeploymentDescriptor(), "cf", m, true, true)).Should(Succeed())
    79  			Ω(ep.GetManifestPath()).Should(BeAnExistingFile())
    80  			Ω(ep.GetMtadPath()).Should(BeAnExistingFile())
    81  		})
    82  
    83  		It("Meta creation fails - fails on conversion by platform", func() {
    84  			m, err := mta.Unmarshal(mtaSingleModule)
    85  			Ω(err).Should(Succeed())
    86  			createDirInTmpFolder("testproject", "app")
    87  			createDirInTmpFolder("testproject", "META-INF")
    88  			cfg := platform.PlatformConfig
    89  			platform.PlatformConfig = []byte(`very bad config`)
    90  			Ω(genMetaInfo(&ep, &ep, &ep, ep.IsDeploymentDescriptor(), "cf", m, true, true)).Should(HaveOccurred())
    91  			platform.PlatformConfig = cfg
    92  		})
    93  
    94  		It("Fails on create file for manifest path", func() {
    95  			loc := testLoc{ep}
    96  			m, err := mta.Unmarshal(mtaSingleModule)
    97  			Ω(err).Should(Succeed())
    98  			Ω(genMetaInfo(&loc, &ep, &ep, ep.IsDeploymentDescriptor(), "cf", m, true, true)).Should(HaveOccurred())
    99  		})
   100  
   101  		var _ = Describe("Fails on setManifestDesc", func() {
   102  			var config []byte
   103  
   104  			BeforeEach(func() {
   105  				config = make([]byte, len(version.VersionConfig))
   106  				copy(config, version.VersionConfig)
   107  				// Simplified commands configuration (performance purposes). removed "npm prune --production"
   108  				version.VersionConfig = []byte(`
   109  cli_version:["x"]
   110  `)
   111  			})
   112  
   113  			AfterEach(func() {
   114  				version.VersionConfig = make([]byte, len(config))
   115  				copy(version.VersionConfig, config)
   116  				Ω(os.RemoveAll(getResultPath())).Should(Succeed())
   117  			})
   118  
   119  			It("Fails on get version", func() {
   120  				m, err := mta.Unmarshal(mtaSingleModule)
   121  				Ω(err).Should(Succeed())
   122  				Ω(genMetaInfo(&ep, &ep, &ep, ep.IsDeploymentDescriptor(), "cf", m, true, true)).Should(HaveOccurred())
   123  			})
   124  		})
   125  	})
   126  
   127  	var _ = Describe("Generate Commands", func() {
   128  
   129  		AfterEach(func() {
   130  			Ω(os.RemoveAll(getResultPath())).Should(Succeed())
   131  		})
   132  
   133  		readFileContent := func(ep dir.IMtaParser) *mta.MTA {
   134  			mtaObj, _ := ep.ParseFile()
   135  			return mtaObj
   136  		}
   137  
   138  		It("Generate Meta", func() {
   139  			createMtahtml5TmpFolder()
   140  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
   141  			Ω(generateMeta(&ep, &ep, false, "cf", true, true)).Should(Succeed())
   142  			Ω(readFileContent(&dir.Loc{SourcePath: getFullPathInTmpFolder("mtahtml5", "META-INF"), Descriptor: "dep"})).
   143  				Should(Equal(readFileContent(&dir.Loc{SourcePath: getTestPath("golden"), Descriptor: "dep"})))
   144  		})
   145  
   146  		It("Generate Meta - fails on missing module path in temporary folder", func() {
   147  			createMtahtml5WithMissingModuleTmpFolder()
   148  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
   149  			Ω(generateMeta(&ep, &ep, false, "cf", false, true)).Should(HaveOccurred())
   150  		})
   151  
   152  		It("Generate Meta - doesn't fail on missing module path in temporary folder because module configured as no-source", func() {
   153  			createMtahtml5WithMissingModuleTmpFolder()
   154  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(), MtaFilename: "mtaWithNoSource.yaml"}
   155  			Ω(generateMeta(&ep, &ep, false, "cf", true, true)).Should(Succeed())
   156  			Ω(readFileContent(&dir.Loc{SourcePath: getFullPathInTmpFolder("mtahtml5", "META-INF"), Descriptor: "dep"})).
   157  				Should(Equal(readFileContent(&dir.Loc{SourcePath: getTestPath("goldenNoSource"), Descriptor: "dep"})))
   158  		})
   159  
   160  		It("Generate Meta - mta not exists", func() {
   161  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(),
   162  				MtaFilename: "mtaNotExists.yaml"}
   163  			err := generateMeta(&ep, &ep, false, "cf", true, true)
   164  			checkError(err, ep.GetMtaYamlPath())
   165  		})
   166  
   167  		Describe("mocking platform", func() {
   168  
   169  			var platformConfig []byte
   170  
   171  			BeforeEach(func() {
   172  				platformConfig = platform.PlatformConfig
   173  				platform.PlatformConfig = []byte("wrong config")
   174  			})
   175  
   176  			AfterEach(func() {
   177  				platform.PlatformConfig = platformConfig
   178  			})
   179  
   180  			It("Generate Meta fails on platform parsing", func() {
   181  				createMtahtml5TmpFolder()
   182  				ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
   183  				err := generateMeta(&ep, &ep, false, "cf", true, true)
   184  				Ω(err).Should(HaveOccurred())
   185  				Ω(err.Error()).Should(ContainSubstring(fmt.Sprintf(genMTADTypeTypeCnvMsg, "cf")))
   186  				Ω(err.Error()).Should(ContainSubstring(platform.UnmarshalFailedMsg))
   187  			})
   188  		})
   189  
   190  		It("Generate Mtar", func() {
   191  			createMtahtml5TmpFolder()
   192  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
   193  			err := generateMeta(&ep, &ep, false, "cf", true, true)
   194  			Ω(err).Should(Succeed())
   195  			mtarPath, err := generateMtar(&ep, &ep, &ep, true, "")
   196  			Ω(err).Should(Succeed())
   197  			Ω(mtarPath).Should(BeAnExistingFile())
   198  		})
   199  	})
   200  	Describe("ExecuteMerge", func() {
   201  		resultFileName := "result.yaml"
   202  		resultFilePath := getTestPath("result", resultFileName)
   203  
   204  		It("Succeeds with single mtaext file", func() {
   205  			err := ExecuteMerge(getTestPath("mta_with_ext"), getResultPath(), []string{"cf-mtaext.mtaext"}, resultFileName, os.Getwd)
   206  			Ω(err).Should(Succeed())
   207  			Ω(resultFilePath).Should(BeAnExistingFile())
   208  			compareMTAContent(getTestPath("mta_with_ext", "golden1.yaml"), resultFilePath)
   209  		})
   210  		It("Succeeds with two mtaext files", func() {
   211  			err := ExecuteMerge(getTestPath("mta_with_ext"), getResultPath(), []string{"other.mtaext", "cf-mtaext.mtaext"}, resultFileName, os.Getwd)
   212  			Ω(err).Should(Succeed())
   213  			Ω(resultFilePath).Should(BeAnExistingFile())
   214  			compareMTAContent(getTestPath("mta_with_ext", "golden2.yaml"), resultFilePath)
   215  		})
   216  		It("Fails when the result file name is not sent", func() {
   217  			err := ExecuteMerge(getTestPath("mta_with_ext"), getResultPath(), []string{"cf-mtaext.mtaext"}, "", os.Getwd)
   218  			checkError(err, mergeNameRequiredMsg)
   219  		})
   220  		It("Fails when the result file already exists", func() {
   221  			Ω(dir.CreateDirIfNotExist(getResultPath())).Should(Succeed())
   222  			createFileInGivenPath(resultFilePath)
   223  
   224  			err := ExecuteMerge(getTestPath("mta_with_ext"), getResultPath(), []string{"cf-mtaext.mtaext"}, resultFileName, os.Getwd)
   225  			checkError(err, mergeFailedOnFileCreationMsg, resultFilePath)
   226  		})
   227  		It("Fails when the result directory is a file", func() {
   228  			Ω(dir.CreateDirIfNotExist(filepath.Dir(getResultPath()))).Should(Succeed())
   229  			createFileInGivenPath(getResultPath())
   230  
   231  			err := ExecuteMerge(getTestPath("mta_with_ext"), getResultPath(), []string{"cf-mtaext.mtaext"}, resultFileName, os.Getwd)
   232  			checkError(err, dir.FolderCreationFailedMsg, getResultPath())
   233  		})
   234  		It("Fails when the mtaext file doesn't exist", func() {
   235  			err := ExecuteMerge(getTestPath("mta_with_ext"), getResultPath(), []string{"invalid.yaml"}, resultFileName, os.Getwd)
   236  			checkError(err, getTestPath("mta_with_ext", "invalid.yaml"))
   237  		})
   238  		It("Fails when wdGetter fails", func() {
   239  			err := ExecuteMerge("", getResultPath(), []string{"cf-mtaext.yaml"}, resultFileName, func() (string, error) {
   240  				return "", errors.New("an error occurred")
   241  			})
   242  			checkError(err, "an error occurred")
   243  		})
   244  	})
   245  })
   246  
   247  type testLoc struct {
   248  	loc dir.Loc
   249  }
   250  
   251  func (loc *testLoc) GetMetaPath() string {
   252  	return loc.loc.GetMetaPath()
   253  }
   254  
   255  func (loc *testLoc) GetMtadPath() string {
   256  	return loc.loc.GetMtadPath()
   257  }
   258  
   259  func (loc *testLoc) GetManifestPath() string {
   260  	return filepath.Join(loc.loc.GetManifestPath(), "folderNotExists", "MANIFEST.MF")
   261  }
   262  
   263  func (loc *testLoc) GetMtarDir(targetProvided bool) string {
   264  	return loc.loc.GetMtarDir(targetProvided)
   265  }
   266  
   267  func (loc *testLoc) GetSourceModuleDir(modulePath string) string {
   268  	return loc.loc.GetSourceModuleDir(modulePath)
   269  }
   270  
   271  func (loc *testLoc) GetTargetModuleDir(moduleName string) string {
   272  	return loc.loc.GetTargetModuleDir(moduleName)
   273  }
   274  
   275  func (loc *testLoc) GetSourceModuleArtifactRelPath(modulePath, artifactPath string) (string, error) {
   276  	return loc.loc.GetSourceModuleArtifactRelPath(modulePath, artifactPath)
   277  }
   278  
   279  func (loc *testLoc) GetTargetTmpRoot() string {
   280  	return loc.loc.GetTargetTmpRoot()
   281  }
   282  
   283  func createMtahtml5TmpFolder() {
   284  	createDirInTmpFolder("mtahtml5", "ui5app2")
   285  	createDirInTmpFolder("mtahtml5", "ui5app")
   286  	createDirInTmpFolder("mtahtml5", "META-INF")
   287  	createFileInTmpFolder("mtahtml5", "ui5app", "data.zip")
   288  	createFileInTmpFolder("mtahtml5", "ui5app2", "data.zip")
   289  	createFileInTmpFolder("mtahtml5", "xs-security.json")
   290  }
   291  
   292  func createMtahtml5WithMissingModuleTmpFolder() {
   293  	createDirInTmpFolder("mtahtml5", "ui5app2")
   294  	createDirInTmpFolder("mtahtml5", "META-INF")
   295  	createFileInTmpFolder("mtahtml5", "ui5app2", "data.zip")
   296  	createFileInTmpFolder("mtahtml5", "xs-security.json")
   297  }
   298  
   299  func compareMTAContent(expectedFileName string, actualFileName string) {
   300  	actual, err := ioutil.ReadFile(expectedFileName)
   301  	Ω(err).Should(Succeed())
   302  	actualMta, err := mta.Unmarshal(actual)
   303  	Ω(err).Should(Succeed())
   304  	expected, err := ioutil.ReadFile(actualFileName)
   305  	Ω(err).Should(Succeed())
   306  	expectedMta, err := mta.Unmarshal(expected)
   307  	Ω(err).Should(Succeed())
   308  	Ω(actualMta).Should(Equal(expectedMta))
   309  }