github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/abapEnvironmentRunAUnitTest_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  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type abapEnvironmentRunAUnitTestOptions struct {
    19  	AUnitConfig          string `json:"aUnitConfig,omitempty"`
    20  	Repositories         string `json:"repositories,omitempty"`
    21  	CfAPIEndpoint        string `json:"cfApiEndpoint,omitempty"`
    22  	CfOrg                string `json:"cfOrg,omitempty"`
    23  	CfServiceInstance    string `json:"cfServiceInstance,omitempty"`
    24  	CfServiceKeyName     string `json:"cfServiceKeyName,omitempty"`
    25  	CfSpace              string `json:"cfSpace,omitempty"`
    26  	Username             string `json:"username,omitempty"`
    27  	Password             string `json:"password,omitempty"`
    28  	Host                 string `json:"host,omitempty"`
    29  	AUnitResultsFileName string `json:"aUnitResultsFileName,omitempty"`
    30  	GenerateHTML         bool   `json:"generateHTML,omitempty"`
    31  }
    32  
    33  // AbapEnvironmentRunAUnitTestCommand Runs an AUnit Test
    34  func AbapEnvironmentRunAUnitTestCommand() *cobra.Command {
    35  	const STEP_NAME = "abapEnvironmentRunAUnitTest"
    36  
    37  	metadata := abapEnvironmentRunAUnitTestMetadata()
    38  	var stepConfig abapEnvironmentRunAUnitTestOptions
    39  	var startTime time.Time
    40  	var logCollector *log.CollectorHook
    41  	var splunkClient *splunk.Splunk
    42  	telemetryClient := &telemetry.Telemetry{}
    43  
    44  	var createAbapEnvironmentRunAUnitTestCmd = &cobra.Command{
    45  		Use:   STEP_NAME,
    46  		Short: "Runs an AUnit Test",
    47  		Long: `This step is for triggering an [AUnit](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/cdd19e3a5c49458291ec65d8d86e2b9a.html) test run on an SAP BTP ABAP Environment system.
    48  Please provide either of the following options:
    49  
    50  * The host and credentials of the BTP ABAP Environment system itself. The credentials must be configured for the Communication Scenario SAP_COM_0735.
    51  * The Cloud Foundry parameters (API endpoint, organization, space), credentials, the service instance for the ABAP service and the service key for the Communication Scenario SAP_COM_0735.
    52  * Only provide one of those options with the respective credentials. If all values are provided, the direct communication (via host) has priority.
    53  
    54  Regardless of the option you chose, please make sure to provide the object set containing the objects that you want to be checked analog to the examples listed on this page.`,
    55  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    56  			startTime = time.Now()
    57  			log.SetStepName(STEP_NAME)
    58  			log.SetVerbose(GeneralConfig.Verbose)
    59  
    60  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    61  
    62  			path, _ := os.Getwd()
    63  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    64  			log.RegisterHook(fatalHook)
    65  
    66  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    67  			if err != nil {
    68  				log.SetErrorCategory(log.ErrorConfiguration)
    69  				return err
    70  			}
    71  			log.RegisterSecret(stepConfig.Username)
    72  			log.RegisterSecret(stepConfig.Password)
    73  
    74  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    75  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    76  				log.RegisterHook(&sentryHook)
    77  			}
    78  
    79  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    80  				splunkClient = &splunk.Splunk{}
    81  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    82  				log.RegisterHook(logCollector)
    83  			}
    84  
    85  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    86  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    87  			}
    88  
    89  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    90  			if err != nil {
    91  				return err
    92  			}
    93  			if err = validation.ValidateStruct(stepConfig); err != nil {
    94  				log.SetErrorCategory(log.ErrorConfiguration)
    95  				return err
    96  			}
    97  
    98  			return nil
    99  		},
   100  		Run: func(_ *cobra.Command, _ []string) {
   101  			stepTelemetryData := telemetry.CustomData{}
   102  			stepTelemetryData.ErrorCode = "1"
   103  			handler := func() {
   104  				config.RemoveVaultSecretFiles()
   105  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   106  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   107  				stepTelemetryData.PiperCommitHash = GitCommit
   108  				telemetryClient.SetData(&stepTelemetryData)
   109  				telemetryClient.Send()
   110  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   111  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   112  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   113  						GeneralConfig.HookConfig.SplunkConfig.Token,
   114  						GeneralConfig.HookConfig.SplunkConfig.Index,
   115  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   116  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   117  				}
   118  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   119  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   120  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   121  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   122  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   123  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   124  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   125  				}
   126  			}
   127  			log.DeferExitHandler(handler)
   128  			defer handler()
   129  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   130  			abapEnvironmentRunAUnitTest(stepConfig, &stepTelemetryData)
   131  			stepTelemetryData.ErrorCode = "0"
   132  			log.Entry().Info("SUCCESS")
   133  		},
   134  	}
   135  
   136  	addAbapEnvironmentRunAUnitTestFlags(createAbapEnvironmentRunAUnitTestCmd, &stepConfig)
   137  	return createAbapEnvironmentRunAUnitTestCmd
   138  }
   139  
   140  func addAbapEnvironmentRunAUnitTestFlags(cmd *cobra.Command, stepConfig *abapEnvironmentRunAUnitTestOptions) {
   141  	cmd.Flags().StringVar(&stepConfig.AUnitConfig, "aUnitConfig", os.Getenv("PIPER_aUnitConfig"), "Path to a YAML configuration file for the object set to be checked during the AUnit test run")
   142  	cmd.Flags().StringVar(&stepConfig.Repositories, "repositories", os.Getenv("PIPER_repositories"), "Specifies a YAML file containing the repositories configuration")
   143  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API endpoint")
   144  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry org")
   145  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Parameter of ServiceInstance Name to delete Cloud Foundry Service")
   146  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Parameter of Cloud Foundry Service Key to be created")
   147  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "Cloud Foundry Space")
   148  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0735")
   149  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0735")
   150  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP BTP ABAP Environment system")
   151  	cmd.Flags().StringVar(&stepConfig.AUnitResultsFileName, "aUnitResultsFileName", `AUnitResults.xml`, "Specifies output file name for the results from the AUnit run.")
   152  	cmd.Flags().BoolVar(&stepConfig.GenerateHTML, "generateHTML", false, "Specifies whether the AUnit results should also be generated as an HTML document")
   153  
   154  	cmd.MarkFlagRequired("username")
   155  	cmd.MarkFlagRequired("password")
   156  }
   157  
   158  // retrieve step metadata
   159  func abapEnvironmentRunAUnitTestMetadata() config.StepData {
   160  	var theMetaData = config.StepData{
   161  		Metadata: config.StepMetadata{
   162  			Name:        "abapEnvironmentRunAUnitTest",
   163  			Aliases:     []config.Alias{},
   164  			Description: "Runs an AUnit Test",
   165  		},
   166  		Spec: config.StepSpec{
   167  			Inputs: config.StepInputs{
   168  				Secrets: []config.StepSecrets{
   169  					{Name: "abapCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the BTP ABAP Environment system or the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cfCredentialsId", Deprecated: false}}},
   170  				},
   171  				Parameters: []config.StepParameters{
   172  					{
   173  						Name:        "aUnitConfig",
   174  						ResourceRef: []config.ResourceReference{},
   175  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   176  						Type:        "string",
   177  						Mandatory:   false,
   178  						Aliases:     []config.Alias{},
   179  						Default:     os.Getenv("PIPER_aUnitConfig"),
   180  					},
   181  					{
   182  						Name:        "repositories",
   183  						ResourceRef: []config.ResourceReference{},
   184  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   185  						Type:        "string",
   186  						Mandatory:   false,
   187  						Aliases:     []config.Alias{},
   188  						Default:     os.Getenv("PIPER_repositories"),
   189  					},
   190  					{
   191  						Name:        "cfApiEndpoint",
   192  						ResourceRef: []config.ResourceReference{},
   193  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   194  						Type:        "string",
   195  						Mandatory:   false,
   196  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   197  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   198  					},
   199  					{
   200  						Name:        "cfOrg",
   201  						ResourceRef: []config.ResourceReference{},
   202  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   203  						Type:        "string",
   204  						Mandatory:   false,
   205  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   206  						Default:     os.Getenv("PIPER_cfOrg"),
   207  					},
   208  					{
   209  						Name:        "cfServiceInstance",
   210  						ResourceRef: []config.ResourceReference{},
   211  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   212  						Type:        "string",
   213  						Mandatory:   false,
   214  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   215  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   216  					},
   217  					{
   218  						Name:        "cfServiceKeyName",
   219  						ResourceRef: []config.ResourceReference{},
   220  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   221  						Type:        "string",
   222  						Mandatory:   false,
   223  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKey"}},
   224  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   225  					},
   226  					{
   227  						Name:        "cfSpace",
   228  						ResourceRef: []config.ResourceReference{},
   229  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   230  						Type:        "string",
   231  						Mandatory:   false,
   232  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   233  						Default:     os.Getenv("PIPER_cfSpace"),
   234  					},
   235  					{
   236  						Name: "username",
   237  						ResourceRef: []config.ResourceReference{
   238  							{
   239  								Name:  "abapCredentialsId",
   240  								Param: "username",
   241  								Type:  "secret",
   242  							},
   243  						},
   244  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   245  						Type:      "string",
   246  						Mandatory: true,
   247  						Aliases:   []config.Alias{},
   248  						Default:   os.Getenv("PIPER_username"),
   249  					},
   250  					{
   251  						Name: "password",
   252  						ResourceRef: []config.ResourceReference{
   253  							{
   254  								Name:  "abapCredentialsId",
   255  								Param: "password",
   256  								Type:  "secret",
   257  							},
   258  						},
   259  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   260  						Type:      "string",
   261  						Mandatory: true,
   262  						Aliases:   []config.Alias{},
   263  						Default:   os.Getenv("PIPER_password"),
   264  					},
   265  					{
   266  						Name:        "host",
   267  						ResourceRef: []config.ResourceReference{},
   268  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   269  						Type:        "string",
   270  						Mandatory:   false,
   271  						Aliases:     []config.Alias{},
   272  						Default:     os.Getenv("PIPER_host"),
   273  					},
   274  					{
   275  						Name:        "aUnitResultsFileName",
   276  						ResourceRef: []config.ResourceReference{},
   277  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   278  						Type:        "string",
   279  						Mandatory:   false,
   280  						Aliases:     []config.Alias{},
   281  						Default:     `AUnitResults.xml`,
   282  					},
   283  					{
   284  						Name:        "generateHTML",
   285  						ResourceRef: []config.ResourceReference{},
   286  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   287  						Type:        "bool",
   288  						Mandatory:   false,
   289  						Aliases:     []config.Alias{},
   290  						Default:     false,
   291  					},
   292  				},
   293  			},
   294  			Containers: []config.Container{
   295  				{Name: "cf", Image: "ppiper/cf-cli:v12"},
   296  			},
   297  		},
   298  	}
   299  	return theMetaData
   300  }