Skip to content

Commit 29c58bd

Browse files
authored
Allow to dump documents resulting of system tests (#2059)
Setting `ELASTIC_PACKAGE_TEST_DUMP_SCENARIO_DOCS` dumps documents generated by system tests into a file in the temporary directory.
1 parent a211752 commit 29c58bd

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,9 @@ There are available some environment variables that could be used to change some
695695
- `ELASTIC_PACKAGE_DISABLE_ELASTIC_AGENT_WOLFI`: If set to `true`, the Elastic Agent image used for running agents will be using the Ubuntu docker images
696696
(e.g. `docker.elastic.co/elastic-agent/elastic-agent-complete`). If set to `false`, the Elastic Agent image used for the running agents will be based on the wolfi
697697
images (e.g. `docker.elastic.co/elastic-agent/elastic-agent-wolfi`). Default: `false`.
698+
- `ELASTIC_PACKAGE_TEST_DUMP_SCENARIO_DOCS. If the variable is set, elastic-package will dump to a file the documents generated
699+
by system tests before they are verified. This is useful to know exactly what fields are being verified when investigating
700+
issues on this step. Documents are dumped to a file in the system temporary directory. It is disabled by default.
698701

699702
- To configure the Elastic stack to be used by `elastic-package`:
700703
- `ELASTIC_PACKAGE_ELASTICSEARCH_HOST`: Host of the elasticsearch (e.g. https://127.0.0.1:9200)

internal/testrunner/runners/system/tester.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ var (
123123
},
124124
},
125125
}
126-
enableIndependentAgents = environment.WithElasticPackagePrefix("TEST_ENABLE_INDEPENDENT_AGENT")
126+
enableIndependentAgentsEnv = environment.WithElasticPackagePrefix("TEST_ENABLE_INDEPENDENT_AGENT")
127+
dumpScenarioDocsEnv = environment.WithElasticPackagePrefix("TEST_DUMP_SCENARIO_DOCS")
127128
)
128129

129130
type tester struct {
@@ -257,7 +258,7 @@ func NewSystemTester(options SystemTesterOptions) (*tester, error) {
257258

258259
// If the environment variable is present, it always has preference over the root
259260
// privileges value (if any) defined in the manifest file
260-
v, ok := os.LookupEnv(enableIndependentAgents)
261+
v, ok := os.LookupEnv(enableIndependentAgentsEnv)
261262
if ok {
262263
r.runIndependentElasticAgent = strings.ToLower(v) == "true"
263264
}
@@ -1528,9 +1529,36 @@ func (r *tester) runTest(ctx context.Context, config *testConfig, svcInfo servic
15281529
return result.WithError(err)
15291530
}
15301531

1532+
if dump, ok := os.LookupEnv(dumpScenarioDocsEnv); ok && dump != "" {
1533+
err := dumpScenarioDocs(scenario.docs)
1534+
if err != nil {
1535+
return nil, fmt.Errorf("failed to dump scenario docs: %w", err)
1536+
}
1537+
}
1538+
15311539
return r.validateTestScenario(ctx, result, scenario, config)
15321540
}
15331541

1542+
func dumpScenarioDocs(docs any) error {
1543+
timestamp := time.Now().Format("20060102150405")
1544+
path := filepath.Join(os.TempDir(), fmt.Sprintf("elastic-package-test-docs-dump-%s.json", timestamp))
1545+
f, err := os.Create(path)
1546+
if err != nil {
1547+
return fmt.Errorf("failed to create dump file: %w", err)
1548+
}
1549+
defer f.Close()
1550+
1551+
logger.Infof("Dumping scenario documents to %s", path)
1552+
1553+
enc := json.NewEncoder(f)
1554+
enc.SetIndent("", " ")
1555+
enc.SetEscapeHTML(false)
1556+
if err := enc.Encode(docs); err != nil {
1557+
return fmt.Errorf("failed to encode docs: %w", err)
1558+
}
1559+
return nil
1560+
}
1561+
15341562
func checkEnrolledAgents(ctx context.Context, client *kibana.Client, agentInfo agentdeployer.AgentInfo, svcInfo servicedeployer.ServiceInfo, runIndependentElasticAgent bool) ([]kibana.Agent, error) {
15351563
var agents []kibana.Agent
15361564

tools/readme/readme.md.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ There are available some environment variables that could be used to change some
237237
- `ELASTIC_PACKAGE_DISABLE_ELASTIC_AGENT_WOLFI`: If set to `true`, the Elastic Agent image used for running agents will be using the Ubuntu docker images
238238
(e.g. `docker.elastic.co/elastic-agent/elastic-agent-complete`). If set to `false`, the Elastic Agent image used for the running agents will be based on the wolfi
239239
images (e.g. `docker.elastic.co/elastic-agent/elastic-agent-wolfi`). Default: `false`.
240+
- `ELASTIC_PACKAGE_TEST_DUMP_SCENARIO_DOCS. If the variable is set, elastic-package will dump to a file the documents generated
241+
by system tests before they are verified. This is useful to know exactly what fields are being verified when investigating
242+
issues on this step. Documents are dumped to a file in the system temporary directory. It is disabled by default.
240243

241244
- To configure the Elastic stack to be used by `elastic-package`:
242245
- `ELASTIC_PACKAGE_ELASTICSEARCH_HOST`: Host of the elasticsearch (e.g. https://127.0.0.1:9200)

0 commit comments

Comments
 (0)