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