Skip to content
Closed
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
28 changes: 28 additions & 0 deletions internal/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"github.com/elastic/go-ucfg"
"github.com/elastic/go-ucfg/yaml"

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

const (
Expand All @@ -34,6 +36,7 @@ const (
// VarValue represents a variable value as defined in a package or data stream
// manifest file.
type VarValue struct {
dict map[string]interface{}
scalar interface{}
list []interface{}
}
Expand All @@ -42,6 +45,8 @@ type VarValue struct {
// manifest file into a VarValue.
func (vv *VarValue) Unpack(value interface{}) error {
switch u := value.(type) {
case map[string]interface{}:
vv.dict = u
case []interface{}:
vv.list = u
default:
Expand All @@ -57,10 +62,33 @@ func (vv VarValue) MarshalJSON() ([]byte, error) {
return json.Marshal(vv.scalar)
} else if vv.list != nil {
return json.Marshal(vv.list)
} else if vv.dict != nil {
return json.Marshal(vv.dict)
}
return []byte("null"), nil
}

// GetChildValue helps accessing subvalues when the value is a dictionary.
// This is generally a workaround for ucfg not keeping the structure of
// the read configuration (https://github.com/elastic/go-ucfg/issues/124).
// For variables we want their full names.
func (vv *VarValue) GetChildValue(name string) (VarValue, bool) {
if vv.dict == nil {
return VarValue{}, false
}

ms := common.MapStr(vv.dict)
value, err := ms.GetValue(name)
if err != nil {
return VarValue{}, false
}

var varValue VarValue
// Ignoring error as this cannot fail.
_ = varValue.Unpack(value)
return varValue, true
}

// Variable is an instance of configuration variable (named, typed).
type Variable struct {
Name string `config:"name" json:"name" yaml:"name"`
Expand Down
22 changes: 21 additions & 1 deletion internal/testrunner/runners/system/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func setKibanaVariables(definitions []packages.Variable, values map[string]packa
for _, definition := range definitions {
val := definition.Default

value, exists := values[definition.Name]
value, exists := getConfigValue(values, definition.Name)
if exists {
val = value
}
Expand All @@ -657,6 +657,26 @@ func setKibanaVariables(definitions []packages.Variable, values map[string]packa
return vars
}

func getConfigValue(values map[string]packages.VarValue, name string) (packages.VarValue, bool) {
value, found := values[name]
if found {
return value, true
}

// Workaround when the value has been expanded.
root, leaf, found := strings.Cut(name, ".")
if !found {
return packages.VarValue{}, false
}

parent, found := values[root]
if !found {
return packages.VarValue{}, false
}

return parent.GetChildValue(leaf)
}

// getDataStreamIndex returns the index of the data stream whose input name
// matches. Otherwise it returns the 0.
func getDataStreamIndex(inputName string, ds packages.DataStreamManifest) int {
Expand Down
21 changes: 15 additions & 6 deletions internal/testrunner/runners/system/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,29 @@ func newConfig(configFilePath string, ctxt servicedeployer.ServiceContext, servi
return nil, errors.Wrapf(err, "could not apply context to test configuration file: %s", configFilePath)
}

var c testConfig
cfg, err := yaml.NewConfig(data, ucfg.PathSep("."))
c, err := parseConfig(configFilePath, data)
if err != nil {
return nil, errors.Wrapf(err, "unable to load system test configuration file: %s", configFilePath)
}
if err := cfg.Unpack(&c); err != nil {
return nil, errors.Wrapf(err, "unable to unpack system test configuration file: %s", configFilePath)
return nil, err
}

// Save path
c.Path = configFilePath
c.ServiceVariantName = serviceVariantName
return &c, nil
}

func parseConfig(configFilePath string, data []byte) (testConfig, error) {
var c testConfig
cfg, err := yaml.NewConfig(data, ucfg.PathSep("."))
if err != nil {
return c, errors.Wrapf(err, "unable to load system test configuration file: %s", configFilePath)
}
if err := cfg.Unpack(&c); err != nil {
return c, errors.Wrapf(err, "unable to unpack system test configuration file: %s", configFilePath)
}
return c, nil
}

func listConfigFiles(systemTestFolderPath string) (files []string, err error) {
fHandle, err := os.Open(systemTestFolderPath)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions internal/testrunner/runners/system/test_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 (
"testing"

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

func TestGetExpandedConfigValue(t *testing.T) {
const configData = `
vars:
jmx.mappings: |
- mbean: 'java.lang:type=Runtime'
attributes:
- attr: Uptime
field: uptime
`

config, err := parseConfig("test", []byte(configData))
require.NoError(t, err)

_, found := getConfigValue(config.Vars, "jmx.mappings")
assert.True(t, found)
}