Skip to content

Commit a4eca5e

Browse files
authored
Record with go-vcr instead of doing our custom recording (#1566)
We record requests in some tests so we can reproduce them without having running services. We use our own framework now, that has some limitations. It can only record GET requests, and it can only record one response for any request, so it cannot be used for tests where the state changes. Give a try to https://pkg.go.dev/gopkg.in/dnaeon/go-vcr.v3 for this purpose. With go-vcr, each session is stored in a file, called "cassette", that stores the sequence of interactions. By default it matches requests per method and per URL, and each interaction is only matched once, so multiple requests to the same endpoint can be recorded with different responses. This seems to be enough for all the cases we have now.
1 parent 08ea257 commit a4eca5e

File tree

78 files changed

+3088
-314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+3088
-314
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/spf13/cobra v1.8.0
3535
github.com/stretchr/testify v1.8.4
3636
golang.org/x/tools v0.15.0
37+
gopkg.in/dnaeon/go-vcr.v3 v3.1.2
3738
gopkg.in/yaml.v3 v3.0.1
3839
gotest.tools/gotestsum v1.11.0
3940
helm.sh/helm/v3 v3.13.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
906906
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
907907
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
908908
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
909+
gopkg.in/dnaeon/go-vcr.v3 v3.1.2 h1:F1smfXBqQqwpVifDfUBQG6zzaGjzT+EnVZakrOdr5wA=
910+
gopkg.in/dnaeon/go-vcr.v3 v3.1.2/go.mod h1:2IMOnnlx9I6u9x+YBsM3tAMx6AlOxnJ0pWxQAzZ79Ag=
909911
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
910912
gopkg.in/hjson/hjson-go.v3 v3.0.1/go.mod h1:X6zrTSVeImfwfZLfgQdInl9mWjqPqgH90jom9nym/lw=
911913
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=

internal/dump/agentpolicies_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,30 @@ func TestDumpAgentPolicies(t *testing.T) {
2424
// - Configure environment variables for this stack (eval "$(elastic-package stack shellinit)").
2525
// - Run tests.
2626
// - Check that recorded files make sense and commit them.
27+
// To update the suite:
28+
// - Reproduce the scenario as described in the comments.
29+
// - Remove the files that you want to update.
30+
// - Follow the same steps to create a new suite.
31+
// - Check if the changes are the expected ones and commit them.
2732
suites := []*agentPoliciesDumpSuite{
2833
&agentPoliciesDumpSuite{
34+
// To reproduce this scenario:
35+
// - Start stack with version 7.17.0.
36+
// - Install nginx package.
2937
AgentPolicy: "499b5aa7-d214-5b5d-838b-3cd76469844e",
3038
PackageName: "nginx",
31-
RecordDir: "./testdata/fleet-7-mock-dump-all",
39+
Record: "./testdata/fleet-7-mock-dump-all",
3240
DumpDirAll: "./testdata/fleet-7-dump/all",
3341
DumpDirPackage: "./testdata/fleet-7-dump/package",
3442
DumpDirAgentPolicy: "./testdata/fleet-7-dump/agentpolicy",
3543
},
3644
&agentPoliciesDumpSuite{
45+
// To reproduce this scenario:
46+
// - Start stack with version 8.0.0.
47+
// - Install nginx package.
3748
AgentPolicy: "fleet-server-policy",
3849
PackageName: "nginx",
39-
RecordDir: "./testdata/fleet-8-mock-dump-all",
50+
Record: "./testdata/fleet-8-mock-dump-all",
4051
DumpDirAll: "./testdata/fleet-8-dump/all",
4152
DumpDirPackage: "./testdata/fleet-8-dump/package",
4253
DumpDirAgentPolicy: "./testdata/fleet-8-dump/agentpolicy",
@@ -57,8 +68,8 @@ type agentPoliciesDumpSuite struct {
5768
// AgentPolicy is the name of the agent policy to look for.
5869
PackageName string
5970

60-
// RecordDir is where responses from Kibana are recorded.
61-
RecordDir string
71+
// Record is where responses from Kibana are recorded.
72+
Record string
6273

6374
// DumpDirAll is where the expected dumped files are stored when looking for all agent policies.
6475
DumpDirAll string
@@ -111,7 +122,7 @@ func (s *agentPoliciesDumpSuite) SetupTest() {
111122
}
112123

113124
func (s *agentPoliciesDumpSuite) TestDumpAll() {
114-
client := kibanatest.NewClient(s.T(), s.RecordDir)
125+
client := kibanatest.NewClient(s.T(), s.Record)
115126

116127
outputDir := s.T().TempDir()
117128
dumper := NewAgentPoliciesDumper(client)
@@ -128,7 +139,7 @@ func (s *agentPoliciesDumpSuite) TestDumpAll() {
128139
}
129140

130141
func (s *agentPoliciesDumpSuite) TestDumpByPackage() {
131-
client := kibanatest.NewClient(s.T(), s.RecordDir)
142+
client := kibanatest.NewClient(s.T(), s.Record)
132143

133144
outputDir := s.T().TempDir()
134145
dumper := NewAgentPoliciesDumper(client)
@@ -145,7 +156,7 @@ func (s *agentPoliciesDumpSuite) TestDumpByPackage() {
145156
}
146157

147158
func (s *agentPoliciesDumpSuite) TestDumpByName() {
148-
client := kibanatest.NewClient(s.T(), s.RecordDir)
159+
client := kibanatest.NewClient(s.T(), s.Record)
149160

150161
outputDir := s.T().TempDir()
151162
dumper := NewAgentPoliciesDumper(client)

internal/dump/indextemplates.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12+
"net/http"
1213
"slices"
1314

1415
"github.com/elastic/elastic-package/internal/elasticsearch"
@@ -77,6 +78,10 @@ func getIndexTemplatesForPackage(ctx context.Context, api *elasticsearch.API, pa
7778
}
7879
defer resp.Body.Close()
7980

81+
if resp.StatusCode == http.StatusNotFound {
82+
// Some packages don't have index templates.
83+
return nil, nil
84+
}
8085
if resp.IsError() {
8186
return nil, fmt.Errorf("failed to get index templates: %s", resp.String())
8287
}

internal/dump/ingestpipelines.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12+
"net/http"
1213
"slices"
1314

1415
"github.com/elastic/elastic-package/internal/elasticsearch"
@@ -71,6 +72,10 @@ func getIngestPipelineByID(ctx context.Context, api *elasticsearch.API, id strin
7172
}
7273
defer resp.Body.Close()
7374

75+
// Ingest templates referenced by other templates may not exist.
76+
if resp.StatusCode == http.StatusNotFound {
77+
return nil, nil
78+
}
7479
if resp.IsError() {
7580
return nil, fmt.Errorf("failed to get ingest pipeline %s: %s", id, resp.String())
7681
}

internal/dump/installedobjects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (e *InstalledObjectsDumper) getIngestPipelines(ctx context.Context) ([]Inge
223223
names := getIngestPipelinesFromTemplates(templates)
224224
ingestPipelines, err := getIngestPipelines(ctx, e.client, names...)
225225
if err != nil {
226-
return nil, fmt.Errorf("failed to get ingest pipelines: %w", err)
226+
return nil, fmt.Errorf("failed to get ingest pipelines from templates: %w", err)
227227
}
228228
e.ingestPipelines = ingestPipelines
229229
}

internal/dump/installedobjects_test.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,35 @@ func TestDumpInstalledObjects(t *testing.T) {
3030
// - Configure environment variables for this stack (eval "$(elastic-package stack shellinit)").
3131
// - Run tests.
3232
// - Check that recorded files make sense and commit them.
33+
// To update a suite:
34+
// - Reproduce the scenario as described in the comments.
35+
// - Remove the files that you want to update.
36+
// - Follow the same steps to create a new suite.
37+
// - Check if the changes are the expected ones and commit them.
3338
suites := []*installedObjectsDumpSuite{
3439
&installedObjectsDumpSuite{
40+
// To reproduce the scenario:
41+
// - Start the stack with version 7.16.2.
42+
// - Install apache package (1.3.4).
3543
PackageName: "apache",
36-
RecordDir: "./testdata/elasticsearch-7-mock-dump-apache",
44+
Record: "./testdata/elasticsearch-7-mock-dump-apache",
3745
DumpDir: "./testdata/elasticsearch-7-apache-dump-all",
3846
},
3947
&installedObjectsDumpSuite{
48+
// To reproduce the scenario:
49+
// - Start the stack with version 8.1.0.
50+
// - Install apache package (1.3.6).
4051
PackageName: "apache",
41-
RecordDir: "./testdata/elasticsearch-8-mock-dump-apache",
52+
Record: "./testdata/elasticsearch-8-mock-dump-apache",
4253
DumpDir: "./testdata/elasticsearch-8-apache-dump-all",
4354
},
4455
&installedObjectsDumpSuite{
56+
// To reproduce the scenario:
57+
// - Start the stack with version 8.9.0.
58+
// - Install dga package (2.1.0).
59+
// - Manually replace the `compressed_definition` fields with "//REDACTED//".
4560
PackageName: "dga",
46-
RecordDir: "./testdata/elasticsearch-8-mock-dump-dga",
61+
Record: "./testdata/elasticsearch-8-mock-dump-dga",
4762
DumpDir: "./testdata/elasticsearch-8-dga-dump-all",
4863
},
4964
}
@@ -59,8 +74,8 @@ type installedObjectsDumpSuite struct {
5974
// PackageName is the name of the package.
6075
PackageName string
6176

62-
// RecordDir is where responses from Elasticsearch are recorded.
63-
RecordDir string
77+
// Record is where responses from Elasticsearch are recorded.
78+
Record string
6479

6580
// DumpDir is where the expected dumped files are stored.
6681
DumpDir string
@@ -82,7 +97,7 @@ func (s *installedObjectsDumpSuite) SetupTest() {
8297
}
8398

8499
func (s *installedObjectsDumpSuite) TestDumpAll() {
85-
client := estest.NewClient(s.T(), s.RecordDir)
100+
client := estest.NewClient(s.T(), s.Record)
86101

87102
outputDir := s.T().TempDir()
88103
dumper := NewInstalledObjectsDumper(client.API, s.PackageName)
@@ -99,7 +114,7 @@ func (s *installedObjectsDumpSuite) TestDumpAll() {
99114
}
100115

101116
func (s *installedObjectsDumpSuite) TestDumpSome() {
102-
client := estest.NewClient(s.T(), s.RecordDir)
117+
client := estest.NewClient(s.T(), s.Record)
103118
dumper := NewInstalledObjectsDumper(client.API, s.PackageName)
104119

105120
// In a map so order of execution is randomized.

internal/dump/testdata/elasticsearch-7-apache-dump-all/ilm_policies/logs.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 1,
3-
"modified_date": "2022-01-25T18:01:46.058Z",
3+
"modified_date": "2023-11-27T13:55:33.162Z",
44
"policy": {
55
"phases": {
66
"hot": {
@@ -20,10 +20,10 @@
2020
},
2121
"in_use_by": {
2222
"indices": [
23-
".ds-logs-elastic_agent-default-2022.01.25-000001",
24-
".ds-logs-elastic_agent.metricbeat-default-2022.01.25-000001",
25-
".ds-logs-elastic_agent.filebeat-default-2022.01.25-000001",
26-
".ds-logs-elastic_agent.fleet_server-default-2022.01.25-000001"
23+
".ds-logs-elastic_agent.metricbeat-default-2023.11.27-000001",
24+
".ds-logs-elastic_agent.fleet_server-default-2023.11.27-000001",
25+
".ds-logs-elastic_agent.filebeat-default-2023.11.27-000001",
26+
".ds-logs-elastic_agent-default-2023.11.27-000001"
2727
],
2828
"data_streams": [
2929
"logs-elastic_agent-default",
@@ -33,12 +33,15 @@
3333
],
3434
"composable_templates": [
3535
"logs-apache.access",
36+
"logs-elastic_agent.cloudbeat",
3637
"logs-elastic_agent.apm_server",
38+
"logs-elastic_agent.cloud_defend",
3739
"logs-system.security",
3840
"logs-system.auth",
3941
"logs-elastic_agent.metricbeat",
4042
"logs-elastic_agent.filebeat",
4143
"logs-elastic_agent.packetbeat",
44+
"logs-elastic_agent.filebeat_input",
4245
"logs-elastic_agent.endpoint_security",
4346
"logs-elastic_agent.fleet_server",
4447
"logs-apache.error",

internal/dump/testdata/elasticsearch-7-apache-dump-all/ilm_policies/metrics.json

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 1,
3-
"modified_date": "2022-01-25T18:01:48.410Z",
3+
"modified_date": "2023-11-27T13:55:33.210Z",
44
"policy": {
55
"phases": {
66
"hot": {
@@ -20,21 +20,21 @@
2020
},
2121
"in_use_by": {
2222
"indices": [
23-
".ds-metrics-system.socket_summary-default-2022.01.25-000001",
24-
".ds-metrics-system.cpu-default-2022.01.25-000001",
25-
".ds-metrics-elastic_agent.metricbeat-default-2022.01.25-000001",
26-
".ds-metrics-system.uptime-default-2022.01.25-000001",
27-
".ds-metrics-system.process-default-2022.01.25-000001",
28-
".ds-metrics-system.memory-default-2022.01.25-000001",
29-
".ds-metrics-system.diskio-default-2022.01.25-000001",
30-
".ds-metrics-elastic_agent.fleet_server-default-2022.01.25-000001",
31-
".ds-metrics-elastic_agent.filebeat-default-2022.01.25-000001",
32-
".ds-metrics-system.load-default-2022.01.25-000001",
33-
".ds-metrics-system.process.summary-default-2022.01.25-000001",
34-
".ds-metrics-elastic_agent.elastic_agent-default-2022.01.25-000001",
35-
".ds-metrics-system.filesystem-default-2022.01.25-000001",
36-
".ds-metrics-system.network-default-2022.01.25-000001",
37-
".ds-metrics-system.fsstat-default-2022.01.25-000001"
23+
".ds-metrics-system.process.summary-default-2023.11.27-000001",
24+
".ds-metrics-system.fsstat-default-2023.11.27-000001",
25+
".ds-metrics-system.uptime-default-2023.11.27-000001",
26+
".ds-metrics-system.network-default-2023.11.27-000001",
27+
".ds-metrics-system.filesystem-default-2023.11.27-000001",
28+
".ds-metrics-elastic_agent.elastic_agent-default-2023.11.27-000001",
29+
".ds-metrics-system.socket_summary-default-2023.11.27-000001",
30+
".ds-metrics-system.diskio-default-2023.11.27-000001",
31+
".ds-metrics-elastic_agent.filebeat-default-2023.11.27-000001",
32+
".ds-metrics-system.process-default-2023.11.27-000001",
33+
".ds-metrics-system.cpu-default-2023.11.27-000001",
34+
".ds-metrics-elastic_agent.fleet_server-default-2023.11.27-000001",
35+
".ds-metrics-elastic_agent.metricbeat-default-2023.11.27-000001",
36+
".ds-metrics-system.memory-default-2023.11.27-000001",
37+
".ds-metrics-system.load-default-2023.11.27-000001"
3838
],
3939
"data_streams": [
4040
"metrics-system.filesystem-default",
@@ -68,11 +68,13 @@
6868
"metrics-system.load",
6969
"metrics-system.core",
7070
"metrics-elastic_agent.filebeat",
71+
"metrics-elastic_agent.filebeat_input",
7172
"metrics-system.uptime",
7273
"metrics-system.process.summary",
7374
"metrics-system.cpu",
7475
"metrics-elastic_agent.heartbeat",
7576
"metrics-system.diskio",
77+
"metrics-elastic_agent.cloudbeat",
7678
"metrics-elastic_agent.metricbeat",
7779
"metrics-elastic_agent.auditbeat",
7880
"metrics-system.network",

internal/dump/testdata/elasticsearch-7-apache-dump-all/index_templates/logs-apache.access.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@
137137
"properties": {
138138
"name": {
139139
"ignore_above": 1024,
140-
"type": "keyword"
140+
"type": "keyword",
141+
"fields": {}
141142
}
142143
}
143144
}
@@ -267,7 +268,8 @@
267268
"properties": {
268269
"path": {
269270
"ignore_above": 1024,
270-
"type": "keyword"
271+
"type": "keyword",
272+
"fields": {}
271273
}
272274
}
273275
},
@@ -468,29 +470,33 @@
468470
"properties": {
469471
"name": {
470472
"ignore_above": 1024,
471-
"type": "keyword"
473+
"type": "keyword",
474+
"fields": {}
472475
}
473476
}
474477
},
475478
"user_agent": {
476479
"properties": {
477480
"original": {
478481
"ignore_above": 1024,
479-
"type": "keyword"
482+
"type": "keyword",
483+
"fields": {}
480484
},
481485
"os": {
482486
"properties": {
483487
"name": {
484488
"ignore_above": 1024,
485-
"type": "keyword"
489+
"type": "keyword",
490+
"fields": {}
486491
},
487492
"version": {
488493
"ignore_above": 1024,
489494
"type": "keyword"
490495
},
491496
"full": {
492497
"ignore_above": 1024,
493-
"type": "keyword"
498+
"type": "keyword",
499+
"fields": {}
494500
}
495501
}
496502
},

0 commit comments

Comments
 (0)