Skip to content

Commit

Permalink
refactor: move tests back into handler packages
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoshkin committed Oct 16, 2023
1 parent 12bba4e commit f14289a
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package clusterconfig
package ami

import (
"testing"
Expand All @@ -10,6 +10,7 @@ import (

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

Expand All @@ -19,39 +20,7 @@ func TestVariableValidation(t *testing.T) {
clusterconfig.MetaVariableName,
ptr.To(v1alpha1.ClusterConfigSpec{AWS: &v1alpha1.AWSSpec{}}.VariableSchema()),
true,
NewVariable,
capitest.VariableTestDef{
Name: "specified region",
Vals: v1alpha1.ClusterConfigSpec{
AWS: &v1alpha1.AWSSpec{
Region: ptr.To(v1alpha1.Region("a-specified-region")),
},
},
},
capitest.VariableTestDef{
Name: "specified IAM instance profile",
Vals: v1alpha1.ClusterConfigSpec{
ControlPlane: &v1alpha1.NodeConfigSpec{
AWS: &v1alpha1.AWSNodeSpec{
IAMInstanceProfile: ptr.To(
v1alpha1.IAMInstanceProfile(
"control-plane.cluster-api-provider-aws.sigs.k8s.io",
),
),
},
},
},
},
capitest.VariableTestDef{
Name: "specified instance type",
Vals: v1alpha1.ClusterConfigSpec{
ControlPlane: &v1alpha1.NodeConfigSpec{
AWS: &v1alpha1.AWSNodeSpec{
InstanceType: ptr.To(v1alpha1.InstanceType("m5.small")),
},
},
},
},
awsclusterconfig.NewVariable,
capitest.VariableTestDef{
Name: "AMI specification",
Vals: v1alpha1.ClusterConfigSpec{
Expand Down
42 changes: 42 additions & 0 deletions pkg/handlers/aws/mutation/iaminstanceprofile/variables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package iaminstanceprofile

import (
"testing"

"k8s.io/utils/ptr"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

func TestVariableValidation(t *testing.T) {
capitest.ValidateDiscoverVariables(
t,
clusterconfig.MetaVariableName,
ptr.To(v1alpha1.ClusterConfigSpec{AWS: &v1alpha1.AWSSpec{}}.VariableSchema()),
true,
awsclusterconfig.NewVariable,
capitest.VariableTestDef{
Name: "AMI specification",
Vals: v1alpha1.ClusterConfigSpec{
ControlPlane: &v1alpha1.NodeConfigSpec{
AWS: &v1alpha1.AWSNodeSpec{
AMISpec: &v1alpha1.AMISpec{
ID: "ami-1234",
Lookup: &v1alpha1.AMILookup{
Format: "capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*",
BaseOS: "rhel-8.4",
Org: "12345678",
},
},
},
},
},
},
)
}
35 changes: 35 additions & 0 deletions pkg/handlers/aws/mutation/instancetype/variables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package instancetype

import (
"testing"

"k8s.io/utils/ptr"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

func TestVariableValidation(t *testing.T) {
capitest.ValidateDiscoverVariables(
t,
clusterconfig.MetaVariableName,
ptr.To(v1alpha1.ClusterConfigSpec{AWS: &v1alpha1.AWSSpec{}}.VariableSchema()),
true,
awsclusterconfig.NewVariable,
capitest.VariableTestDef{
Name: "specified instance type",
Vals: v1alpha1.ClusterConfigSpec{
ControlPlane: &v1alpha1.NodeConfigSpec{
AWS: &v1alpha1.AWSNodeSpec{
InstanceType: ptr.To(v1alpha1.InstanceType("m5.small")),
},
},
},
},
)
}
33 changes: 33 additions & 0 deletions pkg/handlers/aws/mutation/region/variables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package region

import (
"testing"

"k8s.io/utils/ptr"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

func TestVariableValidation(t *testing.T) {
capitest.ValidateDiscoverVariables(
t,
clusterconfig.MetaVariableName,
ptr.To(v1alpha1.ClusterConfigSpec{AWS: &v1alpha1.AWSSpec{}}.VariableSchema()),
true,
awsclusterconfig.NewVariable,
capitest.VariableTestDef{
Name: "specified region",
Vals: v1alpha1.ClusterConfigSpec{
AWS: &v1alpha1.AWSSpec{
Region: ptr.To(v1alpha1.Region("a-specified-region")),
},
},
},
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package clusterconfig
package customimage

import (
"testing"
Expand All @@ -10,6 +10,7 @@ import (

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
dockerclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

Expand All @@ -19,7 +20,7 @@ func TestVariableValidation(t *testing.T) {
clusterconfig.MetaVariableName,
ptr.To(v1alpha1.ClusterConfigSpec{Docker: &v1alpha1.DockerSpec{}}.VariableSchema()),
true,
NewVariable,
dockerclusterconfig.NewVariable,
capitest.VariableTestDef{
Name: "valid",
Vals: v1alpha1.ClusterConfigSpec{
Expand Down
136 changes: 0 additions & 136 deletions pkg/handlers/generic/clusterconfig/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package clusterconfig
import (
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
Expand All @@ -20,141 +19,6 @@ func TestVariableValidation(t *testing.T) {
ptr.To(v1alpha1.GenericClusterConfig{}.VariableSchema()),
false,
NewVariable,
// CNI
capitest.VariableTestDef{
Name: "CNI: set with valid provider",
Vals: v1alpha1.GenericClusterConfig{
Addons: &v1alpha1.Addons{
CNI: &v1alpha1.CNI{
Provider: v1alpha1.CNIProviderCalico,
},
},
},
},
capitest.VariableTestDef{
Name: "set with invalid provider",
Vals: v1alpha1.GenericClusterConfig{
Addons: &v1alpha1.Addons{
CNI: &v1alpha1.CNI{
Provider: "invalid-provider",
},
},
},
ExpectError: true,
},
// HTTPProxy
capitest.VariableTestDef{
Name: "HTTPProxy: valid proxy config",
Vals: v1alpha1.GenericClusterConfig{
Proxy: &v1alpha1.HTTPProxy{
HTTP: "http://a.b.c.example.com",
HTTPS: "https://a.b.c.example.com",
AdditionalNo: []string{"d.e.f.example.com"},
},
},
},
// ExtraAPIServerCertSANs
capitest.VariableTestDef{
Name: "ExtraAPIServerCertSANs: single valid SAN",
Vals: v1alpha1.GenericClusterConfig{
ExtraAPIServerCertSANs: v1alpha1.ExtraAPIServerCertSANs{"a.b.c.example.com"},
},
},
capitest.VariableTestDef{
Name: "ExtraAPIServerCertSANs: single invalid SAN",
Vals: v1alpha1.GenericClusterConfig{
ExtraAPIServerCertSANs: v1alpha1.ExtraAPIServerCertSANs{"invalid:san"},
},
ExpectError: true,
},
capitest.VariableTestDef{
Name: "ExtraAPIServerCertSANs: duplicate valid SANs",
Vals: v1alpha1.GenericClusterConfig{
ExtraAPIServerCertSANs: v1alpha1.ExtraAPIServerCertSANs{
"a.b.c.example.com",
"a.b.c.example.com",
},
},
ExpectError: true,
},
// KubernetesImageRepository
capitest.VariableTestDef{
Name: "KubernetesImageRepository: set",
Vals: v1alpha1.GenericClusterConfig{
KubernetesImageRepository: ptr.To(
v1alpha1.KubernetesImageRepository("my-registry.io/my-org/my-repo"),
),
},
},
// Etcd
capitest.VariableTestDef{
Name: "Etcd: unset",
Vals: v1alpha1.GenericClusterConfig{
Etcd: &v1alpha1.Etcd{},
},
},
capitest.VariableTestDef{
Name: "Etcd: set with valid image values",
Vals: v1alpha1.GenericClusterConfig{
Etcd: &v1alpha1.Etcd{
Image: &v1alpha1.Image{
Repository: "my-registry.io/my-org/my-repo",
Tag: "v3.5.99_custom.0",
},
},
},
},
capitest.VariableTestDef{
Name: "Etcd: set with invalid image repository",
Vals: v1alpha1.GenericClusterConfig{
Etcd: &v1alpha1.Etcd{
Image: &v1alpha1.Image{
Repository: "https://this.should.not.have.a.scheme",
},
},
},
ExpectError: true,
},
capitest.VariableTestDef{
Name: "Etcd: set with invalid image tag",
Vals: v1alpha1.GenericClusterConfig{
Etcd: &v1alpha1.Etcd{
Image: &v1alpha1.Image{
Tag: "this:is:not:a:valid:tag",
},
},
},
ExpectError: true,
},
// ImageRegistryCredentials
capitest.VariableTestDef{
Name: "ImageRegistryCredentials: without a Secret",
Vals: v1alpha1.GenericClusterConfig{
ImageRegistries: v1alpha1.ImageRegistries{
ImageRegistryCredentials: []v1alpha1.ImageRegistryCredentialsResource{
{
URL: "http://a.b.c.example.com",
},
},
},
},
},
capitest.VariableTestDef{
Name: "ImageRegistryCredentials: with a Secret",
Vals: v1alpha1.GenericClusterConfig{
ImageRegistries: v1alpha1.ImageRegistries{
ImageRegistryCredentials: []v1alpha1.ImageRegistryCredentialsResource{
{
URL: "http://a.b.c.example.com",
Secret: &corev1.ObjectReference{
Name: "a.b.c.example.com-creds",
},
},
},
},
},
},
// Combined
capitest.VariableTestDef{
Name: "valid config",
Vals: v1alpha1.GenericClusterConfig{
Expand Down
45 changes: 45 additions & 0 deletions pkg/handlers/generic/lifecycle/cni/variables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package cni

import (
"testing"

"k8s.io/utils/ptr"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

func TestVariableValidation(t *testing.T) {
capitest.ValidateDiscoverVariables(
t,
clusterconfig.MetaVariableName,
ptr.To(v1alpha1.GenericClusterConfig{}.VariableSchema()),
false,
clusterconfig.NewVariable,
capitest.VariableTestDef{
Name: "set with valid provider",
Vals: v1alpha1.GenericClusterConfig{
Addons: &v1alpha1.Addons{
CNI: &v1alpha1.CNI{
Provider: v1alpha1.CNIProviderCalico,
},
},
},
},
capitest.VariableTestDef{
Name: "set with invalid provider",
Vals: v1alpha1.GenericClusterConfig{
Addons: &v1alpha1.Addons{
CNI: &v1alpha1.CNI{
Provider: "invalid-provider",
},
},
},
ExpectError: true,
},
)
}
Loading

0 comments on commit f14289a

Please sign in to comment.