|
| 1 | +/* |
| 2 | +Copyright 2023 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 util |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 29 | +) |
| 30 | + |
| 31 | +func TestNCPSupportFW(t *testing.T) { |
| 32 | + scheme := runtime.NewScheme() |
| 33 | + _ = clientgoscheme.AddToScheme(scheme) |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + client client.Client |
| 38 | + want bool |
| 39 | + wantErr bool |
| 40 | + }{ |
| 41 | + { |
| 42 | + "No version configmap", |
| 43 | + fake.NewClientBuilder().WithScheme(scheme).WithObjects().Build(), |
| 44 | + false, |
| 45 | + true, |
| 46 | + }, |
| 47 | + { |
| 48 | + "non-semver version", |
| 49 | + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("nosemver")).Build(), |
| 50 | + false, |
| 51 | + true, |
| 52 | + }, |
| 53 | + { |
| 54 | + "compatible version lower end", |
| 55 | + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap(NCPVersionSupportFW)).Build(), |
| 56 | + true, |
| 57 | + false, |
| 58 | + }, |
| 59 | + { |
| 60 | + "compatible version upper end", |
| 61 | + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.0.9999")).Build(), |
| 62 | + true, |
| 63 | + false, |
| 64 | + }, |
| 65 | + { |
| 66 | + "incompatible version lower end", |
| 67 | + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.0.0")).Build(), |
| 68 | + false, |
| 69 | + false, |
| 70 | + }, |
| 71 | + { |
| 72 | + "incompatible version upper end", |
| 73 | + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap(NCPVersionSupportFWEnded)).Build(), |
| 74 | + false, |
| 75 | + false, |
| 76 | + }, |
| 77 | + } |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + ctx := context.Background() |
| 81 | + got, err := NCPSupportFW(ctx, tt.client) |
| 82 | + if (err != nil) != tt.wantErr { |
| 83 | + t.Errorf("NCPSupportFW() error = %v, wantErr %v", err, tt.wantErr) |
| 84 | + return |
| 85 | + } |
| 86 | + if got != tt.want { |
| 87 | + t.Errorf("NCPSupportFW() = %v, want %v", got, tt.want) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func newNCPConfigMap(version string) client.Object { |
| 94 | + return &corev1.ConfigMap{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Name: NCPVersionConfigMap, |
| 97 | + Namespace: NCPNamespace, |
| 98 | + }, |
| 99 | + Data: map[string]string{ |
| 100 | + NCPVersionKey: version, |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
0 commit comments