Skip to content

Add metadata retrieved from the context to the user agent when a new HTTP client is created #2789

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

Merged
merged 6 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated integration test
  • Loading branch information
cmaglie committed Jan 22, 2025
commit fc6d0a6cdb3b6d533d3986462bc990350cb8d651
17 changes: 14 additions & 3 deletions internal/integrationtest/arduino-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,21 @@ func NewArduinoCliWithinEnvironment(env *Environment, config *ArduinoCLIConfig)
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests.
// The Environment must be disposed by calling the CleanUp method via defer.
func CreateEnvForDaemon(t *testing.T) (*Environment, *ArduinoCLI) {
return CreateEnvForDaemonWithUserAgent(t, "cli-test/0.0.0")
}

// CreateEnvForDaemonWithUserAgent performs the minimum required operations to start the arduino-cli daemon.
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests.
// The Environment must be disposed by calling the CleanUp method via defer.
func CreateEnvForDaemonWithUserAgent(t *testing.T, userAgent string) (*Environment, *ArduinoCLI) {
env := NewEnvironment(t)

cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"),
UseSharedStagingFolder: true,
})

_ = cli.StartDaemon(false)
_ = cli.StartDaemon(false, userAgent)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return CreateEnvForDaemonWithUserAgent(t, "cli-test/0.0.0")
}
// CreateEnvForDaemonWithUserAgent performs the minimum required operations to start the arduino-cli daemon.
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests.
// The Environment must be disposed by calling the CleanUp method via defer.
func CreateEnvForDaemonWithUserAgent(t *testing.T, userAgent string) (*Environment, *ArduinoCLI) {
env := NewEnvironment(t)
cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"),
UseSharedStagingFolder: true,
})
_ = cli.StartDaemon(false)
_ = cli.StartDaemon(false, userAgent)
env := NewEnvironment(t)
cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"),
UseSharedStagingFolder: true,
})
_ = cli.StartDaemon(false)

CreateEnvForDaemonWithUserAgent this function is used only by the CreateEnvForDaemon

I would simplify and put everything in the same function as it was before.
The useragent will be hardcoded inside the StartDaemon

return env, cli
}

Expand Down Expand Up @@ -410,7 +417,7 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
}

// StartDaemon starts the Arduino CLI daemon. It returns the address of the daemon.
func (cli *ArduinoCLI) StartDaemon(verbose bool) string {
func (cli *ArduinoCLI) StartDaemon(verbose bool, userAgent string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (cli *ArduinoCLI) StartDaemon(verbose bool, userAgent string) string {
func (cli *ArduinoCLI) StartDaemon(verbose bool) string {

I cannot think of uses cases that we need to pass different userAgent. The StartDaemon currently is only called in the CreateEnvForDaemon. It's responsibility is to create the grpclient and initializing grpc options.

My suggestion is to just hardcode the userAgent in the grpc option. See later suggestion

args := []string{"daemon", "--json"}
if cli.cliConfigPath != nil {
args = append([]string{"--config-file", cli.cliConfigPath.String()}, args...)
Expand Down Expand Up @@ -450,7 +457,11 @@ func (cli *ArduinoCLI) StartDaemon(verbose bool) string {
for retries := 5; retries > 0; retries-- {
time.Sleep(time.Second)

conn, err := grpc.NewClient(cli.daemonAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(
cli.daemonAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUserAgent(userAgent),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
grpc.WithUserAgent(userAgent),
grpc.WithUserAgent("cli-test/0.0.0"),

I'd simply hardcode the user-agent here.

)
if err != nil {
connErr = err
continue
Expand Down
5 changes: 4 additions & 1 deletion internal/integrationtest/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func (env *Environment) HTTPServeFile(port uint16, path *paths.Path, isDaemon bo
http.ServeFile(w, r, path.String())
if isDaemon {
// Test that the user-agent contains metadata from the context when the CLI is in daemon mode
require.Contains(t, r.Header.Get("User-Agent"), "arduino-cli/git-snapshot grpc-go")
userAgent := r.Header.Get("User-Agent")
require.Contains(t, userAgent, "arduino-cli/git-snapshot")
require.Contains(t, userAgent, "cli-test/0.0.0")
require.Contains(t, userAgent, "grpc-go")
}
})
server := &http.Server{
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy