...

Source file src/kubevirt.io/client-go/kubecli/migrationpolicy_test.go

Documentation: kubevirt.io/client-go/kubecli

     1  package kubecli
     2  
     3  /*
     4  * This file is part of the KubeVirt project
     5  *
     6  * Licensed under the Apache License, Version 2.0 (the "License");
     7  * you may not use this file except in compliance with the License.
     8  * You may obtain a copy of the License at
     9  *
    10  *     http://www.apache.org/licenses/LICENSE-2.0
    11  *
    12  * Unless required by applicable law or agreed to in writing, software
    13  * distributed under the License is distributed on an "AS IS" BASIS,
    14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  * See the License for the specific language governing permissions and
    16  * limitations under the License.
    17  *
    18  * Copyright 2021 Red Hat, Inc.
    19  *
    20   */
    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  
    31  	v1alpha12 "kubevirt.io/api/migrations/v1alpha1"
    32  
    33  	"k8s.io/apimachinery/pkg/api/errors"
    34  	k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    35  	"k8s.io/apimachinery/pkg/runtime/schema"
    36  )
    37  
    38  const (
    39  	testPolicyName = "testpolicy"
    40  	proxyPath      = "/proxy/path"
    41  )
    42  
    43  var _ = Describe("Kubevirt MigrationPolicy Client", func() {
    44  
    45  	var server *ghttp.Server
    46  	var basePath, policyPath string
    47  
    48  	BeforeEach(func() {
    49  		basePath = "/apis/migrations.kubevirt.io/v1alpha1/migrationpolicies"
    50  		policyPath = path.Join(basePath, testPolicyName)
    51  
    52  		server = ghttp.NewServer()
    53  	})
    54  
    55  	expectPoliciesAreEqual := func(actual, expect *v1alpha12.MigrationPolicy) {
    56  		// TODO: Workaround until I figure out what's wrong here
    57  		actual.Kind = expect.Kind
    58  		actual.APIVersion = expect.APIVersion
    59  		Expect(actual).To(Equal(expect))
    60  	}
    61  
    62  	DescribeTable("should fetch a MigrationPolicy", func(proxyPath string) {
    63  		client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
    64  		Expect(err).ToNot(HaveOccurred())
    65  
    66  		policy := NewMinimalMigrationPolicy(testPolicyName)
    67  		server.AppendHandlers(ghttp.CombineHandlers(
    68  			ghttp.VerifyRequest("GET", path.Join(proxyPath, policyPath)),
    69  			ghttp.RespondWithJSONEncoded(http.StatusOK, policy),
    70  		))
    71  		fetchedMigrationPolicy, err := client.MigrationPolicy().Get(context.Background(), testPolicyName, k8smetav1.GetOptions{})
    72  
    73  		Expect(server.ReceivedRequests()).To(HaveLen(1))
    74  		Expect(err).ToNot(HaveOccurred())
    75  		expectPoliciesAreEqual(fetchedMigrationPolicy, policy)
    76  	},
    77  		Entry("with regular server URL", ""),
    78  		Entry("with proxied server URL", proxyPath),
    79  	)
    80  
    81  	DescribeTable("should detect non existent Migration Policy", func(proxyPath string) {
    82  		client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
    83  		Expect(err).ToNot(HaveOccurred())
    84  
    85  		server.AppendHandlers(ghttp.CombineHandlers(
    86  			ghttp.VerifyRequest("GET", path.Join(proxyPath, policyPath)),
    87  			ghttp.RespondWithJSONEncoded(http.StatusNotFound, errors.NewNotFound(schema.GroupResource{}, testPolicyName)),
    88  		))
    89  		_, err = client.MigrationPolicy().Get(context.Background(), testPolicyName, k8smetav1.GetOptions{})
    90  
    91  		Expect(server.ReceivedRequests()).To(HaveLen(1))
    92  		Expect(err).To(HaveOccurred())
    93  		Expect(errors.IsNotFound(err)).To(BeTrue())
    94  	},
    95  		Entry("with regular server URL", ""),
    96  		Entry("with proxied server URL", proxyPath),
    97  	)
    98  
    99  	DescribeTable("should fetch a MigrationPolicy list", func(proxyPath string) {
   100  		client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
   101  		Expect(err).ToNot(HaveOccurred())
   102  
   103  		policy := NewMinimalMigrationPolicy(testPolicyName)
   104  		server.AppendHandlers(ghttp.CombineHandlers(
   105  			ghttp.VerifyRequest("GET", path.Join(proxyPath, basePath)),
   106  			ghttp.RespondWithJSONEncoded(http.StatusOK, NewMinimalMigrationPolicyList(*policy)),
   107  		))
   108  		fetchedMigrationPolicy, err := client.MigrationPolicy().List(context.Background(), k8smetav1.ListOptions{})
   109  
   110  		Expect(err).ToNot(HaveOccurred())
   111  		Expect(server.ReceivedRequests()).To(HaveLen(1))
   112  		Expect(fetchedMigrationPolicy.Items).To(HaveLen(1))
   113  		expectPoliciesAreEqual(&fetchedMigrationPolicy.Items[0], policy)
   114  	},
   115  		Entry("with regular server URL", ""),
   116  		Entry("with proxied server URL", proxyPath),
   117  	)
   118  
   119  	DescribeTable("should create a MigrationPolicy", func(proxyPath string) {
   120  		client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
   121  		Expect(err).ToNot(HaveOccurred())
   122  
   123  		policy := NewMinimalMigrationPolicy(testPolicyName)
   124  		server.AppendHandlers(ghttp.CombineHandlers(
   125  			ghttp.VerifyRequest("POST", path.Join(proxyPath, basePath)),
   126  			ghttp.RespondWithJSONEncoded(http.StatusCreated, policy),
   127  		))
   128  		createdMigrationPolicy, err := client.MigrationPolicy().Create(context.Background(), policy, k8smetav1.CreateOptions{})
   129  
   130  		Expect(server.ReceivedRequests()).To(HaveLen(1))
   131  		Expect(err).ToNot(HaveOccurred())
   132  		expectPoliciesAreEqual(createdMigrationPolicy, policy)
   133  	},
   134  		Entry("with regular server URL", ""),
   135  		Entry("with proxied server URL", proxyPath),
   136  	)
   137  
   138  	DescribeTable("should update a MigrationPolicy", func(proxyPath string) {
   139  		client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
   140  		Expect(err).ToNot(HaveOccurred())
   141  
   142  		policy := NewMinimalMigrationPolicy(testPolicyName)
   143  		server.AppendHandlers(ghttp.CombineHandlers(
   144  			ghttp.VerifyRequest("PUT", path.Join(proxyPath, policyPath)),
   145  			ghttp.RespondWithJSONEncoded(http.StatusOK, policy),
   146  		))
   147  		updatedMigrationPolicy, err := client.MigrationPolicy().Update(context.Background(), policy, k8smetav1.UpdateOptions{})
   148  
   149  		Expect(server.ReceivedRequests()).To(HaveLen(1))
   150  		Expect(err).ToNot(HaveOccurred())
   151  		expectPoliciesAreEqual(updatedMigrationPolicy, policy)
   152  	},
   153  		Entry("with regular server URL", ""),
   154  		Entry("with proxied server URL", proxyPath),
   155  	)
   156  
   157  	DescribeTable("should delete a MigrationPolicy", func(proxyPath string) {
   158  		client, err := GetKubevirtClientFromFlags(server.URL()+proxyPath, "")
   159  		Expect(err).ToNot(HaveOccurred())
   160  
   161  		server.AppendHandlers(ghttp.CombineHandlers(
   162  			ghttp.VerifyRequest("DELETE", path.Join(proxyPath, policyPath)),
   163  			ghttp.RespondWithJSONEncoded(http.StatusOK, nil),
   164  		))
   165  		err = client.MigrationPolicy().Delete(context.Background(), testPolicyName, k8smetav1.DeleteOptions{})
   166  
   167  		Expect(server.ReceivedRequests()).To(HaveLen(1))
   168  		Expect(err).ToNot(HaveOccurred())
   169  	},
   170  		Entry("with regular server URL", ""),
   171  		Entry("with proxied server URL", proxyPath),
   172  	)
   173  
   174  	AfterEach(func() {
   175  		server.Close()
   176  	})
   177  })
   178  

View as plain text