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 v1 "k8s.io/api/autoscaling/v1"
31 k8sv1 "k8s.io/api/core/v1"
32 "k8s.io/apimachinery/pkg/api/errors"
33 k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34 "k8s.io/apimachinery/pkg/runtime/schema"
35 virtv1 "kubevirt.io/api/core/v1"
36 )
37
38 var _ = Describe("Kubevirt VirtualMachineInstanceReplicaSet Client", func() {
39 var server *ghttp.Server
40 basePath := "/apis/kubevirt.io/v1/namespaces/default/virtualmachineinstancereplicasets"
41 rsPath := path.Join(basePath, "testrs")
42 proxyPath := "/proxy/path"
43
44 BeforeEach(func() {
45 server = ghttp.NewServer()
46 })
47
48 DescribeTable("should fetch a VirtualMachineInstanceReplicaSet", func(proxyPath string) {
49 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
50 Expect(err).ToNot(HaveOccurred())
51
52 rs := NewMinimalVirtualMachineInstanceReplicaSet("testrs")
53 server.AppendHandlers(ghttp.CombineHandlers(
54 ghttp.VerifyRequest("GET", path.Join(proxyPath, rsPath)),
55 ghttp.RespondWithJSONEncoded(http.StatusOK, rs),
56 ))
57 fetchedVMIReplicaSet, err := client.ReplicaSet(k8sv1.NamespaceDefault).Get(context.Background(), "testrs", k8smetav1.GetOptions{})
58
59 Expect(server.ReceivedRequests()).To(HaveLen(1))
60 Expect(err).ToNot(HaveOccurred())
61 Expect(fetchedVMIReplicaSet).To(Equal(rs))
62 },
63 Entry("with regular server URL", ""),
64 Entry("with proxied server URL", proxyPath),
65 )
66
67 DescribeTable("should detect non existent VMIReplicaSets", func(proxyPath string) {
68 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
69 Expect(err).ToNot(HaveOccurred())
70
71 server.AppendHandlers(ghttp.CombineHandlers(
72 ghttp.VerifyRequest("GET", path.Join(proxyPath, rsPath)),
73 ghttp.RespondWithJSONEncoded(http.StatusNotFound, errors.NewNotFound(schema.GroupResource{}, "testrs")),
74 ))
75 _, err = client.ReplicaSet(k8sv1.NamespaceDefault).Get(context.Background(), "testrs", k8smetav1.GetOptions{})
76
77 Expect(server.ReceivedRequests()).To(HaveLen(1))
78 Expect(err).To(HaveOccurred())
79 Expect(errors.IsNotFound(err)).To(BeTrue())
80 },
81 Entry("with regular server URL", ""),
82 Entry("with proxied server URL", proxyPath),
83 )
84
85 DescribeTable("should fetch a VirtualMachineInstanceReplicaSet list", func(proxyPath string) {
86 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
87 Expect(err).ToNot(HaveOccurred())
88
89 rs := NewMinimalVirtualMachineInstanceReplicaSet("testrs")
90 server.AppendHandlers(ghttp.CombineHandlers(
91 ghttp.VerifyRequest("GET", path.Join(proxyPath, basePath)),
92 ghttp.RespondWithJSONEncoded(http.StatusOK, NewVirtualMachineInstanceReplicaSetList(*rs)),
93 ))
94 fetchedVMIReplicaSetList, err := client.ReplicaSet(k8sv1.NamespaceDefault).List(context.Background(), k8smetav1.ListOptions{})
95 apiVersion, kind := virtv1.VirtualMachineInstanceReplicaSetGroupVersionKind.ToAPIVersionAndKind()
96
97 Expect(err).ToNot(HaveOccurred())
98 Expect(server.ReceivedRequests()).To(HaveLen(1))
99 Expect(fetchedVMIReplicaSetList.Items).To(HaveLen(1))
100 Expect(fetchedVMIReplicaSetList.Items[0].APIVersion).To(Equal(apiVersion))
101 Expect(fetchedVMIReplicaSetList.Items[0].Kind).To(Equal(kind))
102 Expect(fetchedVMIReplicaSetList.Items[0]).To(Equal(*rs))
103 },
104 Entry("with regular server URL", ""),
105 Entry("with proxied server URL", proxyPath),
106 )
107
108 DescribeTable("should create a VirtualMachineInstanceReplicaSet", func(proxyPath string) {
109 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
110 Expect(err).ToNot(HaveOccurred())
111
112 rs := NewMinimalVirtualMachineInstanceReplicaSet("testrs")
113 server.AppendHandlers(ghttp.CombineHandlers(
114 ghttp.VerifyRequest("POST", path.Join(proxyPath, basePath)),
115 ghttp.RespondWithJSONEncoded(http.StatusCreated, rs),
116 ))
117 createdVMIReplicaSet, err := client.ReplicaSet(k8sv1.NamespaceDefault).Create(context.Background(), rs, k8smetav1.CreateOptions{})
118
119 Expect(server.ReceivedRequests()).To(HaveLen(1))
120 Expect(err).ToNot(HaveOccurred())
121 Expect(createdVMIReplicaSet).To(Equal(rs))
122 },
123 Entry("with regular server URL", ""),
124 Entry("with proxied server URL", proxyPath),
125 )
126
127 DescribeTable("should update a VirtualMachineInstanceReplicaSet", func(proxyPath string) {
128 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
129 Expect(err).ToNot(HaveOccurred())
130
131 rs := NewMinimalVirtualMachineInstanceReplicaSet("testrs")
132 server.AppendHandlers(ghttp.CombineHandlers(
133 ghttp.VerifyRequest("PUT", path.Join(proxyPath, rsPath)),
134 ghttp.RespondWithJSONEncoded(http.StatusOK, rs),
135 ))
136 updatedVMIReplicaSet, err := client.ReplicaSet(k8sv1.NamespaceDefault).Update(context.Background(), rs, k8smetav1.UpdateOptions{})
137
138 Expect(server.ReceivedRequests()).To(HaveLen(1))
139 Expect(err).ToNot(HaveOccurred())
140 Expect(updatedVMIReplicaSet).To(Equal(rs))
141 },
142 Entry("with regular server URL", ""),
143 Entry("with proxied server URL", proxyPath),
144 )
145
146 DescribeTable("should update a VirtualMachineInstanceReplicaSet scale subresource", func(proxyPath string) {
147 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
148 Expect(err).ToNot(HaveOccurred())
149
150 rs := NewMinimalVirtualMachineInstanceReplicaSet("testrs")
151 scale := &v1.Scale{Spec: v1.ScaleSpec{Replicas: 3}}
152 server.AppendHandlers(ghttp.CombineHandlers(
153 ghttp.VerifyRequest("PUT", path.Join(proxyPath, rsPath, "scale")),
154 ghttp.RespondWithJSONEncoded(http.StatusOK, scale),
155 ))
156 scaleResponse, err := client.ReplicaSet(k8sv1.NamespaceDefault).UpdateScale(context.Background(), rs.Name, scale)
157
158 Expect(server.ReceivedRequests()).To(HaveLen(1))
159 Expect(err).ToNot(HaveOccurred())
160 Expect(scaleResponse).To(Equal(scale))
161 },
162 Entry("with regular server URL", ""),
163 Entry("with proxied server URL", proxyPath),
164 )
165
166 DescribeTable("should get a VirtualMachineInstanceReplicaSet scale subresource", func(proxyPath string) {
167 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
168 Expect(err).ToNot(HaveOccurred())
169
170 rs := NewMinimalVirtualMachineInstanceReplicaSet("testrs")
171 scale := &v1.Scale{Spec: v1.ScaleSpec{Replicas: 3}}
172 server.AppendHandlers(ghttp.CombineHandlers(
173 ghttp.VerifyRequest("GET", path.Join(proxyPath, rsPath, "scale")),
174 ghttp.RespondWithJSONEncoded(http.StatusOK, scale),
175 ))
176 scaleResponse, err := client.ReplicaSet(k8sv1.NamespaceDefault).GetScale(context.Background(), rs.Name, k8smetav1.GetOptions{})
177
178 Expect(server.ReceivedRequests()).To(HaveLen(1))
179 Expect(err).ToNot(HaveOccurred())
180 Expect(scaleResponse).To(Equal(scale))
181 },
182 Entry("with regular server URL", ""),
183 Entry("with proxied server URL", proxyPath),
184 )
185
186 DescribeTable("should delete a VirtualMachineInstanceReplicaSet", func(proxyPath string) {
187 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
188 Expect(err).ToNot(HaveOccurred())
189
190 server.AppendHandlers(ghttp.CombineHandlers(
191 ghttp.VerifyRequest("DELETE", path.Join(proxyPath, rsPath)),
192 ghttp.RespondWithJSONEncoded(http.StatusOK, nil),
193 ))
194 err = client.ReplicaSet(k8sv1.NamespaceDefault).Delete(context.Background(), "testrs", k8smetav1.DeleteOptions{})
195
196 Expect(server.ReceivedRequests()).To(HaveLen(1))
197 Expect(err).ToNot(HaveOccurred())
198 },
199 Entry("with regular server URL", ""),
200 Entry("with proxied server URL", proxyPath),
201 )
202
203 AfterEach(func() {
204 server.Close()
205 })
206 })
207
View as plain text