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

     1  package artifacts
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/ginkgo/extensions/table"
     9  	. "github.com/onsi/gomega"
    10  
    11  	"github.com/SAP/cloud-mta-build-tool/internal/archive"
    12  	"github.com/SAP/cloud-mta/mta"
    13  )
    14  
    15  var _ = Describe("Mtar", func() {
    16  	var _ = Describe("Generate", func() {
    17  
    18  		AfterEach(func() {
    19  			os.RemoveAll(getResultPath())
    20  		})
    21  
    22  		var _ = Describe("ExecuteGenMtar", func() {
    23  			It("Sanity, target provided", func() {
    24  				createMtahtml5TmpFolder()
    25  				Ω(ExecuteGenMeta(getTestPath("mtahtml5"), getResultPath(), "dev", nil, "cf", os.Getwd)).Should(Succeed())
    26  				Ω(ExecuteGenMtar(getTestPath("mtahtml5"), getResultPath(), "true", "dev", nil, "", os.Getwd)).Should(Succeed())
    27  				Ω(getTestPath("result", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile())
    28  			})
    29  
    30  			It("Sanity, target not provided", func() {
    31  				createMtahtml5TmpFolder()
    32  				Ω(ExecuteGenMeta(getTestPath("mtahtml5"), getResultPath(), "dev", nil, "cf", os.Getwd)).Should(Succeed())
    33  				Ω(ExecuteGenMtar(getTestPath("mtahtml5"), getResultPath(), "false", "dev", nil, "", os.Getwd)).Should(Succeed())
    34  				Ω(getTestPath("result", "mta_archives", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile())
    35  			})
    36  
    37  			It("Fails on location initialization", func() {
    38  				Ω(ExecuteGenMtar("", getResultPath(), "true", "dev", nil, "", func() (string, error) {
    39  					return "", errors.New("err")
    40  				})).Should(HaveOccurred())
    41  			})
    42  
    43  			It("Fails - wrong source", func() {
    44  				Ω(ExecuteGenMtar(getTestPath("mtahtml6"), getResultPath(), "true", "dev", nil, "", os.Getwd)).Should(HaveOccurred())
    45  			})
    46  		})
    47  
    48  		It("Generate Mtar - Sanity", func() {
    49  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
    50  			createMtahtml5TmpFolder()
    51  			Ω(generateMeta(&ep, &ep, false, "cf", true, true)).Should(Succeed())
    52  			mtarPath, err := generateMtar(&ep, &ep, &ep, true, "")
    53  			Ω(err).Should(Succeed())
    54  			Ω(mtarPath).Should(BeAnExistingFile())
    55  		})
    56  
    57  		It("Generate Mtar - Fails on wrong source", func() {
    58  			ep := dir.Loc{SourcePath: getTestPath("not_existing"), TargetPath: getResultPath()}
    59  			ep1 := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
    60  			_, err := generateMtar(&ep, &ep1, &ep1, true, "")
    61  			Ω(err).Should(HaveOccurred())
    62  		})
    63  
    64  		It("Generate Mtar - Invalid mta", func() {
    65  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(), MtaFilename: "mtaBroken.yaml"}
    66  			_, err := generateMtar(&ep, &ep, &ep, true, "")
    67  			Ω(err).Should(HaveOccurred())
    68  		})
    69  		It("Generate Mtar - Mta not exists", func() {
    70  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(), MtaFilename: "mtaNotExists.yaml"}
    71  			_, err := generateMtar(&ep, &ep, &ep, true, "")
    72  			Ω(err).Should(HaveOccurred())
    73  		})
    74  		It("Generate Mtar - results file exists, folder results can't be created ", func() {
    75  			file, _ := os.Create(getTestPath("result"))
    76  			defer file.Close()
    77  			ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
    78  			_, err := generateMtar(&ep, &ep, &ep, true, "")
    79  			Ω(err).Should(HaveOccurred())
    80  		})
    81  		DescribeTable("isTargetProvided", func(target, provided string, expected bool) {
    82  			Ω(isTargetProvided(target, provided)).Should(Equal(expected))
    83  		},
    84  			Entry("Sanity", "", "true", true),
    85  			Entry("Wrong provided value", "", "xx", false),
    86  			Entry("Empty provided value, target path provided", "path", "", true),
    87  			Entry("Empty provided value, no target path provided", "", "", false),
    88  		)
    89  
    90  		var _ = Describe("Target Failures", func() {
    91  			var _ = DescribeTable("Invalid location", func(loc *testMtarLoc) {
    92  				ep := dir.Loc{}
    93  				_, err := generateMtar(loc, &ep, &ep, true, "")
    94  				Ω(err).Should(HaveOccurred())
    95  			},
    96  				Entry("Fails on GetTargetTmpDir", &testMtarLoc{
    97  					tmpDir:    "",
    98  					targetDir: getResultPath(),
    99  				}),
   100  				Entry("Fails on GetTarget", &testMtarLoc{
   101  					tmpDir:    getTestPath("result", "mtahtml5", "mtahtml5"),
   102  					targetDir: "",
   103  				}))
   104  		})
   105  	})
   106  
   107  	var _ = DescribeTable("getMtarFileName", func(mtarName, expected string) {
   108  		m := mta.MTA{ID: "proj", Version: "0.1.5"}
   109  		Ω(getMtarFileName(&m, mtarName)).Should(Equal(expected))
   110  	},
   111  		Entry("default mtar", "", "proj_0.1.5.mtar"),
   112  		Entry("default supporting make file", "*", "proj_0.1.5.mtar"),
   113  		Entry("default supporting make file", "abc", "abc.mtar"),
   114  		Entry("default supporting make file", "abc.zip", "abc.zip"),
   115  	)
   116  })
   117  
   118  type testMtarLoc struct {
   119  	tmpDir    string
   120  	targetDir string
   121  }
   122  
   123  func (loc *testMtarLoc) GetTarget() string {
   124  	return loc.targetDir
   125  }
   126  func (loc *testMtarLoc) GetTargetTmpDir() string {
   127  	return loc.tmpDir
   128  }