1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-16 11:09:33 +02:00
sap-jenkins-library/pkg/generator/helper/testdata/TestProcessMetaFiles/step_code_generated.golden

169 lines
4.4 KiB
Plaintext
Raw Normal View History

package cmd
import (
2019-11-04 17:07:30 +02:00
"os"
"fmt"
"path/filepath"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
2020-01-29 14:17:54 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/piperenv"
"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)
}
}
var myTestStepOptions testStepOptions
// TestStepCommand Test description
func TestStepCommand() *cobra.Command {
metadata := testStepMetadata()
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 {
log.SetStepName("testStep")
log.SetVerbose(GeneralConfig.Verbose)
return PrepareConfig(cmd, &metadata, "testStep", &myTestStepOptions, config.OpenPiperFile)
},
RunE: func(cmd *cobra.Command, args []string) error {
handler := func() {
commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
influxTest.persist(GeneralConfig.EnvRootPath, "influxTest")
}
log.DeferExitHandler(handler)
defer handler()
2020-01-29 14:17:54 +02:00
telemetry.Initialize(GeneralConfig.NoTelemetry, "testStep")
telemetry.Send(&telemetry.CustomData{})
return testStep(myTestStepOptions, &commonPipelineEnvironment, &influxTest)
},
}
addTestStepFlags(createTestStepCmd)
return createTestStepCmd
}
func addTestStepFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&myTestStepOptions.Param0, "param0", "val0", "param0 description")
cmd.Flags().StringVar(&myTestStepOptions.Param1, "param1", os.Getenv("PIPER_param1"), "param1 description")
cmd.Flags().StringVar(&myTestStepOptions.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
}