This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
ingest_manager_test.go
231 lines (190 loc) · 6.83 KB
/
ingest_manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"context"
"os"
"strings"
"testing"
"time"
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
"github.com/cucumber/messages-go/v10"
"github.com/elastic/e2e-testing/cli/config"
apme2e "github.com/elastic/e2e-testing/internal"
"github.com/elastic/e2e-testing/internal/common"
"github.com/elastic/e2e-testing/internal/deploy"
"github.com/elastic/e2e-testing/internal/kibana"
"github.com/elastic/e2e-testing/internal/shell"
"github.com/elastic/e2e-testing/internal/utils"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"go.elastic.co/apm"
)
var imts IngestManagerTestSuite
var tx *apm.Transaction
var stepSpan *apm.Span
func setUpSuite() {
config.Init()
kibanaClient, err := kibana.NewClient()
if err != nil {
log.Error(err)
os.Exit(1)
}
common.InitVersions()
imts = IngestManagerTestSuite{
Fleet: &FleetTestSuite{
kibanaClient: kibanaClient,
deployer: deploy.New(common.Provider),
},
}
}
func InitializeIngestManagerTestScenario(ctx *godog.ScenarioContext) {
ctx.BeforeScenario(func(p *messages.Pickle) {
log.Trace("Before Fleet scenario")
tx = apme2e.StartTransaction(p.GetName(), "test.scenario")
tx.Context.SetLabel("suite", "fleet")
// context is initialised at the step hook, we are initialising it here to prevent panics
imts.Fleet.currentContext = context.Background()
imts.Fleet.beforeScenario()
})
ctx.AfterScenario(func(p *messages.Pickle, err error) {
log.Trace("After Fleet scenario")
if err != nil {
e := apm.DefaultTracer.NewError(err)
e.Context.SetLabel("scenario", p.GetName())
e.Context.SetLabel("gherkin_type", "scenario")
e.Send()
}
f := func() {
tx.End()
apm.DefaultTracer.Flush(nil)
}
defer f()
imts.Fleet.afterScenario()
})
ctx.BeforeStep(func(step *godog.Step) {
stepSpan = tx.StartSpan(step.GetText(), "test.scenario.step", nil)
imts.Fleet.currentContext = apm.ContextWithSpan(context.Background(), stepSpan)
})
ctx.AfterStep(func(st *godog.Step, err error) {
if err != nil {
e := apm.DefaultTracer.NewError(err)
e.Context.SetLabel("step", st.GetText())
e.Context.SetLabel("gherkin_type", "step")
e.Send()
}
if stepSpan != nil {
stepSpan.End()
}
})
ctx.Step(`^the "([^"]*)" process is in the "([^"]*)" state on the host$`, imts.processStateOnTheHost)
ctx.Step(`^there are "([^"]*)" instances of the "([^"]*)" process in the "([^"]*)" state$`, imts.thereAreInstancesOfTheProcessInTheState)
imts.Fleet.contributeSteps(ctx)
}
func InitializeIngestManagerTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
setUpSuite()
var suiteTx *apm.Transaction
var suiteParentSpan *apm.Span
var suiteContext = context.Background()
// instrumentation
defer apm.DefaultTracer.Flush(nil)
suiteTx = apme2e.StartTransaction("Initialise Fleet", "test.suite")
defer suiteTx.End()
suiteParentSpan = suiteTx.StartSpan("Before Fleet test suite", "test.suite.before", nil)
suiteContext = apm.ContextWithSpan(suiteContext, suiteParentSpan)
defer suiteParentSpan.End()
deployer := deploy.New(common.Provider)
err := deployer.PreBootstrap(suiteContext)
if err != nil {
log.WithField("error", err).Fatal("Unable to run pre-bootstrap initialization")
}
// FIXME: This needs to go into deployer code for docker somehow. Must resolve
// cyclic imports since common.defaults now imports deploy module
if !shell.GetEnvBool("SKIP_PULL") && common.Provider != "remote" {
images := []string{
"docker.elastic.co/beats/elastic-agent:" + common.BeatVersion,
"docker.elastic.co/beats/elastic-agent-ubi8:" + common.BeatVersion,
"docker.elastic.co/elasticsearch/elasticsearch:" + common.StackVersion,
"docker.elastic.co/observability-ci/elastic-agent:" + common.BeatVersion,
"docker.elastic.co/observability-ci/elastic-agent-ubi8:" + common.BeatVersion,
"docker.elastic.co/observability-ci/elasticsearch:" + common.StackVersion,
"docker.elastic.co/observability-ci/kibana:" + common.KibanaVersion,
}
if !strings.HasPrefix(common.KibanaVersion, "pr") {
images = append(images, "docker.elastic.co/kibana/kibana:"+common.KibanaVersion)
}
deploy.PullImages(suiteContext, images)
}
common.ProfileEnv = map[string]string{
"kibanaVersion": common.KibanaVersion,
"stackPlatform": "linux/" + utils.GetArchitecture(),
"stackVersion": common.StackVersion,
}
common.ProfileEnv["kibanaProfile"] = "default"
common.ProfileEnv["kibanaDockerNamespace"] = "kibana"
if strings.HasPrefix(common.KibanaVersion, "pr") || utils.IsCommit(common.KibanaVersion) {
// because it comes from a PR
common.ProfileEnv["kibanaDockerNamespace"] = "observability-ci"
common.ProfileEnv["KIBANA_IMAGE_REF_CUSTOM"] = "docker.elastic.co/observability-ci/kibana:" + common.KibanaVersion
}
if common.Provider != "remote" {
err := bootstrapFleet(suiteContext, common.ProfileEnv)
if err != nil {
log.WithError(err).Fatal("Could not bootstrap Fleet runtime dependencies")
}
} else {
err := imts.Fleet.kibanaClient.WaitForFleet(suiteContext)
if err != nil {
log.WithError(err).Fatal("Could not determine Fleet's readiness.")
}
}
imts.Fleet.Version = common.BeatVersionBase
imts.Fleet.RuntimeDependenciesStartDate = time.Now().UTC()
})
ctx.AfterSuite(func() {
f := func() {
apm.DefaultTracer.Flush(nil)
}
defer f()
// instrumentation
var suiteTx *apm.Transaction
var suiteParentSpan *apm.Span
var suiteContext = context.Background()
defer apm.DefaultTracer.Flush(nil)
suiteTx = apme2e.StartTransaction("Tear Down Fleet", "test.suite")
defer suiteTx.End()
suiteParentSpan = suiteTx.StartSpan("After Fleet test suite", "test.suite.after", nil)
suiteContext = apm.ContextWithSpan(suiteContext, suiteParentSpan)
defer suiteParentSpan.End()
if !common.DeveloperMode && common.Provider != "remote" {
log.Debug("Destroying Fleet runtime dependencies")
deployer := deploy.New(common.Provider)
deployer.Destroy(suiteContext, deploy.NewServiceRequest(common.FleetProfileName))
}
})
}
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "progress", // can define default values
}
func init() {
godog.BindCommandLineFlags("godog.", &opts) // godog v0.11.0 (latest)
}
func TestMain(m *testing.M) {
flag.Parse()
opts.Paths = flag.Args()
status := godog.TestSuite{
Name: "godogs",
TestSuiteInitializer: InitializeIngestManagerTestSuite,
ScenarioInitializer: InitializeIngestManagerTestScenario,
Options: &opts,
}.Run()
// Optional: Run `testing` package's logic besides godog.
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}