Skip to content

Commit

Permalink
Add tests for MatchesTemplateClonedFrom filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Warren Fernandes committed Jul 13, 2020
1 parent d6733bd commit c626454
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions controlplane/kubeadm/internal/machinefilters/machine_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ limitations under the License.
package machinefilters_test

import (
"context"
"testing"
"time"

. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/machinefilters"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func falseFilter(_ *clusterv1.Machine) bool {
Expand Down Expand Up @@ -189,6 +196,7 @@ func TestMatchesKubernetesVersion(t *testing.T) {
g := NewWithT(t)
g.Expect(machinefilters.MatchesKubernetesVersion("some_ver")(nil)).To(BeFalse())
})

t.Run("nil machine.Spec.Version returns false", func(t *testing.T) {
g := NewWithT(t)
machine := &clusterv1.Machine{
Expand All @@ -198,6 +206,7 @@ func TestMatchesKubernetesVersion(t *testing.T) {
}
g.Expect(machinefilters.MatchesKubernetesVersion("some_ver")(machine)).To(BeFalse())
})

t.Run("machine.Spec.Version returns true if matches", func(t *testing.T) {
g := NewWithT(t)
kversion := "some_ver"
Expand All @@ -208,6 +217,7 @@ func TestMatchesKubernetesVersion(t *testing.T) {
}
g.Expect(machinefilters.MatchesKubernetesVersion("some_ver")(machine)).To(BeTrue())
})

t.Run("machine.Spec.Version returns false if does not match", func(t *testing.T) {
g := NewWithT(t)
kversion := "some_ver_2"
Expand All @@ -218,5 +228,116 @@ func TestMatchesKubernetesVersion(t *testing.T) {
}
g.Expect(machinefilters.MatchesKubernetesVersion("some_ver")(machine)).To(BeFalse())
})
}

func TestMatchesTemplateClonedFrom(t *testing.T) {
t.Run("nil machine returns false", func(t *testing.T) {
g := NewWithT(t)
g.Expect(
machinefilters.MatchesTemplateClonedFrom(context.TODO(), nil, nil)(nil),
).To(BeFalse())
})

t.Run("returns true if machine not found", func(t *testing.T) {
g := NewWithT(t)
client := fake.NewFakeClient()
kcp := &controlplanev1.KubeadmControlPlane{}
machine := &clusterv1.Machine{
Spec: clusterv1.MachineSpec{
InfrastructureRef: corev1.ObjectReference{
Kind: "KubeadmConfig",
Namespace: "default",
Name: "test",
APIVersion: bootstrapv1.GroupVersion.String(),
},
},
}
g.Expect(
machinefilters.MatchesTemplateClonedFrom(context.TODO(), client, kcp)(machine),
).To(BeTrue())
})

t.Run("annotation", func(t *testing.T) {
kcp := &controlplanev1.KubeadmControlPlane{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
InfrastructureTemplate: corev1.ObjectReference{
Kind: "GenericMachineTemplate",
Namespace: "default",
Name: "infra-foo",
APIVersion: "generic.io/v1",
},
},
}
machine := &clusterv1.Machine{
Spec: clusterv1.MachineSpec{
InfrastructureRef: corev1.ObjectReference{
APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha3",
Kind: "InfrastructureMachine",
Name: "infra-config1",
Namespace: "default",
},
},
}
tests := []struct {
name string
annotations map[string]interface{}
expectMatch bool
}{
{
name: "returns true if annotations don't exist",
annotations: map[string]interface{}{},
expectMatch: true,
},
{
name: "returns false if annotations don't match anything",
annotations: map[string]interface{}{
clusterv1.TemplateClonedFromNameAnnotation: "barfoo1",
clusterv1.TemplateClonedFromGroupKindAnnotation: "barfoo2",
},
expectMatch: false,
},
{
name: "returns false if TemplateClonedFromNameAnnotation matches but GroupKind doesn't",
annotations: map[string]interface{}{
clusterv1.TemplateClonedFromNameAnnotation: "infra-foo",
clusterv1.TemplateClonedFromGroupKindAnnotation: "barfoo2",
},
expectMatch: false,
},
{
name: "returns true if both annotations match",
annotations: map[string]interface{}{
clusterv1.TemplateClonedFromNameAnnotation: "infra-foo",
clusterv1.TemplateClonedFromGroupKindAnnotation: "GenericMachineTemplate.generic.io",
},
expectMatch: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
infraConfig := unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "InfrastructureMachine",
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha3",
"metadata": map[string]interface{}{
"name": "infra-config1",
"namespace": "default",
"annotations": tt.annotations,
},
},
}
scheme := runtime.NewScheme()
client := fake.NewFakeClientWithScheme(scheme, &infraConfig)

g.Expect(
machinefilters.MatchesTemplateClonedFrom(context.TODO(), client, kcp)(machine),
).To(Equal(tt.expectMatch))
})
}
})
}

0 comments on commit c626454

Please sign in to comment.