github.com/xgoffin/jenkins-library@v1.154.0/cmd/containerExecuteStructureTests_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 containerExecuteStructureTestsOptions struct {
    19  	PullImage          bool   `json:"pullImage,omitempty"`
    20  	TestConfiguration  string `json:"testConfiguration,omitempty"`
    21  	TestDriver         string `json:"testDriver,omitempty"`
    22  	TestImage          string `json:"testImage,omitempty"`
    23  	TestReportFilePath string `json:"testReportFilePath,omitempty"`
    24  }
    25  
    26  // ContainerExecuteStructureTestsCommand In this step [Container Structure Tests](https://github.com/GoogleContainerTools/container-structure-test) are executed.
    27  func ContainerExecuteStructureTestsCommand() *cobra.Command {
    28  	const STEP_NAME = "containerExecuteStructureTests"
    29  
    30  	metadata := containerExecuteStructureTestsMetadata()
    31  	var stepConfig containerExecuteStructureTestsOptions
    32  	var startTime time.Time
    33  	var logCollector *log.CollectorHook
    34  	var splunkClient *splunk.Splunk
    35  	telemetryClient := &telemetry.Telemetry{}
    36  
    37  	var createContainerExecuteStructureTestsCmd = &cobra.Command{
    38  		Use:   STEP_NAME,
    39  		Short: "In this step [Container Structure Tests](https://github.com/GoogleContainerTools/container-structure-test) are executed.",
    40  		Long: `This testing framework allows you to execute different test types against a Docker container, for example:
    41  - Command tests (only if a Docker Deamon is available)
    42  - File existence tests
    43  - File content tests
    44  - Metadata test`,
    45  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    46  			startTime = time.Now()
    47  			log.SetStepName(STEP_NAME)
    48  			log.SetVerbose(GeneralConfig.Verbose)
    49  
    50  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    51  
    52  			path, _ := os.Getwd()
    53  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    54  			log.RegisterHook(fatalHook)
    55  
    56  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    57  			if err != nil {
    58  				log.SetErrorCategory(log.ErrorConfiguration)
    59  				return err
    60  			}
    61  
    62  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    63  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    64  				log.RegisterHook(&sentryHook)
    65  			}
    66  
    67  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    68  				splunkClient = &splunk.Splunk{}
    69  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    70  				log.RegisterHook(logCollector)
    71  			}
    72  
    73  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    74  			if err != nil {
    75  				return err
    76  			}
    77  			if err = validation.ValidateStruct(stepConfig); err != nil {
    78  				log.SetErrorCategory(log.ErrorConfiguration)
    79  				return err
    80  			}
    81  
    82  			return nil
    83  		},
    84  		Run: func(_ *cobra.Command, _ []string) {
    85  			stepTelemetryData := telemetry.CustomData{}
    86  			stepTelemetryData.ErrorCode = "1"
    87  			handler := func() {
    88  				config.RemoveVaultSecretFiles()
    89  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    90  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    91  				stepTelemetryData.PiperCommitHash = GitCommit
    92  				telemetryClient.SetData(&stepTelemetryData)
    93  				telemetryClient.Send()
    94  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    95  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    96  				}
    97  			}
    98  			log.DeferExitHandler(handler)
    99  			defer handler()
   100  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   101  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   102  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   103  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   104  					GeneralConfig.HookConfig.SplunkConfig.Token,
   105  					GeneralConfig.HookConfig.SplunkConfig.Index,
   106  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   107  			}
   108  			containerExecuteStructureTests(stepConfig, &stepTelemetryData)
   109  			stepTelemetryData.ErrorCode = "0"
   110  			log.Entry().Info("SUCCESS")
   111  		},
   112  	}
   113  
   114  	addContainerExecuteStructureTestsFlags(createContainerExecuteStructureTestsCmd, &stepConfig)
   115  	return createContainerExecuteStructureTestsCmd
   116  }
   117  
   118  func addContainerExecuteStructureTestsFlags(cmd *cobra.Command, stepConfig *containerExecuteStructureTestsOptions) {
   119  	cmd.Flags().BoolVar(&stepConfig.PullImage, "pullImage", false, "Force a pull of the tested image before running tests. Only relevant for testDriver 'docker'.")
   120  	cmd.Flags().StringVar(&stepConfig.TestConfiguration, "testConfiguration", os.Getenv("PIPER_testConfiguration"), "Container structure test configuration in yml or json format. You can pass a pattern in order to execute multiple tests.")
   121  	cmd.Flags().StringVar(&stepConfig.TestDriver, "testDriver", os.Getenv("PIPER_testDriver"), "Container structure test driver to be used for testing, please see https://github.com/GoogleContainerTools/container-structure-test for details.")
   122  	cmd.Flags().StringVar(&stepConfig.TestImage, "testImage", os.Getenv("PIPER_testImage"), "Image to be tested")
   123  	cmd.Flags().StringVar(&stepConfig.TestReportFilePath, "testReportFilePath", `cst-report.json`, "Path and name of the test report which will be generated")
   124  
   125  	cmd.MarkFlagRequired("testConfiguration")
   126  	cmd.MarkFlagRequired("testImage")
   127  }
   128  
   129  // retrieve step metadata
   130  func containerExecuteStructureTestsMetadata() config.StepData {
   131  	var theMetaData = config.StepData{
   132  		Metadata: config.StepMetadata{
   133  			Name:        "containerExecuteStructureTests",
   134  			Aliases:     []config.Alias{},
   135  			Description: "In this step [Container Structure Tests](https://github.com/GoogleContainerTools/container-structure-test) are executed.",
   136  		},
   137  		Spec: config.StepSpec{
   138  			Inputs: config.StepInputs{
   139  				Parameters: []config.StepParameters{
   140  					{
   141  						Name:        "pullImage",
   142  						ResourceRef: []config.ResourceReference{},
   143  						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"},
   144  						Type:        "bool",
   145  						Mandatory:   false,
   146  						Aliases:     []config.Alias{},
   147  						Default:     false,
   148  					},
   149  					{
   150  						Name:        "testConfiguration",
   151  						ResourceRef: []config.ResourceReference{},
   152  						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"},
   153  						Type:        "string",
   154  						Mandatory:   true,
   155  						Aliases:     []config.Alias{},
   156  						Default:     os.Getenv("PIPER_testConfiguration"),
   157  					},
   158  					{
   159  						Name:        "testDriver",
   160  						ResourceRef: []config.ResourceReference{},
   161  						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"},
   162  						Type:        "string",
   163  						Mandatory:   false,
   164  						Aliases:     []config.Alias{},
   165  						Default:     os.Getenv("PIPER_testDriver"),
   166  					},
   167  					{
   168  						Name:        "testImage",
   169  						ResourceRef: []config.ResourceReference{},
   170  						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"},
   171  						Type:        "string",
   172  						Mandatory:   true,
   173  						Aliases:     []config.Alias{},
   174  						Default:     os.Getenv("PIPER_testImage"),
   175  					},
   176  					{
   177  						Name:        "testReportFilePath",
   178  						ResourceRef: []config.ResourceReference{},
   179  						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"},
   180  						Type:        "string",
   181  						Mandatory:   false,
   182  						Aliases:     []config.Alias{},
   183  						Default:     `cst-report.json`,
   184  					},
   185  				},
   186  			},
   187  			Containers: []config.Container{
   188  				{Image: "ppiper/container-structure-test", Options: []config.Option{{Name: "-u", Value: "0"}, {Name: "--entrypoint", Value: ""}}},
   189  			},
   190  		},
   191  	}
   192  	return theMetaData
   193  }