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