|
| 1 | +/* |
| 2 | + Copyright 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 scope |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/klog/v2" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 28 | + |
| 29 | + infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" |
| 30 | + expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2" |
| 31 | + "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger" |
| 32 | + "sigs.k8s.io/cluster-api-provider-aws/v2/util/system" |
| 33 | +) |
| 34 | + |
| 35 | +func TestNewROSANetworkScope(t *testing.T) { |
| 36 | + g := NewGomegaWithT(t) |
| 37 | + |
| 38 | + scheme := runtime.NewScheme() |
| 39 | + corev1.AddToScheme(scheme) |
| 40 | + infrav1.AddToScheme(scheme) |
| 41 | + expinfrav1.AddToScheme(scheme) |
| 42 | + |
| 43 | + clusterControllerIdentity := &infrav1.AWSClusterControllerIdentity{ |
| 44 | + TypeMeta: metav1.TypeMeta{ |
| 45 | + Kind: string(infrav1.ControllerIdentityKind), |
| 46 | + }, |
| 47 | + ObjectMeta: metav1.ObjectMeta{ |
| 48 | + Name: "default", |
| 49 | + }, |
| 50 | + Spec: infrav1.AWSClusterControllerIdentitySpec{ |
| 51 | + AWSClusterIdentitySpec: infrav1.AWSClusterIdentitySpec{ |
| 52 | + AllowedNamespaces: &infrav1.AllowedNamespaces{}, |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + staticSecret := &corev1.Secret{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{ |
| 59 | + Name: "static-secret", |
| 60 | + Namespace: system.GetManagerNamespace(), |
| 61 | + }, |
| 62 | + Data: map[string][]byte{ |
| 63 | + "AccessKeyID": []byte("access-key-id"), |
| 64 | + "SecretAccessKey": []byte("secret-access-key"), |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + clusterStaticIdentity := &infrav1.AWSClusterStaticIdentity{ |
| 69 | + ObjectMeta: metav1.ObjectMeta{ |
| 70 | + Name: "cluster-static-identity", |
| 71 | + }, |
| 72 | + Spec: infrav1.AWSClusterStaticIdentitySpec{ |
| 73 | + SecretRef: "static-secret", |
| 74 | + AWSClusterIdentitySpec: infrav1.AWSClusterIdentitySpec{ |
| 75 | + AllowedNamespaces: &infrav1.AllowedNamespaces{}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterControllerIdentity, staticSecret, clusterStaticIdentity).Build() |
| 81 | + |
| 82 | + rosaNetwork := expinfrav1.ROSANetwork{ |
| 83 | + TypeMeta: metav1.TypeMeta{ |
| 84 | + Kind: "ROSANetwork", |
| 85 | + APIVersion: "v1beta2", |
| 86 | + }, |
| 87 | + ObjectMeta: metav1.ObjectMeta{ |
| 88 | + Name: "test-rosa-net", |
| 89 | + Namespace: "test-namespace", |
| 90 | + }, |
| 91 | + Spec: expinfrav1.ROSANetworkSpec{ |
| 92 | + IdentityRef: &infrav1.AWSIdentityReference{ |
| 93 | + Name: "default", |
| 94 | + Kind: "AWSClusterControllerIdentity", |
| 95 | + }, |
| 96 | + }, |
| 97 | + Status: expinfrav1.ROSANetworkStatus{}, |
| 98 | + } |
| 99 | + |
| 100 | + rosaNetScopeParams := ROSANetworkScopeParams{ |
| 101 | + Client: fakeClient, |
| 102 | + ControllerName: "test-rosanet-controller", |
| 103 | + Endpoints: []ServiceEndpoint{}, |
| 104 | + Logger: logger.NewLogger(klog.Background()), |
| 105 | + ROSANetwork: &rosaNetwork, |
| 106 | + } |
| 107 | + |
| 108 | + rosaNetScope, err := NewROSANetworkScope(rosaNetScopeParams) |
| 109 | + g.Expect(err).NotTo(HaveOccurred()) |
| 110 | + g.Expect(rosaNetScope.ControllerName()).To(Equal("test-rosanet-controller")) |
| 111 | + g.Expect(rosaNetScope.InfraCluster()).To(Equal(&rosaNetwork)) |
| 112 | + g.Expect(rosaNetScope.InfraClusterName()).To(Equal("test-rosa-net")) |
| 113 | + g.Expect(rosaNetScope.Namespace()).To(Equal("test-namespace")) |
| 114 | + g.Expect(rosaNetScope.IdentityRef()).To(Equal(rosaNetwork.Spec.IdentityRef)) |
| 115 | + g.Expect(rosaNetScope.Session()).ToNot(BeNil()) |
| 116 | + |
| 117 | + // AWSClusterStaticIdentity |
| 118 | + rosaNetwork.Spec.IdentityRef.Name = "cluster-static-identity" |
| 119 | + rosaNetwork.Spec.IdentityRef.Kind = "AWSClusterStaticIdentity" |
| 120 | + rosaNetScope, err = NewROSANetworkScope(rosaNetScopeParams) |
| 121 | + g.Expect(err).NotTo(HaveOccurred()) |
| 122 | + g.Expect(rosaNetScope.Session()).ToNot(BeNil()) |
| 123 | +} |
0 commit comments