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/telemetry" "github.com/spf13/cobra" ) type testStepOptions struct { Param0 string `json:"param0,omitempty"` Param1 string `json:"param1,omitempty"` Param2 string `json:"param2,omitempty"` } type testStepCommonPipelineEnvironment struct { artifactVersion string git struct { commitID string branch string } } func (p *testStepCommonPipelineEnvironment) persist(path, resourceName string) { content := []struct{ category string name string value string }{ {category: "", name: "artifactVersion", value: p.artifactVersion}, {category: "git", name: "commitId", value: p.git.commitID}, {category: "git", name: "branch", value: p.git.branch}, } errCount := 0 for _, param := range content { err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) if err != nil { log.Entry().WithError(err).Error("Error persisting piper environment.") errCount++ } } if errCount > 0 { os.Exit(1) } } type testStepInfluxTest struct { m1 struct { fields struct { f1 string } tags struct { t1 string } } } func (i *testStepInfluxTest) persist(path, resourceName string) { measurementContent := []struct{ measurement string valType string name string value string }{ {valType: config.InfluxField, measurement: "m1" , name: "f1", value: i.m1.fields.f1}, {valType: config.InfluxTag, measurement: "m1" , name: "t1", value: i.m1.tags.t1}, } 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 { os.Exit(1) } } // TestStepCommand Test description func TestStepCommand() *cobra.Command { metadata := testStepMetadata() var stepConfig testStepOptions var startTime time.Time var commonPipelineEnvironment testStepCommonPipelineEnvironment var influxTest testStepInfluxTest var createTestStepCmd = &cobra.Command{ Use: "testStep", Short: "Test description", Long: `Long Test description`, PreRunE: func(cmd *cobra.Command, args []string) error { startTime = time.Now() log.SetStepName("testStep") log.SetVerbose(GeneralConfig.Verbose) return PrepareConfig(cmd, &metadata, "testStep", &stepConfig, config.OpenPiperFile) }, Run: func(cmd *cobra.Command, args []string) { telemetryData := telemetry.CustomData{} telemetryData.ErrorCode = "1" handler := func() { commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") influxTest.persist(GeneralConfig.EnvRootPath, "influxTest") telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) telemetry.Send(&telemetryData) } log.DeferExitHandler(handler) defer handler() telemetry.Initialize(GeneralConfig.NoTelemetry, "testStep") testStep(stepConfig, &telemetryData, &commonPipelineEnvironment, &influxTest) telemetryData.ErrorCode = "0" }, } addTestStepFlags(createTestStepCmd, &stepConfig) return createTestStepCmd } func addTestStepFlags(cmd *cobra.Command, stepConfig *testStepOptions) { cmd.Flags().StringVar(&stepConfig.Param0, "param0", "val0", "param0 description") cmd.Flags().StringVar(&stepConfig.Param1, "param1", os.Getenv("PIPER_param1"), "param1 description") cmd.Flags().StringVar(&stepConfig.Param2, "param2", os.Getenv("PIPER_param2"), "param1 description") cmd.MarkFlagRequired("param0") cmd.MarkFlagRequired("param2") } // retrieve step metadata func testStepMetadata() config.StepData { var theMetaData = config.StepData{ Spec: config.StepSpec{ Inputs: config.StepInputs{ Parameters: []config.StepParameters{ { Name: "param0", ResourceRef: []config.ResourceReference{}, Scope: []string{"GENERAL","PARAMETERS",}, Type: "string", Mandatory: true, Aliases: []config.Alias{}, }, { Name: "param1", ResourceRef: []config.ResourceReference{}, Scope: []string{"PARAMETERS",}, Type: "string", Mandatory: false, Aliases: []config.Alias{}, }, { Name: "param2", ResourceRef: []config.ResourceReference{}, Scope: []string{"PARAMETERS",}, Type: "string", Mandatory: true, Aliases: []config.Alias{}, }, }, }, }, } return theMetaData }