2020-02-21 13:56:09 +02:00
|
|
|
// Code generated by piper's step-generator. DO NOT EDIT.
|
|
|
|
|
2019-10-25 14:58:59 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-01-31 15:06:08 +02:00
|
|
|
"fmt"
|
2020-04-28 07:42:02 +02:00
|
|
|
"os"
|
2020-01-31 15:06:08 +02:00
|
|
|
"time"
|
|
|
|
|
2019-10-25 14:58:59 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/config"
|
2019-11-04 15:43:33 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2020-01-29 14:17:54 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2019-10-25 14:58:59 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type karmaExecuteTestsOptions struct {
|
|
|
|
InstallCommand string `json:"installCommand,omitempty"`
|
|
|
|
ModulePath string `json:"modulePath,omitempty"`
|
|
|
|
RunCommand string `json:"runCommand,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// KarmaExecuteTestsCommand Executes the Karma test runner
|
|
|
|
func KarmaExecuteTestsCommand() *cobra.Command {
|
2020-04-28 07:42:02 +02:00
|
|
|
const STEP_NAME = "karmaExecuteTests"
|
|
|
|
|
2019-10-25 14:58:59 +02:00
|
|
|
metadata := karmaExecuteTestsMetadata()
|
2020-02-04 11:46:43 +02:00
|
|
|
var stepConfig karmaExecuteTestsOptions
|
2020-01-31 15:06:08 +02:00
|
|
|
var startTime time.Time
|
2020-01-15 13:16:25 +02:00
|
|
|
|
2019-10-25 14:58:59 +02:00
|
|
|
var createKarmaExecuteTestsCmd = &cobra.Command{
|
2020-04-28 07:42:02 +02:00
|
|
|
Use: STEP_NAME,
|
2019-10-25 14:58:59 +02:00
|
|
|
Short: "Executes the Karma test runner",
|
|
|
|
Long: `In this step the ([Karma test runner](http://karma-runner.github.io)) is executed.
|
|
|
|
|
|
|
|
The step is using the ` + "`" + `seleniumExecuteTest` + "`" + ` step to spin up two containers in a Docker network:
|
|
|
|
|
|
|
|
* a Selenium/Chrome container (` + "`" + `selenium/standalone-chrome` + "`" + `)
|
2020-01-29 12:17:56 +02:00
|
|
|
* a NodeJS container (` + "`" + `node:lts-stretch` + "`" + `)
|
2019-10-25 14:58:59 +02:00
|
|
|
|
|
|
|
In the Docker network, the containers can be referenced by the values provided in ` + "`" + `dockerName` + "`" + ` and ` + "`" + `sidecarName` + "`" + `, the default values are ` + "`" + `karma` + "`" + ` and ` + "`" + `selenium` + "`" + `. These values must be used in the ` + "`" + `hostname` + "`" + ` properties of the test configuration ([Karma](https://karma-runner.github.io/1.0/config/configuration-file.html) and [WebDriver](https://github.com/karma-runner/karma-webdriver-launcher#usage)).
|
|
|
|
|
|
|
|
!!! note
|
|
|
|
In a Kubernetes environment, the containers both need to be referenced with ` + "`" + `localhost` + "`" + `.`,
|
2020-06-23 18:05:21 +02:00
|
|
|
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
2020-01-31 15:06:08 +02:00
|
|
|
startTime = time.Now()
|
2020-04-28 07:42:02 +02:00
|
|
|
log.SetStepName(STEP_NAME)
|
2019-11-06 17:22:50 +02:00
|
|
|
log.SetVerbose(GeneralConfig.Verbose)
|
2020-04-28 07:42:02 +02:00
|
|
|
|
|
|
|
path, _ := os.Getwd()
|
|
|
|
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
|
|
|
log.RegisterHook(fatalHook)
|
|
|
|
|
|
|
|
err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
|
2020-04-16 14:37:45 +02:00
|
|
|
if err != nil {
|
2020-06-24 10:04:05 +02:00
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
2020-04-16 14:37:45 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-05-05 08:36:24 +02:00
|
|
|
|
|
|
|
if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
|
|
|
|
sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
|
|
|
|
log.RegisterHook(&sentryHook)
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:37:45 +02:00
|
|
|
return nil
|
2019-10-25 14:58:59 +02:00
|
|
|
},
|
2020-06-23 18:05:21 +02:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2020-01-31 15:06:08 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
telemetryData.ErrorCode = "1"
|
|
|
|
handler := func() {
|
|
|
|
telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
2020-09-29 13:49:40 +02:00
|
|
|
telemetryData.ErrorCategory = log.GetErrorCategory().String()
|
2020-01-31 15:06:08 +02:00
|
|
|
telemetry.Send(&telemetryData)
|
|
|
|
}
|
|
|
|
log.DeferExitHandler(handler)
|
|
|
|
defer handler()
|
2020-04-28 07:42:02 +02:00
|
|
|
telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
|
2020-02-04 11:46:43 +02:00
|
|
|
karmaExecuteTests(stepConfig, &telemetryData)
|
2020-01-31 15:06:08 +02:00
|
|
|
telemetryData.ErrorCode = "0"
|
2020-05-18 21:52:54 +02:00
|
|
|
log.Entry().Info("SUCCESS")
|
2019-10-25 14:58:59 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
addKarmaExecuteTestsFlags(createKarmaExecuteTestsCmd, &stepConfig)
|
2019-10-25 14:58:59 +02:00
|
|
|
return createKarmaExecuteTestsCmd
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
func addKarmaExecuteTestsFlags(cmd *cobra.Command, stepConfig *karmaExecuteTestsOptions) {
|
2020-05-25 19:48:59 +02:00
|
|
|
cmd.Flags().StringVar(&stepConfig.InstallCommand, "installCommand", `npm install --quiet`, "The command that is executed to install the test tool.")
|
|
|
|
cmd.Flags().StringVar(&stepConfig.ModulePath, "modulePath", `.`, "Define the path of the module to execute tests on.")
|
|
|
|
cmd.Flags().StringVar(&stepConfig.RunCommand, "runCommand", `npm run karma`, "The command that is executed to start the tests.")
|
2019-10-25 14:58:59 +02:00
|
|
|
|
|
|
|
cmd.MarkFlagRequired("installCommand")
|
|
|
|
cmd.MarkFlagRequired("modulePath")
|
|
|
|
cmd.MarkFlagRequired("runCommand")
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve step metadata
|
|
|
|
func karmaExecuteTestsMetadata() config.StepData {
|
|
|
|
var theMetaData = config.StepData{
|
2020-03-19 18:24:35 +02:00
|
|
|
Metadata: config.StepMetadata{
|
|
|
|
Name: "karmaExecuteTests",
|
|
|
|
Aliases: []config.Alias{},
|
|
|
|
},
|
2019-10-25 14:58:59 +02:00
|
|
|
Spec: config.StepSpec{
|
|
|
|
Inputs: config.StepInputs{
|
|
|
|
Parameters: []config.StepParameters{
|
|
|
|
{
|
2020-01-15 13:16:25 +02:00
|
|
|
Name: "installCommand",
|
|
|
|
ResourceRef: []config.ResourceReference{},
|
|
|
|
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
|
|
|
|
Type: "string",
|
|
|
|
Mandatory: true,
|
|
|
|
Aliases: []config.Alias{},
|
2019-10-25 14:58:59 +02:00
|
|
|
},
|
|
|
|
{
|
2020-01-15 13:16:25 +02:00
|
|
|
Name: "modulePath",
|
|
|
|
ResourceRef: []config.ResourceReference{},
|
|
|
|
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
|
|
|
Type: "string",
|
|
|
|
Mandatory: true,
|
|
|
|
Aliases: []config.Alias{},
|
2019-10-25 14:58:59 +02:00
|
|
|
},
|
|
|
|
{
|
2020-01-15 13:16:25 +02:00
|
|
|
Name: "runCommand",
|
|
|
|
ResourceRef: []config.ResourceReference{},
|
|
|
|
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
|
|
|
|
Type: "string",
|
|
|
|
Mandatory: true,
|
|
|
|
Aliases: []config.Alias{},
|
2019-10-25 14:58:59 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return theMetaData
|
|
|
|
}
|