Skip to content

Commit

Permalink
Merge branch 'main' into bug/tg-output-handling-1453
Browse files Browse the repository at this point in the history
  • Loading branch information
denis256 authored Dec 9, 2024
2 parents b59ca00 + 485b3e0 commit 47d7b3d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 6 deletions.
5 changes: 2 additions & 3 deletions modules/http-helper/http_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ func HTTPDoWithOptionsE(
) (int, string, error) {
logger.Default.Logf(t, "Making an HTTP %s call to URL %s", options.Method, options.Url)

tr := &http.Transport{
TLSClientConfig: options.TlsConfig,
}
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig = options.TlsConfig

client := http.Client{
// By default, Go does not impose a timeout, so an HTTP connection attempt can hang for a LONG time.
Expand Down
24 changes: 24 additions & 0 deletions modules/http-helper/http_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -204,3 +205,26 @@ func failRetryHandler(w http.ResponseWriter, r *http.Request) {
w.Write(bytes)
}
}

func TestGlobalProxy(t *testing.T) {
proxiedURL := ""
httpProxy := getTestServerForFunction(func(w http.ResponseWriter, r *http.Request) {
proxiedURL = r.RequestURI
bodyCopyHandler(w, r)
})
t.Cleanup(httpProxy.Close)

t.Setenv("HTTP_PROXY", httpProxy.URL)
targetURL := "http://www.notexist.com/"
body := "should be copied"

st, b, err := HTTPDoWithOptionsE(t, HttpDoOptions{
Url: targetURL,
Method: http.MethodPost,
Body: strings.NewReader(body),
})
require.NoError(t, err)
assert.Equal(t, http.StatusOK, st)
assert.Equal(t, targetURL, proxiedURL)
assert.Equal(t, body, b)
}
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")
}
4 changes: 3 additions & 1 deletion test/terraform_aws_rds_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -75,12 +76,13 @@ func TestTerraformAwsRdsExample(t *testing.T) {
awsRegion := aws.GetRandomStableRegion(t, nil, nil)
engineVersion := aws.GetValidEngineVersion(t, awsRegion, tt.engineName, tt.majorEngineVersion)
instanceType := aws.GetRecommendedRdsInstanceType(t, awsRegion, tt.engineName, engineVersion, []string{"db.t2.micro", "db.t3.micro", "db.t3.small"})
moduleFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/terraform-aws-rds-example")

// Construct the terraform options with default retryable errors to handle the most common retryable errors in
// terraform testing.
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../examples/terraform-aws-rds-example",
TerraformDir: moduleFolder,

// Variables to pass to our Terraform code using -var options
// "username" and "password" should not be passed from here in a production scenario.
Expand Down

0 comments on commit 47d7b3d

Please sign in to comment.