github.com/midokura/kubeedge@v1.2.0-mido.0/tests/e2e/device_crd/device_crd_suit_test.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8     http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package device_crd
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"net/http"
    23  	"path"
    24  	"path/filepath"
    25  	"runtime"
    26  	"testing"
    27  
    28  	. "github.com/onsi/ginkgo"
    29  	. "github.com/onsi/gomega"
    30  
    31  	"github.com/kubeedge/kubeedge/tests/e2e/constants"
    32  	"github.com/kubeedge/kubeedge/tests/e2e/utils"
    33  )
    34  
    35  //context to load config and access across the package
    36  var (
    37  	ctx          *utils.TestContext
    38  	nodeSelector string
    39  	NodeName     string
    40  )
    41  var (
    42  	deviceCRDPath      = "../../../build/crds/devices/devices_v1alpha1_device.yaml"
    43  	deviceModelCRDPath = "../../../build/crds/devices/devices_v1alpha1_devicemodel.yaml"
    44  	crdHandler         = "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions"
    45  	deviceCRD          = "devices.devices.kubeedge.io"
    46  	deviceModelCRD     = "devicemodels.devices.kubeedge.io"
    47  )
    48  
    49  //Function to run the Ginkgo Test
    50  func TestEdgecoreAppDeployment(t *testing.T) {
    51  
    52  	RegisterFailHandler(Fail)
    53  	var _ = BeforeSuite(func() {
    54  		client := &http.Client{}
    55  		utils.Infof("Before Suite Execution")
    56  		ctx = utils.NewTestContext(utils.LoadConfig())
    57  		NodeName = "integration-node-" + utils.GetRandomString(10)
    58  		nodeSelector = "node-" + utils.GetRandomString(3)
    59  
    60  		//Generate Cerificates for Edge and Cloud nodes copy to respective folders
    61  		Expect(utils.GenerateCerts()).Should(BeNil())
    62  		//Do the neccessary config changes in Cloud and Edge nodes
    63  		Expect(utils.DeploySetup(ctx, NodeName, "deployment")).Should(BeNil())
    64  		//Run ./cloudcore binary
    65  		Expect(utils.StartCloudCore()).Should(BeNil())
    66  		//Register the Edge Node to Master
    67  		Expect(utils.RegisterNodeToMaster(NodeName, ctx.Cfg.K8SMasterForKubeEdge+constants.NodeHandler, nodeSelector)).Should(BeNil())
    68  		//Run ./edgecore after node registration
    69  		Expect(utils.StartEdgeCore()).Should(BeNil())
    70  		//Check node successfully registered or not
    71  		Eventually(func() string {
    72  			status := utils.CheckNodeReadyStatus(ctx.Cfg.K8SMasterForKubeEdge+constants.NodeHandler, NodeName)
    73  			utils.Infof("Node Name: %v, Node Status: %v", NodeName, status)
    74  			return status
    75  		}, "60s", "4s").Should(Equal("Running"), "Node register to the k8s master is unsuccessfull !!")
    76  		//Apply the CRDs
    77  		filePath := path.Join(getpwd(), deviceModelCRDPath)
    78  		deviceModelBody, err := ioutil.ReadFile(filePath)
    79  		Expect(err).Should(BeNil())
    80  		BodyBuf := bytes.NewReader(deviceModelBody)
    81  		req, err := http.NewRequest(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+crdHandler, BodyBuf)
    82  		Expect(err).Should(BeNil())
    83  		req.Header.Set("Content-Type", "application/yaml")
    84  		resp, err := client.Do(req)
    85  		Expect(err).Should(BeNil())
    86  		Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
    87  		filePath = path.Join(getpwd(), deviceCRDPath)
    88  		deviceBody, err := ioutil.ReadFile(filePath)
    89  		Expect(err).Should(BeNil())
    90  		BodyBuf = bytes.NewReader(deviceBody)
    91  		req, err = http.NewRequest(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+crdHandler, BodyBuf)
    92  		Expect(err).Should(BeNil())
    93  		req.Header.Set("Content-Type", "application/yaml")
    94  		resp, err = client.Do(req)
    95  		Expect(err).Should(BeNil())
    96  		Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
    97  		err = utils.MqttConnect()
    98  		Expect(err).To(BeNil())
    99  	})
   100  	AfterSuite(func() {
   101  		By("After Suite Execution....!")
   102  		//Deregister the edge node from master
   103  		Expect(utils.DeRegisterNodeFromMaster(ctx.Cfg.K8SMasterForKubeEdge+constants.NodeHandler, NodeName)).Should(BeNil())
   104  		Eventually(func() int {
   105  			statuscode := utils.CheckNodeDeleteStatus(ctx.Cfg.K8SMasterForKubeEdge+constants.NodeHandler, NodeName)
   106  			utils.Infof("Node Name: %v, Node Statuscode: %v", NodeName, statuscode)
   107  			return statuscode
   108  		}, "60s", "4s").Should(Equal(http.StatusNotFound), "Node register to the k8s master is unsuccessfull !!")
   109  		client := &http.Client{}
   110  		req, err := http.NewRequest(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+crdHandler+"/"+deviceModelCRD, nil)
   111  		Expect(err).Should(BeNil())
   112  		req.Header.Set("Content-Type", "application/yaml")
   113  		resp, err := client.Do(req)
   114  		Expect(err).Should(BeNil())
   115  		Expect(resp.StatusCode).Should(Equal(http.StatusOK))
   116  		req, err = http.NewRequest(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+crdHandler+"/"+deviceCRD, nil)
   117  		Expect(err).Should(BeNil())
   118  		req.Header.Set("Content-Type", "application/yaml")
   119  		resp, err = client.Do(req)
   120  		Expect(err).Should(BeNil())
   121  		Expect(resp.StatusCode).Should(Equal(http.StatusOK))
   122  
   123  		//Run the Cleanup steps to kill edgecore and cloudcore binaries
   124  		Expect(utils.CleanUp("device_crd")).Should(BeNil())
   125  		utils.Infof("Cleanup is Successfull !!")
   126  	})
   127  	RunSpecs(t, "kubeedge Device Managemnet Suite")
   128  }
   129  
   130  func getpwd() string {
   131  	_, file, _, _ := runtime.Caller(0)
   132  	dir, err := filepath.Abs(filepath.Dir(file))
   133  	Expect(err).Should(BeNil())
   134  	return dir
   135  }