|
| 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 fleet |
| 6 | + |
| 7 | +import ( |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/elastic/fleet-server/v7/internal/pkg/build" |
| 12 | + "github.com/elastic/fleet-server/v7/internal/pkg/config" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAgentModeFlag(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + flags []string |
| 21 | + expect func() *config.Config |
| 22 | + }{{ |
| 23 | + name: "no flags", |
| 24 | + flags: []string{}, |
| 25 | + expect: func() *config.Config { |
| 26 | + cfg := &config.Config{} |
| 27 | + cfg.InitDefaults() |
| 28 | + cfg.Output.Elasticsearch.InitDefaults() // NOTE this is implicitly called when ucfg parses the top level cfg object, but we need to explicitly call it for testing. |
| 29 | + return cfg |
| 30 | + }, |
| 31 | + }, { |
| 32 | + name: "debug log flag", |
| 33 | + flags: []string{"E logging.level=debug"}, // flag is the k:v separated by a space, key does not have the "-" prefix |
| 34 | + expect: func() *config.Config { |
| 35 | + cfg := &config.Config{} |
| 36 | + cfg.InitDefaults() |
| 37 | + cfg.Output.Elasticsearch.InitDefaults() |
| 38 | + cfg.Logging.Level = "debug" |
| 39 | + return cfg |
| 40 | + }, |
| 41 | + }} |
| 42 | + |
| 43 | + for _, tc := range tests { |
| 44 | + t.Run(tc.name, func(t *testing.T) { |
| 45 | + cmd := NewCommand(build.Info{}) |
| 46 | + for _, flag := range tc.flags { |
| 47 | + arr := strings.Split(flag, " ") |
| 48 | + err := cmd.Flags().Set(arr[0], arr[1]) |
| 49 | + require.NoError(t, err) |
| 50 | + } |
| 51 | + |
| 52 | + cfgObj := cmd.Flags().Lookup("E").Value.(*config.Flag) //nolint:errcheck // same as in main |
| 53 | + cfgCLI := cfgObj.Config() |
| 54 | + cfg, err := config.FromConfig(cfgCLI) |
| 55 | + require.NoError(t, err) |
| 56 | + require.Equal(t, tc.expect(), cfg) |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
0 commit comments