Skip to content
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

Agent error log level is mismatched #14424

Merged
merged 6 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
3 changes: 3 additions & 0 deletions changelog/14424.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
agent: Fix log level mismatch between ERR and ERROR
```
2 changes: 1 addition & 1 deletion command/agent/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func logLevelToStringPtr(level hclog.Level) *string {
case hclog.Warn:
levelStr = "WARN"
case hclog.Error:
levelStr = "ERROR"
levelStr = "ERR"
default:
levelStr = "INFO"
}
Expand Down
73 changes: 71 additions & 2 deletions command/agent/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestCacheConfigNoListener(t *testing.T) {
assert.NotNil(t, ctConfig.Vault.Transport.CustomDialer)
}

func TestServerRun(t *testing.T) {
func createHttpTestServer() *httptest.Server {
// create http test server
mux := http.NewServeMux()
mux.HandleFunc("/v1/kv/myapp/config", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -203,8 +203,13 @@ func TestServerRun(t *testing.T) {
fmt.Fprintln(w, `{"errors":["1 error occurred:\n\t* permission denied\n\n"]}`)
})

ts := httptest.NewServer(mux)
return httptest.NewServer(mux)
}

func TestServerRun(t *testing.T) {
ts := createHttpTestServer()
defer ts.Close()

tmpDir, err := ioutil.TempDir("", "agent-tests")
defer os.RemoveAll(tmpDir)
if err != nil {
Expand Down Expand Up @@ -400,6 +405,70 @@ func TestServerRun(t *testing.T) {
}
}

// TestNewServerLogLevels tests that the server can be started with any log
// level.
func TestNewServerLogLevels(t *testing.T) {
ts := createHttpTestServer()
defer ts.Close()

tmpDir, err := ioutil.TempDir("", "agent-tests")
Copy link
Member

Choose a reason for hiding this comment

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

Since we're using Go 1.17+, it might be a good opportunity to swap this call to os. MkdirTemp in here and elsewhere in this file since that's what it really calls under the hood.

The doc mentions that it is preferred to call funcs from os over ioutil:

As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Done. I also replaced ReadFile so we can remove the ioutil import.

defer os.RemoveAll(tmpDir)
if err != nil {
t.Fatal(err)
}

levels := []hclog.Level{hclog.NoLevel, hclog.Trace, hclog.Debug, hclog.Info, hclog.Warn, hclog.Error}
for _, level := range levels {
name := fmt.Sprintf("log_%s", level)
t.Run(name, func(t *testing.T) {
server := NewServer(&ServerConfig{
Logger: logging.NewVaultLogger(level),
LogWriter: hclog.DefaultOutput,
LogLevel: level,
ExitAfterAuth: true,
AgentConfig: &config.Config{
Vault: &config.Vault{
Address: ts.URL,
},
},
})
if server == nil {
t.Fatal("nil server returned")
}
defer server.Stop()

templateTokenCh := make(chan string, 1)

templateTest := &ctconfig.TemplateConfig{
Contents: pointerutil.StringPtr(templateContents),
}
dstFile := fmt.Sprintf("%s/%s", tmpDir, name)
templateTest.Destination = pointerutil.StringPtr(dstFile)
templatesToRender := []*ctconfig.TemplateConfig{templateTest}

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()

errCh := make(chan error)
go func() {
errCh <- server.Run(ctx, templateTokenCh, templatesToRender)
}()

// send a dummy value to trigger auth so the server will exit
templateTokenCh <- "test"

select {
case <-ctx.Done():
t.Fatal("timeout reached before templates were rendered")
case err := <-errCh:
if err != nil {
t.Fatalf("did not expect error, got: %v", err)
}
}
})
}
}

var jsonResponse = `
{
"request_id": "8af096e9-518c-7351-eff5-5ba20554b21f",
Expand Down