|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 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 common_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" |
| 27 | + condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" |
| 28 | + internalcommon "github.com/openstack-k8s-operators/nova-operator/internal/common" |
| 29 | + corev1 "k8s.io/api/core/v1" |
| 30 | + k8s_errors "k8s.io/apimachinery/pkg/api/errors" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + ctrl "sigs.k8s.io/controller-runtime" |
| 33 | +) |
| 34 | + |
| 35 | +func TestEnsureNetworkAttachments_noAttachments(t *testing.T) { |
| 36 | + ctx := context.Background() |
| 37 | + requeueTimeout := 5 * time.Second |
| 38 | + conditions := &condition.Conditions{} |
| 39 | + h := newTestHelper(t, newNetworkTestScheme(t)) |
| 40 | + |
| 41 | + annotations, result, err := internalcommon.EnsureNetworkAttachments( |
| 42 | + ctx, h, nil, conditions, requeueTimeout, |
| 43 | + ) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("expected no error, got %v", err) |
| 46 | + } |
| 47 | + if result != (ctrl.Result{}) { |
| 48 | + t.Fatalf("expected empty result, got %#v", result) |
| 49 | + } |
| 50 | + want := map[string]string{networkv1.NetworkAttachmentAnnot: "[]"} |
| 51 | + if annotations == nil || annotations[networkv1.NetworkAttachmentAnnot] != want[networkv1.NetworkAttachmentAnnot] { |
| 52 | + t.Fatalf("expected annotations %v, got %v", want, annotations) |
| 53 | + } |
| 54 | + if conditions.Get(condition.NetworkAttachmentsReadyCondition) != nil { |
| 55 | + t.Fatal("expected NetworkAttachmentsReady condition to be unset") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestEnsureNetworkAttachments_missingNAD(t *testing.T) { |
| 60 | + const nadName = "missing-nad" |
| 61 | + ctx := context.Background() |
| 62 | + requeueTimeout := 5 * time.Second |
| 63 | + conditions := &condition.Conditions{} |
| 64 | + h := newTestHelper(t, newNetworkTestScheme(t)) |
| 65 | + |
| 66 | + annotations, result, err := internalcommon.EnsureNetworkAttachments( |
| 67 | + ctx, h, []string{nadName}, conditions, requeueTimeout, |
| 68 | + ) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("expected no error, got %v", err) |
| 71 | + } |
| 72 | + if result.RequeueAfter != requeueTimeout { |
| 73 | + t.Fatalf("expected RequeueAfter %s, got %s", requeueTimeout, result.RequeueAfter) |
| 74 | + } |
| 75 | + if annotations != nil { |
| 76 | + t.Fatalf("expected nil annotations, got %v", annotations) |
| 77 | + } |
| 78 | + |
| 79 | + nadReady := conditions.Get(condition.NetworkAttachmentsReadyCondition) |
| 80 | + if nadReady == nil { |
| 81 | + t.Fatal("expected NetworkAttachmentsReady condition to be set") |
| 82 | + } |
| 83 | + if nadReady.Status != corev1.ConditionFalse { |
| 84 | + t.Fatalf("expected status False, got %q", nadReady.Status) |
| 85 | + } |
| 86 | + if nadReady.Reason != condition.ErrorReason { |
| 87 | + t.Fatalf("expected reason %q, got %q", condition.ErrorReason, nadReady.Reason) |
| 88 | + } |
| 89 | + wantMessage := fmt.Sprintf(condition.NetworkAttachmentsReadyWaitingMessage, nadName) |
| 90 | + if nadReady.Message != wantMessage { |
| 91 | + t.Fatalf("expected message %q, got %q", wantMessage, nadReady.Message) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestEnsureNetworkAttachments_getError(t *testing.T) { |
| 96 | + const nadName = "broken-nad" |
| 97 | + ctx := context.Background() |
| 98 | + requeueTimeout := 5 * time.Second |
| 99 | + conditions := &condition.Conditions{} |
| 100 | + getErr := k8s_errors.NewInternalError(errTestNADGet) |
| 101 | + h := newTestHelperWithGetError(t, newNetworkTestScheme(t), getErr) |
| 102 | + |
| 103 | + annotations, result, err := internalcommon.EnsureNetworkAttachments( |
| 104 | + ctx, h, []string{nadName}, conditions, requeueTimeout, |
| 105 | + ) |
| 106 | + if err == nil { |
| 107 | + t.Fatal("expected error, got nil") |
| 108 | + } |
| 109 | + if !strings.Contains(err.Error(), "apiserver unavailable") { |
| 110 | + t.Fatalf("expected wrapped apiserver error, got %v", err) |
| 111 | + } |
| 112 | + if result != (ctrl.Result{}) { |
| 113 | + t.Fatalf("expected empty result, got %#v", result) |
| 114 | + } |
| 115 | + if annotations != nil { |
| 116 | + t.Fatalf("expected nil annotations, got %v", annotations) |
| 117 | + } |
| 118 | + |
| 119 | + nadReady := conditions.Get(condition.NetworkAttachmentsReadyCondition) |
| 120 | + if nadReady == nil { |
| 121 | + t.Fatal("expected NetworkAttachmentsReady condition to be set") |
| 122 | + } |
| 123 | + wantMessage := fmt.Sprintf(condition.NetworkAttachmentsErrorMessage, err.Error()) |
| 124 | + if nadReady.Message != wantMessage { |
| 125 | + t.Fatalf("expected condition message %q, got %q", wantMessage, nadReady.Message) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestEnsureNetworkAttachments_nadPresent(t *testing.T) { |
| 130 | + const nadName = "internalapi" |
| 131 | + ctx := context.Background() |
| 132 | + requeueTimeout := 5 * time.Second |
| 133 | + conditions := &condition.Conditions{} |
| 134 | + scheme := newNetworkTestScheme(t) |
| 135 | + nad := &networkv1.NetworkAttachmentDefinition{ |
| 136 | + ObjectMeta: metav1.ObjectMeta{ |
| 137 | + Name: nadName, |
| 138 | + Namespace: testNamespace, |
| 139 | + }, |
| 140 | + } |
| 141 | + h := newTestHelper(t, scheme, nad) |
| 142 | + |
| 143 | + annotations, result, err := internalcommon.EnsureNetworkAttachments( |
| 144 | + ctx, h, []string{nadName}, conditions, requeueTimeout, |
| 145 | + ) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("expected no error, got %v", err) |
| 148 | + } |
| 149 | + if result != (ctrl.Result{}) { |
| 150 | + t.Fatalf("expected empty result, got %#v", result) |
| 151 | + } |
| 152 | + want := fmt.Sprintf( |
| 153 | + `[{"name":"%s","namespace":"%s","interface":"%s"}]`, |
| 154 | + nadName, testNamespace, nadName, |
| 155 | + ) |
| 156 | + if annotations == nil || annotations[networkv1.NetworkAttachmentAnnot] != want { |
| 157 | + t.Fatalf("expected annotation %q, got %v", want, annotations) |
| 158 | + } |
| 159 | + if conditions.Get(condition.NetworkAttachmentsReadyCondition) != nil { |
| 160 | + t.Fatal("expected NetworkAttachmentsReady condition to be unset") |
| 161 | + } |
| 162 | +} |
0 commit comments