package update import ( "context" "time" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "mashibing.com/pkg/mashibing-deployment/test/framework" ) // 真正的测试函数 // 测试从Ingress模式更新为Nodeport模式 func UpdateI2NMsbDeployment(ctx *framework.TestContext, f *framework.Framework) { var ( // 1. 准备测试数据 ctFilePath = "update/testdata/update-ingress.yaml" ctUpdateFilePath = "update/testdata/update-i2n.yaml" obj = &unstructured.Unstructured{Object: make(map[string]interface{})} objUpdate = &unstructured.Unstructured{Object: make(map[string]interface{})} dc dynamic.Interface cs *kubernetes.Clientset // 3. 准备测试用到的全局变量 msbGVR = schema.GroupVersionResource{ Group: "apps.mashibing.com", Version: "v1", Resource: "msbdeployments", } err error ) BeforeEach(func() { // 2. 加载测试数据 err = f.LoadYamlToUnstructured(ctFilePath, obj) Expect(err).Should(BeNil()) err = f.LoadYamlToUnstructured(ctUpdateFilePath, objUpdate) Expect(err).Should(BeNil()) // 4. 初始化测试用到的全局变量 dc = ctx.CreateDynamicClient() cs = ctx.CreateClientSet() }) Context("Update msbdeployment mod ingress to nodeport", func() { It("Should be create mod ingress success", func() { _, err = dc.Resource(msbGVR).Namespace("default").Create(context.TODO(), obj, metav1.CreateOptions{}) Expect(err).Should(BeNil()) By("Sleep 1 second wait creating done") time.Sleep(time.Second) }) It("Should be exist msbdeployment", func() { _, err = dc.Resource(msbGVR).Namespace("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).Should(BeNil()) }) It("Should be exist deployment", func() { _, err = cs.AppsV1().Deployments("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).Should(BeNil()) }) It("Should be exist service", func() { _, err = cs.CoreV1().Services("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).Should(BeNil()) }) It("Should be exist ingress", func() { _, err = cs.NetworkingV1().Ingresses("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).Should(BeNil()) }) It("Should be update to nodeport success", func() { var md *unstructured.Unstructured md, err = dc.Resource(msbGVR).Namespace("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).Should(BeNil()) objUpdate.SetResourceVersion(md.GetResourceVersion()) _, err = dc.Resource(msbGVR).Namespace("default").Update(context.TODO(), objUpdate, metav1.UpdateOptions{}) Expect(err).Should(BeNil()) By("Sleep 1 second wait creating done") time.Sleep(time.Second) }) It("Should not be exist ingress", func() { _, err = cs.NetworkingV1().Ingresses("default").Get(context.TODO(), objUpdate.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) }) Context("Delete msbdeployment i2n", func() { It("Should be delete mod ingress success", func() { err = dc.Resource(msbGVR).Namespace("default").Delete(context.TODO(), objUpdate.GetName(), metav1.DeleteOptions{}) Expect(err).Should(BeNil()) By("Sleep 3 second wait deleting done") time.Sleep(3 * time.Second) }) It("Should not be exist msbdeployment", func() { _, err = dc.Resource(msbGVR).Namespace("default").Get(context.TODO(), objUpdate.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) It("Should not be exist deployment", func() { _, err = cs.AppsV1().Deployments("default").Get(context.TODO(), objUpdate.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) It("Should not be exist service", func() { _, err = cs.CoreV1().Services("default").Get(context.TODO(), objUpdate.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) It("Should not be exist ingress", func() { _, err = cs.NetworkingV1().Ingresses("default").Get(context.TODO(), objUpdate.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) }) }