Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/elastic/go-elasticsearch/v7 v7.17.10
github.com/elastic/go-licenser v0.4.2
github.com/elastic/go-resource v0.2.0
github.com/elastic/go-ucfg v0.9.0
github.com/elastic/go-ucfg v0.8.8
github.com/elastic/package-spec/v3 v3.5.7
github.com/fatih/color v1.18.0
github.com/go-viper/mapstructure/v2 v2.5.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ github.com/elastic/go-licenser v0.4.2 h1:bPbGm8bUd8rxzSswFOqvQh1dAkKGkgAmrPxbUi+
github.com/elastic/go-licenser v0.4.2/go.mod h1:W8eH6FaZDR8fQGm+7FnVa7MxI1b/6dAqxz+zPB8nm5c=
github.com/elastic/go-resource v0.2.0 h1:T92tw+THqISnCKaZBijlZMpEpCYkFkwsOgLQxKX6pqA=
github.com/elastic/go-resource v0.2.0/go.mod h1:KySNvn044vVpPCX1osrkB5MQHemDM5RnkeBFW51CiRo=
github.com/elastic/go-ucfg v0.9.0 h1:QRO2O8rv6oORD6N1gDc27bj2jgSdlPFW+t+Uf3MJkik=
github.com/elastic/go-ucfg v0.9.0/go.mod h1:6Z66LNkFK5xAlWg3Ny7qgtrvBUadaAcor+kYxw2pXBk=
github.com/elastic/go-ucfg v0.8.8 h1:54KIF/2zFKfl0MzsSOCGOsZ3O2bnjFQJ0nDJcLhviyk=
github.com/elastic/go-ucfg v0.8.8/go.mod h1:4E8mPOLSUV9hQ7sgLEJ4bvt0KhMuDJa8joDT2QGAEKA=
github.com/elastic/gojsonschema v1.2.1 h1:cUMbgsz0wyEB4x7xf3zUEvUVDl6WCz2RKcQPul8OsQc=
github.com/elastic/gojsonschema v1.2.1/go.mod h1:biw5eBS2Z4T02wjATMRSfecfjCmwaDPvuaqf844gLrg=
github.com/elastic/kbncontent v0.1.4 h1:GoUkJkqkn2H6iJTnOHcxEqYVVYyjvcebLQVaSR1aSvU=
Expand Down Expand Up @@ -452,9 +452,11 @@ gopkg.in/dnaeon/go-vcr.v3 v3.2.0 h1:Rltp0Vf+Aq0u4rQXgmXgtgoRDStTnFN83cWgSGSoRzM=
gopkg.in/dnaeon/go-vcr.v3 v3.2.0/go.mod h1:2IMOnnlx9I6u9x+YBsM3tAMx6AlOxnJ0pWxQAzZ79Ag=
gopkg.in/evanphx/json-patch.v4 v4.13.0 h1:czT3CmqEaQ1aanPc5SdlgQrrEIb8w/wwCvWWnfEbYzo=
gopkg.in/evanphx/json-patch.v4 v4.13.0/go.mod h1:p8EYWUEYMpynmqDbY58zCKCFZw8pRWMG4EsWvDvM72M=
gopkg.in/hjson/hjson-go.v3 v3.0.1/go.mod h1:X6zrTSVeImfwfZLfgQdInl9mWjqPqgH90jom9nym/lw=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
173 changes: 173 additions & 0 deletions internal/testrunner/runners/system/testconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// 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 system

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-package/internal/servicedeployer"

"github.com/elastic/elastic-package/internal/packages"
)

func TestNewConfig(t *testing.T) {
t.Run("minimal config loads successfully", func(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "test-some-config.yml")
err := os.WriteFile(configPath, []byte(`
input: log
service: nginx
`), 0644)
require.NoError(t, err)

cfg, err := newConfig(configPath, servicedeployer.ServiceInfo{}, "")
require.NoError(t, err)
require.NotNil(t, cfg)
assert.Equal(t, "log", cfg.Input)
assert.Equal(t, "nginx", cfg.Service)
assert.Empty(t, cfg.Vars)
})

t.Run("vars with data_stream.dataset are detected", func(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "test-dataset-config.yml")
err := os.WriteFile(configPath, []byte(`
input: log
service: nginx
vars:
data_stream.dataset: other.name
`), 0644)
require.NoError(t, err)

cfg, err := newConfig(configPath, servicedeployer.ServiceInfo{}, "")
require.NoError(t, err)
require.NotNil(t, cfg)

v, err := cfg.Vars.GetValue("data_stream.dataset")
require.NoError(t, err)
Comment on lines +53 to +54
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not found in v0.9.0

Error: Received unexpected error: key not found
Test: TestNewConfig/vars_with_data_stream.dataset_are_detected 

ds, ok := v.(string)
require.True(t, ok, "data_stream.dataset should be a string")
assert.Equal(t, "other.name", ds)
})

t.Run("vars with data_stream.dataset and other vars", func(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "test-multi-vars-config.yml")
err := os.WriteFile(configPath, []byte(`
input: log
service: nginx
vars:
data_stream.dataset: other.name
some.other.var: value
`), 0644)
require.NoError(t, err)

cfg, err := newConfig(configPath, servicedeployer.ServiceInfo{}, "")
require.NoError(t, err)
require.NotNil(t, cfg)

v, err := cfg.Vars.GetValue("data_stream.dataset")
require.NoError(t, err)
ds, ok := v.(string)
require.True(t, ok)
assert.Equal(t, "other.name", ds)

v2, err := cfg.Vars.GetValue("some.other.var")
require.NoError(t, err)
val, ok := v2.(string)
require.True(t, ok)
assert.Equal(t, "value", val)
})

t.Run("data_stream.vars are detected", func(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "test-datastream-vars-config.yml")
err := os.WriteFile(configPath, []byte(`
input: log
service: nginx
data_stream:
vars:
dataset: my.dataset
`), 0644)
require.NoError(t, err)

cfg, err := newConfig(configPath, servicedeployer.ServiceInfo{}, "")
require.NoError(t, err)
require.NotNil(t, cfg)

v, err := cfg.DataStream.Vars.GetValue("dataset")
require.NoError(t, err)
ds, ok := v.(string)
require.True(t, ok)
assert.Equal(t, "my.dataset", ds)
})

t.Run("missing config file returns error", func(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "test-nonexistent-config.yml")

cfg, err := newConfig(configPath, servicedeployer.ServiceInfo{}, "")
assert.Error(t, err)
assert.Nil(t, cfg)
assert.Contains(t, err.Error(), "unable to find system test configuration file")
})
}

func TestNewConfig_ConfigName(t *testing.T) {
t.Run("name is derived from config file", func(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "test-my-scenario-config.yml")
err := os.WriteFile(configPath, []byte(`
input: log
service: nginx
`), 0644)
require.NoError(t, err)

cfg, err := newConfig(configPath, servicedeployer.ServiceInfo{}, "")
require.NoError(t, err)
assert.Equal(t, "my-scenario", cfg.Name())
})

t.Run("name includes variant when set", func(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "test-my-scenario-config.yml")
err := os.WriteFile(configPath, []byte(`
input: log
service: nginx
`), 0644)
require.NoError(t, err)

cfg, err := newConfig(configPath, servicedeployer.ServiceInfo{}, "variant-a")
require.NoError(t, err)
assert.Equal(t, "my-scenario (variant: variant-a)", cfg.Name())
})
}

// Ensure that vars with data_stream.dataset work with getExpectedDatasetForTest
// (used when building data stream names).
func TestNewConfig_VarsUsedByGetExpectedDatasetForTest(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "test-dataset-config.yml")
err := os.WriteFile(configPath, []byte(`
input: log
service: nginx
vars:
data_stream.dataset: other.name
`), 0644)
require.NoError(t, err)

cfg, err := newConfig(configPath, servicedeployer.ServiceInfo{}, "")
require.NoError(t, err)
require.NotNil(t, cfg)

// Same logic as in getExpectedDatasetForTest for input packages.
got := getExpectedDatasetForTest("input", "default.dataset", packages.PolicyTemplate{Name: "bar"}, cfg.Vars)
assert.Equal(t, "other.name", got, "getExpectedDatasetForTest should use vars.data_stream.dataset")
}