-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add runtime version to user agent if present #1542
Changes from 2 commits
b61b903
7b516ec
fa17d88
6f6605f
88be7aa
cdc1c4d
c7706e9
e01e62c
c01511e
4a0a707
c3da678
72b62eb
16e6fc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,16 @@ var ( | |
|
||
func init() { | ||
versionString = strings.TrimSpace(versionString) | ||
userAgent = "cloud-sql-proxy/" + versionString | ||
userAgent = getUserAgentString() | ||
} | ||
|
||
func getUserAgentString() string { | ||
userAgentString := "cloud-sql-proxy/" + versionString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are inside a function about user agent, this could juse be |
||
operatorVersion, isSet := os.LookupEnv("CLOUD_SQL_PROXY_OPERATOR_VERSION") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here -- a short variable is ok since it's clear what it is. Also, |
||
if isSet { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if !isSet return? |
||
userAgentString = userAgentString + " cloud-sql-proxy-operator/" + operatorVersion | ||
} | ||
return userAgentString | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
@@ -81,6 +82,18 @@ func invokeProxyCommand(args []string) (*Command, error) { | |
return c, err | ||
} | ||
|
||
func Test_UserAgentWithOperatorVersion(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've been omitting underscores in test names. Let's remove the underscore here. |
||
os.Setenv("CLOUD_SQL_PROXY_OPERATOR_VERSION", "0.0.1") | ||
defer os.Unsetenv("CLOUD_SQL_PROXY_OPERATOR_VERSION") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be run through go-fmt. |
||
|
||
expected := "cloud-sql-proxy-operator/0.0.1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
userAgentString := getUserAgentString() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if !strings.Contains(userAgentString, expected) { | ||
t.Errorf("expected userAgent to contain: %v; got: %v", expected, userAgentString) | ||
} | ||
|
||
} | ||
|
||
func TestNewCommandArguments(t *testing.T) { | ||
tcs := []struct { | ||
desc string | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go prefers to leave off
get
for Getters. https://go.dev/doc/effective_go#Getters