11package create
22
33import (
4- "context"
54 "strings"
65 "testing"
7-
8- "github.com/containers/kubernetes-mcp-server/pkg/api"
9- internalk8s "github.com/containers/kubernetes-mcp-server/pkg/kubernetes"
106)
117
12- type mockToolCallRequest struct {
13- arguments map [string ]interface {}
14- }
15-
16- func (m * mockToolCallRequest ) GetArguments () map [string ]any {
17- return m .arguments
18- }
19-
20- func TestCreate (t * testing.T ) {
8+ // Test the YAML rendering directly without creating resources
9+ func TestRenderVMYaml (t * testing.T ) {
2110 tests := []struct {
2211 name string
23- args map [ string ] interface {}
12+ params vmParams
2413 wantErr bool
2514 checkFunc func (t * testing.T , result string )
2615 }{
2716 {
28- name : "creates VM with basic settings" ,
29- args : map [string ]interface {}{
30- "namespace" : "test-ns" ,
31- "name" : "test-vm" ,
32- "workload" : "fedora" ,
17+ name : "renders VM with basic settings" ,
18+ params : vmParams {
19+ Namespace : "test-ns" ,
20+ Name : "test-vm" ,
21+ ContainerDisk : "quay.io/containerdisks/fedora:latest" ,
22+ RunStrategy : "Halted" ,
3323 },
3424 wantErr : false ,
3525 checkFunc : func (t * testing.T , result string ) {
36- if ! strings .Contains (result , "VirtualMachine Creation Plan" ) {
37- t .Errorf ("Expected 'VirtualMachine Creation Plan' header in result" )
26+ if ! strings .Contains (result , "apiVersion: kubevirt.io/v1" ) {
27+ t .Errorf ("Expected apiVersion in YAML" )
28+ }
29+ if ! strings .Contains (result , "kind: VirtualMachine" ) {
30+ t .Errorf ("Expected kind VirtualMachine in YAML" )
3831 }
3932 if ! strings .Contains (result , "name: test-vm" ) {
4033 t .Errorf ("Expected VM name test-vm in YAML" )
@@ -51,12 +44,13 @@ func TestCreate(t *testing.T) {
5144 },
5245 },
5346 {
54- name : "creates VM with instancetype" ,
55- args : map [string ]interface {}{
56- "namespace" : "test-ns" ,
57- "name" : "test-vm" ,
58- "workload" : "ubuntu" ,
59- "instancetype" : "u1.medium" ,
47+ name : "renders VM with instancetype" ,
48+ params : vmParams {
49+ Namespace : "test-ns" ,
50+ Name : "test-vm" ,
51+ ContainerDisk : "quay.io/containerdisks/ubuntu:24.04" ,
52+ Instancetype : "u1.medium" ,
53+ RunStrategy : "Halted" ,
6054 },
6155 wantErr : false ,
6256 checkFunc : func (t * testing.T , result string ) {
@@ -66,19 +60,20 @@ func TestCreate(t *testing.T) {
6660 if ! strings .Contains (result , "kind: VirtualMachineClusterInstancetype" ) {
6761 t .Errorf ("Expected VirtualMachineClusterInstancetype in YAML manifest" )
6862 }
69- // When instancetype is set, memory should not be in the YAML resources section
70- if strings .Contains (result , "resources: \n requests: \n memory: " ) {
71- t .Errorf ("Should not have memory resources when instancetype is specified" )
63+ // When instancetype is set, memory should not be in the YAML
64+ if strings .Contains (result , "guest: 2Gi " ) {
65+ t .Errorf ("Should not have guest memory when instancetype is specified" )
7266 }
7367 },
7468 },
7569 {
76- name : "creates VM with preference" ,
77- args : map [string ]interface {}{
78- "namespace" : "test-ns" ,
79- "name" : "test-vm" ,
80- "workload" : "rhel" ,
81- "preference" : "rhel.9" ,
70+ name : "renders VM with preference" ,
71+ params : vmParams {
72+ Namespace : "test-ns" ,
73+ Name : "test-vm" ,
74+ ContainerDisk : "registry.redhat.io/rhel9/rhel-guest-image:latest" ,
75+ Preference : "rhel.9" ,
76+ RunStrategy : "Halted" ,
8277 },
8378 wantErr : false ,
8479 checkFunc : func (t * testing.T , result string ) {
@@ -91,11 +86,12 @@ func TestCreate(t *testing.T) {
9186 },
9287 },
9388 {
94- name : "creates VM with custom container disk" ,
95- args : map [string ]interface {}{
96- "namespace" : "test-ns" ,
97- "name" : "test-vm" ,
98- "workload" : "quay.io/myrepo/myimage:v1.0" ,
89+ name : "renders VM with custom container disk" ,
90+ params : vmParams {
91+ Namespace : "test-ns" ,
92+ Name : "test-vm" ,
93+ ContainerDisk : "quay.io/myrepo/myimage:v1.0" ,
94+ RunStrategy : "Halted" ,
9995 },
10096 wantErr : false ,
10197 checkFunc : func (t * testing.T , result string ) {
@@ -105,68 +101,65 @@ func TestCreate(t *testing.T) {
105101 },
106102 },
107103 {
108- name : "missing namespace" ,
109- args : map [string ]interface {}{
110- "name" : "test-vm" ,
111- "workload" : "fedora" ,
104+ name : "renders VM with DataSource" ,
105+ params : vmParams {
106+ Namespace : "test-ns" ,
107+ Name : "test-vm" ,
108+ UseDataSource : true ,
109+ DataSourceName : "fedora" ,
110+ DataSourceNamespace : "openshift-virtualization-os-images" ,
111+ RunStrategy : "Halted" ,
112112 },
113- wantErr : true ,
114- },
115- {
116- name : "missing name" ,
117- args : map [string ]interface {}{
118- "namespace" : "test-ns" ,
119- "workload" : "fedora" ,
113+ wantErr : false ,
114+ checkFunc : func (t * testing.T , result string ) {
115+ if ! strings .Contains (result , "dataVolumeTemplates" ) {
116+ t .Errorf ("Expected dataVolumeTemplates in YAML" )
117+ }
118+ if ! strings .Contains (result , "kind: DataSource" ) {
119+ t .Errorf ("Expected DataSource kind in YAML" )
120+ }
121+ if ! strings .Contains (result , "name: fedora" ) {
122+ t .Errorf ("Expected DataSource name in YAML" )
123+ }
124+ if ! strings .Contains (result , "openshift-virtualization-os-images" ) {
125+ t .Errorf ("Expected DataSource namespace in YAML" )
126+ }
120127 },
121- wantErr : true ,
122128 },
123129 {
124- name : "missing workload defaults to fedora" ,
125- args : map [string ]interface {}{
126- "namespace" : "test-ns" ,
127- "name" : "test-vm" ,
130+ name : "renders VM with autostart (runStrategy Always)" ,
131+ params : vmParams {
132+ Namespace : "test-ns" ,
133+ Name : "test-vm" ,
134+ ContainerDisk : "quay.io/containerdisks/fedora:latest" ,
135+ RunStrategy : "Always" ,
128136 },
129137 wantErr : false ,
130138 checkFunc : func (t * testing.T , result string ) {
131- if ! strings .Contains (result , "quay.io/containerdisks/fedora:latest " ) {
132- t .Errorf ("Expected default fedora container disk in result " )
139+ if ! strings .Contains (result , "runStrategy: Always " ) {
140+ t .Errorf ("Expected runStrategy: Always in YAML " )
133141 }
134142 },
135143 },
136144 }
137145
138146 for _ , tt := range tests {
139147 t .Run (tt .name , func (t * testing.T ) {
140- params := api.ToolHandlerParams {
141- Context : context .Background (),
142- Kubernetes : & internalk8s.Kubernetes {},
143- ToolCallRequest : & mockToolCallRequest {arguments : tt .args },
144- }
145-
146- result , err := create (params )
147- if err != nil {
148- t .Errorf ("create() unexpected Go error: %v" , err )
149- return
150- }
151-
152- if result == nil {
153- t .Error ("Expected non-nil result" )
154- return
155- }
148+ result , err := renderVMYaml (tt .params )
156149
157150 if tt .wantErr {
158- if result . Error == nil {
159- t .Error ("Expected error in result.Error , got nil" )
151+ if err == nil {
152+ t .Error ("Expected error, got nil" )
160153 }
161154 } else {
162- if result . Error != nil {
163- t .Errorf ("Expected no error in result , got: %v" , result . Error )
155+ if err != nil {
156+ t .Errorf ("Expected no error, got: %v" , err )
164157 }
165- if result . Content == "" {
166- t .Error ("Expected non-empty result content " )
158+ if result == "" {
159+ t .Error ("Expected non-empty result" )
167160 }
168161 if tt .checkFunc != nil {
169- tt .checkFunc (t , result . Content )
162+ tt .checkFunc (t , result )
170163 }
171164 }
172165 })
0 commit comments