|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/annotations" |
| 8 | + |
| 9 | + awssdk "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 17 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 18 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/equality" |
| 19 | + testclient "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 20 | +) |
| 21 | + |
| 22 | +var svc1 = &corev1.Service{ |
| 23 | + ObjectMeta: metav1.ObjectMeta{ |
| 24 | + Namespace: "awesome-ns", |
| 25 | + Name: "svc-1", |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +var svc2 = &corev1.Service{ |
| 30 | + ObjectMeta: metav1.ObjectMeta{ |
| 31 | + Namespace: "awesome-ns", |
| 32 | + Name: "svc-2", |
| 33 | + }, |
| 34 | +} |
| 35 | + |
| 36 | +func Test_defaultEnhancedBackendBuilder_Build(t *testing.T) { |
| 37 | + portTCP := intstr.FromString("tcp") |
| 38 | + |
| 39 | + type env struct { |
| 40 | + svcs []*corev1.Service |
| 41 | + } |
| 42 | + type args struct { |
| 43 | + svc *corev1.Service |
| 44 | + action Action |
| 45 | + backendServices map[types.NamespacedName]*corev1.Service |
| 46 | + } |
| 47 | + |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + env env |
| 51 | + args args |
| 52 | + want EnhancedBackend |
| 53 | + wantBackendServices map[types.NamespacedName]*corev1.Service |
| 54 | + wantErr error |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "builds enhanced backend", |
| 58 | + env: env{svcs: []*corev1.Service{svc1}}, |
| 59 | + args: args{ |
| 60 | + svc: svc1, |
| 61 | + action: Action{ |
| 62 | + Type: ActionTypeForward, |
| 63 | + ForwardConfig: &ForwardActionConfig{ |
| 64 | + TargetGroups: []TargetGroupTuple{ |
| 65 | + { |
| 66 | + ServiceName: awssdk.String("svc-1"), |
| 67 | + ServicePort: &portTCP, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + backendServices: map[types.NamespacedName]*corev1.Service{}, |
| 73 | + }, |
| 74 | + wantBackendServices: map[types.NamespacedName]*corev1.Service{ |
| 75 | + types.NamespacedName{Namespace: "awesome-ns", Name: "svc-1"}: svc1, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + ctx := context.Background() |
| 82 | + k8sSchema := runtime.NewScheme() |
| 83 | + clientgoscheme.AddToScheme(k8sSchema) |
| 84 | + k8sClient := testclient.NewClientBuilder().WithScheme(k8sSchema).Build() |
| 85 | + for _, svc := range tt.env.svcs { |
| 86 | + assert.NoError(t, k8sClient.Create(ctx, svc.DeepCopy())) |
| 87 | + } |
| 88 | + annotationParser := annotations.NewSuffixAnnotationParser("service.beta.kubernetes.io") |
| 89 | + b := &defaultEnhancedBackendBuilder{ |
| 90 | + k8sClient: k8sClient, |
| 91 | + annotationParser: annotationParser, |
| 92 | + } |
| 93 | + |
| 94 | + err := b.Build(context.Background(), tt.args.svc, tt.args.action, tt.args.backendServices) |
| 95 | + assert.NoError(t, err) |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func Test_defaultEnhancedBackendBuilder_loadBackendServices(t *testing.T) { |
| 101 | + port80 := intstr.FromInt(80) |
| 102 | + |
| 103 | + type env struct { |
| 104 | + svcs []*corev1.Service |
| 105 | + } |
| 106 | + type args struct { |
| 107 | + action *Action |
| 108 | + namespace string |
| 109 | + backendServices map[types.NamespacedName]*corev1.Service |
| 110 | + } |
| 111 | + tests := []struct { |
| 112 | + name string |
| 113 | + env env |
| 114 | + args args |
| 115 | + wantAction Action |
| 116 | + wantBackendServices map[types.NamespacedName]*corev1.Service |
| 117 | + wantErr error |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "forward to a single service", |
| 121 | + env: env{ |
| 122 | + svcs: []*corev1.Service{svc1, svc2}, |
| 123 | + }, |
| 124 | + args: args{ |
| 125 | + action: &Action{ |
| 126 | + Type: ActionTypeForward, |
| 127 | + ForwardConfig: &ForwardActionConfig{ |
| 128 | + TargetGroups: []TargetGroupTuple{ |
| 129 | + { |
| 130 | + ServiceName: awssdk.String("svc-1"), |
| 131 | + ServicePort: &port80, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + namespace: "awesome-ns", |
| 137 | + backendServices: map[types.NamespacedName]*corev1.Service{}, |
| 138 | + }, |
| 139 | + wantBackendServices: map[types.NamespacedName]*corev1.Service{ |
| 140 | + types.NamespacedName{Namespace: "awesome-ns", Name: "svc-1"}: svc1, |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "forward to multiple services", |
| 145 | + env: env{ |
| 146 | + svcs: []*corev1.Service{svc1, svc2}, |
| 147 | + }, |
| 148 | + args: args{ |
| 149 | + action: &Action{ |
| 150 | + Type: ActionTypeForward, |
| 151 | + ForwardConfig: &ForwardActionConfig{ |
| 152 | + TargetGroups: []TargetGroupTuple{ |
| 153 | + { |
| 154 | + ServiceName: awssdk.String("svc-1"), |
| 155 | + ServicePort: &port80, |
| 156 | + }, |
| 157 | + { |
| 158 | + ServiceName: awssdk.String("svc-2"), |
| 159 | + ServicePort: &port80, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + namespace: "awesome-ns", |
| 165 | + backendServices: map[types.NamespacedName]*corev1.Service{}, |
| 166 | + }, |
| 167 | + wantBackendServices: map[types.NamespacedName]*corev1.Service{ |
| 168 | + types.NamespacedName{Namespace: "awesome-ns", Name: "svc-1"}: svc1, |
| 169 | + types.NamespacedName{Namespace: "awesome-ns", Name: "svc-2"}: svc2, |
| 170 | + }, |
| 171 | + }, |
| 172 | + } |
| 173 | + for _, tt := range tests { |
| 174 | + t.Run(tt.name, func(t *testing.T) { |
| 175 | + ctx := context.Background() |
| 176 | + k8sSchema := runtime.NewScheme() |
| 177 | + clientgoscheme.AddToScheme(k8sSchema) |
| 178 | + k8sClient := testclient.NewClientBuilder().WithScheme(k8sSchema).Build() |
| 179 | + for _, svc := range tt.env.svcs { |
| 180 | + assert.NoError(t, k8sClient.Create(ctx, svc.DeepCopy())) |
| 181 | + } |
| 182 | + |
| 183 | + b := &defaultEnhancedBackendBuilder{ |
| 184 | + k8sClient: k8sClient, |
| 185 | + } |
| 186 | + err := b.loadBackendServices(ctx, tt.args.action, tt.args.namespace, tt.args.backendServices) |
| 187 | + if tt.wantErr != nil { |
| 188 | + assert.EqualError(t, err, tt.wantErr.Error()) |
| 189 | + } else { |
| 190 | + assert.NoError(t, err) |
| 191 | + opt := equality.IgnoreFakeClientPopulatedFields() |
| 192 | + assert.True(t, cmp.Equal(tt.wantBackendServices, tt.args.backendServices, opt), |
| 193 | + "diff: %v", cmp.Diff(tt.wantBackendServices, tt.args.backendServices, opt)) |
| 194 | + } |
| 195 | + }) |
| 196 | + } |
| 197 | +} |
0 commit comments