mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
24aafb0b69
* add vaultSecretFileReferences * fix test * fix test * go generate * remove code duplication Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
202 lines
5.6 KiB
Plaintext
202 lines
5.6 KiB
Plaintext
// 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/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 interface{}
|
|
}{
|
|
{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 {
|
|
log.Entry().Fatal("failed to persist Piper environment")
|
|
}
|
|
}
|
|
|
|
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 interface{}
|
|
}{
|
|
{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 {
|
|
log.Entry().Fatal("failed to persist Influx environment")
|
|
}
|
|
}
|
|
|
|
|
|
// TestStepCommand Test description
|
|
func TestStepCommand() *cobra.Command {
|
|
const STEP_NAME = "testStep"
|
|
|
|
metadata := testStepMetadata()
|
|
var stepConfig testStepOptions
|
|
var startTime time.Time
|
|
var commonPipelineEnvironment testStepCommonPipelineEnvironment
|
|
var influxTest testStepInfluxTest
|
|
|
|
var createTestStepCmd = &cobra.Command{
|
|
Use: STEP_NAME,
|
|
Short: "Test description",
|
|
Long: `Long Test description`,
|
|
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)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
Run: func(_ *cobra.Command, _ []string) {
|
|
telemetryData := telemetry.CustomData{}
|
|
telemetryData.ErrorCode = "1"
|
|
handler := func() {
|
|
config.RemoveVaultSecretFiles()
|
|
commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
|
|
influxTest.persist(GeneralConfig.EnvRootPath, "influxTest")
|
|
telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
|
telemetryData.ErrorCategory = log.GetErrorCategory().String()
|
|
telemetry.Send(&telemetryData)
|
|
}
|
|
log.DeferExitHandler(handler)
|
|
defer handler()
|
|
telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
|
|
testStep(stepConfig, &telemetryData, &commonPipelineEnvironment, &influxTest)
|
|
telemetryData.ErrorCode = "0"
|
|
log.Entry().Info("SUCCESS")
|
|
},
|
|
}
|
|
|
|
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{
|
|
Metadata: config.StepMetadata{
|
|
Name: "testStep",
|
|
Aliases: []config.Alias{{Name: "testStepAlias", Deprecated: true},},
|
|
},
|
|
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
|
|
}
|