github.com/jaylevin/jenkins-library@v1.230.4/cmd/uiVeri5ExecuteTests_generated.go (about) 1 // Code generated by piper's step-generator. DO NOT EDIT. 2 3 package cmd 4 5 import ( 6 "fmt" 7 "os" 8 "reflect" 9 "strings" 10 "time" 11 12 "github.com/SAP/jenkins-library/pkg/config" 13 "github.com/SAP/jenkins-library/pkg/gcs" 14 "github.com/SAP/jenkins-library/pkg/log" 15 "github.com/SAP/jenkins-library/pkg/splunk" 16 "github.com/SAP/jenkins-library/pkg/telemetry" 17 "github.com/SAP/jenkins-library/pkg/validation" 18 "github.com/bmatcuk/doublestar" 19 "github.com/spf13/cobra" 20 ) 21 22 type uiVeri5ExecuteTestsOptions struct { 23 InstallCommand string `json:"installCommand,omitempty"` 24 RunCommand string `json:"runCommand,omitempty"` 25 RunOptions []string `json:"runOptions,omitempty"` 26 TestOptions string `json:"testOptions,omitempty"` 27 TestServerURL string `json:"testServerUrl,omitempty"` 28 } 29 30 type uiVeri5ExecuteTestsReports struct { 31 } 32 33 func (p *uiVeri5ExecuteTestsReports) persist(stepConfig uiVeri5ExecuteTestsOptions, gcpJsonKeyFilePath string, gcsBucketId string, gcsFolderPath string, gcsSubFolder string) { 34 if gcsBucketId == "" { 35 log.Entry().Info("persisting reports to GCS is disabled, because gcsBucketId is empty") 36 return 37 } 38 log.Entry().Info("Uploading reports to Google Cloud Storage...") 39 content := []gcs.ReportOutputParam{ 40 {FilePattern: "**/TEST-*.xml", ParamRef: "", StepResultType: "acceptance-test"}, 41 {FilePattern: "**/requirement.mapping", ParamRef: "", StepResultType: "requirement-mapping"}, 42 {FilePattern: "**/delivery.mapping", ParamRef: "", StepResultType: "delivery-mapping"}, 43 } 44 envVars := []gcs.EnvVar{ 45 {Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: gcpJsonKeyFilePath, Modified: false}, 46 } 47 gcsClient, err := gcs.NewClient(gcs.WithEnvVars(envVars)) 48 if err != nil { 49 log.Entry().Errorf("creation of GCS client failed: %v", err) 50 return 51 } 52 defer gcsClient.Close() 53 structVal := reflect.ValueOf(&stepConfig).Elem() 54 inputParameters := map[string]string{} 55 for i := 0; i < structVal.NumField(); i++ { 56 field := structVal.Type().Field(i) 57 if field.Type.String() == "string" { 58 paramName := strings.Split(field.Tag.Get("json"), ",") 59 paramValue, _ := structVal.Field(i).Interface().(string) 60 inputParameters[paramName[0]] = paramValue 61 } 62 } 63 if err := gcs.PersistReportsToGCS(gcsClient, content, inputParameters, gcsFolderPath, gcsBucketId, gcsSubFolder, doublestar.Glob, os.Stat); err != nil { 64 log.Entry().Errorf("failed to persist reports: %v", err) 65 } 66 } 67 68 // UiVeri5ExecuteTestsCommand Executes UI5 e2e tests using uiVeri5 69 func UiVeri5ExecuteTestsCommand() *cobra.Command { 70 const STEP_NAME = "uiVeri5ExecuteTests" 71 72 metadata := uiVeri5ExecuteTestsMetadata() 73 var stepConfig uiVeri5ExecuteTestsOptions 74 var startTime time.Time 75 var reports uiVeri5ExecuteTestsReports 76 var logCollector *log.CollectorHook 77 var splunkClient *splunk.Splunk 78 telemetryClient := &telemetry.Telemetry{} 79 80 var createUiVeri5ExecuteTestsCmd = &cobra.Command{ 81 Use: STEP_NAME, 82 Short: "Executes UI5 e2e tests using uiVeri5", 83 Long: `In this step the ([UIVeri5 tests](https://github.com/SAP/ui5-uiveri5)) are executed.`, 84 PreRunE: func(cmd *cobra.Command, _ []string) error { 85 startTime = time.Now() 86 log.SetStepName(STEP_NAME) 87 log.SetVerbose(GeneralConfig.Verbose) 88 89 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 90 91 path, _ := os.Getwd() 92 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 93 log.RegisterHook(fatalHook) 94 95 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 96 if err != nil { 97 log.SetErrorCategory(log.ErrorConfiguration) 98 return err 99 } 100 101 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 102 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 103 log.RegisterHook(&sentryHook) 104 } 105 106 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 107 splunkClient = &splunk.Splunk{} 108 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 109 log.RegisterHook(logCollector) 110 } 111 112 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 113 if err != nil { 114 return err 115 } 116 if err = validation.ValidateStruct(stepConfig); err != nil { 117 log.SetErrorCategory(log.ErrorConfiguration) 118 return err 119 } 120 121 return nil 122 }, 123 Run: func(_ *cobra.Command, _ []string) { 124 stepTelemetryData := telemetry.CustomData{} 125 stepTelemetryData.ErrorCode = "1" 126 handler := func() { 127 reports.persist(stepConfig, GeneralConfig.GCPJsonKeyFilePath, GeneralConfig.GCSBucketId, GeneralConfig.GCSFolderPath, GeneralConfig.GCSSubFolder) 128 config.RemoveVaultSecretFiles() 129 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 130 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 131 stepTelemetryData.PiperCommitHash = GitCommit 132 telemetryClient.SetData(&stepTelemetryData) 133 telemetryClient.Send() 134 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 135 splunkClient.Send(telemetryClient.GetData(), logCollector) 136 } 137 } 138 log.DeferExitHandler(handler) 139 defer handler() 140 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 141 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 142 splunkClient.Initialize(GeneralConfig.CorrelationID, 143 GeneralConfig.HookConfig.SplunkConfig.Dsn, 144 GeneralConfig.HookConfig.SplunkConfig.Token, 145 GeneralConfig.HookConfig.SplunkConfig.Index, 146 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 147 } 148 uiVeri5ExecuteTests(stepConfig, &stepTelemetryData) 149 stepTelemetryData.ErrorCode = "0" 150 log.Entry().Info("SUCCESS") 151 }, 152 } 153 154 addUiVeri5ExecuteTestsFlags(createUiVeri5ExecuteTestsCmd, &stepConfig) 155 return createUiVeri5ExecuteTestsCmd 156 } 157 158 func addUiVeri5ExecuteTestsFlags(cmd *cobra.Command, stepConfig *uiVeri5ExecuteTestsOptions) { 159 cmd.Flags().StringVar(&stepConfig.InstallCommand, "installCommand", `npm install @ui5/uiveri5 --global --quiet`, "The command that is executed to install the uiveri5 test tool.") 160 cmd.Flags().StringVar(&stepConfig.RunCommand, "runCommand", `/home/node/.npm-global/bin/uiveri5`, "The command that is executed to start the tests.") 161 cmd.Flags().StringSliceVar(&stepConfig.RunOptions, "runOptions", []string{`--seleniumAddress=http://localhost:4444/wd/hub`}, "Options to append to the runCommand, last parameter has to be path to conf.js (default if missing: ./conf.js).") 162 cmd.Flags().StringVar(&stepConfig.TestOptions, "testOptions", os.Getenv("PIPER_testOptions"), "Deprecated and will result in an error if set. Please use runOptions instead. Split the testOptions string at the whitespaces when migrating it into a list of runOptions.") 163 cmd.Flags().StringVar(&stepConfig.TestServerURL, "testServerUrl", os.Getenv("PIPER_testServerUrl"), "URL pointing to the deployment.") 164 165 cmd.MarkFlagRequired("installCommand") 166 cmd.MarkFlagRequired("runCommand") 167 cmd.MarkFlagRequired("runOptions") 168 } 169 170 // retrieve step metadata 171 func uiVeri5ExecuteTestsMetadata() config.StepData { 172 var theMetaData = config.StepData{ 173 Metadata: config.StepMetadata{ 174 Name: "uiVeri5ExecuteTests", 175 Aliases: []config.Alias{}, 176 Description: "Executes UI5 e2e tests using uiVeri5", 177 }, 178 Spec: config.StepSpec{ 179 Inputs: config.StepInputs{ 180 Resources: []config.StepResources{ 181 {Name: "buildDescriptor", Type: "stash"}, 182 {Name: "tests", Type: "stash"}, 183 }, 184 Parameters: []config.StepParameters{ 185 { 186 Name: "installCommand", 187 ResourceRef: []config.ResourceReference{}, 188 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 189 Type: "string", 190 Mandatory: true, 191 Aliases: []config.Alias{}, 192 Default: `npm install @ui5/uiveri5 --global --quiet`, 193 }, 194 { 195 Name: "runCommand", 196 ResourceRef: []config.ResourceReference{}, 197 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 198 Type: "string", 199 Mandatory: true, 200 Aliases: []config.Alias{}, 201 Default: `/home/node/.npm-global/bin/uiveri5`, 202 }, 203 { 204 Name: "runOptions", 205 ResourceRef: []config.ResourceReference{}, 206 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 207 Type: "[]string", 208 Mandatory: true, 209 Aliases: []config.Alias{}, 210 Default: []string{`--seleniumAddress=http://localhost:4444/wd/hub`}, 211 }, 212 { 213 Name: "testOptions", 214 ResourceRef: []config.ResourceReference{}, 215 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 216 Type: "string", 217 Mandatory: false, 218 Aliases: []config.Alias{}, 219 Default: os.Getenv("PIPER_testOptions"), 220 }, 221 { 222 Name: "testServerUrl", 223 ResourceRef: []config.ResourceReference{}, 224 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 225 Type: "string", 226 Mandatory: false, 227 Aliases: []config.Alias{}, 228 Default: os.Getenv("PIPER_testServerUrl"), 229 }, 230 }, 231 }, 232 Containers: []config.Container{ 233 {Name: "uiVeri5", 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"}, 234 }, 235 Sidecars: []config.Container{ 236 {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"}}}, 237 }, 238 Outputs: config.StepOutputs{ 239 Resources: []config.StepResources{ 240 { 241 Name: "reports", 242 Type: "reports", 243 Parameters: []map[string]interface{}{ 244 {"filePattern": "**/TEST-*.xml", "type": "acceptance-test"}, 245 {"filePattern": "**/requirement.mapping", "type": "requirement-mapping"}, 246 {"filePattern": "**/delivery.mapping", "type": "delivery-mapping"}, 247 }, 248 }, 249 }, 250 }, 251 }, 252 } 253 return theMetaData 254 }