Skip to content

Commit ef1a3fc

Browse files
authored
Set debug level in system tests for agent (#1660)
Set debug log level in system tests for agent, once system tests are finished the log level is set with the previous value.
1 parent a75fd6f commit ef1a3fc

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

internal/kibana/agents.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ type Agent struct {
2929
Host struct {
3030
Name string `json:"name"`
3131
} `json:"host"`
32+
Elastic struct {
33+
Agent struct {
34+
LogLevel string `json:"log_level"`
35+
} `json:"agent"`
36+
} `json:"elastic"`
3237
} `json:"local_metadata"`
3338
}
3439

internal/kibana/fleet.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"net/http"
12+
"time"
1213
)
1314

1415
// DefaultFleetServerURL returns the default Fleet server configured in Kibana
@@ -43,3 +44,52 @@ func (c *Client) DefaultFleetServerURL() (string, error) {
4344

4445
return "", errors.New("could not find the fleet server URL for this project")
4546
}
47+
48+
func (c *Client) SetAgentLogLevel(agentID, level string) error {
49+
path := fmt.Sprintf("%s/agents/%s/actions", FleetAPI, agentID)
50+
51+
type fleetAction struct {
52+
Action struct {
53+
Type string `json:"type"`
54+
Data struct {
55+
LogLevel string `json:"log_level"`
56+
} `json:"data"`
57+
} `json:"action"`
58+
}
59+
60+
action := fleetAction{}
61+
action.Action.Type = "SETTINGS"
62+
action.Action.Data.LogLevel = level
63+
64+
reqBody, err := json.Marshal(action)
65+
if err != nil {
66+
return fmt.Errorf("could not convert action settingr (request) to JSON: %w", err)
67+
}
68+
69+
statusCode, respBody, err := c.post(path, reqBody)
70+
if err != nil {
71+
return fmt.Errorf("could not update agent settings: %w", err)
72+
}
73+
74+
if statusCode != http.StatusOK {
75+
return fmt.Errorf("could not set new log level; API status code = %d; response body = %s", statusCode, respBody)
76+
}
77+
78+
type actionResponse struct {
79+
ID string `json:"id"`
80+
CreatedAt time.Time `json:"created_at"`
81+
Type string `json:"type"`
82+
Data struct {
83+
LogLevel string `json:"log_level"`
84+
} `json:"data"`
85+
Agents []string `json:"agents"`
86+
}
87+
var resp struct {
88+
Item actionResponse `json:"item"`
89+
}
90+
91+
if err := json.Unmarshal(respBody, &resp); err != nil {
92+
return fmt.Errorf("could not convert actions agent (response) to JSON: %w", err)
93+
}
94+
return nil
95+
}

internal/testrunner/runners/system/runner.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ type runner struct {
102102
options testrunner.TestOptions
103103
pipelines []ingest.Pipeline
104104
// Execution order of following handlers is defined in runner.TearDown() method.
105-
deleteTestPolicyHandler func() error
106-
deletePackageHandler func() error
107-
resetAgentPolicyHandler func() error
108-
shutdownServiceHandler func() error
109-
wipeDataStreamHandler func() error
105+
deleteTestPolicyHandler func() error
106+
deletePackageHandler func() error
107+
resetAgentPolicyHandler func() error
108+
resetAgentLogLevelHandler func() error
109+
shutdownServiceHandler func() error
110+
wipeDataStreamHandler func() error
110111
}
111112

112113
// Type returns the type of test that can be run by this test runner.
@@ -153,6 +154,13 @@ func (r *runner) tearDownTest() error {
153154
r.resetAgentPolicyHandler = nil
154155
}
155156

157+
if r.resetAgentLogLevelHandler != nil {
158+
if err := r.resetAgentLogLevelHandler(); err != nil {
159+
return err
160+
}
161+
r.resetAgentLogLevelHandler = nil
162+
}
163+
156164
if r.deleteTestPolicyHandler != nil {
157165
if err := r.deleteTestPolicyHandler(); err != nil {
158166
return err
@@ -632,6 +640,21 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
632640
Revision: agent.PolicyRevision,
633641
}
634642

643+
logger.Debug("Set Debug log level to agent")
644+
origLogLevel := agent.LocalMetadata.Elastic.Agent.LogLevel
645+
err = r.options.KibanaClient.SetAgentLogLevel(agent.ID, "debug")
646+
if err != nil {
647+
return result.WithError(fmt.Errorf("error setting log level debug for agent %s: %w", agent.ID, err))
648+
}
649+
r.resetAgentLogLevelHandler = func() error {
650+
logger.Debugf("reassigning original log level %q back to agent...", origLogLevel)
651+
652+
if err := r.options.KibanaClient.SetAgentLogLevel(agent.ID, origLogLevel); err != nil {
653+
return fmt.Errorf("error reassigning original log level to agent: %w", err)
654+
}
655+
return nil
656+
}
657+
635658
// Assign policy to agent
636659
r.resetAgentPolicyHandler = func() error {
637660
logger.Debug("reassigning original policy back to agent...")

0 commit comments

Comments
 (0)