Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade MBT build command "strict" parameter #1108

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var initCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// Generate build script
err := tpl.ExecuteMake(initCmdSrc, initCmdTrg, initCmdExtensions, makefile, initCmdMode, os.Getwd, true)
err := tpl.ExecuteMake(initCmdSrc, initCmdTrg, initCmdExtensions, makefile, initCmdMode, os.Getwd, true, buildCmdStrict)
logError(err)
},
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/mtad.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var mtadGenCmd = &cobra.Command{
Long: "Generates a deployment descriptor ('mtad.yaml') file from the 'mta.yaml' file",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
err := artifacts.ExecuteMtadGen(mtadGenCmdSrc, mtadGenCmdTrg, mtadGenCmdExtensions, mtadGenCmdPlatform, os.Getwd)
err := artifacts.ExecuteMtadGen(mtadGenCmdSrc, mtadGenCmdTrg, mtadGenCmdExtensions, mtadGenCmdPlatform, os.Getwd, buildCmdStrict)
logError(err)
return err
},
Expand All @@ -39,5 +39,7 @@ func init() {
"The MTA extension descriptors")
mtadGenCmd.Flags().StringVarP(&mtadGenCmdPlatform, "platform", "p", "cf",
`The deployment platform; supported platforms: "cf", "xsa", "neo"`)
mtadGenCmd.Flags().BoolVarP(&buildCmdStrict, "strict", "", true,
`If set to true, duplicated fields and fields not defined in the "mta.yaml" schema are reported as errors; if set to false, they are reported as warnings`)
mtadGenCmd.Flags().BoolP("help", "h", false, `Displays detailed information about the 'mtad gen' command`)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/SAP/cloud-mta-build-tool
go 1.13

require (
github.com/SAP/cloud-mta v1.0.6
github.com/SAP/cloud-mta v1.0.8
github.com/deckarep/golang-set v1.7.1
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mattn/goveralls v0.0.11 // indirect
Expand All @@ -17,4 +17,4 @@ require (
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/sys v0.0.0-20220222200937-f2425489ef4c // indirect
gopkg.in/yaml.v2 v2.4.0
)
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/SAP/cloud-mta v1.0.6 h1:LKCeqiAevPwAnMkuHyK/2JhL3RM7FpRs3mF1hmQwHGY=
github.com/SAP/cloud-mta v1.0.6/go.mod h1:raToVpkWBCajEnid35ewX0BNsrGpZgZ62m70MGhai1M=
github.com/SAP/cloud-mta v1.0.8 h1:b3/kN4vmcfH8CHoyVQ27no/lcncWTnQ35hDEunuqXHw=
github.com/SAP/cloud-mta v1.0.8/go.mod h1:raToVpkWBCajEnid35ewX0BNsrGpZgZ62m70MGhai1M=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down
7 changes: 6 additions & 1 deletion internal/archive/module_location.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package dir

import (
"github.com/SAP/cloud-mta/mta"
"path/filepath"

"github.com/SAP/cloud-mta/mta"
)

// ModuleLoc - module location type that provides services for stand alone module build command
Expand Down Expand Up @@ -47,6 +48,10 @@ func (ep *ModuleLoc) ParseFile() (*mta.MTA, error) {
return ep.loc.ParseFile()
}

func (ep *ModuleLoc) SetStrictParmeter(strick bool) bool {
return ep.loc.SetStrictParmeter(strick)
}

// ModuleLocation - provides target location of stand alone MTA module build result
func ModuleLocation(loc *Loc, targetPathDefined bool) *ModuleLoc {
return &ModuleLoc{loc: loc, targetPathDefined: targetPathDefined}
Expand Down
10 changes: 9 additions & 1 deletion internal/archive/module_location_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dir

import (
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
)

var _ = Describe("ModuleLocation", func() {
Expand Down Expand Up @@ -68,6 +69,13 @@ var _ = Describe("ModuleLocation", func() {
Ω(module2.Parameters["memory"]).Should(Equal("256M"))
})

It("SetStrictParmeter", func() {
projectLoc, err := Location(getPath("testdata"), getPath("testdata"), Dev, []string{}, os.Getwd)
Ω(err).Should(Succeed())
moduleLoc := ModuleLocation(projectLoc, false)
Ω(moduleLoc.SetStrictParmeter(true)).Should(Equal(true))
})

It("ModuleLocation", func() {
target := getPath("testdata", "result")
projectLoc, err := Location(getPath("testdata"), target, Dev, []string{}, os.Getwd)
Expand Down
21 changes: 19 additions & 2 deletions internal/archive/mta_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/pkg/errors"

"github.com/SAP/cloud-mta-build-tool/internal/logs"
"github.com/SAP/cloud-mta/mta"
)

Expand All @@ -25,9 +26,12 @@ const (
SBomTempFolderSuffix = "_mta_sbom_tmp"
)

var strict bool = true

// IMtaParser - MTA Parser interface
type IMtaParser interface {
ParseFile() (*mta.MTA, error)
SetStrictParmeter(bStrict bool) bool
}

// IDescriptor - descriptor interface
Expand Down Expand Up @@ -234,10 +238,23 @@ func (ep *Loc) IsDeploymentDescriptor() bool {
return ep.Descriptor == Dep
}

func (ep *Loc) SetStrictParmeter(bStrict bool) bool {
strict = bStrict
return strict
}

// ParseFile returns a reference to the MTA object resulting from the given mta.yaml file merged with the extension descriptors.
func (ep *Loc) ParseFile() (*mta.MTA, error) {
mtaFile, _, err := mta.GetMtaFromFile(ep.GetMtaYamlPath(), ep.GetExtensionFilePaths(), true)
return mtaFile, err
if strict == true {
mtaFile, _, err := mta.GetMtaFromFile(ep.GetMtaYamlPath(), ep.GetExtensionFilePaths(), true)
return mtaFile, err
} else {
mtaFile, msg, err := mta.GetMtaFromFileWithStrict(ep.GetMtaYamlPath(), ep.GetExtensionFilePaths(), true, strict)
if msg != nil {
logs.Logger.Warning(msg)
}
return mtaFile, err
}
}

// GetExtensionFilePaths returns the MTA extension descriptor full paths
Expand Down
2 changes: 2 additions & 0 deletions internal/archive/mta_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ var _ = Describe("ParseFile", func() {

It("Parse the mta.yaml file and returns it when there are no extension files", func() {
ep := Loc{SourcePath: filepath.Join(wd, "testdata", "testext")}
Ω(ep.SetStrictParmeter(true)).Should(Equal(true))

mta, err := ep.ParseFile()
Ω(mta).ShouldNot(BeNil())
Ω(err).Should(Succeed())
Expand Down
14 changes: 8 additions & 6 deletions internal/artifacts/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package artifacts

import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"

"gopkg.in/yaml.v2"

"github.com/pkg/errors"

"github.com/SAP/cloud-mta-build-tool/internal/archive"
dir "github.com/SAP/cloud-mta-build-tool/internal/archive"
"github.com/SAP/cloud-mta-build-tool/internal/logs"
"github.com/SAP/cloud-mta-build-tool/internal/platform"
"github.com/SAP/cloud-mta/mta"
Expand All @@ -22,10 +23,10 @@ func ExecuteGenMeta(source, target, desc string, extensions []string, platform s
if err != nil {
return errors.Wrap(err, "failed to generate metadata when initializing the location")
}
return executeGenMetaByLocation(loc, loc, platform, true, true)
return executeGenMetaByLocation(loc, loc, platform, true, true, true)
}

func executeGenMetaByLocation(loc *dir.Loc, targetArtifacts dir.ITargetArtifacts, platform string, createMetaInf bool, validatePaths bool) error {
func executeGenMetaByLocation(loc *dir.Loc, targetArtifacts dir.ITargetArtifacts, platform string, createMetaInf bool, validatePaths bool, strict bool) error {
// validate platform
platform, err := validatePlatform(platform)
if err != nil {
Expand All @@ -37,14 +38,15 @@ func executeGenMetaByLocation(loc *dir.Loc, targetArtifacts dir.ITargetArtifacts
return err
}

err = generateMeta(loc, targetArtifacts, loc.IsDeploymentDescriptor(), platform, createMetaInf, validatePaths)
err = generateMeta(loc, targetArtifacts, loc.IsDeploymentDescriptor(), platform, createMetaInf, validatePaths, strict)
return err
}

// generateMeta - generate metadata artifacts
func generateMeta(loc *dir.Loc, targetArtifacts dir.ITargetArtifacts, deploymentDescriptor bool, platform string, createMetaInf bool, validatePaths bool) error {
func generateMeta(loc *dir.Loc, targetArtifacts dir.ITargetArtifacts, deploymentDescriptor bool, platform string, createMetaInf bool, validatePaths bool, strict bool) error {

// parse MTA file
loc.SetStrictParmeter(strict)
m, err := loc.ParseFile()
if err != nil {
return errors.Wrapf(err, genMetaMsg)
Expand Down
12 changes: 6 additions & 6 deletions internal/artifacts/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,29 @@ cli_version:["x"]
It("Generate Meta", func() {
createMtahtml5TmpFolder()
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
Ω(generateMeta(&ep, &ep, false, "cf", true, true)).Should(Succeed())
Ω(generateMeta(&ep, &ep, false, "cf", true, true, true)).Should(Succeed())
Ω(readFileContent(&dir.Loc{SourcePath: getFullPathInTmpFolder("mtahtml5", "META-INF"), Descriptor: "dep"})).
Should(Equal(readFileContent(&dir.Loc{SourcePath: getTestPath("golden"), Descriptor: "dep"})))
})

It("Generate Meta - fails on missing module path in temporary folder", func() {
createMtahtml5WithMissingModuleTmpFolder()
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
Ω(generateMeta(&ep, &ep, false, "cf", false, true)).Should(HaveOccurred())
Ω(generateMeta(&ep, &ep, false, "cf", false, true, true)).Should(HaveOccurred())
})

It("Generate Meta - doesn't fail on missing module path in temporary folder because module configured as no-source", func() {
createMtahtml5WithMissingModuleTmpFolder()
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(), MtaFilename: "mtaWithNoSource.yaml"}
Ω(generateMeta(&ep, &ep, false, "cf", true, true)).Should(Succeed())
Ω(generateMeta(&ep, &ep, false, "cf", true, true, true)).Should(Succeed())
Ω(readFileContent(&dir.Loc{SourcePath: getFullPathInTmpFolder("mtahtml5", "META-INF"), Descriptor: "dep"})).
Should(Equal(readFileContent(&dir.Loc{SourcePath: getTestPath("goldenNoSource"), Descriptor: "dep"})))
})

It("Generate Meta - mta not exists", func() {
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(),
MtaFilename: "mtaNotExists.yaml"}
err := generateMeta(&ep, &ep, false, "cf", true, true)
err := generateMeta(&ep, &ep, false, "cf", true, true, true)
checkError(err, ep.GetMtaYamlPath())
})

Expand All @@ -180,7 +180,7 @@ cli_version:["x"]
It("Generate Meta fails on platform parsing", func() {
createMtahtml5TmpFolder()
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
err := generateMeta(&ep, &ep, false, "cf", true, true)
err := generateMeta(&ep, &ep, false, "cf", true, true, true)
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring(fmt.Sprintf(genMTADTypeTypeCnvMsg, "cf")))
Ω(err.Error()).Should(ContainSubstring(platform.UnmarshalFailedMsg))
Expand All @@ -190,7 +190,7 @@ cli_version:["x"]
It("Generate Mtar", func() {
createMtahtml5TmpFolder()
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
err := generateMeta(&ep, &ep, false, "cf", true, true)
err := generateMeta(&ep, &ep, false, "cf", true, true, true)
Ω(err).Should(Succeed())
mtarPath, err := generateMtar(&ep, &ep, &ep, true, "")
Ω(err).Should(Succeed())
Expand Down
6 changes: 3 additions & 3 deletions internal/artifacts/mtad.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/SAP/cloud-mta/mta"

"github.com/SAP/cloud-mta-build-tool/internal/archive"
dir "github.com/SAP/cloud-mta-build-tool/internal/archive"
"github.com/SAP/cloud-mta-build-tool/internal/buildops"
"github.com/SAP/cloud-mta-build-tool/internal/logs"
)
Expand All @@ -39,14 +39,14 @@ func (loc *mtadLoc) GetMtarDir(targetProvided bool) string {
}

// ExecuteMtadGen - generates MTAD from MTA
func ExecuteMtadGen(source, target string, extensions []string, platform string, wdGetter func() (string, error)) error {
func ExecuteMtadGen(source, target string, extensions []string, platform string, wdGetter func() (string, error), strict bool) error {
logs.Logger.Info("generating the MTAD file...")
loc, err := dir.Location(source, target, dir.Dev, extensions, wdGetter)
if err != nil {
return errors.Wrap(err, "generation of the MTAD file failed when initializing the location")
}

return executeGenMetaByLocation(loc, &mtadLoc{target}, platform, false, false)
return executeGenMetaByLocation(loc, &mtadLoc{target}, platform, false, false, strict)
}

func validatePlatform(platform string) (string, error) {
Expand Down
16 changes: 8 additions & 8 deletions internal/artifacts/mtad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
"gopkg.in/yaml.v2"

"github.com/SAP/cloud-mta-build-tool/internal/archive"
dir "github.com/SAP/cloud-mta-build-tool/internal/archive"
"github.com/SAP/cloud-mta-build-tool/internal/buildops"
"github.com/SAP/cloud-mta-build-tool/internal/platform"
"github.com/SAP/cloud-mta/mta"
Expand All @@ -27,36 +27,36 @@ var _ = Describe("Mtad", func() {

var _ = Describe("ExecuteMtadGen", func() {
It("Sanity", func() {
Ω(ExecuteMtadGen(getTestPath("mta"), getTestPath("result"), nil, "cf", os.Getwd)).Should(Succeed())
Ω(ExecuteMtadGen(getTestPath("mta"), getTestPath("result"), nil, "cf", os.Getwd, true)).Should(Succeed())
Ω(getTestPath("result", "mtad.yaml")).Should(BeAnExistingFile())
})
It("Fails on creating META-INF folder", func() {
createDirInTmpFolder("mta")
file, err := os.Create(getFullPathInTmpFolder("mta", "META-INF"))
Ω(err).Should(Succeed())
Ω(file.Close()).Should(Succeed())
Ω(ExecuteMtadGen(getTestPath("mta", "META-INF"), getTestPath("result"), nil, "cf", os.Getwd)).Should(HaveOccurred())
Ω(ExecuteMtadGen(getTestPath("mta", "META-INF"), getTestPath("result"), nil, "cf", os.Getwd, true)).Should(HaveOccurred())
})
It("Fails on location initialization", func() {
Ω(ExecuteMtadGen("", getTestPath("result"), nil, "cf", func() (string, error) {
return "", errors.New("err")
})).Should(HaveOccurred())
}, true)).Should(HaveOccurred())
})
It("Fails on platform validation", func() {
Ω(ExecuteMtadGen(getTestPath("mta"), getTestPath("result"), nil, "ab", func() (string, error) {
return "", errors.New("err")
})).Should(HaveOccurred())
}, true)).Should(HaveOccurred())
})
It("Fails on broken extension file - parse ext fails", func() {
Ω(ExecuteMtadGen(getTestPath("mtaWithBrokenExt"), getTestPath("result"), []string{"cf-mtaext.yaml"}, "cf", os.Getwd)).Should(HaveOccurred())
Ω(ExecuteMtadGen(getTestPath("mtaWithBrokenExt"), getTestPath("result"), []string{"cf-mtaext.yaml"}, "cf", os.Getwd, true)).Should(HaveOccurred())
})
It("Fails on wrong source path - parse fails", func() {
Ω(ExecuteMtadGen(getTestPath("mtax"), getTestPath("result"), nil, "cf", os.Getwd)).Should(HaveOccurred())
Ω(ExecuteMtadGen(getTestPath("mtax"), getTestPath("result"), nil, "cf", os.Getwd, true)).Should(HaveOccurred())
})
It("Fails on broken platforms configuration", func() {
cfg := platform.PlatformConfig
platform.PlatformConfig = []byte("abc abc")
Ω(ExecuteMtadGen(getTestPath("mta"), getTestPath("result"), nil, "cf", os.Getwd)).Should(HaveOccurred())
Ω(ExecuteMtadGen(getTestPath("mta"), getTestPath("result"), nil, "cf", os.Getwd, true)).Should(HaveOccurred())
platform.PlatformConfig = cfg
})
})
Expand Down
4 changes: 2 additions & 2 deletions internal/artifacts/mtar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"

"github.com/SAP/cloud-mta-build-tool/internal/archive"
dir "github.com/SAP/cloud-mta-build-tool/internal/archive"
"github.com/SAP/cloud-mta/mta"
)

Expand Down Expand Up @@ -48,7 +48,7 @@ var _ = Describe("Mtar", func() {
It("Generate Mtar - Sanity", func() {
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
createMtahtml5TmpFolder()
Ω(generateMeta(&ep, &ep, false, "cf", true, true)).Should(Succeed())
Ω(generateMeta(&ep, &ep, false, "cf", true, true, true)).Should(Succeed())
mtarPath, err := generateMtar(&ep, &ep, &ep, true, "")
Ω(err).Should(Succeed())
Ω(mtarPath).Should(BeAnExistingFile())
Expand Down
2 changes: 1 addition & 1 deletion internal/artifacts/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ExecBuild(makefileTmp, source, target string, extensions []string, mode, mt
}

// (1) generate build script
err = tpl.ExecuteMake(source, "", extensions, makefileTmp, mode, wdGetter, useDefaultMbt)
err = tpl.ExecuteMake(source, "", extensions, makefileTmp, mode, wdGetter, useDefaultMbt, strict)
if err != nil {
return err
}
Expand Down
Loading