-
Notifications
You must be signed in to change notification settings - Fork 453
/
v1alpha1_stability_test.go
131 lines (102 loc) · 4.33 KB
/
v1alpha1_stability_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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 v1alpha1_test
import (
"fmt"
"os"
"testing"
"github.com/blang/semver/v4"
"github.com/siderolabs/gen/ensure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/siderolabs/talos/pkg/machinery/config"
"github.com/siderolabs/talos/pkg/machinery/config/configpatcher"
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
"github.com/siderolabs/talos/pkg/machinery/config/generate"
"github.com/siderolabs/talos/pkg/machinery/config/generate/secrets"
"github.com/siderolabs/talos/pkg/machinery/config/machine"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/gendata"
)
// TestConfigEncodingStability ensures that the encoding of a configuration is stable as we moved forward with the config format.
func TestConfigEncodingStability(t *testing.T) {
t.Parallel()
// flip this to generate missing configs
const generateMode = false
secretsBundle, err := secrets.LoadBundle("testdata/stability/secrets.yaml")
require.NoError(t, err)
versionContracts := []*config.VersionContract{
config.TalosVersion1_3,
config.TalosVersion1_4,
config.TalosVersion1_5,
config.TalosVersion1_6,
}
currentVersion := ensure.Value(semver.ParseTolerant(gendata.VersionTag))
currentVersion.Patch = 0
maxContractVersion := ensure.Value(semver.ParseTolerant(versionContracts[len(versionContracts)-1].String()))
require.True(t, currentVersion.LTE(maxContractVersion), "latest version contract is not tested")
for _, versionContract := range versionContracts {
versionContract := versionContract
t.Run(versionContract.String(), func(t *testing.T) {
t.Parallel()
t.Run("base", func(t *testing.T) {
t.Parallel()
in, err := generate.NewInput("base", "https://base:6443", "1.28.0",
generate.WithSecretsBundle(secretsBundle),
generate.WithVersionContract(versionContract),
)
require.NoError(t, err)
testConfigStability(t, in, versionContract, "base", generateMode)
})
t.Run("with overrides", func(t *testing.T) {
t.Parallel()
in, err := generate.NewInput("base", "https://base:6443", "1.28.0",
generate.WithSecretsBundle(secretsBundle),
generate.WithVersionContract(versionContract),
generate.WithAdditionalSubjectAltNames([]string{"foo", "bar"}),
generate.WithAllowSchedulingOnControlPlanes(true),
generate.WithDNSDomain("example.com"),
generate.WithInstallDisk("/dev/vda"),
generate.WithInstallExtraKernelArgs([]string{"foo=bar", "bar=baz"}),
generate.WithLocalAPIServerPort(5443),
generate.WithSysctls(map[string]string{"foo": "bar"}),
generate.WithClusterCNIConfig(&v1alpha1.CNIConfig{
CNIName: "custom",
CNIUrls: []string{"https://example.com/cni.yaml"},
}),
generate.WithRegistryMirror("ghcr.io", "https://ghcr.io.my-mirror.com"),
)
require.NoError(t, err)
patches, err := configpatcher.LoadPatches([]string{"@testdata/stability/patch.yaml"})
require.NoError(t, err)
testConfigStability(t, in, versionContract, "overrides", generateMode, patches...)
})
})
}
}
func testConfigStability(t *testing.T, in *generate.Input, versionContract *config.VersionContract, flavor string, generateMode bool, patches ...configpatcher.Patch) {
t.Helper()
for _, machineType := range []machine.Type{
machine.TypeControlPlane,
machine.TypeWorker,
} {
cfg, err := in.Config(machineType)
require.NoError(t, err)
cfgBytes, err := cfg.EncodeBytes(encoder.WithComments(encoder.CommentsDisabled))
require.NoError(t, err)
patched, err := configpatcher.Apply(configpatcher.WithBytes(cfgBytes), patches)
require.NoError(t, err)
cfgBytes, err = patched.Bytes()
require.NoError(t, err)
expectedPath := fmt.Sprintf("testdata/stability/%s/%s-%s.yaml", versionContract, flavor, machineType)
expectedBytes, err := os.ReadFile(expectedPath)
if os.IsNotExist(err) && generateMode {
require.NoError(t, os.WriteFile(expectedPath, cfgBytes, 0o644))
t.Logf("generated %s", expectedPath)
continue
}
require.NoError(t, err)
assert.Equal(t, string(expectedBytes), string(cfgBytes), "config encoding mismatch for %s", expectedPath)
}
}