Skip to content

Commit

Permalink
Set user agent appends for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Sep 4, 2020
1 parent 01a7e95 commit b28c270
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
2 changes: 2 additions & 0 deletions tfexec/internal/e2etest/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func runTestVersions(t *testing.T, versions []string, fixtureName string, cb fun
t.Fatal(err)
}

tf.SetAppendUserAgent("tfexec-e2etest")

runningVersion, _, err := tf.Version(context.Background(), false)
if err != nil {
t.Fatalf("unable to determin running version (expected %q): %s", tfv, err)
Expand Down
40 changes: 21 additions & 19 deletions tfexec/internal/testutil/tfcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func (tf *TFCache) Version(t *testing.T, v string) string {
}

func (tf *TFCache) find(t *testing.T, key string, finder func(dir string) tfinstall.ExecPathFinder) string {

t.Helper()

if tf.dir == "" {
Expand All @@ -58,29 +57,32 @@ func (tf *TFCache) find(t *testing.T, key string, finder func(dir string) tfinst
tf.Lock()
defer tf.Unlock()

path, ok := tf.execs[key]
if !ok {
keyDir := key
keyDir = strings.ReplaceAll(keyDir, ":", "-")
keyDir = strings.ReplaceAll(keyDir, "/", "-")
if path, ok := tf.execs[key]; ok {
return path
}

keyDir := key
keyDir = strings.ReplaceAll(keyDir, ":", "-")
keyDir = strings.ReplaceAll(keyDir, "/", "-")

dir := filepath.Join(tf.dir, keyDir)
dir := filepath.Join(tf.dir, keyDir)

t.Logf("caching exec %q in dir %q", key, dir)
t.Logf("caching exec %q in dir %q", key, dir)

err := os.MkdirAll(dir, 0777)
if err != nil {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic(fmt.Sprintf("unable to mkdir %q: %s", dir, err))
}
err := os.MkdirAll(dir, 0777)
if err != nil {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic(fmt.Sprintf("unable to mkdir %q: %s", dir, err))
}

path, err = tfinstall.Find(context.Background(), finder(dir))
if err != nil {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic(fmt.Sprintf("error installing terraform %q: %s", key, err))
}
tf.execs[key] = path
ctx := context.Background()
ctx = tfinstall.WithUserAgentAppend(ctx, "tfexec-testutil")
path, err := tfinstall.Find(ctx, finder(dir))
if err != nil {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic(fmt.Sprintf("error installing terraform %q: %s", key, err))
}
tf.execs[key] = path

return path
}
2 changes: 1 addition & 1 deletion tfinstall/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func downloadWithVerification(ctx context.Context, tfVersion string, installDir

httpGetter := &getter.HttpGetter{
Netrc: true,
Client: newHTTPClient(),
Client: newHTTPClient(ctx),
}
client := getter.Client{
Ctx: ctx,
Expand Down
17 changes: 16 additions & 1 deletion tfinstall/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tfinstall

import (
"context"
"fmt"
"net/http"
"os"
Expand All @@ -11,6 +12,16 @@ import (
intversion "github.com/hashicorp/terraform-exec/internal/version"
)

type contextKey string

var (
userAgentAppendContextKey = contextKey("tf-append-user-agent")
)

func WithUserAgentAppend(ctx context.Context, appendUserAgent string) context.Context {
return context.WithValue(ctx, userAgentAppendContextKey, appendUserAgent)
}

type userAgentRoundTripper struct {
inner http.RoundTripper
userAgent string
Expand All @@ -23,8 +34,12 @@ func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, e
return rt.inner.RoundTrip(req)
}

func newHTTPClient() *http.Client {
func newHTTPClient(ctx context.Context) *http.Client {
appendUA := os.Getenv("TF_APPEND_USER_AGENT")
if ctxUA, ok := ctx.Value(userAgentAppendContextKey).(string); ok {
appendUA = strings.TrimSpace(appendUA + " " + ctxUA)
}

userAgent := strings.TrimSpace(fmt.Sprintf("HashiCorp-tfinstall/%s %s", intversion.ModuleVersion(), appendUA))

cli := cleanhttp.DefaultPooledClient()
Expand Down

0 comments on commit b28c270

Please sign in to comment.