package create import ( "context" . "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" "time" "mashibing.com/pkg/mashibing-deployment/test/framework" ) // 真正的测试函数 // 测试创建Ingress模式 func CreateIngressMsbDeployment(ctx *framework.TestContext, f *framework.Framework) { var ( // 1. 准备测试数据 ctFilePath = "create/testdata/create-ingress.yaml" obj = &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()) // 4. 初始化测试用到的全局变量 dc = ctx.CreateDynamicClient() cs = ctx.CreateClientSet() }) Context("Create msbdeployment mod ingress", 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()) }) }) Context("Delete msbdeployment mod ingress", func() { It("Should be delete mod ingress success", func() { err = dc.Resource(msbGVR).Namespace("default").Delete(context.TODO(), obj.GetName(), metav1.DeleteOptions{}) Expect(err).Should(BeNil()) By("Sleep 1 second wait deleting done") time.Sleep(time.Second) }) It("Should not be exist msbdeployment", func() { _, err = dc.Resource(msbGVR).Namespace("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) It("Should not be exist deployment", func() { _, err = cs.AppsV1().Deployments("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) It("Should not be exist service", func() { _, err = cs.CoreV1().Services("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) It("Should not be exist ingress", func() { _, err = cs.NetworkingV1().Ingresses("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{}) Expect(err).ShouldNot(BeNil()) }) }) }