Skip to content

Commit f1106b9

Browse files
authored
Check logs from elastic-agent in system tests (#1256)
Check logs from elastic-agent container during the system tests to check if there have been some known messages that represent known failures. If there exist any message like those, system tests will fail with those errors. Those known messages can be specified through regex patterns.
1 parent fe3bd40 commit f1106b9

File tree

10 files changed

+550
-88
lines changed

10 files changed

+550
-88
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ The command ensures that the package is aligned with the package spec and the RE
263263
_Context: global_
264264

265265
Use this command to add, remove, and manage multiple config profiles.
266-
267-
Individual user profiles appear in ~/.elastic-package/stack, and contain all the config files needed by the "stack" subcommand.
266+
267+
Individual user profiles appear in ~/.elastic-package/stack, and contain all the config files needed by the "stack" subcommand.
268268
Once a new profile is created, it can be specified with the -p flag, or the ELASTIC_PACKAGE_PROFILE environment variable.
269269
User profiles can be configured with a "config.yml" file in the profile directory.
270270

cmd/profiles.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const tableFormat = "table"
2828

2929
func setupProfilesCommand() *cobraext.Command {
3030
profilesLongDescription := `Use this command to add, remove, and manage multiple config profiles.
31-
32-
Individual user profiles appear in ~/.elastic-package/stack, and contain all the config files needed by the "stack" subcommand.
31+
32+
Individual user profiles appear in ~/.elastic-package/stack, and contain all the config files needed by the "stack" subcommand.
3333
Once a new profile is created, it can be specified with the -p flag, or the ELASTIC_PACKAGE_PROFILE environment variable.
3434
User profiles can be configured with a "config.yml" file in the profile directory.`
3535

@@ -247,21 +247,3 @@ func profileToList(profiles []profile.Metadata, currentProfile string) [][]strin
247247

248248
return profileList
249249
}
250-
251-
func availableProfilesAsAList() ([]string, error) {
252-
loc, err := locations.NewLocationManager()
253-
if err != nil {
254-
return []string{}, errors.Wrap(err, "error fetching profile path")
255-
}
256-
257-
profileNames := []string{}
258-
profileList, err := profile.FetchAllProfiles(loc.ProfileDir())
259-
if err != nil {
260-
return profileNames, errors.Wrap(err, "error fetching all profiles")
261-
}
262-
for _, prof := range profileList {
263-
profileNames = append(profileNames, prof.Name)
264-
}
265-
266-
return profileNames, nil
267-
}

cmd/stack.go

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/elastic/elastic-package/internal/cobraext"
1616
"github.com/elastic/elastic-package/internal/common"
1717
"github.com/elastic/elastic-package/internal/install"
18-
"github.com/elastic/elastic-package/internal/profile"
1918
"github.com/elastic/elastic-package/internal/stack"
2019
)
2120

@@ -73,12 +72,12 @@ func setupStackCommand() *cobraext.Command {
7372
return cobraext.FlagParsingError(err, cobraext.StackVersionFlagName)
7473
}
7574

76-
profile, err := getProfileFlag(cmd)
75+
profile, err := cobraext.GetProfileFlag(cmd)
7776
if err != nil {
7877
return err
7978
}
8079

81-
provider, err := getProviderFromProfile(cmd, profile, true)
80+
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, true)
8281
if err != nil {
8382
return err
8483
}
@@ -112,12 +111,12 @@ func setupStackCommand() *cobraext.Command {
112111
RunE: func(cmd *cobra.Command, args []string) error {
113112
cmd.Println("Take down the Elastic stack")
114113

115-
profile, err := getProfileFlag(cmd)
114+
profile, err := cobraext.GetProfileFlag(cmd)
116115
if err != nil {
117116
return err
118117
}
119118

120-
provider, err := getProviderFromProfile(cmd, profile, false)
119+
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, false)
121120
if err != nil {
122121
return err
123122
}
@@ -141,12 +140,12 @@ func setupStackCommand() *cobraext.Command {
141140
RunE: func(cmd *cobra.Command, args []string) error {
142141
cmd.Println("Update the Elastic stack")
143142

144-
profile, err := getProfileFlag(cmd)
143+
profile, err := cobraext.GetProfileFlag(cmd)
145144
if err != nil {
146145
return err
147146
}
148147

149-
provider, err := getProviderFromProfile(cmd, profile, false)
148+
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, false)
150149
if err != nil {
151150
return err
152151
}
@@ -184,7 +183,7 @@ func setupStackCommand() *cobraext.Command {
184183
fmt.Fprintf(cmd.OutOrStderr(), "Detected shell: %s\n", shellName)
185184
}
186185

187-
profile, err := getProfileFlag(cmd)
186+
profile, err := cobraext.GetProfileFlag(cmd)
188187
if err != nil {
189188
return err
190189
}
@@ -209,12 +208,12 @@ func setupStackCommand() *cobraext.Command {
209208
return cobraext.FlagParsingError(err, cobraext.StackDumpOutputFlagName)
210209
}
211210

212-
profile, err := getProfileFlag(cmd)
211+
profile, err := cobraext.GetProfileFlag(cmd)
213212
if err != nil {
214213
return err
215214
}
216215

217-
provider, err := getProviderFromProfile(cmd, profile, false)
216+
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, false)
218217
if err != nil {
219218
return err
220219
}
@@ -239,12 +238,12 @@ func setupStackCommand() *cobraext.Command {
239238
Use: "status",
240239
Short: "Show status of the stack services",
241240
RunE: func(cmd *cobra.Command, args []string) error {
242-
profile, err := getProfileFlag(cmd)
241+
profile, err := cobraext.GetProfileFlag(cmd)
243242
if err != nil {
244243
return err
245244
}
246245

247-
provider, err := getProviderFromProfile(cmd, profile, false)
246+
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, false)
248247
if err != nil {
249248
return err
250249
}
@@ -321,57 +320,3 @@ func printStatus(cmd *cobra.Command, servicesStatus []stack.ServiceStatus) {
321320
t.SetStyle(table.StyleRounded)
322321
cmd.Println(t.Render())
323322
}
324-
325-
func getProfileFlag(cmd *cobra.Command) (*profile.Profile, error) {
326-
profileName, err := cmd.Flags().GetString(cobraext.ProfileFlagName)
327-
if err != nil {
328-
return nil, cobraext.FlagParsingError(err, cobraext.ProfileFlagName)
329-
}
330-
if profileName == "" {
331-
config, err := install.Configuration()
332-
if err != nil {
333-
return nil, fmt.Errorf("cannot read configuration: %w", err)
334-
}
335-
profileName = config.CurrentProfile()
336-
}
337-
338-
p, err := profile.LoadProfile(profileName)
339-
if errors.Is(err, profile.ErrNotAProfile) {
340-
list, err := availableProfilesAsAList()
341-
if err != nil {
342-
return nil, errors.Wrap(err, "error listing known profiles")
343-
}
344-
if len(list) == 0 {
345-
return nil, fmt.Errorf("%s is not a valid profile", profileName)
346-
}
347-
return nil, fmt.Errorf("%s is not a valid profile, known profiles are: %s", profileName, strings.Join(list, ", "))
348-
}
349-
if err != nil {
350-
return nil, errors.Wrap(err, "error loading profile")
351-
}
352-
353-
return p, nil
354-
}
355-
356-
func getProviderFromProfile(cmd *cobra.Command, profile *profile.Profile, checkFlag bool) (stack.Provider, error) {
357-
var providerName = stack.DefaultProvider
358-
stackConfig, err := stack.LoadConfig(profile)
359-
if err != nil {
360-
return nil, err
361-
}
362-
if stackConfig.Provider != "" {
363-
providerName = stackConfig.Provider
364-
}
365-
366-
if checkFlag {
367-
providerFlag, err := cmd.Flags().GetString(cobraext.StackProviderFlagName)
368-
if err != nil {
369-
return nil, cobraext.FlagParsingError(err, cobraext.StackProviderFlagName)
370-
}
371-
if providerFlag != "" {
372-
providerName = providerFlag
373-
}
374-
}
375-
376-
return stack.BuildProvider(providerName, profile)
377-
}

cmd/testrunner.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/elastic/elastic-package/internal/cobraext"
1717
"github.com/elastic/elastic-package/internal/common"
1818
"github.com/elastic/elastic-package/internal/elasticsearch"
19+
"github.com/elastic/elastic-package/internal/install"
1920
"github.com/elastic/elastic-package/internal/packages"
2021
"github.com/elastic/elastic-package/internal/signal"
2122
"github.com/elastic/elastic-package/internal/testrunner"
@@ -71,6 +72,7 @@ func setupTestCommand() *cobraext.Command {
7172
cmd.PersistentFlags().BoolP(cobraext.TestCoverageFlagName, "", false, cobraext.TestCoverageFlagDescription)
7273
cmd.PersistentFlags().DurationP(cobraext.DeferCleanupFlagName, "", 0, cobraext.DeferCleanupFlagDescription)
7374
cmd.PersistentFlags().String(cobraext.VariantFlagName, "", cobraext.VariantFlagDescription)
75+
cmd.PersistentFlags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))
7476

7577
for testType, runner := range testrunner.TestRunners() {
7678
action := testTypeCommandActionFactory(runner)
@@ -207,6 +209,11 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
207209

208210
variantFlag, _ := cmd.Flags().GetString(cobraext.VariantFlagName)
209211

212+
profile, err := cobraext.GetProfileFlag(cmd)
213+
if err != nil {
214+
return err
215+
}
216+
210217
esClient, err := elasticsearch.NewClient()
211218
if err != nil {
212219
return errors.Wrap(err, "can't create Elasticsearch client")
@@ -226,6 +233,7 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
226233
DeferCleanup: deferCleanup,
227234
ServiceVariant: variantFlag,
228235
WithCoverage: testCoverage,
236+
Profile: profile,
229237
})
230238

231239
results = append(results, r...)

internal/cobraext/profiles.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package cobraext
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
11+
"github.com/pkg/errors"
12+
"github.com/spf13/cobra"
13+
14+
"github.com/elastic/elastic-package/internal/configuration/locations"
15+
"github.com/elastic/elastic-package/internal/install"
16+
"github.com/elastic/elastic-package/internal/profile"
17+
"github.com/elastic/elastic-package/internal/stack"
18+
)
19+
20+
// GetProfileFlag returns the profile information
21+
func GetProfileFlag(cmd *cobra.Command) (*profile.Profile, error) {
22+
profileName, err := cmd.Flags().GetString(ProfileFlagName)
23+
if err != nil {
24+
return nil, FlagParsingError(err, ProfileFlagName)
25+
}
26+
if profileName == "" {
27+
config, err := install.Configuration()
28+
if err != nil {
29+
return nil, fmt.Errorf("cannot read configuration: %w", err)
30+
}
31+
profileName = config.CurrentProfile()
32+
}
33+
34+
p, err := profile.LoadProfile(profileName)
35+
if errors.Is(err, profile.ErrNotAProfile) {
36+
list, err := availableProfilesAsAList()
37+
if err != nil {
38+
return nil, errors.Wrap(err, "error listing known profiles")
39+
}
40+
if len(list) == 0 {
41+
return nil, fmt.Errorf("%s is not a valid profile", profileName)
42+
}
43+
return nil, fmt.Errorf("%s is not a valid profile, known profiles are: %s", profileName, strings.Join(list, ", "))
44+
}
45+
if err != nil {
46+
return nil, errors.Wrap(err, "error loading profile")
47+
}
48+
49+
return p, nil
50+
}
51+
52+
func availableProfilesAsAList() ([]string, error) {
53+
loc, err := locations.NewLocationManager()
54+
if err != nil {
55+
return []string{}, errors.Wrap(err, "error fetching profile path")
56+
}
57+
58+
profileNames := []string{}
59+
profileList, err := profile.FetchAllProfiles(loc.ProfileDir())
60+
if err != nil {
61+
return profileNames, errors.Wrap(err, "error fetching all profiles")
62+
}
63+
for _, prof := range profileList {
64+
profileNames = append(profileNames, prof.Name)
65+
}
66+
67+
return profileNames, nil
68+
}
69+
70+
// GetStackProviderFromProfile returns the provider related to the given profile
71+
func GetStackProviderFromProfile(cmd *cobra.Command, profile *profile.Profile, checkFlag bool) (stack.Provider, error) {
72+
var providerName = stack.DefaultProvider
73+
stackConfig, err := stack.LoadConfig(profile)
74+
if err != nil {
75+
return nil, err
76+
}
77+
if stackConfig.Provider != "" {
78+
providerName = stackConfig.Provider
79+
}
80+
81+
if checkFlag {
82+
providerFlag, err := cmd.Flags().GetString(StackProviderFlagName)
83+
if err != nil {
84+
return nil, FlagParsingError(err, StackProviderFlagName)
85+
}
86+
if providerFlag != "" {
87+
providerName = providerFlag
88+
}
89+
}
90+
91+
return stack.BuildProvider(providerName, profile)
92+
}

internal/stack/dump.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ func writeLogFiles(logsPath, serviceName string, content []byte) {
7676
logger.Errorf("can't write service logs (service: %s): %v", serviceName, err)
7777
}
7878
}
79+
80+
// DumpLogsFile returns the file path to the logs of a given service
81+
func DumpLogsFile(options DumpOptions, serviceName string) string {
82+
return filepath.Join(options.Output, "logs", fmt.Sprintf("%s.log", serviceName))
83+
}

0 commit comments

Comments
 (0)