|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.openwhisk.operation |
| 19 | + |
| 20 | +import akka.http.scaladsl.Http |
| 21 | +import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials} |
| 22 | +import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest, StatusCodes} |
| 23 | +import akka.http.scaladsl.unmarshalling.Unmarshal |
| 24 | +import akka.stream.ActorMaterializer |
| 25 | +import common._ |
| 26 | +import common.rest.HttpConnection |
| 27 | +import org.apache.openwhisk.core.connector.PrewarmContainerDataList |
| 28 | +import org.apache.openwhisk.core.connector.PrewarmContainerDataProtocol._ |
| 29 | +import org.junit.runner.RunWith |
| 30 | +import org.scalatest.Matchers |
| 31 | +import org.scalatest.concurrent.ScalaFutures |
| 32 | +import org.scalatest.junit.JUnitRunner |
| 33 | +import spray.json._ |
| 34 | +import system.rest.RestUtil |
| 35 | + |
| 36 | +import scala.concurrent.duration._ |
| 37 | +import scala.util.Random |
| 38 | + |
| 39 | +@RunWith(classOf[JUnitRunner]) |
| 40 | +class RuntimeConfigurationTests |
| 41 | + extends TestHelpers |
| 42 | + with RestUtil |
| 43 | + with Matchers |
| 44 | + with ScalaFutures |
| 45 | + with WskActorSystem |
| 46 | + with StreamLogging { |
| 47 | + |
| 48 | + implicit val materializer = ActorMaterializer() |
| 49 | + |
| 50 | + val kind = "nodejs:10" |
| 51 | + val memory = 128 |
| 52 | + var count = new Random().nextInt(3) + 1 |
| 53 | + |
| 54 | + def getRuntimes = { |
| 55 | + s""" { |
| 56 | + "runtimes": { |
| 57 | + "nodejs": [{ |
| 58 | + "kind": "${kind}", |
| 59 | + "default": true, |
| 60 | + "image": { |
| 61 | + "prefix": "openwhisk", |
| 62 | + "name": "action-nodejs-v10", |
| 63 | + "tag": "nightly" |
| 64 | + }, |
| 65 | + "deprecated": false, |
| 66 | + "attached": { |
| 67 | + "attachmentName": "codefile", |
| 68 | + "attachmentType": "text/plain" |
| 69 | + }, |
| 70 | + "stemCells": [{ |
| 71 | + "count": ${count}, |
| 72 | + "memory": "${memory} MB" |
| 73 | + }] |
| 74 | + }] |
| 75 | + } |
| 76 | + }""" |
| 77 | + } |
| 78 | + |
| 79 | + val invokerProtocol = WhiskProperties.getInvokerProtocol |
| 80 | + val invokerAddress = WhiskProperties.getBaseInvokerAddress |
| 81 | + val invokerUsername = WhiskProperties.getInvokerUsername |
| 82 | + val invokerPassword = WhiskProperties.getInvokerPassword |
| 83 | + val invokerAuthHeader = Authorization(BasicHttpCredentials(invokerUsername, invokerPassword)) |
| 84 | + |
| 85 | + val controllerProtocol = WhiskProperties.getControllerProtocol |
| 86 | + val controllerAddress = WhiskProperties.getBaseControllerAddress |
| 87 | + val controllerUsername = WhiskProperties.getControllerUsername |
| 88 | + val controllerPassword = WhiskProperties.getControllerPassword |
| 89 | + val controllerAuthHeader = Authorization(BasicHttpCredentials(controllerUsername, controllerPassword)) |
| 90 | + |
| 91 | + val getRuntimeUrl = s"${invokerProtocol}://${invokerAddress}/getRuntime" |
| 92 | + val invokerChangeRuntimeUrl = s"${invokerProtocol}://${invokerAddress}/config/runtime" |
| 93 | + val controllerChangeRuntimeUrl = |
| 94 | + s"${controllerProtocol}://${controllerAddress}/config/runtime" |
| 95 | + |
| 96 | + it should "change assigned invoker node's runtime config directly" in { |
| 97 | + //Change config |
| 98 | + Http() |
| 99 | + .singleRequest( |
| 100 | + HttpRequest( |
| 101 | + method = HttpMethods.POST, |
| 102 | + uri = s"${invokerChangeRuntimeUrl}", |
| 103 | + headers = List(invokerAuthHeader), |
| 104 | + entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, getRuntimes)), |
| 105 | + connectionContext = HttpConnection.getContext(invokerProtocol)) |
| 106 | + .map { response => |
| 107 | + response.status shouldBe StatusCodes.OK |
| 108 | + } |
| 109 | + |
| 110 | + Thread.sleep(5.seconds.toMillis) |
| 111 | + |
| 112 | + //Cal the prewarm container number whether right |
| 113 | + Http() |
| 114 | + .singleRequest( |
| 115 | + HttpRequest(method = HttpMethods.GET, uri = s"${getRuntimeUrl}", headers = List(invokerAuthHeader)), |
| 116 | + connectionContext = HttpConnection.getContext(invokerProtocol)) |
| 117 | + .map { response => |
| 118 | + response.status shouldBe StatusCodes.OK |
| 119 | + val prewarmContainerDataList = |
| 120 | + Unmarshal(response).to[String].futureValue.parseJson.convertTo[PrewarmContainerDataList] |
| 121 | + val nodejs10ContainerData = prewarmContainerDataList.items.filter { prewarmContainerData => |
| 122 | + prewarmContainerData.kind == kind && prewarmContainerData.memory == memory |
| 123 | + } |
| 124 | + nodejs10ContainerData.head.number shouldBe count |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + it should "change all managed invokers's prewarm config via controller" in { |
| 129 | + //Change runtime config |
| 130 | + Http() |
| 131 | + .singleRequest( |
| 132 | + HttpRequest( |
| 133 | + method = HttpMethods.POST, |
| 134 | + uri = s"${controllerChangeRuntimeUrl}", |
| 135 | + headers = List(controllerAuthHeader), |
| 136 | + entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, getRuntimes)), |
| 137 | + connectionContext = HttpConnection.getContext(controllerProtocol)) |
| 138 | + .map { response => |
| 139 | + response.status shouldBe StatusCodes.OK |
| 140 | + } |
| 141 | + |
| 142 | + Thread.sleep(5.seconds.toMillis) |
| 143 | + |
| 144 | + //Cal the prewarm container number whether right |
| 145 | + Http() |
| 146 | + .singleRequest( |
| 147 | + HttpRequest(method = HttpMethods.GET, uri = s"${getRuntimeUrl}", headers = List(invokerAuthHeader)), |
| 148 | + connectionContext = HttpConnection.getContext(invokerProtocol)) |
| 149 | + .map { response => |
| 150 | + response.status shouldBe StatusCodes.OK |
| 151 | + val prewarmContainerDataList = |
| 152 | + Unmarshal(response).to[String].futureValue.parseJson.convertTo[PrewarmContainerDataList] |
| 153 | + val nodejs10ContainerData = prewarmContainerDataList.items.filter { prewarmContainerData => |
| 154 | + prewarmContainerData.kind == kind && prewarmContainerData.memory == memory |
| 155 | + } |
| 156 | + nodejs10ContainerData.head.number shouldBe count |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments