1
19
20 package kubecli
21
22 import (
23 "context"
24 "net/http"
25 "path"
26
27 . "github.com/onsi/ginkgo/v2"
28 . "github.com/onsi/gomega"
29 "github.com/onsi/gomega/ghttp"
30 k8sv1 "k8s.io/api/core/v1"
31 "k8s.io/apimachinery/pkg/api/errors"
32 k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33 "k8s.io/apimachinery/pkg/runtime/schema"
34 v1 "kubevirt.io/api/core/v1"
35 )
36
37 var _ = Describe("Kubevirt VirtualMachineInstancePreset Client", func() {
38 var server *ghttp.Server
39 basePath := "/apis/kubevirt.io/v1/namespaces/default/virtualmachineinstancepresets"
40 presetPath := path.Join(basePath, "testpreset")
41 proxyPath := "/proxy/path"
42
43 BeforeEach(func() {
44 server = ghttp.NewServer()
45 })
46
47 DescribeTable("should fetch a VirtualMachineInstancePreset", func(proxyPath string) {
48 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
49 Expect(err).ToNot(HaveOccurred())
50
51 preset := NewMinimalVirtualMachineInstancePreset("testpreset")
52 server.AppendHandlers(ghttp.CombineHandlers(
53 ghttp.VerifyRequest("GET", path.Join(proxyPath, presetPath)),
54 ghttp.RespondWithJSONEncoded(http.StatusOK, preset),
55 ))
56 fetchedVMIPreset, err := client.VirtualMachineInstancePreset(k8sv1.NamespaceDefault).Get(context.Background(), "testpreset", k8smetav1.GetOptions{})
57
58 Expect(server.ReceivedRequests()).To(HaveLen(1))
59 Expect(err).ToNot(HaveOccurred())
60 Expect(fetchedVMIPreset).To(Equal(preset))
61 },
62 Entry("with regular server URL", ""),
63 Entry("with proxied server URL", proxyPath),
64 )
65
66 DescribeTable("should detect non existent VMIPresets", func(proxyPath string) {
67 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
68 Expect(err).ToNot(HaveOccurred())
69
70 server.AppendHandlers(ghttp.CombineHandlers(
71 ghttp.VerifyRequest("GET", path.Join(proxyPath, presetPath)),
72 ghttp.RespondWithJSONEncoded(http.StatusNotFound, errors.NewNotFound(schema.GroupResource{}, "testpreset")),
73 ))
74 _, err = client.VirtualMachineInstancePreset(k8sv1.NamespaceDefault).Get(context.Background(), "testpreset", k8smetav1.GetOptions{})
75
76 Expect(server.ReceivedRequests()).To(HaveLen(1))
77 Expect(err).To(HaveOccurred())
78 Expect(errors.IsNotFound(err)).To(BeTrue(), "Expected an IsNotFound error to have occurred")
79 },
80 Entry("with regular server URL", ""),
81 Entry("with proxied server URL", proxyPath),
82 )
83
84 DescribeTable("should fetch a VirtualMachineInstancePreset list", func(proxyPath string) {
85 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
86 Expect(err).ToNot(HaveOccurred())
87
88 preset := NewMinimalVirtualMachineInstancePreset("testpreset")
89 server.AppendHandlers(ghttp.CombineHandlers(
90 ghttp.VerifyRequest("GET", path.Join(proxyPath, basePath)),
91 ghttp.RespondWithJSONEncoded(http.StatusOK, NewVirtualMachineInstancePresetList(*preset)),
92 ))
93 fetchedVMIPresetList, err := client.VirtualMachineInstancePreset(k8sv1.NamespaceDefault).List(context.Background(), k8smetav1.ListOptions{})
94 apiVersion, kind := v1.VirtualMachineInstancePresetGroupVersionKind.ToAPIVersionAndKind()
95
96 Expect(err).ToNot(HaveOccurred())
97 Expect(server.ReceivedRequests()).To(HaveLen(1))
98 Expect(fetchedVMIPresetList.Items).To(HaveLen(1))
99 Expect(fetchedVMIPresetList.Items[0].APIVersion).To(Equal(apiVersion))
100 Expect(fetchedVMIPresetList.Items[0].Kind).To(Equal(kind))
101 Expect(fetchedVMIPresetList.Items[0]).To(Equal(*preset))
102 },
103 Entry("with regular server URL", ""),
104 Entry("with proxied server URL", proxyPath),
105 )
106
107 DescribeTable("should create a VirtualMachineInstancePreset", func(proxyPath string) {
108 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
109 Expect(err).ToNot(HaveOccurred())
110
111 preset := NewMinimalVirtualMachineInstancePreset("testpreset")
112 server.AppendHandlers(ghttp.CombineHandlers(
113 ghttp.VerifyRequest("POST", path.Join(proxyPath, basePath)),
114 ghttp.RespondWithJSONEncoded(http.StatusCreated, preset),
115 ))
116 createdVMIPreset, err := client.VirtualMachineInstancePreset(k8sv1.NamespaceDefault).Create(context.Background(), preset, k8smetav1.CreateOptions{})
117
118 Expect(server.ReceivedRequests()).To(HaveLen(1))
119 Expect(err).ToNot(HaveOccurred())
120 Expect(createdVMIPreset).To(Equal(preset))
121 },
122 Entry("with regular server URL", ""),
123 Entry("with proxied server URL", proxyPath),
124 )
125
126 DescribeTable("should update a VirtualMachineInstancePreset", func(proxyPath string) {
127 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
128 Expect(err).ToNot(HaveOccurred())
129
130 preset := NewMinimalVirtualMachineInstancePreset("testpreset")
131 server.AppendHandlers(ghttp.CombineHandlers(
132 ghttp.VerifyRequest("PUT", path.Join(proxyPath, presetPath)),
133 ghttp.RespondWithJSONEncoded(http.StatusOK, preset),
134 ))
135 updatedVMIPreset, err := client.VirtualMachineInstancePreset(k8sv1.NamespaceDefault).Update(context.Background(), preset, k8smetav1.UpdateOptions{})
136
137 Expect(server.ReceivedRequests()).To(HaveLen(1))
138 Expect(err).ToNot(HaveOccurred())
139 Expect(updatedVMIPreset).To(Equal(preset))
140 },
141 Entry("with regular server URL", ""),
142 Entry("with proxied server URL", proxyPath),
143 )
144
145 DescribeTable("should delete a VirtualMachineInstancePreset", func(proxyPath string) {
146 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
147 Expect(err).ToNot(HaveOccurred())
148
149 server.AppendHandlers(ghttp.CombineHandlers(
150 ghttp.VerifyRequest("DELETE", path.Join(proxyPath, presetPath)),
151 ghttp.RespondWithJSONEncoded(http.StatusOK, nil),
152 ))
153 err = client.VirtualMachineInstancePreset(k8sv1.NamespaceDefault).Delete(context.Background(), "testpreset", k8smetav1.DeleteOptions{})
154
155 Expect(server.ReceivedRequests()).To(HaveLen(1))
156 Expect(err).ToNot(HaveOccurred())
157 },
158 Entry("with regular server URL", ""),
159 Entry("with proxied server URL", proxyPath),
160 )
161
162 AfterEach(func() {
163 server.Close()
164 })
165 })
166
View as plain text