Skip to content

Commit a990ca7

Browse files
Merge pull request #1480 from gruntwork-io/nolog-ec2-keypair
do not log ec2 keypair
2 parents 572df21 + f89179d commit a990ca7

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

modules/test-structure/save_test_data.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func formatPackerOptionsPath(testFolder string) string {
6565
// SaveEc2KeyPair serializes and saves an Ec2KeyPair into the given folder. This allows you to create an Ec2KeyPair during setup
6666
// and to reuse that Ec2KeyPair later during validation and teardown.
6767
func SaveEc2KeyPair(t testing.TestingT, testFolder string, keyPair *aws.Ec2Keypair) {
68-
SaveTestData(t, formatEc2KeyPairPath(testFolder), true, keyPair)
68+
saveTestData(t, formatEc2KeyPairPath(testFolder), true, keyPair, false)
6969
}
7070

7171
// LoadEc2KeyPair loads and unserializes an Ec2KeyPair from the given folder. This allows you to reuse an Ec2KeyPair that was
@@ -193,6 +193,15 @@ func FormatTestDataPath(testFolder string, filename string) string {
193193
// any contents that exist in the file found at `path` will be overwritten. This has the potential for causing duplicated resources
194194
// and should be used with caution. If `overwrite` is `false`, the save will be skipped and a warning will be logged.
195195
func SaveTestData(t testing.TestingT, path string, overwrite bool, value interface{}) {
196+
saveTestData(t, path, overwrite, value, true)
197+
}
198+
199+
// saveTestData serializes and saves a value used at test time to the given path. This allows you to create some sort of test data
200+
// (e.g., TerraformOptions) during setup and to reuse this data later during validation and teardown. If `overwrite` is `true`,
201+
// any contents that exist in the file found at `path` will be overwritten. This has the potential for causing duplicated resources
202+
// and should be used with caution. If `overwrite` is `false`, the save will be skipped and a warning will be logged.
203+
// If `loggedVal` is `true`, the value will be logged as JSON.
204+
func saveTestData(t testing.TestingT, path string, overwrite bool, value interface{}, loggedVal bool) {
196205
logger.Default.Logf(t, "Storing test data in %s so it can be reused later", path)
197206

198207
if IsTestDataPresent(t, path) {
@@ -209,7 +218,9 @@ func SaveTestData(t testing.TestingT, path string, overwrite bool, value interfa
209218
t.Fatalf("Failed to convert value %s to JSON: %v", path, err)
210219
}
211220

212-
logger.Default.Logf(t, "Marshalled JSON: %s", string(bytes))
221+
if loggedVal {
222+
logger.Default.Logf(t, "Marshalled JSON: %s", string(bytes))
223+
}
213224

214225
parentDir := filepath.Dir(path)
215226
if err := os.MkdirAll(parentDir, 0777); err != nil {

modules/test-structure/save_test_data_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package test_structure
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"os"
7+
"strings"
58
"testing"
69

10+
"github.com/gruntwork-io/terratest/modules/aws"
711
"github.com/gruntwork-io/terratest/modules/files"
812
"github.com/gruntwork-io/terratest/modules/k8s"
13+
"github.com/gruntwork-io/terratest/modules/logger"
14+
"github.com/gruntwork-io/terratest/modules/ssh"
915
"github.com/gruntwork-io/terratest/modules/terraform"
16+
gotesting "github.com/gruntwork-io/terratest/modules/testing"
1017
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
1119
)
1220

1321
type testData struct {
@@ -272,3 +280,37 @@ func TestSaveAndLoadKubectlOptions(t *testing.T) {
272280
actualData := LoadKubectlOptions(t, tmpFolder)
273281
assert.Equal(t, expectedData, actualData)
274282
}
283+
284+
type tStringLogger struct {
285+
sb strings.Builder
286+
}
287+
288+
func (l *tStringLogger) Logf(t gotesting.TestingT, format string, args ...interface{}) {
289+
l.sb.WriteString(fmt.Sprintf(format, args...))
290+
l.sb.WriteRune('\n')
291+
}
292+
293+
func TestSaveAndLoadEC2KeyPair(t *testing.T) {
294+
def, slogger := logger.Default, &tStringLogger{}
295+
logger.Default = logger.New(slogger)
296+
t.Cleanup(func() {
297+
logger.Default = def
298+
})
299+
300+
keyPair, err := ssh.GenerateRSAKeyPairE(t, 2048)
301+
require.NoError(t, err)
302+
ec2KeyPair := &aws.Ec2Keypair{
303+
KeyPair: keyPair,
304+
Name: "test-ec2-key-pair",
305+
Region: "us-east-1",
306+
}
307+
storedEC2KeyPair, err := json.Marshal(ec2KeyPair)
308+
require.NoError(t, err)
309+
310+
tmpFolder := t.TempDir()
311+
SaveEc2KeyPair(t, tmpFolder, ec2KeyPair)
312+
loadedEC2KeyPair := LoadEc2KeyPair(t, tmpFolder)
313+
assert.Equal(t, ec2KeyPair, loadedEC2KeyPair)
314+
315+
assert.NotContains(t, slogger.sb.String(), string(storedEC2KeyPair), "stored ec2 key pair should not be logged")
316+
}

0 commit comments

Comments
 (0)