|
| 1 | +/* |
| 2 | +Copyright 2024 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 loadbalancers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net/http" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud" |
| 25 | + "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" |
| 26 | + "github.com/google/go-cmp/cmp" |
| 27 | + "google.golang.org/api/compute/v1" |
| 28 | + "google.golang.org/api/googleapi" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/client-go/kubernetes/scheme" |
| 31 | + infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1" |
| 32 | + "sigs.k8s.io/cluster-api-provider-gcp/cloud/scope" |
| 33 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 35 | +) |
| 36 | + |
| 37 | +func init() { |
| 38 | + _ = clusterv1.AddToScheme(scheme.Scheme) |
| 39 | + _ = infrav1.AddToScheme(scheme.Scheme) |
| 40 | +} |
| 41 | + |
| 42 | +func getBaseClusterScope() (*scope.ClusterScope, error) { |
| 43 | + fakec := fake.NewClientBuilder(). |
| 44 | + WithScheme(scheme.Scheme). |
| 45 | + Build() |
| 46 | + |
| 47 | + fakeCluster := &clusterv1.Cluster{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "my-cluster", |
| 50 | + Namespace: "default", |
| 51 | + }, |
| 52 | + Spec: clusterv1.ClusterSpec{}, |
| 53 | + } |
| 54 | + |
| 55 | + fakeGCPCluster := &infrav1.GCPCluster{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Name: "my-cluster", |
| 58 | + Namespace: "default", |
| 59 | + }, |
| 60 | + Spec: infrav1.GCPClusterSpec{ |
| 61 | + Project: "my-proj", |
| 62 | + Region: "us-central1", |
| 63 | + }, |
| 64 | + Status: infrav1.GCPClusterStatus{ |
| 65 | + FailureDomains: clusterv1.FailureDomains{ |
| 66 | + "us-central1-a": clusterv1.FailureDomainSpec{ControlPlane: true}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + clusterScope, err := scope.NewClusterScope(context.TODO(), scope.ClusterScopeParams{ |
| 71 | + Client: fakec, |
| 72 | + Cluster: fakeCluster, |
| 73 | + GCPCluster: fakeGCPCluster, |
| 74 | + GCPServices: scope.GCPServices{ |
| 75 | + Compute: &compute.Service{}, |
| 76 | + }, |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + return clusterScope, nil |
| 83 | +} |
| 84 | + |
| 85 | +func TestService_createOrGetInstanceGroup(t *testing.T) { |
| 86 | + tests := []struct { |
| 87 | + name string |
| 88 | + scope func(s *scope.ClusterScope) Scope |
| 89 | + mockInstanceGroup *cloud.MockInstanceGroups |
| 90 | + want []*compute.InstanceGroup |
| 91 | + wantErr bool |
| 92 | + }{ |
| 93 | + { |
| 94 | + name: "error getting instanceGroup with non 404 error code (should return an error)", |
| 95 | + scope: func(s *scope.ClusterScope) Scope { return s }, |
| 96 | + mockInstanceGroup: &cloud.MockInstanceGroups{ |
| 97 | + ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"}, |
| 98 | + Objects: map[meta.Key]*cloud.MockInstanceGroupsObj{}, |
| 99 | + GetHook: func(_ context.Context, _ *meta.Key, _ *cloud.MockInstanceGroups) (bool, *compute.InstanceGroup, error) { |
| 100 | + return true, &compute.InstanceGroup{}, &googleapi.Error{Code: http.StatusBadRequest} |
| 101 | + }, |
| 102 | + }, |
| 103 | + want: []*compute.InstanceGroup{}, |
| 104 | + wantErr: true, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "instanceGroup name is overridden (should create instanceGroup)", |
| 108 | + scope: func(s *scope.ClusterScope) Scope { |
| 109 | + var tagOverride = "master" |
| 110 | + s.GCPCluster.Spec.LoadBalancer = infrav1.LoadBalancerSpec{ |
| 111 | + APIServerInstanceGroupTagOverride: &tagOverride, |
| 112 | + } |
| 113 | + return s |
| 114 | + }, |
| 115 | + mockInstanceGroup: &cloud.MockInstanceGroups{ |
| 116 | + ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"}, |
| 117 | + Objects: map[meta.Key]*cloud.MockInstanceGroupsObj{}, |
| 118 | + }, |
| 119 | + want: []*compute.InstanceGroup{ |
| 120 | + { |
| 121 | + Name: "my-cluster-master-us-central1-a", |
| 122 | + NamedPorts: []*compute.NamedPort{{Name: "apiserver", Port: 6443}}, |
| 123 | + SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/zones/us-central1-a/instanceGroups/my-cluster-master-us-central1-a", |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "instanceGroup does not exist (should create instanceGroup)", |
| 129 | + scope: func(s *scope.ClusterScope) Scope { return s }, |
| 130 | + mockInstanceGroup: &cloud.MockInstanceGroups{ |
| 131 | + ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"}, |
| 132 | + Objects: map[meta.Key]*cloud.MockInstanceGroupsObj{}, |
| 133 | + }, |
| 134 | + want: []*compute.InstanceGroup{ |
| 135 | + { |
| 136 | + Name: "my-cluster-apiserver-us-central1-a", |
| 137 | + NamedPorts: []*compute.NamedPort{{Name: "apiserver", Port: 6443}}, |
| 138 | + SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/zones/us-central1-a/instanceGroups/my-cluster-apiserver-us-central1-a", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + for _, tt := range tests { |
| 144 | + t.Run(tt.name, func(t *testing.T) { |
| 145 | + ctx := context.TODO() |
| 146 | + |
| 147 | + clusterScope, err := getBaseClusterScope() |
| 148 | + if err != nil { |
| 149 | + t.Fatal(err) |
| 150 | + } |
| 151 | + s := New(tt.scope(clusterScope)) |
| 152 | + s.instancegroups = tt.mockInstanceGroup |
| 153 | + got, err := s.createOrGetInstanceGroups(ctx) |
| 154 | + if (err != nil) != tt.wantErr { |
| 155 | + t.Errorf("Service s.createOrGetInstanceGroups() error = %v, wantErr %v", err, tt.wantErr) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + if d := cmp.Diff(tt.want, got); d != "" { |
| 160 | + t.Errorf("Service s.createOrGetInstanceGroups() mismatch (-want +got):\n%s", d) |
| 161 | + } |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments