|
| 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 v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + |
| 25 | + impdevv1alpha1 "github.com/syscode-labs/imp/api/v1alpha1" |
| 26 | +) |
| 27 | + |
| 28 | +// newVM builds a minimal ImpVM for use in tests. |
| 29 | +// Pass empty string for templateRef, classRef, or image to leave them unset. |
| 30 | +func newVM(templateRef, classRef, image string) *impdevv1alpha1.ImpVM { |
| 31 | + vm := &impdevv1alpha1.ImpVM{ |
| 32 | + ObjectMeta: metav1.ObjectMeta{ |
| 33 | + Name: "test-vm", |
| 34 | + Namespace: "default", |
| 35 | + }, |
| 36 | + } |
| 37 | + if templateRef != "" { |
| 38 | + vm.Spec.TemplateRef = &impdevv1alpha1.LocalObjectRef{Name: templateRef} |
| 39 | + } |
| 40 | + if classRef != "" { |
| 41 | + vm.Spec.ClassRef = &impdevv1alpha1.ClusterObjectRef{Name: classRef} |
| 42 | + } |
| 43 | + vm.Spec.Image = image |
| 44 | + return vm |
| 45 | +} |
| 46 | + |
| 47 | +// --- Defaulter tests ------------------------------------------------------- |
| 48 | + |
| 49 | +func TestImpVMWebhook_Default_SetsLifecycle(t *testing.T) { |
| 50 | + wh := &ImpVMWebhook{} |
| 51 | + vm := newVM("", "my-class", "my-image") |
| 52 | + // lifecycle is empty |
| 53 | + |
| 54 | + if err := wh.Default(context.Background(), vm); err != nil { |
| 55 | + t.Fatalf("Default() returned unexpected error: %v", err) |
| 56 | + } |
| 57 | + if vm.Spec.Lifecycle != impdevv1alpha1.VMLifecycleEphemeral { |
| 58 | + t.Errorf("expected lifecycle=%q, got %q", impdevv1alpha1.VMLifecycleEphemeral, vm.Spec.Lifecycle) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestImpVMWebhook_Default_PreservesExistingLifecycle(t *testing.T) { |
| 63 | + wh := &ImpVMWebhook{} |
| 64 | + vm := newVM("", "my-class", "my-image") |
| 65 | + vm.Spec.Lifecycle = impdevv1alpha1.VMLifecyclePersistent |
| 66 | + |
| 67 | + if err := wh.Default(context.Background(), vm); err != nil { |
| 68 | + t.Fatalf("Default() returned unexpected error: %v", err) |
| 69 | + } |
| 70 | + if vm.Spec.Lifecycle != impdevv1alpha1.VMLifecyclePersistent { |
| 71 | + t.Errorf("expected lifecycle=%q, got %q", impdevv1alpha1.VMLifecyclePersistent, vm.Spec.Lifecycle) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// --- ValidateCreate tests -------------------------------------------------- |
| 76 | + |
| 77 | +func TestImpVMWebhook_ValidateCreate_BothRefs(t *testing.T) { |
| 78 | + wh := &ImpVMWebhook{} |
| 79 | + vm := newVM("my-template", "my-class", "my-image") |
| 80 | + |
| 81 | + _, err := wh.ValidateCreate(context.Background(), vm) |
| 82 | + if err == nil { |
| 83 | + t.Fatal("expected error for both templateRef and classRef set, got nil") |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestImpVMWebhook_ValidateCreate_NoRefs(t *testing.T) { |
| 88 | + wh := &ImpVMWebhook{} |
| 89 | + vm := newVM("", "", "") |
| 90 | + |
| 91 | + _, err := wh.ValidateCreate(context.Background(), vm) |
| 92 | + if err == nil { |
| 93 | + t.Fatal("expected error for neither templateRef nor classRef set, got nil") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestImpVMWebhook_ValidateCreate_ClassRefWithoutImage(t *testing.T) { |
| 98 | + wh := &ImpVMWebhook{} |
| 99 | + vm := newVM("", "my-class", "") // classRef set, image empty |
| 100 | + |
| 101 | + _, err := wh.ValidateCreate(context.Background(), vm) |
| 102 | + if err == nil { |
| 103 | + t.Fatal("expected error when classRef is set without image, got nil") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestImpVMWebhook_ValidateCreate_Valid_ClassRef(t *testing.T) { |
| 108 | + wh := &ImpVMWebhook{} |
| 109 | + vm := newVM("", "my-class", "my-image") |
| 110 | + |
| 111 | + _, err := wh.ValidateCreate(context.Background(), vm) |
| 112 | + if err != nil { |
| 113 | + t.Errorf("expected no error for valid classRef+image, got: %v", err) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestImpVMWebhook_ValidateCreate_Valid_TemplateRef(t *testing.T) { |
| 118 | + wh := &ImpVMWebhook{} |
| 119 | + vm := newVM("my-template", "", "") // templateRef only, no image required |
| 120 | + |
| 121 | + _, err := wh.ValidateCreate(context.Background(), vm) |
| 122 | + if err != nil { |
| 123 | + t.Errorf("expected no error for valid templateRef, got: %v", err) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// --- ValidateUpdate tests -------------------------------------------------- |
| 128 | + |
| 129 | +func TestImpVMWebhook_ValidateUpdate_NodeNameImmutable(t *testing.T) { |
| 130 | + wh := &ImpVMWebhook{} |
| 131 | + oldVM := newVM("my-template", "", "") |
| 132 | + oldVM.Spec.NodeName = "node-1" |
| 133 | + newVM := newVM("my-template", "", "") |
| 134 | + newVM.Spec.NodeName = "node-2" |
| 135 | + |
| 136 | + _, err := wh.ValidateUpdate(context.Background(), oldVM, newVM) |
| 137 | + if err == nil { |
| 138 | + t.Fatal("expected error when nodeName is changed after being set, got nil") |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestImpVMWebhook_ValidateUpdate_NodeNameCanBeSetFromEmpty(t *testing.T) { |
| 143 | + wh := &ImpVMWebhook{} |
| 144 | + oldVM := newVM("my-template", "", "") |
| 145 | + oldVM.Spec.NodeName = "" |
| 146 | + newVM := newVM("my-template", "", "") |
| 147 | + newVM.Spec.NodeName = "node-1" |
| 148 | + |
| 149 | + _, err := wh.ValidateUpdate(context.Background(), oldVM, newVM) |
| 150 | + if err != nil { |
| 151 | + t.Errorf("expected no error when setting nodeName from empty, got: %v", err) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestImpVMWebhook_ValidateUpdate_NodeNameUnchanged(t *testing.T) { |
| 156 | + wh := &ImpVMWebhook{} |
| 157 | + oldVM := newVM("my-template", "", "") |
| 158 | + oldVM.Spec.NodeName = "node-1" |
| 159 | + newVM := newVM("my-template", "", "") |
| 160 | + newVM.Spec.NodeName = "node-1" |
| 161 | + |
| 162 | + _, err := wh.ValidateUpdate(context.Background(), oldVM, newVM) |
| 163 | + if err != nil { |
| 164 | + t.Errorf("expected no error when nodeName is unchanged, got: %v", err) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// --- ValidateDelete tests -------------------------------------------------- |
| 169 | + |
| 170 | +func TestImpVMWebhook_ValidateDelete(t *testing.T) { |
| 171 | + wh := &ImpVMWebhook{} |
| 172 | + vm := newVM("my-template", "", "") |
| 173 | + |
| 174 | + _, err := wh.ValidateDelete(context.Background(), vm) |
| 175 | + if err != nil { |
| 176 | + t.Errorf("expected no error on delete, got: %v", err) |
| 177 | + } |
| 178 | +} |
0 commit comments