Skip to content

Commit

Permalink
test: add unit-test for machine config validation
Browse files Browse the repository at this point in the history
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 <smirnov.andrey@gmail.com>
  • Loading branch information
smira authored and talos-bot committed Mar 29, 2021
1 parent a610681 commit c38a161
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}

Expand Down
116 changes: 116 additions & 0 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_validation_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit c38a161

Please sign in to comment.