github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/modules/helm/format_test.go (about)

     1  package helm
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestFormatSetValuesAsArgs(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	testCases := []struct {
    18  		name         string
    19  		setValues    map[string]string
    20  		setStrValues map[string]string
    21  		expected     []string
    22  		expectedStr  []string
    23  	}{
    24  		{
    25  			"EmptyValue",
    26  			map[string]string{},
    27  			map[string]string{},
    28  			[]string{},
    29  			[]string{},
    30  		},
    31  		{
    32  			"SingleValue",
    33  			map[string]string{"containerImage": "null"},
    34  			map[string]string{"numericString": "123123123123"},
    35  			[]string{"--set", "containerImage=null"},
    36  			[]string{"--set-string", "numericString=123123123123"},
    37  		},
    38  		{
    39  			"MultipleValues",
    40  			map[string]string{
    41  				"containerImage.repository": "nginx",
    42  				"containerImage.tag":        "v1.15.4",
    43  			},
    44  			map[string]string{
    45  				"numericString": "123123123123",
    46  				"otherString":   "null",
    47  			},
    48  			[]string{
    49  				"--set", "containerImage.repository=nginx",
    50  				"--set", "containerImage.tag=v1.15.4",
    51  			},
    52  			[]string{
    53  				"--set-string", "numericString=123123123123",
    54  				"--set-string", "otherString=null",
    55  			},
    56  		},
    57  	}
    58  
    59  	for _, testCase := range testCases {
    60  		// Capture the range value and force it into this scope. Otherwise, it is defined outside this block so it can
    61  		// change when the subtests parallelize and switch contexts.
    62  		testCase := testCase
    63  
    64  		t.Run(testCase.name, func(t *testing.T) {
    65  			t.Parallel()
    66  			assert.Equal(t, formatSetValuesAsArgs(testCase.setValues, "--set"), testCase.expected)
    67  			assert.Equal(t, formatSetValuesAsArgs(testCase.setStrValues, "--set-string"), testCase.expectedStr)
    68  		})
    69  	}
    70  }
    71  
    72  func TestFormatSetFilesAsArgs(t *testing.T) {
    73  	t.Parallel()
    74  
    75  	paths, err := createTempFiles(2)
    76  	defer deleteTempFiles(paths)
    77  	require.NoError(t, err)
    78  	absPathList := absPaths(t, paths)
    79  
    80  	testCases := []struct {
    81  		name     string
    82  		setFiles map[string]string
    83  		expected []string
    84  	}{
    85  		{
    86  			"EmptyValue",
    87  			map[string]string{},
    88  			[]string{},
    89  		},
    90  		{
    91  			"SingleValue",
    92  			map[string]string{"containerImage": paths[0]},
    93  			[]string{"--set-file", fmt.Sprintf("containerImage=%s", absPathList[0])},
    94  		},
    95  		{
    96  			"MultipleValues",
    97  			map[string]string{
    98  				"containerImage.repository": paths[0],
    99  				"containerImage.tag":        paths[1],
   100  			},
   101  			[]string{
   102  				"--set-file", fmt.Sprintf("containerImage.repository=%s", absPathList[0]),
   103  				"--set-file", fmt.Sprintf("containerImage.tag=%s", absPathList[1]),
   104  			},
   105  		},
   106  	}
   107  
   108  	// We create a subtest group that is NOT parallel, so the main test waits for all the tests to finish. This way, we
   109  	// don't delete the files until the subtests finish.
   110  	t.Run("group", func(t *testing.T) {
   111  		for _, testCase := range testCases {
   112  			// Capture the range value and force it into this scope. Otherwise, it is defined outside this block so it can
   113  			// change when the subtests parallelize and switch contexts.
   114  			testCase := testCase
   115  
   116  			t.Run(testCase.name, func(t *testing.T) {
   117  				t.Parallel()
   118  				assert.Equal(t, formatSetFilesAsArgs(t, testCase.setFiles), testCase.expected)
   119  			})
   120  		}
   121  	})
   122  }
   123  
   124  func TestFormatValuesFilesAsArgs(t *testing.T) {
   125  	t.Parallel()
   126  
   127  	paths, err := createTempFiles(2)
   128  	defer deleteTempFiles(paths)
   129  	require.NoError(t, err)
   130  	absPathList := absPaths(t, paths)
   131  
   132  	testCases := []struct {
   133  		name        string
   134  		valuesFiles []string
   135  		expected    []string
   136  	}{
   137  		{
   138  			"EmptyValue",
   139  			[]string{},
   140  			[]string{},
   141  		},
   142  		{
   143  			"SingleValue",
   144  			[]string{paths[0]},
   145  			[]string{"-f", absPathList[0]},
   146  		},
   147  		{
   148  			"MultipleValues",
   149  			paths,
   150  			[]string{
   151  				"-f", absPathList[0],
   152  				"-f", absPathList[1],
   153  			},
   154  		},
   155  	}
   156  
   157  	// We create a subtest group that is NOT parallel, so the main test waits for all the tests to finish. This way, we
   158  	// don't delete the files until the subtests finish.
   159  	t.Run("group", func(t *testing.T) {
   160  		for _, testCase := range testCases {
   161  			// Capture the range value and force it into this scope. Otherwise, it is defined outside this block so it can
   162  			// change when the subtests parallelize and switch contexts.
   163  			testCase := testCase
   164  
   165  			t.Run(testCase.name, func(t *testing.T) {
   166  				t.Parallel()
   167  				assert.Equal(t, formatValuesFilesAsArgs(t, testCase.valuesFiles), testCase.expected)
   168  			})
   169  		}
   170  	})
   171  }
   172  
   173  // createTempFiles will create numFiles temporary files that can pass the abspath checks.
   174  func createTempFiles(numFiles int) ([]string, error) {
   175  	paths := []string{}
   176  	for i := 0; i < numFiles; i++ {
   177  		tmpFile, err := ioutil.TempFile("", "")
   178  		defer tmpFile.Close()
   179  		// We don't use require or t.Fatal here so that we give a chance to delete any temp files that were created
   180  		// before this error
   181  		if err != nil {
   182  			return paths, err
   183  		}
   184  		paths = append(paths, tmpFile.Name())
   185  	}
   186  	return paths, nil
   187  }
   188  
   189  // deleteTempFiles will delete all the given temp file paths
   190  func deleteTempFiles(paths []string) {
   191  	for _, path := range paths {
   192  		os.Remove(path)
   193  	}
   194  }
   195  
   196  // absPaths will return the absolute paths of each path in the list
   197  func absPaths(t *testing.T, paths []string) []string {
   198  	out := []string{}
   199  	for _, path := range paths {
   200  		absPath, err := filepath.Abs(path)
   201  		require.NoError(t, err)
   202  		out = append(out, absPath)
   203  	}
   204  	return out
   205  }