github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/getConfig_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/SAP/jenkins-library/pkg/config"
    14  	"github.com/spf13/cobra"
    15  	flag "github.com/spf13/pflag"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func configOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, error) {
    20  	var r string
    21  	switch name {
    22  	case "TestAddCustomDefaults_default1":
    23  		r = "default1"
    24  	case "TestAddCustomDefaults_default2":
    25  		r = "default3"
    26  	default:
    27  		r = ""
    28  	}
    29  	return io.NopCloser(strings.NewReader(r)), nil
    30  }
    31  
    32  func TestConfigCommand(t *testing.T) {
    33  	cmd := ConfigCommand()
    34  
    35  	gotReq := []string{}
    36  	gotOpt := []string{}
    37  
    38  	cmd.Flags().VisitAll(func(pflag *flag.Flag) {
    39  		annotations, found := pflag.Annotations[cobra.BashCompOneRequiredFlag]
    40  		if found && annotations[0] == "true" {
    41  			gotReq = append(gotReq, pflag.Name)
    42  		} else {
    43  			gotOpt = append(gotOpt, pflag.Name)
    44  		}
    45  	})
    46  
    47  	t.Run("Required flags", func(t *testing.T) {
    48  		exp := []string{}
    49  		assert.Equal(t, exp, gotReq, "required flags incorrect")
    50  	})
    51  
    52  	t.Run("Optional flags", func(t *testing.T) {
    53  		exp := []string{"contextConfig", "output", "outputFile", "parametersJSON", "stageConfig", "stageConfigAcceptedParams", "stepMetadata", "stepName"}
    54  		assert.Equal(t, exp, gotOpt, "optional flags incorrect")
    55  	})
    56  
    57  	t.Run("Run", func(t *testing.T) {
    58  		t.Run("Success case", func(t *testing.T) {
    59  			configOptions.OpenFile = configOpenFileMock
    60  			configOptions.StepName = "githubCreateIssue"
    61  			cmd.Run(cmd, []string{})
    62  		})
    63  	})
    64  }
    65  
    66  func TestDefaultsAndFilters(t *testing.T) {
    67  	metadata := config.StepData{
    68  		Spec: config.StepSpec{
    69  			Inputs: config.StepInputs{
    70  				Parameters: []config.StepParameters{
    71  					{Name: "paramOne", Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS", "ENV"}},
    72  				},
    73  			},
    74  		},
    75  	}
    76  
    77  	t.Run("Context config", func(t *testing.T) {
    78  		configOptions.ContextConfig = true
    79  		defer func() { configOptions.ContextConfig = false }()
    80  		defaults, filters, err := defaultsAndFilters(&metadata, "stepName")
    81  
    82  		assert.Equal(t, 1, len(defaults), "getting defaults failed")
    83  		assert.Equal(t, 3, len(filters.All), "wrong number of filter values")
    84  		assert.NoError(t, err, "error occurred but none expected")
    85  	})
    86  
    87  	t.Run("Step config", func(t *testing.T) {
    88  		defaults, filters, err := defaultsAndFilters(&metadata, "stepName")
    89  		assert.Equal(t, 0, len(defaults), "getting defaults failed")
    90  		assert.Equal(t, 2, len(filters.All), "wrong number of filter values")
    91  		assert.NoError(t, err, "error occurred but none expected")
    92  	})
    93  }
    94  
    95  func TestApplyContextConditions(t *testing.T) {
    96  
    97  	tt := []struct {
    98  		name     string
    99  		metadata config.StepData
   100  		conf     config.StepConfig
   101  		expected map[string]interface{}
   102  	}{
   103  		{
   104  			name:     "no context conditions",
   105  			metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{}}},
   106  			conf:     config.StepConfig{Config: map[string]interface{}{}},
   107  			expected: map[string]interface{}{},
   108  		},
   109  		{
   110  			name: "context condition not met",
   111  			metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
   112  				{
   113  					Image: "myDefaultImage:latest",
   114  					Conditions: []config.Condition{
   115  						{
   116  							ConditionRef: "strings-equal",
   117  							Params: []config.Param{
   118  								{Name: "param1", Value: "val2"},
   119  							},
   120  						},
   121  					},
   122  				},
   123  			}}},
   124  			conf: config.StepConfig{Config: map[string]interface{}{
   125  				"param1": "val1",
   126  				"val1":   map[string]interface{}{"dockerImage": "myTestImage:latest"},
   127  			}},
   128  			expected: map[string]interface{}{
   129  				"param1": "val1",
   130  				"val1":   map[string]interface{}{"dockerImage": "myTestImage:latest"},
   131  			},
   132  		},
   133  		{
   134  			name: "context condition met",
   135  			metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
   136  				{
   137  					Image: "myDefaultImage:latest",
   138  					Conditions: []config.Condition{
   139  						{
   140  							ConditionRef: "strings-equal",
   141  							Params: []config.Param{
   142  								{Name: "param1", Value: "val1"},
   143  							},
   144  						},
   145  					},
   146  				},
   147  			}}},
   148  			conf: config.StepConfig{Config: map[string]interface{}{
   149  				"param1": "val1",
   150  				"val1":   map[string]interface{}{"dockerImage": "myTestImage:latest"},
   151  			}},
   152  			expected: map[string]interface{}{
   153  				"param1":      "val1",
   154  				"dockerImage": "myTestImage:latest",
   155  			},
   156  		},
   157  		{
   158  			name: "context condition met - root defined already",
   159  			metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
   160  				{
   161  					Image: "myDefaultImage:latest",
   162  					Conditions: []config.Condition{
   163  						{
   164  							ConditionRef: "strings-equal",
   165  							Params: []config.Param{
   166  								{Name: "param1", Value: "val1"},
   167  							},
   168  						},
   169  					},
   170  				},
   171  			}}},
   172  			conf: config.StepConfig{Config: map[string]interface{}{
   173  				"param1":      "val1",
   174  				"dockerImage": "myTestImage:latest",
   175  			}},
   176  			expected: map[string]interface{}{
   177  				"param1":      "val1",
   178  				"dockerImage": "myTestImage:latest",
   179  			},
   180  		},
   181  		{
   182  			name: "context condition met - root defined and deep value defined",
   183  			metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
   184  				{
   185  					Image: "myDefaultImage:latest",
   186  					Conditions: []config.Condition{
   187  						{
   188  							ConditionRef: "strings-equal",
   189  							Params: []config.Param{
   190  								{Name: "param1", Value: "val1"},
   191  							},
   192  						},
   193  					},
   194  				},
   195  			}}},
   196  			conf: config.StepConfig{Config: map[string]interface{}{
   197  				"param1":      "val1",
   198  				"val1":        map[string]interface{}{"dockerImage": "mySubTestImage:latest"},
   199  				"dockerImage": "myTestImage:latest",
   200  			}},
   201  			expected: map[string]interface{}{
   202  				"param1":      "val1",
   203  				"dockerImage": "myTestImage:latest",
   204  			},
   205  		},
   206  		{
   207  			name: "context condition met - root defined as empty",
   208  			metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
   209  				{
   210  					Image: "myDefaultImage:latest",
   211  					Conditions: []config.Condition{
   212  						{
   213  							ConditionRef: "strings-equal",
   214  							Params: []config.Param{
   215  								{Name: "param1", Value: "val1"},
   216  							},
   217  						},
   218  					},
   219  				},
   220  			}}},
   221  			conf: config.StepConfig{Config: map[string]interface{}{
   222  				"param1":      "val1",
   223  				"dockerImage": "",
   224  			}},
   225  			expected: map[string]interface{}{
   226  				"param1":      "val1",
   227  				"dockerImage": "",
   228  			},
   229  		},
   230  		//ToDo: Sidecar behavior not properly working, expects sidecarImage, ... parameters and not dockerImage
   231  		{
   232  			name: "sidecar context condition met",
   233  			metadata: config.StepData{Spec: config.StepSpec{Sidecars: []config.Container{
   234  				{
   235  					Image: "myTestImage:latest",
   236  					Conditions: []config.Condition{
   237  						{
   238  							ConditionRef: "strings-equal",
   239  							Params: []config.Param{
   240  								{Name: "param1", Value: "val1"},
   241  							},
   242  						},
   243  					},
   244  				},
   245  			}}},
   246  			conf: config.StepConfig{Config: map[string]interface{}{
   247  				"param1": "val1",
   248  				"val1":   map[string]interface{}{"dockerImage": "myTestImage:latest"},
   249  			}},
   250  			expected: map[string]interface{}{
   251  				"param1":      "val1",
   252  				"dockerImage": "myTestImage:latest",
   253  			},
   254  		},
   255  	}
   256  
   257  	for run, test := range tt {
   258  		t.Run(test.name, func(t *testing.T) {
   259  			applyContextConditions(test.metadata, &test.conf)
   260  			assert.Equalf(t, test.expected, test.conf.Config, fmt.Sprintf("Run %v failed", run))
   261  		})
   262  	}
   263  }
   264  
   265  func TestPrepareOutputEnvironment(t *testing.T) {
   266  	outputResources := []config.StepResources{
   267  		{
   268  			Name: "commonPipelineEnvironment",
   269  			Type: "piperEnvironment",
   270  			Parameters: []map[string]interface{}{
   271  				{"name": "param0"},
   272  				{"name": "path1/param1"},
   273  				{"name": "path2/param2"},
   274  			},
   275  		},
   276  		{
   277  			Name: "influx",
   278  			Type: "influx",
   279  			Parameters: []map[string]interface{}{
   280  				{
   281  					"name": "measurement0",
   282  					"fields": []map[string]string{
   283  						{"name": "influx0_0"},
   284  						{"name": "influx0_1"},
   285  					},
   286  				},
   287  				{
   288  					"name": "measurement1",
   289  					"fields": []map[string]string{
   290  						{"name": "influx1_0"},
   291  						{"name": "influx1_1"},
   292  					},
   293  				},
   294  			},
   295  		},
   296  	}
   297  
   298  	dir := t.TempDir()
   299  
   300  	prepareOutputEnvironment(outputResources, dir)
   301  	assert.DirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "path1"))
   302  	assert.DirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "path2"))
   303  	assert.DirExists(t, filepath.Join(dir, "influx", "measurement0"))
   304  	assert.DirExists(t, filepath.Join(dir, "influx", "measurement1"))
   305  	assert.NoDirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "param0"))
   306  	assert.NoDirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "path1", "param1"))
   307  	assert.NoDirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "path2", "param2"))
   308  	assert.NoDirExists(t, filepath.Join(dir, "influx", "measurement0", "influx0_0"))
   309  	assert.NoDirExists(t, filepath.Join(dir, "influx", "measurement1", "influx0_1"))
   310  }