github.com/mponton/terratest@v0.44.0/modules/test-structure/save_test_data_test.go (about)

     1  package test_structure
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/mponton/terratest/modules/files"
     8  	"github.com/mponton/terratest/modules/k8s"
     9  	"github.com/mponton/terratest/modules/terraform"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type testData struct {
    14  	Foo string
    15  	Bar bool
    16  	Baz map[string]interface{}
    17  }
    18  
    19  func TestSaveAndLoadTestData(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	isTestDataPresent := IsTestDataPresent(t, "/file/that/does/not/exist")
    23  	assert.False(t, isTestDataPresent, "Expected no test data would be present because no test data file exists.")
    24  
    25  	tmpFile, err := ioutil.TempFile("", "save-and-load-test-data")
    26  	if err != nil {
    27  		t.Fatalf("Failed to create temp dir: %v", err)
    28  	}
    29  
    30  	expectedData := testData{
    31  		Foo: "foo",
    32  		Bar: true,
    33  		Baz: map[string]interface{}{"abc": "def", "ghi": 1.0, "klm": false},
    34  	}
    35  
    36  	isTestDataPresent = IsTestDataPresent(t, tmpFile.Name())
    37  	assert.False(t, isTestDataPresent, "Expected no test data would be present because file exists but no data has been written yet.")
    38  
    39  	SaveTestData(t, tmpFile.Name(), expectedData)
    40  
    41  	isTestDataPresent = IsTestDataPresent(t, tmpFile.Name())
    42  	assert.True(t, isTestDataPresent, "Expected test data would be present because file exists and data has been written to file.")
    43  
    44  	actualData := testData{}
    45  	LoadTestData(t, tmpFile.Name(), &actualData)
    46  	assert.Equal(t, expectedData, actualData)
    47  
    48  	CleanupTestData(t, tmpFile.Name())
    49  	assert.False(t, files.FileExists(tmpFile.Name()))
    50  }
    51  
    52  func TestIsEmptyJson(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	var jsonValue []byte
    56  	var isEmpty bool
    57  
    58  	jsonValue = []byte("null")
    59  	isEmpty = isEmptyJSON(t, jsonValue)
    60  	assert.True(t, isEmpty, `The JSON literal "null" should be treated as an empty value.`)
    61  
    62  	jsonValue = []byte("false")
    63  	isEmpty = isEmptyJSON(t, jsonValue)
    64  	assert.True(t, isEmpty, `The JSON literal "false" should be treated as an empty value.`)
    65  
    66  	jsonValue = []byte("true")
    67  	isEmpty = isEmptyJSON(t, jsonValue)
    68  	assert.False(t, isEmpty, `The JSON literal "true" should be treated as a non-empty value.`)
    69  
    70  	jsonValue = []byte("0")
    71  	isEmpty = isEmptyJSON(t, jsonValue)
    72  	assert.True(t, isEmpty, `The JSON literal "0" should be treated as an empty value.`)
    73  
    74  	jsonValue = []byte("1")
    75  	isEmpty = isEmptyJSON(t, jsonValue)
    76  	assert.False(t, isEmpty, `The JSON literal "1" should be treated as a non-empty value.`)
    77  
    78  	jsonValue = []byte("{}")
    79  	isEmpty = isEmptyJSON(t, jsonValue)
    80  	assert.True(t, isEmpty, `The JSON value "{}" should be treated as an empty value.`)
    81  
    82  	jsonValue = []byte(`{ "key": "val" }`)
    83  	isEmpty = isEmptyJSON(t, jsonValue)
    84  	assert.False(t, isEmpty, `The JSON value { "key": "val" } should be treated as a non-empty value.`)
    85  
    86  	jsonValue = []byte(`[]`)
    87  	isEmpty = isEmptyJSON(t, jsonValue)
    88  	assert.True(t, isEmpty, `The JSON value "[]" should be treated as an empty value.`)
    89  
    90  	jsonValue = []byte(`[{ "key": "val" }]`)
    91  	isEmpty = isEmptyJSON(t, jsonValue)
    92  	assert.False(t, isEmpty, `The JSON value [{ "key": "val" }] should be treated as a non-empty value.`)
    93  }
    94  
    95  func TestSaveAndLoadTerraformOptions(t *testing.T) {
    96  	t.Parallel()
    97  
    98  	tmpFolder := t.TempDir()
    99  
   100  	expectedData := &terraform.Options{
   101  		TerraformDir: "/abc/def/ghi",
   102  		Vars:         map[string]interface{}{},
   103  	}
   104  	SaveTerraformOptions(t, tmpFolder, expectedData)
   105  
   106  	actualData := LoadTerraformOptions(t, tmpFolder)
   107  	assert.Equal(t, expectedData, actualData)
   108  }
   109  
   110  func TestSaveAndLoadAmiId(t *testing.T) {
   111  	t.Parallel()
   112  
   113  	tmpFolder := t.TempDir()
   114  
   115  	expectedData := "ami-abcd1234"
   116  	SaveAmiId(t, tmpFolder, expectedData)
   117  
   118  	actualData := LoadAmiId(t, tmpFolder)
   119  	assert.Equal(t, expectedData, actualData)
   120  }
   121  
   122  func TestSaveAndLoadArtifactID(t *testing.T) {
   123  	t.Parallel()
   124  
   125  	tmpFolder := t.TempDir()
   126  
   127  	expectedData := "terratest-packer-example-2018-08-08t15-35-19z"
   128  	SaveArtifactID(t, tmpFolder, expectedData)
   129  
   130  	actualData := LoadArtifactID(t, tmpFolder)
   131  	assert.Equal(t, expectedData, actualData)
   132  }
   133  
   134  func TestSaveAndLoadNamedStrings(t *testing.T) {
   135  	t.Parallel()
   136  
   137  	tmpFolder := t.TempDir()
   138  
   139  	name1 := "test-ami"
   140  	expectedData1 := "ami-abcd1234"
   141  
   142  	name2 := "test-ami2"
   143  	expectedData2 := "ami-xyz98765"
   144  
   145  	name3 := "test-image"
   146  	expectedData3 := "terratest-packer-example-2018-08-08t15-35-19z"
   147  
   148  	name4 := "test-image2"
   149  	expectedData4 := "terratest-packer-example-2018-01-03t12-35-00z"
   150  
   151  	SaveString(t, tmpFolder, name1, expectedData1)
   152  	SaveString(t, tmpFolder, name2, expectedData2)
   153  	SaveString(t, tmpFolder, name3, expectedData3)
   154  	SaveString(t, tmpFolder, name4, expectedData4)
   155  
   156  	actualData1 := LoadString(t, tmpFolder, name1)
   157  	actualData2 := LoadString(t, tmpFolder, name2)
   158  	actualData3 := LoadString(t, tmpFolder, name3)
   159  	actualData4 := LoadString(t, tmpFolder, name4)
   160  
   161  	assert.Equal(t, expectedData1, actualData1)
   162  	assert.Equal(t, expectedData2, actualData2)
   163  	assert.Equal(t, expectedData3, actualData3)
   164  	assert.Equal(t, expectedData4, actualData4)
   165  }
   166  
   167  func TestSaveDuplicateTestData(t *testing.T) {
   168  	t.Parallel()
   169  
   170  	tmpFolder := t.TempDir()
   171  
   172  	name := "hello-world"
   173  	val1 := "hello world"
   174  	val2 := "buenos dias, mundo"
   175  
   176  	SaveString(t, tmpFolder, name, val1)
   177  	SaveString(t, tmpFolder, name, val2)
   178  
   179  	actualVal := LoadString(t, tmpFolder, name)
   180  
   181  	assert.Equal(t, val2, actualVal, "Actual test data should use overwritten values")
   182  }
   183  
   184  func TestSaveAndLoadNamedInts(t *testing.T) {
   185  	t.Parallel()
   186  
   187  	tmpFolder := t.TempDir()
   188  
   189  	name1 := "test-int1"
   190  	expectedData1 := 23842834
   191  
   192  	name2 := "test-int2"
   193  	expectedData2 := 52
   194  
   195  	SaveInt(t, tmpFolder, name1, expectedData1)
   196  	SaveInt(t, tmpFolder, name2, expectedData2)
   197  
   198  	actualData1 := LoadInt(t, tmpFolder, name1)
   199  	actualData2 := LoadInt(t, tmpFolder, name2)
   200  
   201  	assert.Equal(t, expectedData1, actualData1)
   202  	assert.Equal(t, expectedData2, actualData2)
   203  }
   204  
   205  func TestSaveAndLoadKubectlOptions(t *testing.T) {
   206  	t.Parallel()
   207  
   208  	tmpFolder := t.TempDir()
   209  
   210  	expectedData := &k8s.KubectlOptions{
   211  		ContextName: "terratest-context",
   212  		ConfigPath:  "~/.kube/config",
   213  		Namespace:   "default",
   214  		Env: map[string]string{
   215  			"TERRATEST_ENV_VAR": "terratest",
   216  		},
   217  	}
   218  	SaveKubectlOptions(t, tmpFolder, expectedData)
   219  
   220  	actualData := LoadKubectlOptions(t, tmpFolder)
   221  	assert.Equal(t, expectedData, actualData)
   222  }