From c38a161ade34f00f7af52d9ae047d7936246e7f0 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Mon, 29 Mar 2021 16:42:41 +0300 Subject: [PATCH] test: add unit-test for machine config validation Follow-up for #3383 I added couple of first tests, we should add more as we go through this code. Even with those tests, I found and fixed two more panics. Signed-off-by: Andrey Smirnov --- .../types/v1alpha1/v1alpha1_validation.go | 10 +- .../v1alpha1/v1alpha1_validation_test.go | 116 ++++++++++++++++++ 2 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 pkg/machinery/config/types/v1alpha1/v1alpha1_validation_test.go diff --git a/pkg/machinery/config/types/v1alpha1/v1alpha1_validation.go b/pkg/machinery/config/types/v1alpha1/v1alpha1_validation.go index 6e790a6bee..229bee4825 100644 --- a/pkg/machinery/config/types/v1alpha1/v1alpha1_validation.go +++ b/pkg/machinery/config/types/v1alpha1/v1alpha1_validation.go @@ -64,6 +64,8 @@ func (c *Config) Validate(mode config.RuntimeMode, options ...config.ValidationO if c.MachineConfig == nil { result = multierror.Append(result, errors.New("machine instructions are required")) + + return result.ErrorOrNil() } if err := c.ClusterConfig.Validate(); err != nil { @@ -133,10 +135,6 @@ func (c *Config) Validate(mode config.RuntimeMode, options ...config.ValidationO } } - if !valid.IsDNSName(c.ClusterConfig.ClusterNetwork.DNSDomain) { - result = multierror.Append(result, fmt.Errorf("%q is not a valid DNS name", c.ClusterConfig.ClusterNetwork.DNSDomain)) - } - for _, label := range []string{constants.EphemeralPartitionLabel, constants.StatePartitionLabel} { encryptionConfig := c.MachineConfig.SystemDiskEncryption().Get(label) if encryptionConfig != nil { @@ -178,6 +176,10 @@ func (c *ClusterConfig) Validate() error { result = multierror.Append(result, fmt.Errorf("invalid controlplane endpoint: %w", err)) } + if c.ClusterNetwork != nil && !valid.IsDNSName(c.ClusterNetwork.DNSDomain) { + result = multierror.Append(result, fmt.Errorf("%q is not a valid DNS name", c.ClusterNetwork.DNSDomain)) + } + return result.ErrorOrNil() } diff --git a/pkg/machinery/config/types/v1alpha1/v1alpha1_validation_test.go b/pkg/machinery/config/types/v1alpha1/v1alpha1_validation_test.go new file mode 100644 index 0000000000..0315943103 --- /dev/null +++ b/pkg/machinery/config/types/v1alpha1/v1alpha1_validation_test.go @@ -0,0 +1,116 @@ +// 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" + "net/url" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/talos-systems/talos/pkg/machinery/config" + "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1" +) + +type runtimeMode struct { + requiresInstall bool +} + +func (m runtimeMode) String() string { + return fmt.Sprintf("runtimeMode(%v)", m.requiresInstall) +} + +func (m runtimeMode) RequiresInstall() bool { + return m.requiresInstall +} + +func TestValidate(t *testing.T) { + endpointURL, err := url.Parse("https://localhost:6443/") + require.NoError(t, err) + + for _, test := range []struct { + name string + config *v1alpha1.Config + requiresInstall bool + expectedError string + }{ + { + name: "NoMachine", + config: &v1alpha1.Config{ + ConfigVersion: "v1alpha1", + }, + expectedError: `1 error occurred: + * machine instructions are required + +`, + }, + { + name: "NoMachineInstall", + config: &v1alpha1.Config{ + ConfigVersion: "v1alpha1", + MachineConfig: &v1alpha1.MachineConfig{}, + ClusterConfig: &v1alpha1.ClusterConfig{ + ControlPlane: &v1alpha1.ControlPlaneConfig{ + Endpoint: &v1alpha1.Endpoint{ + endpointURL, + }, + }, + }, + }, + }, + { + name: "NoMachineInstallRequired", + config: &v1alpha1.Config{ + ConfigVersion: "v1alpha1", + MachineConfig: &v1alpha1.MachineConfig{}, + ClusterConfig: &v1alpha1.ClusterConfig{ + ControlPlane: &v1alpha1.ControlPlaneConfig{ + Endpoint: &v1alpha1.Endpoint{ + endpointURL, + }, + }, + }, + }, + requiresInstall: true, + expectedError: `1 error occurred: + * install instructions are required in "runtimeMode(true)" mode + +`, + }, + { + name: "MachineInstallDisk", + config: &v1alpha1.Config{ + ConfigVersion: "v1alpha1", + MachineConfig: &v1alpha1.MachineConfig{ + MachineInstall: &v1alpha1.InstallConfig{ + InstallDisk: "/dev/vda", + }, + }, + ClusterConfig: &v1alpha1.ClusterConfig{ + ControlPlane: &v1alpha1.ControlPlaneConfig{ + Endpoint: &v1alpha1.Endpoint{ + endpointURL, + }, + }, + }, + }, + requiresInstall: true, + }, + } { + test := test + + t.Run(test.name, func(t *testing.T) { + err := test.config.Validate(runtimeMode{test.requiresInstall}, config.WithLocal()) + + if test.expectedError == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, test.expectedError) + } + }) + } +}