Skip to content

Commit

Permalink
Merge pull request #1480 from gruntwork-io/nolog-ec2-keypair
Browse files Browse the repository at this point in the history
do not log ec2 keypair
  • Loading branch information
james03160927 authored Dec 7, 2024
2 parents 572df21 + f89179d commit a990ca7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
15 changes: 13 additions & 2 deletions modules/test-structure/save_test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func formatPackerOptionsPath(testFolder string) string {
// SaveEc2KeyPair serializes and saves an Ec2KeyPair into the given folder. This allows you to create an Ec2KeyPair during setup
// and to reuse that Ec2KeyPair later during validation and teardown.
func SaveEc2KeyPair(t testing.TestingT, testFolder string, keyPair *aws.Ec2Keypair) {
SaveTestData(t, formatEc2KeyPairPath(testFolder), true, keyPair)
saveTestData(t, formatEc2KeyPairPath(testFolder), true, keyPair, false)
}

// LoadEc2KeyPair loads and unserializes an Ec2KeyPair from the given folder. This allows you to reuse an Ec2KeyPair that was
Expand Down Expand Up @@ -193,6 +193,15 @@ func FormatTestDataPath(testFolder string, filename string) string {
// any contents that exist in the file found at `path` will be overwritten. This has the potential for causing duplicated resources
// and should be used with caution. If `overwrite` is `false`, the save will be skipped and a warning will be logged.
func SaveTestData(t testing.TestingT, path string, overwrite bool, value interface{}) {
saveTestData(t, path, overwrite, value, true)
}

// saveTestData serializes and saves a value used at test time to the given path. This allows you to create some sort of test data
// (e.g., TerraformOptions) during setup and to reuse this data later during validation and teardown. If `overwrite` is `true`,
// any contents that exist in the file found at `path` will be overwritten. This has the potential for causing duplicated resources
// and should be used with caution. If `overwrite` is `false`, the save will be skipped and a warning will be logged.
// If `loggedVal` is `true`, the value will be logged as JSON.
func saveTestData(t testing.TestingT, path string, overwrite bool, value interface{}, loggedVal bool) {
logger.Default.Logf(t, "Storing test data in %s so it can be reused later", path)

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

logger.Default.Logf(t, "Marshalled JSON: %s", string(bytes))
if loggedVal {
logger.Default.Logf(t, "Marshalled JSON: %s", string(bytes))
}

parentDir := filepath.Dir(path)
if err := os.MkdirAll(parentDir, 0777); err != nil {
Expand Down
42 changes: 42 additions & 0 deletions modules/test-structure/save_test_data_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package test_structure

import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/files"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/ssh"
"github.com/gruntwork-io/terratest/modules/terraform"
gotesting "github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type testData struct {
Expand Down Expand Up @@ -272,3 +280,37 @@ func TestSaveAndLoadKubectlOptions(t *testing.T) {
actualData := LoadKubectlOptions(t, tmpFolder)
assert.Equal(t, expectedData, actualData)
}

type tStringLogger struct {
sb strings.Builder
}

func (l *tStringLogger) Logf(t gotesting.TestingT, format string, args ...interface{}) {
l.sb.WriteString(fmt.Sprintf(format, args...))
l.sb.WriteRune('\n')
}

func TestSaveAndLoadEC2KeyPair(t *testing.T) {
def, slogger := logger.Default, &tStringLogger{}
logger.Default = logger.New(slogger)
t.Cleanup(func() {
logger.Default = def
})

keyPair, err := ssh.GenerateRSAKeyPairE(t, 2048)
require.NoError(t, err)
ec2KeyPair := &aws.Ec2Keypair{
KeyPair: keyPair,
Name: "test-ec2-key-pair",
Region: "us-east-1",
}
storedEC2KeyPair, err := json.Marshal(ec2KeyPair)
require.NoError(t, err)

tmpFolder := t.TempDir()
SaveEc2KeyPair(t, tmpFolder, ec2KeyPair)
loadedEC2KeyPair := LoadEc2KeyPair(t, tmpFolder)
assert.Equal(t, ec2KeyPair, loadedEC2KeyPair)

assert.NotContains(t, slogger.sb.String(), string(storedEC2KeyPair), "stored ec2 key pair should not be logged")
}

0 comments on commit a990ca7

Please sign in to comment.