|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha5 |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/onsi/gomega" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + runtime "k8s.io/apimachinery/pkg/runtime" |
| 25 | + ctrlconversion "sigs.k8s.io/controller-runtime/pkg/conversion" |
| 26 | + |
| 27 | + infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha6" |
| 28 | +) |
| 29 | + |
| 30 | +func TestConvertFrom(t *testing.T) { |
| 31 | + g := gomega.NewWithT(t) |
| 32 | + scheme := runtime.NewScheme() |
| 33 | + g.Expect(AddToScheme(scheme)).To(gomega.Succeed()) |
| 34 | + g.Expect(infrav1.AddToScheme(scheme)).To(gomega.Succeed()) |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + spoke ctrlconversion.Convertible |
| 39 | + hub ctrlconversion.Hub |
| 40 | + want ctrlconversion.Convertible |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "conversion must have conversion-data annotation", |
| 44 | + spoke: &OpenStackCluster{}, |
| 45 | + hub: &infrav1.OpenStackCluster{ |
| 46 | + Spec: infrav1.OpenStackClusterSpec{}, |
| 47 | + }, |
| 48 | + want: &OpenStackCluster{ |
| 49 | + Spec: OpenStackClusterSpec{}, |
| 50 | + ObjectMeta: metav1.ObjectMeta{ |
| 51 | + Annotations: map[string]string{ |
| 52 | + "cluster.x-k8s.io/conversion-data": "{\"spec\":{\"allowAllInClusterTraffic\":false,\"apiServerLoadBalancer\":{},\"cloudName\":\"\",\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"managedSecurityGroups\":false,\"network\":{},\"subnet\":{}},\"status\":{\"ready\":false}}", |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "conversion must have conversion-data annotation", |
| 59 | + spoke: &OpenStackClusterTemplate{}, |
| 60 | + hub: &infrav1.OpenStackClusterTemplate{ |
| 61 | + Spec: infrav1.OpenStackClusterTemplateSpec{}, |
| 62 | + }, |
| 63 | + want: &OpenStackClusterTemplate{ |
| 64 | + Spec: OpenStackClusterTemplateSpec{}, |
| 65 | + ObjectMeta: metav1.ObjectMeta{ |
| 66 | + Annotations: map[string]string{ |
| 67 | + "cluster.x-k8s.io/conversion-data": "{\"spec\":{\"template\":{\"spec\":{\"allowAllInClusterTraffic\":false,\"apiServerLoadBalancer\":{},\"cloudName\":\"\",\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"managedSecurityGroups\":false,\"network\":{},\"subnet\":{}}}}}", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "conversion must have conversion-data annotation", |
| 74 | + spoke: &OpenStackMachine{}, |
| 75 | + hub: &infrav1.OpenStackMachine{ |
| 76 | + Spec: infrav1.OpenStackMachineSpec{}, |
| 77 | + }, |
| 78 | + want: &OpenStackMachine{ |
| 79 | + Spec: OpenStackMachineSpec{}, |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Annotations: map[string]string{ |
| 82 | + "cluster.x-k8s.io/conversion-data": "{\"spec\":{\"cloudName\":\"\",\"flavor\":\"\"},\"status\":{\"ready\":false}}", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "conversion must have conversion-data annotation", |
| 89 | + spoke: &OpenStackMachineTemplate{}, |
| 90 | + hub: &infrav1.OpenStackMachineTemplate{ |
| 91 | + Spec: infrav1.OpenStackMachineTemplateSpec{}, |
| 92 | + }, |
| 93 | + want: &OpenStackMachineTemplate{ |
| 94 | + Spec: OpenStackMachineTemplateSpec{}, |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Annotations: map[string]string{ |
| 97 | + "cluster.x-k8s.io/conversion-data": "{\"spec\":{\"template\":{\"spec\":{\"cloudName\":\"\",\"flavor\":\"\"}}}}", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tt := range tests { |
| 105 | + t.Run(tt.name, func(t *testing.T) { |
| 106 | + err := tt.spoke.ConvertFrom(tt.hub) |
| 107 | + g.Expect(err).NotTo(gomega.HaveOccurred()) |
| 108 | + g.Expect(tt.spoke).To(gomega.Equal(tt.want)) |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
0 commit comments