1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Merge branch 'master' into harmonize-docker-arguments

This commit is contained in:
Ramachandra Kamath Arbettu 2019-11-04 16:32:29 +01:00 committed by GitHub
commit d89109f23e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 287 additions and 44 deletions

View File

@ -8,6 +8,8 @@ RUN go test ./... -cover
## ONLY tests so far, building to be added later
# execute build
# RUN go build -o piper
RUN export GIT_COMMIT=$(git rev-parse HEAD) && \
go build -ldflags "-X github.com/SAP/jenkins-library/cmd.GitCommit=${GIT_COMMIT}" -o piper
# FROM gcr.io/distroless/base:latest
# COPY --from=build-env /build/piper /piper

View File

@ -4,3 +4,8 @@ type execRunner interface {
RunExecutable(e string, p ...string) error
Dir(d string)
}
type shellRunner interface {
RunShell(s string, c string) error
Dir(d string)
}

View File

@ -4,30 +4,39 @@ import (
"strings"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/pkg/errors"
"github.com/SAP/jenkins-library/pkg/log"
)
func karmaExecuteTests(myKarmaExecuteTestsOptions karmaExecuteTestsOptions) error {
c := command.Command{}
return runKarma(myKarmaExecuteTestsOptions, &c)
// reroute command output to loging framework
// also log stdout as Karma reports into it
c.Stdout = log.Entry().Writer()
c.Stderr = log.Entry().Writer()
runKarma(myKarmaExecuteTestsOptions, &c)
return nil
}
func runKarma(myKarmaExecuteTestsOptions karmaExecuteTestsOptions, command execRunner) error {
func runKarma(myKarmaExecuteTestsOptions karmaExecuteTestsOptions, command execRunner) {
installCommandTokens := tokenize(myKarmaExecuteTestsOptions.InstallCommand)
command.Dir(myKarmaExecuteTestsOptions.ModulePath)
err := command.RunExecutable(installCommandTokens[0], installCommandTokens[1:]...)
if err != nil {
return errors.Wrapf(err, "failed to execute install command '%v'", myKarmaExecuteTestsOptions.InstallCommand)
log.Entry().
WithError(err).
WithField("command", myKarmaExecuteTestsOptions.InstallCommand).
Fatal("failed to execute install command")
}
runCommandTokens := tokenize(myKarmaExecuteTestsOptions.RunCommand)
command.Dir(myKarmaExecuteTestsOptions.ModulePath)
err = command.RunExecutable(runCommandTokens[0], runCommandTokens[1:]...)
if err != nil {
return errors.Wrapf(err, "failed to execute run command '%v'", myKarmaExecuteTestsOptions.RunCommand)
log.Entry().
WithError(err).
WithField("command", myKarmaExecuteTestsOptions.RunCommand).
Fatal("failed to execute run command")
}
return nil
}
func tokenize(command string) []string {

View File

@ -4,6 +4,7 @@ import (
//"os"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/spf13/cobra"
)
@ -34,6 +35,8 @@ In the Docker network, the containers can be referenced by the values provided i
!!! note
In a Kubernetes environment, the containers both need to be referenced with ` + "`" + `localhost` + "`" + `.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
log.SetStepName("karmaExecuteTests")
log.SetVerbose(generalConfig.verbose)
return PrepareConfig(cmd, &metadata, "karmaExecuteTests", &myKarmaExecuteTestsOptions, openPiperFile)
},
RunE: func(cmd *cobra.Command, args []string) error {

View File

@ -1,43 +1,18 @@
package cmd
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/stretchr/testify/assert"
)
type mockRunner struct {
dir []string
calls []execCall
}
type execCall struct {
exec string
params []string
}
func (m *mockRunner) Dir(d string) {
m.dir = append(m.dir, d)
}
func (m *mockRunner) RunExecutable(e string, p ...string) error {
if e == "fail" {
return fmt.Errorf("error case")
}
exec := execCall{exec: e, params: p}
m.calls = append(m.calls, exec)
return nil
}
func TestRunKarma(t *testing.T) {
t.Run("success case", func(t *testing.T) {
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "npm run test"}
e := mockRunner{}
err := runKarma(opts, &e)
assert.NoError(t, err, "error occured but no error expected")
e := execMockRunner{}
runKarma(opts, &e)
assert.Equal(t, e.dir[0], "./test", "install command dir incorrect")
assert.Equal(t, e.calls[0], execCall{exec: "npm", params: []string{"install", "test"}}, "install command/params incorrect")
@ -48,18 +23,24 @@ func TestRunKarma(t *testing.T) {
})
t.Run("error case install command", func(t *testing.T) {
var hasFailed bool
log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "fail install test", RunCommand: "npm run test"}
e := mockRunner{}
err := runKarma(opts, &e)
assert.Error(t, err, "error expected but none occcured")
e := execMockRunner{}
runKarma(opts, &e)
assert.True(t, hasFailed, "expected command to exit with fatal")
})
t.Run("error case run command", func(t *testing.T) {
var hasFailed bool
log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "fail run test"}
e := mockRunner{}
err := runKarma(opts, &e)
assert.Error(t, err, "error expected but none occcured")
e := execMockRunner{}
runKarma(opts, &e)
assert.True(t, hasFailed, "expected command to exit with fatal")
})
}

View File

@ -39,6 +39,8 @@ var generalConfig generalConfigOptions
func Execute() {
rootCmd.AddCommand(ConfigCommand())
rootCmd.AddCommand(VersionCommand())
rootCmd.AddCommand(KarmaExecuteTestsCommand())
addRootFlags(rootCmd)
if err := rootCmd.Execute(); err != nil {

View File

@ -1,6 +1,7 @@
package cmd
import (
"fmt"
"io"
"io/ioutil"
"strings"
@ -12,6 +13,44 @@ import (
"github.com/stretchr/testify/assert"
)
type execMockRunner struct {
dir []string
calls []execCall
}
type execCall struct {
exec string
params []string
}
type shellMockRunner struct {
dir string
calls []string
}
func (m *execMockRunner) Dir(d string) {
m.dir = append(m.dir, d)
}
func (m *execMockRunner) RunExecutable(e string, p ...string) error {
if e == "fail" {
return fmt.Errorf("error case")
}
exec := execCall{exec: e, params: p}
m.calls = append(m.calls, exec)
return nil
}
func(m *shellMockRunner) Dir(d string) {
m.dir = d
}
func(m *shellMockRunner) RunShell(s string, c string) error {
m.calls = append(m.calls, c)
return nil
}
type stepOptions struct {
TestParam string `json:"testParam,omitempty"`
}

28
cmd/version.go Normal file
View File

@ -0,0 +1,28 @@
package cmd
import (
"fmt"
)
// GitCommit ...
var GitCommit string
// GitTag ...
var GitTag string
func version(myVersionOptions versionOptions) error {
gitCommit, gitTag := "<n/a>", "<n/a>"
if len(GitCommit) > 0 {
gitCommit = GitCommit
}
if len(GitTag) > 0 {
gitTag = GitTag
}
_, err := fmt.Printf("piper-version:\n commit: \"%s\"\n tag: \"%s\"\n", gitCommit, gitTag)
return err
}

52
cmd/version_generated.go Normal file
View File

@ -0,0 +1,52 @@
package cmd
import (
//"os"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/spf13/cobra"
)
type versionOptions struct {
}
var myVersionOptions versionOptions
var versionStepConfigJSON string
// VersionCommand Returns the version of the piper binary
func VersionCommand() *cobra.Command {
metadata := versionMetadata()
var createVersionCmd = &cobra.Command{
Use: "version",
Short: "Returns the version of the piper binary",
Long: `Writes the commit hash and the tag (if any) to stdout and exits with 0.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
log.SetStepName("version")
log.SetVerbose(generalConfig.verbose)
return PrepareConfig(cmd, &metadata, "version", &myVersionOptions, openPiperFile)
},
RunE: func(cmd *cobra.Command, args []string) error {
return version(myVersionOptions)
},
}
addVersionFlags(createVersionCmd)
return createVersionCmd
}
func addVersionFlags(cmd *cobra.Command) {
}
// retrieve step metadata
func versionMetadata() config.StepData {
var theMetaData = config.StepData{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{},
},
},
}
return theMetaData
}

View File

@ -0,0 +1,16 @@
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestVersionCommand(t *testing.T) {
testCmd := VersionCommand()
// only high level testing performed - details are tested in step generation procudure
assert.Equal(t, "version", testCmd.Use, "command name incorrect")
}

61
cmd/version_test.go Normal file
View File

@ -0,0 +1,61 @@
package cmd
import (
"testing"
"os"
"bytes"
"io"
"github.com/stretchr/testify/assert"
)
func TestVersion(t *testing.T) {
t.Run("versionAndTagInitialValues", func(t *testing.T) {
result := runVersionCommand(t, "", "")
assert.Contains(t, result, "commit: \"<n/a>\"")
assert.Contains(t, result, "tag: \"<n/a>\"")
})
t.Run("versionAndTagSet", func(t *testing.T) {
result := runVersionCommand(t, "16bafe", "v1.2.3")
assert.Contains(t, result, "commit: \"16bafe\"")
assert.Contains(t, result, "tag: \"v1.2.3\"")
})
}
func runVersionCommand(t *testing.T, commitID, tag string) string {
orig := os.Stdout
defer func() {os.Stdout = orig}()
r,w,e := os.Pipe()
if e != nil {
t.Error("Cannot setup pipes.")
}
os.Stdout = w
//
// needs to be set in the free wild by the build process:
// go build -ldflags "-X github.com/SAP/jenkins-library/cmd.GitCommit=${GIT_COMMIT} -X github.com/SAP/jenkins-library/cmd.GitTag=${GIT_TAG}"
if len(commitID) > 0 { GitCommit = commitID; }
if len(tag) > 0 { GitTag = tag }
defer func() { GitCommit = ""; GitTag = "" }()
//
//
var myVersionOptions versionOptions
e = version(myVersionOptions)
if e != nil {
t.Error("Version command failed.")
}
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.13
require (
github.com/ghodss/yaml v1.0.0
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.2.2

6
go.sum
View File

@ -12,6 +12,7 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
@ -21,6 +22,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
@ -30,12 +33,15 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

View File

@ -114,7 +114,6 @@ func TestPrepareOut(t *testing.T) {
}
func TestCmdPipes(t *testing.T) {
//cmd := helperCommand(t, "echo", "foo bar", "baz")
cmd := helperCommand("echo", "foo bar", "baz")
defer func() { ExecCommand = exec.Command }()

View File

@ -33,6 +33,7 @@ import (
//"os"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/spf13/cobra"
)
@ -52,6 +53,8 @@ func {{.CobraCmdFuncName}}() *cobra.Command {
Short: "{{.Short}}",
Long: {{ $tick := "` + "`" + `" }}{{ $tick }}{{.Long | longName }}{{ $tick }},
PreRunE: func(cmd *cobra.Command, args []string) error {
log.SetStepName("{{ .StepName }}")
log.SetVerbose(generalConfig.verbose)
return PrepareConfig(cmd, &metadata, "{{ .StepName }}", &my{{ .StepName | title}}Options, openPiperFile)
},
RunE: func(cmd *cobra.Command, args []string) error {

View File

@ -4,6 +4,7 @@ import (
//"os"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/spf13/cobra"
)
@ -24,6 +25,8 @@ func TestStepCommand() *cobra.Command {
Short: "Test description",
Long: `Long Test description`,
PreRunE: func(cmd *cobra.Command, args []string) error {
log.SetStepName("testStep")
log.SetVerbose(generalConfig.verbose)
return PrepareConfig(cmd, &metadata, "testStep", &myTestStepOptions, openPiperFile)
},
RunE: func(cmd *cobra.Command, args []string) error {

28
pkg/log/log.go Normal file
View File

@ -0,0 +1,28 @@
package log
import (
"github.com/sirupsen/logrus"
)
var logger *logrus.Entry
// Entry returns the logger entry or creates one if none is present.
func Entry() *logrus.Entry {
if logger == nil {
logger = logrus.WithField("library", "sap/jenkins-library")
}
return logger
}
// SetVerbose sets the log level with respect to verbose flag.
func SetVerbose(verbose bool) {
if verbose {
//Logger().Debugf("logging set to level: %s", level)
logrus.SetLevel(logrus.DebugLevel)
}
}
// SetStepName sets the stepName field.
func SetStepName(stepName string) {
logger = Entry().WithField("stepName", stepName)
}

View File

@ -440,14 +440,14 @@ steps:
noDefaultExludes: []
pipelineStashFilesBeforeBuild:
stashIncludes:
buildDescriptor: '**/pom.xml, **/.mvn/**, **/assembly.xml, **/.swagger-codegen-ignore, **/package.json, **/requirements.txt, **/setup.py, **/mta*.y*ml, **/.npmrc, Dockerfile, .hadolint.yaml, **/VERSION, **/version.txt, **/Gopkg.*, **/dub.json, **/dub.sdl, **/build.sbt, **/sbtDescriptor.json, **/project/*'
buildDescriptor: '**/pom.xml, **/.mvn/**, **/assembly.xml, **/.swagger-codegen-ignore, **/package.json, **/requirements.txt, **/setup.py, **/mta*.y*ml, **/.npmrc, Dockerfile, .hadolint.yaml, **/VERSION, **/version.txt, **/Gopkg.*, **/dub.json, **/dub.sdl, **/build.sbt, **/sbtDescriptor.json, **/project/*, **/ui5.yaml, **/ui5.yml'
deployDescriptor: '**/manifest*.y*ml, **/*.mtaext.y*ml, **/*.mtaext, **/xs-app.json, helm/**, *.y*ml'
git: '.git/**'
opa5: '**/*.*'
opensourceConfiguration: '**/srcclr.yml, **/vulas-custom.properties, **/.nsprc, **/.retireignore, **/.retireignore.json, **/.snyk, **/wss-unified-agent.config, **/vendor/**/*'
pipelineConfigAndTests: '.pipeline/**'
securityDescriptor: '**/xs-security.json'
tests: '**/pom.xml, **/*.json, **/*.xml, **/src/**, **/node_modules/**, **/specs/**, **/env/**, **/*.js, **/tests/**'
tests: '**/pom.xml, **/*.json, **/*.xml, **/src/**, **/node_modules/**, **/specs/**, **/env/**, **/*.js, **/tests/**, **/*.html, **/*.css, **/*.properties'
stashExcludes:
buildDescriptor: '**/node_modules/**/package.json'
deployDescriptor: ''

View File

@ -0,0 +1,5 @@
metadata:
name: version
description: Returns the version of the piper binary
longDescription: |
Writes the commit hash and the tag (if any) to stdout and exits with 0.