1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-11-06 09:09:19 +02:00

feat(gauge): migrate gaugeExecuteTests to go implementation (#2775)

* gaugeExecuteTests converted to golang

* rewrited gaugeExecuteTests to cross-platform implementation. Now gauge uses npm

* regenerated

* groovy file import fix

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
Mikalai Dzemidzenka
2021-06-01 14:15:10 +03:00
committed by GitHub
parent 3b2e7dc53d
commit c38d231820
29 changed files with 997 additions and 323 deletions

118
cmd/gaugeExecuteTests.go Normal file
View File

@@ -0,0 +1,118 @@
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
var ErrorGaugeInstall error = errors.New("error installing gauge")
var ErrorGaugeRunnerInstall error = errors.New("error installing runner")
var ErrorGaugeRun error = errors.New("error running gauge")
type gaugeExecuteTestsUtils interface {
FileExists(filename string) (bool, error)
MkdirAll(path string, perm os.FileMode) error
SetEnv([]string)
RunExecutable(executable string, params ...string) error
Stdout(io.Writer)
Getenv(key string) string
}
type gaugeExecuteTestsUtilsBundle struct {
*command.Command
*piperutils.Files
}
func newGaugeExecuteTestsUtils() gaugeExecuteTestsUtils {
utils := gaugeExecuteTestsUtilsBundle{
Command: &command.Command{},
Files: &piperutils.Files{},
}
utils.Stdout(log.Writer())
utils.Stderr(log.Writer())
return &utils
}
func gaugeExecuteTests(config gaugeExecuteTestsOptions, telemetryData *telemetry.CustomData, influx *gaugeExecuteTestsInflux) {
utils := newGaugeExecuteTestsUtils()
influx.step_data.fields.gauge = false
err := runGaugeExecuteTests(&config, telemetryData, utils)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
influx.step_data.fields.gauge = true
}
func runGaugeExecuteTests(config *gaugeExecuteTestsOptions, telemetryData *telemetry.CustomData, utils gaugeExecuteTestsUtils) error {
if config.InstallCommand != "" {
err := installGauge(config.InstallCommand, utils)
if err != nil {
return err
}
}
if config.LanguageRunner != "" {
err := installLanguageRunner(config.LanguageRunner, utils)
if err != nil {
return err
}
}
err := runGauge(config, utils)
if err != nil {
return fmt.Errorf("failed to run gauge: %w", err)
}
return nil
}
func installGauge(gaugeInstallCommand string, utils gaugeExecuteTestsUtils) error {
installGaugeTokens := strings.Split(gaugeInstallCommand, " ")
installGaugeTokens = append(installGaugeTokens, "--prefix=~/.npm-global")
err := utils.RunExecutable(installGaugeTokens[0], installGaugeTokens[1:]...)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.Wrap(ErrorGaugeInstall, err.Error())
}
return nil
}
func installLanguageRunner(languageRunner string, utils gaugeExecuteTestsUtils) error {
installParams := []string{"install", languageRunner}
gaugePath := filepath.Join(utils.Getenv("HOME"), "/.npm-global/bin/gauge")
err := utils.RunExecutable(gaugePath, installParams...)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.Wrap(ErrorGaugeRunnerInstall, err.Error())
}
return nil
}
func runGauge(config *gaugeExecuteTestsOptions, utils gaugeExecuteTestsUtils) error {
runCommandTokens := strings.Split(config.RunCommand, " ")
if config.TestOptions != "" {
runCommandTokens = append(runCommandTokens, strings.Split(config.TestOptions, " ")...)
}
gaugePath := filepath.Join(utils.Getenv("HOME"), "/.npm-global/bin/gauge")
err := utils.RunExecutable(gaugePath, runCommandTokens...)
if err != nil {
return errors.Wrap(ErrorGaugeRun, err.Error())
}
return nil
}
func (utils gaugeExecuteTestsUtilsBundle) Getenv(key string) string {
return os.Getenv(key)
}

View File

@@ -0,0 +1,221 @@
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperenv"
"github.com/SAP/jenkins-library/pkg/splunk"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/spf13/cobra"
)
type gaugeExecuteTestsOptions struct {
InstallCommand string `json:"installCommand,omitempty"`
LanguageRunner string `json:"languageRunner,omitempty"`
RunCommand string `json:"runCommand,omitempty"`
TestOptions string `json:"testOptions,omitempty"`
}
type gaugeExecuteTestsInflux struct {
step_data struct {
fields struct {
gauge bool
}
tags struct {
}
}
}
func (i *gaugeExecuteTestsInflux) persist(path, resourceName string) {
measurementContent := []struct {
measurement string
valType string
name string
value interface{}
}{
{valType: config.InfluxField, measurement: "step_data", name: "gauge", value: i.step_data.fields.gauge},
}
errCount := 0
for _, metric := range measurementContent {
err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(metric.measurement, fmt.Sprintf("%vs", metric.valType), metric.name), metric.value)
if err != nil {
log.Entry().WithError(err).Error("Error persisting influx environment.")
errCount++
}
}
if errCount > 0 {
log.Entry().Fatal("failed to persist Influx environment")
}
}
// GaugeExecuteTestsCommand Installs gauge and executes specified gauge tests.
func GaugeExecuteTestsCommand() *cobra.Command {
const STEP_NAME = "gaugeExecuteTests"
metadata := gaugeExecuteTestsMetadata()
var stepConfig gaugeExecuteTestsOptions
var startTime time.Time
var influx gaugeExecuteTestsInflux
var logCollector *log.CollectorHook
var createGaugeExecuteTestsCmd = &cobra.Command{
Use: STEP_NAME,
Short: "Installs gauge and executes specified gauge tests.",
Long: `In this step Gauge ([getgauge.io](https://getgauge.io)) acceptance tests are executed. Using Gauge it will be possible to have a three-tier test layout:
Acceptance Criteria
Test implemenation layer
Application driver layer
This layout is propagated by Jez Humble and Dave Farley in their book "Continuous Delivery" as a way to create maintainable acceptance test suites (see "Continuous Delivery", p. 190ff).
Using Gauge it is possible to write test specifications in [Markdown syntax](http://daringfireball.net/projects/markdown/syntax) and therefore allow e.g. product owners to write the relevant acceptance test specifications. At the same time it allows the developer to implement the steps described in the specification in her development environment.
You can use the [sample projects](https://github.com/getgauge/gauge-mvn-archetypes) of Gauge.`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
startTime = time.Now()
log.SetStepName(STEP_NAME)
log.SetVerbose(GeneralConfig.Verbose)
path, _ := os.Getwd()
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
log.RegisterHook(fatalHook)
err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return err
}
if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
log.RegisterHook(&sentryHook)
}
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
log.RegisterHook(logCollector)
}
return nil
},
Run: func(_ *cobra.Command, _ []string) {
telemetryData := telemetry.CustomData{}
telemetryData.ErrorCode = "1"
handler := func() {
config.RemoveVaultSecretFiles()
influx.persist(GeneralConfig.EnvRootPath, "influx")
telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
telemetryData.ErrorCategory = log.GetErrorCategory().String()
telemetry.Send(&telemetryData)
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
splunk.Send(&telemetryData, logCollector)
}
}
log.DeferExitHandler(handler)
defer handler()
telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
splunk.Initialize(GeneralConfig.CorrelationID,
GeneralConfig.HookConfig.SplunkConfig.Dsn,
GeneralConfig.HookConfig.SplunkConfig.Token,
GeneralConfig.HookConfig.SplunkConfig.Index,
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
}
gaugeExecuteTests(stepConfig, &telemetryData, &influx)
telemetryData.ErrorCode = "0"
log.Entry().Info("SUCCESS")
},
}
addGaugeExecuteTestsFlags(createGaugeExecuteTestsCmd, &stepConfig)
return createGaugeExecuteTestsCmd
}
func addGaugeExecuteTestsFlags(cmd *cobra.Command, stepConfig *gaugeExecuteTestsOptions) {
cmd.Flags().StringVar(&stepConfig.InstallCommand, "installCommand", os.Getenv("PIPER_installCommand"), "Defines the command for installing Gauge. Gauge should be installed using npm. Example: npm install -g @getgauge/cli@1.2.1")
cmd.Flags().StringVar(&stepConfig.LanguageRunner, "languageRunner", os.Getenv("PIPER_languageRunner"), "Defines the Gauge language runner to be used. Example: java")
cmd.Flags().StringVar(&stepConfig.RunCommand, "runCommand", os.Getenv("PIPER_runCommand"), "Defines the command which is used for executing Gauge. Example: run -s -p specs/")
cmd.Flags().StringVar(&stepConfig.TestOptions, "testOptions", os.Getenv("PIPER_testOptions"), "Allows to set specific options for the Gauge execution. Details can be found for example [in the Gauge Maven plugin documentation](https://github.com/getgauge-contrib/gauge-maven-plugin#executing-specs)")
cmd.MarkFlagRequired("runCommand")
}
// retrieve step metadata
func gaugeExecuteTestsMetadata() config.StepData {
var theMetaData = config.StepData{
Metadata: config.StepMetadata{
Name: "gaugeExecuteTests",
Aliases: []config.Alias{},
Description: "Installs gauge and executes specified gauge tests.",
},
Spec: config.StepSpec{
Inputs: config.StepInputs{
Resources: []config.StepResources{
{Name: "buildDescriptor", Type: "stash"},
{Name: "tests", Type: "stash"},
},
Parameters: []config.StepParameters{
{
Name: "installCommand",
ResourceRef: []config.ResourceReference{},
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "languageRunner",
ResourceRef: []config.ResourceReference{},
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "runCommand",
ResourceRef: []config.ResourceReference{},
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "testOptions",
ResourceRef: []config.ResourceReference{},
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
},
},
Containers: []config.Container{
{Name: "gauge", Image: "node:lts-stretch", EnvVars: []config.EnvVar{{Name: "no_proxy", Value: "localhost,selenium,$no_proxy"}, {Name: "NO_PROXY", Value: "localhost,selenium,$NO_PROXY"}}, WorkingDir: "/home/node"},
},
Sidecars: []config.Container{
{Name: "selenium", Image: "selenium/standalone-chrome", EnvVars: []config.EnvVar{{Name: "NO_PROXY", Value: "localhost,selenium,$NO_PROXY"}, {Name: "no_proxy", Value: "localhost,selenium,$no_proxy"}}},
},
Outputs: config.StepOutputs{
Resources: []config.StepResources{
{
Name: "influx",
Type: "influx",
Parameters: []map[string]interface{}{
{"Name": "step_data"}, {"fields": []map[string]string{{"name": "gauge"}}},
},
},
},
},
},
}
return theMetaData
}

View File

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

View File

@@ -0,0 +1,112 @@
package cmd
import (
"errors"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
type gaugeExecuteTestsMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func (utils gaugeExecuteTestsMockUtils) Getenv(key string) string {
if key == "HOME" {
return "/home/node"
}
return ""
}
func TestRunGaugeExecuteTests(t *testing.T) {
t.Parallel()
allFineConfig := gaugeExecuteTestsOptions{
InstallCommand: "npm install -g @getgauge/cli",
LanguageRunner: "java",
RunCommand: "run",
TestOptions: "specs",
}
gaugeBin := "home/node/.npm-global/bin/gauge"
t.Run("success case", func(t *testing.T) {
t.Parallel()
mockUtils := gaugeExecuteTestsMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
err := runGaugeExecuteTests(&allFineConfig, nil, &mockUtils)
assert.NoError(t, err)
assert.Equal(t, mockUtils.ExecMockRunner.Calls[0].Exec, "npm")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[0].Params, []string{"install", "-g", "@getgauge/cli", "--prefix=~/.npm-global"})
assert.Equal(t, mockUtils.ExecMockRunner.Calls[1].Exec, "/home/node/.npm-global/bin/gauge")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[1].Params, []string{"install", "java"})
assert.Equal(t, mockUtils.ExecMockRunner.Calls[2].Exec, "/home/node/.npm-global/bin/gauge")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[2].Params, []string{"run", "specs"})
})
t.Run("fail on installation", func(t *testing.T) {
t.Parallel()
badInstallConfig := allFineConfig
badInstallConfig.InstallCommand = "npm install -g @wronggauge/cli"
mockUtils := gaugeExecuteTestsMockUtils{
ExecMockRunner: &mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"npm install -g @wronggauge/cli": errors.New("cannot find module")}},
FilesMock: &mock.FilesMock{},
}
err := runGaugeExecuteTests(&badInstallConfig, nil, &mockUtils)
assert.True(t, errors.Is(err, ErrorGaugeInstall))
assert.Equal(t, len(mockUtils.ExecMockRunner.Calls), 1)
assert.Equal(t, mockUtils.ExecMockRunner.Calls[0].Exec, "npm")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[0].Params, []string{"install", "-g", "@wronggauge/cli", "--prefix=~/.npm-global"})
})
t.Run("fail on installing language runner", func(t *testing.T) {
t.Parallel()
badInstallConfig := allFineConfig
badInstallConfig.LanguageRunner = "wrong"
mockUtils := gaugeExecuteTestsMockUtils{
ExecMockRunner: &mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{gaugeBin + " install wrong": errors.New("error installing runner")}},
FilesMock: &mock.FilesMock{},
}
err := runGaugeExecuteTests(&badInstallConfig, nil, &mockUtils)
assert.True(t, errors.Is(err, ErrorGaugeRunnerInstall))
assert.Equal(t, len(mockUtils.ExecMockRunner.Calls), 2)
assert.Equal(t, mockUtils.ExecMockRunner.Calls[0].Exec, "npm")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[0].Params, []string{"install", "-g", "@getgauge/cli", "--prefix=~/.npm-global"})
assert.Equal(t, mockUtils.ExecMockRunner.Calls[1].Exec, "/home/node/.npm-global/bin/gauge")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[1].Params, []string{"install", "wrong"})
})
t.Run("fail on gauge run", func(t *testing.T) {
t.Parallel()
mockUtils := gaugeExecuteTestsMockUtils{
ExecMockRunner: &mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{gaugeBin + " run specs": errors.New("error running gauge")}},
FilesMock: &mock.FilesMock{},
}
err := runGaugeExecuteTests(&allFineConfig, nil, &mockUtils)
assert.True(t, errors.Is(err, ErrorGaugeRun))
assert.Equal(t, len(mockUtils.ExecMockRunner.Calls), 3)
assert.Equal(t, mockUtils.ExecMockRunner.Calls[0].Exec, "npm")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[0].Params, []string{"install", "-g", "@getgauge/cli", "--prefix=~/.npm-global"})
assert.Equal(t, mockUtils.ExecMockRunner.Calls[1].Exec, "/home/node/.npm-global/bin/gauge")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[1].Params, []string{"install", "java"})
assert.Equal(t, mockUtils.ExecMockRunner.Calls[2].Exec, "/home/node/.npm-global/bin/gauge")
assert.Equal(t, mockUtils.ExecMockRunner.Calls[2].Params, []string{"run", "specs"})
})
}

View File

@@ -33,6 +33,7 @@ func GetAllStepMetadata() map[string]config.StepData {
"containerExecuteStructureTests": containerExecuteStructureTestsMetadata(),
"detectExecuteScan": detectExecuteScanMetadata(),
"fortifyExecuteScan": fortifyExecuteScanMetadata(),
"gaugeExecuteTests": gaugeExecuteTestsMetadata(),
"gctsCloneRepository": gctsCloneRepositoryMetadata(),
"gctsCreateRepository": gctsCreateRepositoryMetadata(),
"gctsDeploy": gctsDeployMetadata(),

View File

@@ -147,6 +147,7 @@ func Execute() {
rootCmd.AddCommand(IntegrationArtifactUploadCommand())
rootCmd.AddCommand(TerraformExecuteCommand())
rootCmd.AddCommand(ContainerExecuteStructureTestsCommand())
rootCmd.AddCommand(GaugeExecuteTestsCommand())
rootCmd.AddCommand(BatsExecuteTestsCommand())
rootCmd.AddCommand(PipelineCreateScanSummaryCommand())
rootCmd.AddCommand(TransportRequestDocIDFromGitCommand())