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 "k8s.io/apimachinery/pkg/types"
35 v1 "kubevirt.io/api/core/v1"
36 )
37
38 var _ = Describe("Kubevirt Migration Client", func() {
39 var server *ghttp.Server
40 basePath := "/apis/kubevirt.io/v1/namespaces/default/virtualmachineinstancemigrations"
41 migrationPath := path.Join(basePath, "testmigration")
42 proxyPath := "/proxy/path"
43
44 BeforeEach(func() {
45 server = ghttp.NewServer()
46 })
47
48 DescribeTable("should fetch a MigrationMigration", func(proxyPath string) {
49 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
50 Expect(err).ToNot(HaveOccurred())
51
52 migration := NewMinimalMigration("testmigration")
53 server.AppendHandlers(ghttp.CombineHandlers(
54 ghttp.VerifyRequest("GET", path.Join(proxyPath, migrationPath)),
55 ghttp.RespondWithJSONEncoded(http.StatusOK, migration),
56 ))
57 fetchedMigration, err := client.VirtualMachineInstanceMigration(k8sv1.NamespaceDefault).Get(context.Background(), "testmigration", k8smetav1.GetOptions{})
58
59 Expect(server.ReceivedRequests()).To(HaveLen(1))
60 Expect(err).ToNot(HaveOccurred())
61 Expect(fetchedMigration).To(Equal(migration))
62 },
63 Entry("with regular server URL", ""),
64 Entry("with proxied server URL", proxyPath),
65 )
66
67 DescribeTable("should detect non existent Migrations", 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, migrationPath)),
73 ghttp.RespondWithJSONEncoded(http.StatusNotFound, errors.NewNotFound(schema.GroupResource{}, "testmigration")),
74 ))
75 _, err = client.VirtualMachineInstanceMigration(k8sv1.NamespaceDefault).Get(context.Background(), "testmigration", 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 Migration list", func(proxyPath string) {
86 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
87 Expect(err).ToNot(HaveOccurred())
88
89 migration := NewMinimalMigration("testmigration")
90 server.AppendHandlers(ghttp.CombineHandlers(
91 ghttp.VerifyRequest("GET", path.Join(proxyPath, basePath)),
92 ghttp.RespondWithJSONEncoded(http.StatusOK, NewMigrationList(*migration)),
93 ))
94 fetchedMigrationList, err := client.VirtualMachineInstanceMigration(k8sv1.NamespaceDefault).List(context.Background(), k8smetav1.ListOptions{})
95 apiVersion, kind := v1.VirtualMachineInstanceMigrationGroupVersionKind.ToAPIVersionAndKind()
96
97 Expect(server.ReceivedRequests()).To(HaveLen(1))
98 Expect(err).ToNot(HaveOccurred())
99 Expect(fetchedMigrationList.Items).To(HaveLen(1))
100 Expect(fetchedMigrationList.Items[0].APIVersion).To(Equal(apiVersion))
101 Expect(fetchedMigrationList.Items[0].Kind).To(Equal(kind))
102 Expect(fetchedMigrationList.Items[0]).To(Equal(*migration))
103 },
104 Entry("with regular server URL", ""),
105 Entry("with proxied server URL", proxyPath),
106 )
107
108 DescribeTable("should create a Migration", func(proxyPath string) {
109 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
110 Expect(err).ToNot(HaveOccurred())
111
112 migration := NewMinimalMigration("testmigration")
113 server.AppendHandlers(ghttp.CombineHandlers(
114 ghttp.VerifyRequest("POST", path.Join(proxyPath, basePath)),
115 ghttp.RespondWithJSONEncoded(http.StatusCreated, migration),
116 ))
117 createdMigration, err := client.VirtualMachineInstanceMigration(k8sv1.NamespaceDefault).Create(context.Background(), migration, k8smetav1.CreateOptions{})
118
119 Expect(server.ReceivedRequests()).To(HaveLen(1))
120 Expect(err).ToNot(HaveOccurred())
121 Expect(createdMigration).To(Equal(migration))
122 },
123 Entry("with regular server URL", ""),
124 Entry("with proxied server URL", proxyPath),
125 )
126
127 DescribeTable("should update a Migration", func(proxyPath string) {
128 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
129 Expect(err).ToNot(HaveOccurred())
130
131 migration := NewMinimalMigration("testmigration")
132 server.AppendHandlers(ghttp.CombineHandlers(
133 ghttp.VerifyRequest("PUT", path.Join(proxyPath, migrationPath)),
134 ghttp.RespondWithJSONEncoded(http.StatusOK, migration),
135 ))
136 updatedMigration, err := client.VirtualMachineInstanceMigration(k8sv1.NamespaceDefault).Update(context.Background(), migration, k8smetav1.UpdateOptions{})
137
138 Expect(server.ReceivedRequests()).To(HaveLen(1))
139 Expect(err).ToNot(HaveOccurred())
140 Expect(updatedMigration).To(Equal(migration))
141 },
142 Entry("with regular server URL", ""),
143 Entry("with proxied server URL", proxyPath),
144 )
145
146 DescribeTable("should patch a Migration", func(proxyPath string) {
147 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
148 Expect(err).ToNot(HaveOccurred())
149
150 migration := NewMinimalMigration("testmigration")
151 migration.Spec.VMIName = "somethingelse"
152
153 server.AppendHandlers(ghttp.CombineHandlers(
154 ghttp.VerifyRequest("PATCH", path.Join(proxyPath, migrationPath)),
155 ghttp.VerifyBody([]byte("{\"spec\":{\"vmiName\":something}}")),
156 ghttp.RespondWithJSONEncoded(http.StatusOK, migration),
157 ))
158
159 _, err = client.VirtualMachineInstanceMigration(k8sv1.NamespaceDefault).Patch(context.Background(), migration.Name, types.MergePatchType,
160 []byte("{\"spec\":{\"vmiName\":something}}"), k8smetav1.PatchOptions{})
161
162 Expect(server.ReceivedRequests()).To(HaveLen(1))
163 Expect(err).ToNot(HaveOccurred())
164 },
165 Entry("with regular server URL", ""),
166 Entry("with proxied server URL", proxyPath),
167 )
168
169 DescribeTable("should delete a Migration", func(proxyPath string) {
170 client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
171 Expect(err).ToNot(HaveOccurred())
172
173 server.AppendHandlers(ghttp.CombineHandlers(
174 ghttp.VerifyRequest("DELETE", path.Join(proxyPath, migrationPath)),
175 ghttp.RespondWithJSONEncoded(http.StatusOK, nil),
176 ))
177 err = client.VirtualMachineInstanceMigration(k8sv1.NamespaceDefault).Delete(context.Background(), "testmigration", k8smetav1.DeleteOptions{})
178
179 Expect(server.ReceivedRequests()).To(HaveLen(1))
180 Expect(err).ToNot(HaveOccurred())
181 },
182 Entry("with regular server URL", ""),
183 Entry("with proxied server URL", proxyPath),
184 )
185
186 AfterEach(func() {
187 server.Close()
188 })
189 })
190
View as plain text