@@ -17,7 +17,13 @@ limitations under the License.
1717package controller
1818
1919import (
20+ "context"
2021 "testing"
22+
23+ "k8s.io/apimachinery/pkg/runtime"
24+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
25+
26+ impdevv1alpha1 "github.com/syscode-labs/imp/api/v1alpha1"
2127)
2228
2329func TestParseFraction (t * testing.T ) {
@@ -103,3 +109,100 @@ func TestEffectiveMaxVMs(t *testing.T) {
103109 })
104110 }
105111}
112+
113+ func TestResolveClassSpec_DirectClassRef (t * testing.T ) {
114+ scheme := runtime .NewScheme ()
115+ if err := impdevv1alpha1 .AddToScheme (scheme ); err != nil {
116+ t .Fatalf ("scheme: %v" , err )
117+ }
118+
119+ class := & impdevv1alpha1.ImpVMClass {}
120+ class .Name = "small"
121+ class .Spec .VCPU = 2
122+ class .Spec .MemoryMiB = 512
123+
124+ c := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (class ).Build ()
125+
126+ vm := & impdevv1alpha1.ImpVM {}
127+ vm .Namespace = "default"
128+ vm .Name = "test-vm"
129+ vm .Spec .ClassRef = & impdevv1alpha1.ClusterObjectRef {Name : "small" }
130+
131+ spec , err := resolveClassSpec (context .Background (), c , vm )
132+ if err != nil {
133+ t .Fatalf ("unexpected error: %v" , err )
134+ }
135+ if spec .VCPU != 2 {
136+ t .Errorf ("VCPU = %d, want 2" , spec .VCPU )
137+ }
138+ if spec .MemoryMiB != 512 {
139+ t .Errorf ("MemoryMiB = %d, want 512" , spec .MemoryMiB )
140+ }
141+ }
142+
143+ func TestResolveClassSpec_ViaTemplateRef (t * testing.T ) {
144+ scheme := runtime .NewScheme ()
145+ if err := impdevv1alpha1 .AddToScheme (scheme ); err != nil {
146+ t .Fatalf ("scheme: %v" , err )
147+ }
148+
149+ class := & impdevv1alpha1.ImpVMClass {}
150+ class .Name = "large"
151+ class .Spec .VCPU = 4
152+ class .Spec .MemoryMiB = 2048
153+
154+ tmpl := & impdevv1alpha1.ImpVMTemplate {}
155+ tmpl .Namespace = "default"
156+ tmpl .Name = "ubuntu-tmpl"
157+ tmpl .Spec .ClassRef = impdevv1alpha1.ClusterObjectRef {Name : "large" }
158+
159+ c := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (class , tmpl ).Build ()
160+
161+ vm := & impdevv1alpha1.ImpVM {}
162+ vm .Namespace = "default"
163+ vm .Name = "tmpl-vm"
164+ vm .Spec .TemplateRef = & impdevv1alpha1.LocalObjectRef {Name : "ubuntu-tmpl" }
165+
166+ spec , err := resolveClassSpec (context .Background (), c , vm )
167+ if err != nil {
168+ t .Fatalf ("unexpected error: %v" , err )
169+ }
170+ if spec .VCPU != 4 {
171+ t .Errorf ("VCPU = %d, want 4" , spec .VCPU )
172+ }
173+ }
174+
175+ func TestResolveClassSpec_NoRef (t * testing.T ) {
176+ scheme := runtime .NewScheme ()
177+ if err := impdevv1alpha1 .AddToScheme (scheme ); err != nil {
178+ t .Fatalf ("scheme: %v" , err )
179+ }
180+ c := fake .NewClientBuilder ().WithScheme (scheme ).Build ()
181+
182+ vm := & impdevv1alpha1.ImpVM {}
183+ vm .Namespace = "default"
184+ vm .Name = "no-ref"
185+
186+ _ , err := resolveClassSpec (context .Background (), c , vm )
187+ if err == nil {
188+ t .Fatal ("expected error for VM with no classRef or templateRef" )
189+ }
190+ }
191+
192+ func TestResolveClassSpec_ClassNotFound (t * testing.T ) {
193+ scheme := runtime .NewScheme ()
194+ if err := impdevv1alpha1 .AddToScheme (scheme ); err != nil {
195+ t .Fatalf ("scheme: %v" , err )
196+ }
197+ c := fake .NewClientBuilder ().WithScheme (scheme ).Build ()
198+
199+ vm := & impdevv1alpha1.ImpVM {}
200+ vm .Namespace = "default"
201+ vm .Name = "missing-class"
202+ vm .Spec .ClassRef = & impdevv1alpha1.ClusterObjectRef {Name : "nonexistent" }
203+
204+ _ , err := resolveClassSpec (context .Background (), c , vm )
205+ if err == nil {
206+ t .Fatal ("expected error when class does not exist" )
207+ }
208+ }
0 commit comments