github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/tests/e2e/mapper/bluetooth/bluetooth_suite_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 bluetooth 18 19 import ( 20 "bytes" 21 "io/ioutil" 22 "net/http" 23 "os" 24 "os/exec" 25 "path" 26 "path/filepath" 27 "runtime" 28 "testing" 29 "time" 30 31 . "github.com/onsi/ginkgo" 32 . "github.com/onsi/gomega" 33 v1 "k8s.io/api/apps/v1" 34 metav1 "k8s.io/api/core/v1" 35 36 "github.com/kubeedge/kubeedge/cloud/pkg/apis/devices/v1alpha1" 37 "github.com/kubeedge/kubeedge/tests/e2e/utils" 38 ) 39 40 //context to load config and access across the package 41 var ( 42 ctx *utils.TestContext 43 cfg utils.Config 44 nodeSelector string 45 nodeName string 46 ) 47 48 const ( 49 mockHandler = "/apis/devices.kubeedge.io/v1alpha1/namespaces/default/devicemodels" 50 mockInstanceHandler = "/apis/devices.kubeedge.io/v1alpha1/namespaces/default/devices" 51 crdHandler = "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions" 52 appHandler = "/api/v1/namespaces/default/pods" 53 nodeHandler = "/api/v1/nodes" 54 deploymentHandler = "/apis/apps/v1/namespaces/default/deployments" 55 deviceCRD = "devices.devices.kubeedge.io" 56 deviceModelCRD = "devicemodels.devices.kubeedge.io" 57 deviceModelPath = "../../../../build/crds/devices/devices_v1alpha1_devicemodel.yaml" 58 deviceInstancePath = "../../../../build/crds/devices/devices_v1alpha1_device.yaml" 59 devMockInstancePath = "./crds/deviceinstance.yaml" 60 devMockModelPath = "./crds/devicemodel.yaml" 61 makeFilePath = "../../../../mappers/bluetooth_mapper/" 62 sourceConfigPath = "./configuration/config.yaml" 63 destinationConfigPath = "../../../../mappers/bluetooth_mapper/configuration/config.yaml" 64 deployPath = "../../../../mappers/bluetooth_mapper/deployment.yaml" 65 ) 66 67 type Token interface { 68 Wait() bool 69 WaitTimeout(time.Duration) bool 70 Error() error 71 } 72 73 // Testing the basic bluetooth mapper functionalities 74 func TestMapperCharacteristics(t *testing.T) { 75 RegisterFailHandler(Fail) 76 var _ = BeforeSuite(func() { 77 utils.Infof("Before Suite Execution") 78 cfg = utils.LoadConfig() 79 ctx = utils.NewTestContext(cfg) 80 t := time.Now() 81 nodeName = "edge-node-bluetooth" 82 nodeSelector = "node-" + utils.GetRandomString(3) 83 84 //Generate Cerificates for Edge and Cloud nodes copy to respective folders 85 Expect(utils.GenerateCerts()).Should(BeNil()) 86 //Do the neccessary config changes in Cloud and Edge nodes 87 Expect(utils.DeploySetup(ctx, nodeName, "deployment")).Should(BeNil()) 88 89 //Apply CRD for devicemodel 90 curPath := getpwd() 91 file := path.Join(curPath, deviceModelPath) 92 body, err := ioutil.ReadFile(file) 93 Expect(err).Should(BeNil()) 94 client := &http.Client{} 95 BodyBuf := bytes.NewReader(body) 96 req, err := http.NewRequest(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+crdHandler, BodyBuf) 97 Expect(err).Should(BeNil()) 98 req.Header.Set("Content-Type", "application/yaml") 99 resp, err := client.Do(req) 100 Expect(err).To(BeNil()) 101 utils.Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Now().Sub(t)) 102 Expect(resp.StatusCode).Should(Equal(http.StatusCreated)) 103 104 //Apply CRD for deviceinstance 105 curPath = getpwd() 106 file = path.Join(curPath, deviceInstancePath) 107 body, err = ioutil.ReadFile(file) 108 Expect(err).Should(BeNil()) 109 client = &http.Client{} 110 BodyBuf = bytes.NewReader(body) 111 req, err = http.NewRequest(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+crdHandler, BodyBuf) 112 Expect(err).Should(BeNil()) 113 req.Header.Set("Content-Type", "application/yaml") 114 resp, err = client.Do(req) 115 Expect(err).To(BeNil()) 116 utils.Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Now().Sub(t)) 117 Expect(resp.StatusCode).Should(Equal(http.StatusCreated)) 118 119 //Run ./cloudcore binary 120 Expect(utils.StartCloudCore()).Should(BeNil()) 121 122 //Register the Edge Node to Master 123 err = utils.RegisterNodeToMaster(nodeName, ctx.Cfg.K8SMasterForKubeEdge+nodeHandler, nodeSelector) 124 Expect(err).Should(BeNil()) 125 126 //Run ./edgecore after node registration 127 Expect(utils.StartEdgeCore()).Should(BeNil()) 128 129 //Check node successfully registered or not 130 Eventually(func() string { 131 status := utils.CheckNodeReadyStatus(ctx.Cfg.K8SMasterForKubeEdge+nodeHandler, nodeName) 132 utils.Infof("Node Name: %v, Node Status: %v", nodeName, status) 133 return status 134 }, "60s", "4s").Should(Equal("Running"), "Node register to the k8s master is unsuccessfull !!") 135 136 // Adding label to node 137 utils.ApplyLabelToNode(ctx.Cfg.K8SMasterForKubeEdge+nodeHandler+"/"+nodeName, "bluetooth", "true") 138 139 // Changing the config yaml of bluetooth mapper 140 t = time.Now() 141 pwd := getpwd() 142 sourcePath := path.Join(pwd, sourceConfigPath) 143 destinationPath := path.Join(pwd, destinationConfigPath) 144 cmd := exec.Command("cp", sourcePath, destinationPath) 145 err = utils.PrintCombinedOutput(cmd) 146 Expect(err).Should(BeNil()) 147 148 //Building bluetooth mapper 149 curPath = getpwd() 150 newPath := path.Join(curPath, makeFilePath) 151 os.Chdir(newPath) 152 cmd = exec.Command("make", "bluetooth_mapper_image") 153 err = utils.PrintCombinedOutput(cmd) 154 Expect(err).Should(BeNil()) 155 156 //dockertag 157 tagname := cfg.DockerHubUserName + "/bluetooth_mapper:v1.0" 158 cmd = exec.Command("docker", "tag", "bluetooth_mapper:v1.0", tagname) 159 err = utils.PrintCombinedOutput(cmd) 160 Expect(err).Should(BeNil()) 161 162 //docker login 163 cmd = exec.Command("docker", "login", "-u", cfg.DockerHubUserName, "-p", cfg.DockerHubPassword) 164 err = utils.PrintCombinedOutput(cmd) 165 Expect(err).Should(BeNil()) 166 167 //docker push 168 cmd = exec.Command("docker", "push", tagname) 169 err = utils.PrintCombinedOutput(cmd) 170 Expect(err).Should(BeNil()) 171 172 //apply CRD for mock devicemodel 173 curPath = getpwd() 174 file = path.Join(curPath, devMockModelPath) 175 body, err = ioutil.ReadFile(file) 176 Expect(err).Should(BeNil()) 177 client = &http.Client{} 178 BodyBuf = bytes.NewReader(body) 179 req, err = http.NewRequest(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+mockHandler, BodyBuf) 180 Expect(err).Should(BeNil()) 181 req.Header.Set("Content-Type", "application/yaml") 182 resp, err = client.Do(req) 183 Expect(err).To(BeNil()) 184 Expect(resp.StatusCode).Should(Equal(http.StatusCreated)) 185 utils.Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Now().Sub(t)) 186 187 //apply CRD for mock deviceinstance 188 curPath = getpwd() 189 file = path.Join(curPath, devMockInstancePath) 190 body, err = ioutil.ReadFile(file) 191 Expect(err).Should(BeNil()) 192 client = &http.Client{} 193 BodyBuf = bytes.NewReader(body) 194 req, err = http.NewRequest(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+mockInstanceHandler, BodyBuf) 195 Expect(err).Should(BeNil()) 196 req.Header.Set("Content-Type", "application/yaml") 197 resp, err = client.Do(req) 198 Expect(err).To(BeNil()) 199 Expect(resp.StatusCode).Should(Equal(http.StatusCreated)) 200 utils.Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Now().Sub(t)) 201 202 //updating deployment file with edgenode name and dockerhubusername 203 curPath = getpwd() 204 newPath = path.Join(curPath, "../../") 205 os.Chdir(newPath) 206 cmd = exec.Command("bash", "-x", "scripts/bluetoothconfig.sh", ctx.Cfg.DockerHubUserName, nodeName) 207 err = utils.PrintCombinedOutput(cmd) 208 Expect(err).Should(BeNil()) 209 }) 210 211 AfterSuite(func() { 212 By("After Suite Execution....!") 213 //Delete Deployment 214 var podlist metav1.PodList 215 var deploymentList v1.DeploymentList 216 var UID string = "bluetooth-device-mapper-deployment" 217 err := utils.GetDeployments(&deploymentList, ctx.Cfg.K8SMasterForKubeEdge+deploymentHandler) 218 Expect(err).To(BeNil()) 219 for _, deployment := range deploymentList.Items { 220 if deployment.Name == UID { 221 label := nodeName 222 podlist, err = utils.GetPods(ctx.Cfg.K8SMasterForKubeEdge+appHandler, label) 223 Expect(err).To(BeNil()) 224 StatusCode := utils.DeleteDeployment(ctx.Cfg.K8SMasterForKubeEdge+deploymentHandler, deployment.Name) 225 Expect(StatusCode).Should(Equal(http.StatusOK)) 226 } 227 } 228 utils.CheckPodDeleteState(ctx.Cfg.K8SMasterForKubeEdge+appHandler, podlist) 229 230 // Delete mock device instances created 231 var deviceList v1alpha1.DeviceList 232 deviceInstanceList, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+mockInstanceHandler, nil) 233 Expect(err).To(BeNil()) 234 for _, device := range deviceInstanceList { 235 IsDeviceDeleted, statusCode := utils.HandleDeviceInstance(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+mockInstanceHandler, nodeName, "/"+device.Name, "") 236 Expect(IsDeviceDeleted).Should(BeTrue()) 237 Expect(statusCode).Should(Equal(http.StatusOK)) 238 } 239 240 // Delete mock device model created 241 var deviceModelList v1alpha1.DeviceModelList 242 list, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+mockHandler, nil) 243 Expect(err).To(BeNil()) 244 for _, model := range list { 245 IsDeviceModelDeleted, statusCode := utils.HandleDeviceModel(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+mockHandler, "/"+model.Name, "") 246 Expect(IsDeviceModelDeleted).Should(BeTrue()) 247 Expect(statusCode).Should(Equal(http.StatusOK)) 248 } 249 250 //Deleting the created devicemodel and deviceinstance 251 client := &http.Client{} 252 req, err := http.NewRequest(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+crdHandler+"/"+deviceCRD, nil) 253 Expect(err).Should(BeNil()) 254 req.Header.Set("Content-Type", "application/yaml") 255 resp, err := client.Do(req) 256 Expect(err).Should(BeNil()) 257 Expect(resp.StatusCode).Should(Equal(http.StatusOK)) 258 req, err = http.NewRequest(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+crdHandler+"/"+deviceModelCRD, nil) 259 Expect(err).Should(BeNil()) 260 req.Header.Set("Content-Type", "application/yaml") 261 resp, err = client.Do(req) 262 Expect(err).Should(BeNil()) 263 Expect(resp.StatusCode).Should(Equal(http.StatusOK)) 264 265 //Deregister the edge node from master 266 err = utils.DeRegisterNodeFromMaster(ctx.Cfg.K8SMasterForKubeEdge+nodeHandler, nodeName) 267 Expect(err).Should(BeNil()) 268 Eventually(func() int { 269 statuscode := utils.CheckNodeDeleteStatus(ctx.Cfg.K8SMasterForKubeEdge+nodeHandler, nodeName) 270 utils.Infof("Node Name: %v, Node Statuscode: %v", nodeName, statuscode) 271 return statuscode 272 }, "60s", "4s").Should(Equal(http.StatusNotFound), "Node register to the k8s master is unsuccessfull !!") 273 274 Expect(utils.CleanUp("deployment")).Should(BeNil()) 275 time.Sleep(2 * time.Second) 276 277 utils.Infof("Cleanup is Successfull !!") 278 }) 279 RunSpecs(t, "Kubeedge Mapper Test Suite") 280 } 281 282 // This function is used to obtain the present working directory 283 func getpwd() string { 284 _, file, _, _ := runtime.Caller(0) 285 dir, err := filepath.Abs(filepath.Dir(file)) 286 Expect(err).Should(BeNil()) 287 return dir 288 }