talos/internal/pkg/environment/environment_test.go
Andrey Smirnov 0bb7e8a5cf
refactor: split config.Provider into Config & Container
See #7230

This is a step towards preparing for multi-doc config.

Split the `config.Provider` interface into parts which have different
implementation:

* `config.Config` accesses the config itself, it might be implemented by
  `v1alpha1.Config` for example
* `config.Container` will be a set of config documents, which implement
  validation, encoding, etc.

`Version()` method dropped, as it makes little sense and it was almost
not used.

`Raw()` method renamed to `RawV1Alpha1()` to support legacy direct
access to `v1alpha1.Config`, next PR will refactor more to make it
return proper type.

There will be many more changes coming up.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2023-05-23 16:05:16 +04:00

83 lines
1.7 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package environment_test
import (
"testing"
"github.com/siderolabs/go-procfs/procfs"
"github.com/stretchr/testify/assert"
"github.com/siderolabs/talos/internal/pkg/environment"
"github.com/siderolabs/talos/pkg/machinery/config"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
)
func TestGet(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
cmdline string
cfg map[string]string
expected []string
}{
{
name: "empty",
},
{
name: "machine config only",
cfg: map[string]string{
"http_proxy": "http://proxy.example.com:8080",
},
expected: []string{
"http_proxy=http://proxy.example.com:8080",
},
},
{
name: "cmdline only",
cmdline: "talos.environment=foo=bar talos.environment=bar=baz",
expected: []string{
"foo=bar",
"bar=baz",
},
},
{
name: "cmdline and machine config",
cmdline: "talos.environment=foo=bar",
cfg: map[string]string{
"http_proxy": "http://proxy.example.com:8080",
},
expected: []string{
"foo=bar",
"http_proxy=http://proxy.example.com:8080",
},
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cmdline := procfs.NewCmdline(test.cmdline)
var cfg config.Config
if test.cfg != nil {
cfg = &v1alpha1.Config{
MachineConfig: &v1alpha1.MachineConfig{
MachineEnv: test.cfg,
},
}
}
result := environment.GetCmdline(cmdline, cfg)
assert.Equal(t, test.expected, result)
})
}
}