mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 08:51:02 -06:00
added combined tests
This commit is contained in:
parent
3f837f90e6
commit
2cdedaf734
@ -439,7 +439,7 @@ func (b *Backend) getOSSEndpointByRegion(access_key, secret_key, security_token,
|
||||
return nil, fmt.Errorf("unable to initialize the location client: %#v", err)
|
||||
|
||||
}
|
||||
locationClient.AppendUserAgent(httpclient.OpenTFUserAgent, TerraformVersion)
|
||||
locationClient.AppendUserAgent(fmt.Sprintf("%s", httpclient.Application()), TerraformVersion)
|
||||
endpointsResponse, err := locationClient.DescribeEndpoints(args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("describe oss endpoint using region: %#v got an error: %#v", region, err)
|
||||
|
@ -395,8 +395,7 @@ func (b *Backend) Configure(obj cty.Value) tfdiags.Diagnostics {
|
||||
Token: stringAttr(obj, "token"),
|
||||
UserAgentProducts: []*awsbase.UserAgentProduct{
|
||||
{Name: "APN", Version: "1.0"},
|
||||
{Name: httpclient.Organization, Version: "1.0"},
|
||||
{Name: httpclient.Application, Version: version.String()},
|
||||
{Name: httpclient.Application(), Version: version.String()},
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -12,10 +12,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
uaEnvVar = "TF_APPEND_USER_AGENT"
|
||||
Organization = "placeholderplaceholderplaceholder"
|
||||
Application = "OpenTF"
|
||||
OpenTFUserAgent = Organization + "-" + Application
|
||||
appendUaEnvVar = "TF_APPEND_USER_AGENT"
|
||||
customUaEnvVar = "OPENTF_USER_AGENT"
|
||||
defaultApplicationName = "OpenTF"
|
||||
)
|
||||
|
||||
type userAgentRoundTripper struct {
|
||||
@ -31,10 +30,20 @@ func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, e
|
||||
return rt.inner.RoundTrip(req)
|
||||
}
|
||||
|
||||
func TerraformUserAgent(version string) string {
|
||||
ua := fmt.Sprintf("%s/%s (+https://www.opentf.org)", OpenTFUserAgent, version)
|
||||
func Application() string {
|
||||
if customApplication := os.Getenv(customUaEnvVar); customApplication != "" {
|
||||
return customApplication
|
||||
}
|
||||
return defaultApplicationName
|
||||
}
|
||||
|
||||
if add := os.Getenv(uaEnvVar); add != "" {
|
||||
func TerraformUserAgent(version string) string {
|
||||
ua := fmt.Sprintf("%s/%s", defaultApplicationName, version)
|
||||
if customUa := os.Getenv(customUaEnvVar); customUa != "" {
|
||||
ua = customUa
|
||||
}
|
||||
|
||||
if add := os.Getenv(appendUaEnvVar); add != "" {
|
||||
add = strings.TrimSpace(add)
|
||||
if len(add) > 0 {
|
||||
ua += " " + add
|
||||
|
@ -12,11 +12,11 @@ import (
|
||||
)
|
||||
|
||||
func TestUserAgentString_env(t *testing.T) {
|
||||
expectedBase := fmt.Sprintf("%s/%s (+https://www.opentf.org)", OpenTFUserAgent, version.Version)
|
||||
if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
|
||||
defer os.Setenv(uaEnvVar, oldenv)
|
||||
expectedBase := fmt.Sprintf("%s/%s", defaultApplicationName, version.Version)
|
||||
if oldenv, isSet := os.LookupEnv(appendUaEnvVar); isSet {
|
||||
defer os.Setenv(appendUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(uaEnvVar)
|
||||
defer os.Unsetenv(appendUaEnvVar)
|
||||
}
|
||||
|
||||
for i, c := range []struct {
|
||||
@ -34,9 +34,9 @@ func TestUserAgentString_env(t *testing.T) {
|
||||
} {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
if c.additional == "" {
|
||||
os.Unsetenv(uaEnvVar)
|
||||
os.Unsetenv(appendUaEnvVar)
|
||||
} else {
|
||||
os.Setenv(uaEnvVar, c.additional)
|
||||
os.Setenv(appendUaEnvVar, c.additional)
|
||||
}
|
||||
|
||||
actual := TerraformUserAgent(version.Version)
|
||||
@ -49,10 +49,10 @@ func TestUserAgentString_env(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserAgentAppendViaEnvVar(t *testing.T) {
|
||||
if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
|
||||
defer os.Setenv(uaEnvVar, oldenv)
|
||||
if oldenv, isSet := os.LookupEnv(appendUaEnvVar); isSet {
|
||||
defer os.Setenv(appendUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(uaEnvVar)
|
||||
defer os.Unsetenv(appendUaEnvVar)
|
||||
}
|
||||
|
||||
expectedBase := "placeholderplaceholderplaceholder-OpenTF/0.0.0 (+https://www.opentf.org)"
|
||||
@ -70,8 +70,72 @@ func TestUserAgentAppendViaEnvVar(t *testing.T) {
|
||||
|
||||
for i, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
os.Unsetenv(uaEnvVar)
|
||||
os.Setenv(uaEnvVar, tc.envVarValue)
|
||||
os.Unsetenv(appendUaEnvVar)
|
||||
os.Setenv(appendUaEnvVar, tc.envVarValue)
|
||||
givenUA := TerraformUserAgent("0.0.0")
|
||||
if givenUA != tc.expected {
|
||||
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestCustomUserAgentViaEnvVar(t *testing.T) {
|
||||
if oldenv, isSet := os.LookupEnv(customUaEnvVar); isSet {
|
||||
defer os.Setenv(customUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(customUaEnvVar)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
envVarValue string
|
||||
}{
|
||||
{" "},
|
||||
{" \n"},
|
||||
{"test/1"},
|
||||
{"test/1 (comment)"},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
os.Unsetenv(customUaEnvVar)
|
||||
os.Setenv(customUaEnvVar, tc.envVarValue)
|
||||
givenUA := TerraformUserAgent("0.0.0")
|
||||
if givenUA != tc.envVarValue {
|
||||
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.envVarValue, givenUA)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestCustomUserAgentAndAppendViaEnvVar(t *testing.T) {
|
||||
if oldenv, isSet := os.LookupEnv(appendUaEnvVar); isSet {
|
||||
defer os.Setenv(appendUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(appendUaEnvVar)
|
||||
}
|
||||
if oldenv, isSet := os.LookupEnv(customUaEnvVar); isSet {
|
||||
defer os.Setenv(customUaEnvVar, oldenv)
|
||||
} else {
|
||||
defer os.Unsetenv(customUaEnvVar)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
customUaValue string
|
||||
appendUaValue string
|
||||
expected string
|
||||
}{
|
||||
{"", "", "OpenTF/0.0.0"},
|
||||
{"", " ", "OpenTF/0.0.0"},
|
||||
{"", " \n", "OpenTF/0.0.0"},
|
||||
{"", "testy test", "OpenTF/0.0.0 testy test"},
|
||||
{"opensource", "opentf", "opensource opentf"},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
os.Unsetenv(customUaEnvVar)
|
||||
os.Unsetenv(appendUaEnvVar)
|
||||
os.Setenv(customUaEnvVar, tc.customUaValue)
|
||||
os.Setenv(appendUaEnvVar, tc.appendUaValue)
|
||||
givenUA := TerraformUserAgent("0.0.0")
|
||||
if givenUA != tc.expected {
|
||||
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
|
||||
|
Loading…
Reference in New Issue
Block a user