Agent: helper func tweak (#26394)

* tweaked makeTempFile

* removed defer removes for makeTempFile .. files
This commit is contained in:
Peter Wilson
2024-04-12 19:36:43 +01:00
committed by GitHub
parent 2f1334513c
commit 1dc07efb41
2 changed files with 20 additions and 61 deletions

View File

@@ -359,7 +359,6 @@ listener "tcp" {
listenAddr3,
)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)
@@ -461,7 +460,6 @@ listener "tcp" {
`, generateListenerAddress(t))
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)
@@ -490,7 +488,6 @@ func TestAgent_NoAutoAuthTokenIfNotConfigured(t *testing.T) {
// Create token file
tokenFileName := makeTempFile(t, "token-file", serverClient.Token())
defer os.Remove(tokenFileName)
sinkFileName := makeTempFile(t, "sink-file", "")
@@ -533,7 +530,6 @@ vault {
%s
`, serverClient.Address(), apiProxyConfig, listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start proxy
ui, cmd := testAgentCommand(t, logger)
@@ -682,7 +678,6 @@ auto_auth {
config = fmt.Sprintf(config, serverClient.Address(), roleIDPath, secretIDPath, templateConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)
@@ -887,7 +882,6 @@ auto_auth {
config = fmt.Sprintf(config, serverClient.Address(), roleIDPath, secretIDPath, templateConfig, exitAfterAuth)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)
@@ -1013,10 +1007,6 @@ func setupAppRole(t *testing.T, serverClient *api.Client) (string, string) {
// Write the RoleID and SecretID to temp files
roleIDPath := makeTempFile(t, "role_id.txt", roleID+"\n")
secretIDPath := makeTempFile(t, "secret_id.txt", secretID+"\n")
t.Cleanup(func() {
os.Remove(roleIDPath)
os.Remove(secretIDPath)
})
return roleIDPath, secretIDPath
}
@@ -1165,7 +1155,6 @@ auto_auth {
config = fmt.Sprintf(config, roleIDPath, secretIDPath, templateConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)
@@ -1342,7 +1331,6 @@ exit_after_auth = true
config = fmt.Sprintf(config, serverClient.Address(), roleIDPath, secretIDPath, tmpDir, tmpDir, tmpDir)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)
@@ -1452,16 +1440,28 @@ func request(t *testing.T, client *api.Client, req *api.Request, expectedStatusC
return body
}
// makeTempFile creates a temp file and populates it.
// makeTempFile creates a temp file with the specified name, populates it with the
// supplied contents and closes it. The path to the file is returned, also the file
// will be automatically removed when the test which created it, finishes.
func makeTempFile(t *testing.T, name, contents string) string {
t.Helper()
f, err := os.CreateTemp("", name)
f, err := os.Create(filepath.Join(t.TempDir(), name))
if err != nil {
t.Fatal(err)
}
path := f.Name()
f.WriteString(contents)
f.Close()
_, err = f.WriteString(contents)
if err != nil {
t.Fatal(err)
}
err = f.Close()
if err != nil {
t.Fatal(err)
}
return path
}
@@ -1565,8 +1565,7 @@ func TestAgent_Template_Retry(t *testing.T) {
defer os.Setenv(api.EnvVaultAddress, os.Getenv(api.EnvVaultAddress))
os.Unsetenv(api.EnvVaultAddress)
methodConf, cleanup := prepAgentApproleKV(t, serverClient)
defer cleanup()
methodConf := prepAgentApproleKV(t, serverClient)
err := serverClient.Sys().TuneMount("secret", api.MountConfigInput{
Options: map[string]string{
@@ -1669,7 +1668,6 @@ template_config {
`, methodConf, serverClient.Address(), retryConf, templateConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
_, cmd := testAgentCommand(t, logger)
@@ -1749,7 +1747,7 @@ template_config {
// such that the resulting token will have global permissions across /kv
// and /secret mounts. Returns the auto_auth config stanza to setup an Agent
// to connect using approle.
func prepAgentApproleKV(t *testing.T, client *api.Client) (string, func()) {
func prepAgentApproleKV(t *testing.T, client *api.Client) string {
t.Helper()
policyAutoAuthAppRole := `
@@ -1810,11 +1808,7 @@ auto_auth {
}
`, roleIDFile, secretIDFile)
cleanup := func() {
_ = os.Remove(roleIDFile)
_ = os.Remove(secretIDFile)
}
return config, cleanup
return config
}
// TestAgent_AutoAuth_UserAgent tests that the User-Agent sent
@@ -1887,7 +1881,6 @@ api_proxy {
%s
`, serverClient.Address(), listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Unset the environment variable so that agent picks up the right test
// cluster address
@@ -1983,7 +1976,6 @@ vault {
%s
`, serverClient.Address(), listenConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
_, cmd := testAgentCommand(t, logger)
@@ -2075,7 +2067,6 @@ vault {
%s
`, serverClient.Address(), listenConfig, cacheConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
_, cmd := testAgentCommand(t, logger)
@@ -2151,7 +2142,6 @@ vault {
%s
`, serverClient.Address(), cacheConfig, listenConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
_, cmd := testAgentCommand(t, logger)
@@ -2322,7 +2312,6 @@ vault {
%s
`, serverClient.Address(), retryConf, cacheConfig, listenConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
_, cmd := testAgentCommand(t, logger)
@@ -2401,8 +2390,7 @@ func TestAgent_TemplateConfig_ExitOnRetryFailure(t *testing.T) {
defer os.Setenv(api.EnvVaultAddress, os.Getenv(api.EnvVaultAddress))
os.Unsetenv(api.EnvVaultAddress)
autoAuthConfig, cleanup := prepAgentApproleKV(t, serverClient)
defer cleanup()
autoAuthConfig := prepAgentApproleKV(t, serverClient)
err := serverClient.Sys().TuneMount("secret", api.MountConfigInput{
Options: map[string]string{
@@ -2586,7 +2574,6 @@ vault {
`, autoAuthConfig, serverClient.Address(), listenConfig, templateConfig, template)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)
@@ -2701,7 +2688,6 @@ listener "tcp" {
}
`, listenAddr)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logging.NewVaultLogger(hclog.Trace))
@@ -2797,7 +2783,6 @@ cache {}
`, serverClient.Address(), listenAddr, listenAddr2)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
_, cmd := testAgentCommand(t, nil)
@@ -3153,7 +3138,6 @@ vault {
%s
`, serverClient.Address(), listenConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)
@@ -3346,7 +3330,6 @@ func TestAgent_DeleteAfterVersion_Rendering(t *testing.T) {
require.NoError(t, err)
tokenFileName := makeTempFile(t, "token-file", serverClient.Token())
defer os.Remove(tokenFileName)
autoAuthConfig := fmt.Sprintf(`
auto_auth {
@@ -3380,7 +3363,6 @@ template {
config = fmt.Sprintf(config, serverClient.Address(), autoAuthConfig, templateConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the agent
ui, cmd := testAgentCommand(t, logger)

View File

@@ -259,7 +259,6 @@ capabilities = ["deny"]
// Create token file
tokenFileName := makeTempFile(t, "token-file", firstToken)
defer os.Remove(tokenFileName)
sinkFileName := makeTempFile(t, "sink-file", "")
@@ -298,7 +297,6 @@ auto_auth {
%s
`, serverClient.Address(), listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Unset the environment variable so that proxy picks up the right test
// cluster address
@@ -402,7 +400,6 @@ func TestProxy_NoTriggerAutoAuth_ProxyTokenNotAutoAuth(t *testing.T) {
// Create token file
tokenFileName := makeTempFile(t, "token-file", firstToken)
defer os.Remove(tokenFileName)
sinkFileName := makeTempFile(t, "sink-file", "")
@@ -442,7 +439,6 @@ auto_auth {
%s
`, serverClient.Address(), listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Unset the environment variable so that proxy picks up the right test
// cluster address
@@ -556,7 +552,6 @@ func TestProxy_ReTriggerAutoAuth_ForceAutoAuthToken(t *testing.T) {
// Create token file
tokenFileName := makeTempFile(t, "token-file", firstToken)
defer os.Remove(tokenFileName)
sinkFileName := makeTempFile(t, "sink-file", "")
@@ -596,7 +591,6 @@ auto_auth {
%s
`, serverClient.Address(), listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Unset the environment variable so that proxy picks up the right test
// cluster address
@@ -744,8 +738,6 @@ func TestProxy_ReTriggerAutoAuth_ProxyIsAutoAuthToken(t *testing.T) {
// Write the RoleID and SecretID to temp files
roleIDPath := makeTempFile(t, "role_id.txt", roleID+"\n")
secretIDPath := makeTempFile(t, "secret_id.txt", secretID+"\n")
defer os.Remove(roleIDPath)
defer os.Remove(secretIDPath)
sinkFileName := makeTempFile(t, "sink-file", "")
@@ -786,7 +778,6 @@ api_proxy {
%s
`, serverClient.Address(), listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Unset the environment variable so that proxy picks up the right test
// cluster address
@@ -937,8 +928,6 @@ func TestProxy_ReTriggerAutoAuth_RevokedToken(t *testing.T) {
// Write the RoleID and SecretID to temp files
roleIDPath := makeTempFile(t, "role_id.txt", roleID+"\n")
secretIDPath := makeTempFile(t, "secret_id.txt", secretID+"\n")
defer os.Remove(roleIDPath)
defer os.Remove(secretIDPath)
sinkFileName := makeTempFile(t, "sink-file", "")
autoAuthConfig := fmt.Sprintf(`
@@ -978,7 +967,6 @@ api_proxy {
%s
`, serverClient.Address(), listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Unset the environment variable so that proxy picks up the right test
// cluster address
@@ -1134,8 +1122,6 @@ func TestProxy_AutoAuth_UserAgent(t *testing.T) {
// Write the RoleID and SecretID to temp files
roleIDPath := makeTempFile(t, "role_id.txt", roleID+"\n")
secretIDPath := makeTempFile(t, "secret_id.txt", secretID+"\n")
defer os.Remove(roleIDPath)
defer os.Remove(secretIDPath)
sinkFileName := makeTempFile(t, "sink-file", "")
autoAuthConfig := fmt.Sprintf(`
@@ -1175,7 +1161,6 @@ api_proxy {
%s
`, serverClient.Address(), listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Unset the environment variable so that proxy picks up the right test
// cluster address
@@ -1271,7 +1256,6 @@ vault {
%s
`, serverClient.Address(), listenConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the proxy
_, cmd := testProxyCommand(t, logger)
@@ -1363,7 +1347,6 @@ vault {
%s
`, serverClient.Address(), listenConfig, cacheConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start the proxy
_, cmd := testProxyCommand(t, logger)
@@ -1441,7 +1424,6 @@ vault {
%s
`, serverClient.Address(), cacheConfig, listenConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start proxy
_, cmd := testProxyCommand(t, logger)
@@ -1529,7 +1511,6 @@ func TestProxy_NoAutoAuthTokenIfNotConfigured(t *testing.T) {
// Create token file
tokenFileName := makeTempFile(t, "token-file", serverClient.Token())
defer os.Remove(tokenFileName)
sinkFileName := makeTempFile(t, "sink-file", "")
@@ -1572,7 +1553,6 @@ vault {
%s
`, serverClient.Address(), apiProxyConfig, listenConfig, autoAuthConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
// Start proxy
ui, cmd := testProxyCommand(t, logger)
@@ -1726,7 +1706,6 @@ vault {
%s
`, serverClient.Address(), retryConf, cacheConfig, listenConfig)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
_, cmd := testProxyCommand(t, logger)
cmd.startedCh = make(chan struct{})
@@ -1799,7 +1778,6 @@ listener "tcp" {
}
`, listenAddr)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
ui, cmd := testProxyCommand(t, logger)
cmd.client = serverClient
@@ -1889,7 +1867,6 @@ cache {}
`, serverClient.Address(), listenAddr, listenAddr2)
configPath := makeTempFile(t, "config.hcl", config)
defer os.Remove(configPath)
_, cmd := testProxyCommand(t, nil)
cmd.startedCh = make(chan struct{})