Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ningyougang committed Jan 14, 2020
1 parent ffe10a9 commit 556b5bd
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ansible/templates/whisk.properties.j2
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ kafka.hosts={{ kafka_connect_string }}
redis.host={{ groups["redis"]|default([""])|first }}
router.host={{ groups["edge"]|first }}
zookeeper.hosts={{ zookeeper_connect_string }}
invoker.protocol={{ invoker.protocol }}
invoker.hosts={{ groups["invokers"] | map('extract', hostvars, 'ansible_host') | list | join(",") }}
invoker.username={{ invoker.username }}
invoker.password={{ invoker.password }}

edge.host.apiport=443
kafkaras.host.port={{ kafka.ras.port }}
Expand All @@ -57,6 +60,8 @@ controller.hosts={{ groups["controllers"] | map('extract', hostvars, 'ansible_ho
controller.host.basePort={{ controller.basePort }}
controller.instances={{ controller.instances }}
controller.protocol={{ controller.protocol }}
controller.username={{ controller.username }}
controller.password={{ controller.password }}

invoker.container.network=bridge
invoker.container.policy={{ invoker_container_policy_name | default()}}
Expand Down
2 changes: 2 additions & 0 deletions tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def systemIncludes = [
"org/apache/openwhisk/core/apigw/actions/test/**",
"org/apache/openwhisk/core/database/test/*CacheConcurrencyTests*",
"org/apache/openwhisk/core/controller/test/*ControllerApiTests*",
"org/apache/openwhisk/operation/**",
"apigw/healthtests/**",
"ha/**",
"services/**",
Expand All @@ -70,6 +71,7 @@ ext.testSets = [
"org/apache/openwhisk/standalone/**",
"org/apache/openwhisk/core/cli/test/**",
"org/apache/openwhisk/core/limits/**",
"org/apache/openwhisk/operation/**",
"**/*CacheConcurrencyTests*",
"**/*ControllerApiTests*",
"org/apache/openwhisk/testEntities/**",
Expand Down
30 changes: 30 additions & 0 deletions tests/src/test/scala/common/WhiskProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,40 @@ public static int getControllerBasePort() {
return Integer.parseInt(whiskProperties.getProperty("controller.host.basePort"));
}

public static String getControllerProtocol() {
return whiskProperties.getProperty("controller.protocol");
}

public static String getBaseControllerHost() {
return getControllerHosts().split(",")[0];
}

public static String getInvokerProtocol() {
return whiskProperties.getProperty("invoker.protocol");
}


public static String getBaseInvokerAddress(){
return getInvokerHosts()[0] + ":" + whiskProperties.getProperty("invoker.hosts.basePort");
}

public static String getControllerUsername() {
return whiskProperties.getProperty("controller.username");
}

public static String getControllerPassword() {
return whiskProperties.getProperty("controller.password");
}


public static String getInvokerUsername() {
return whiskProperties.getProperty("invoker.username");
}

public static String getInvokerPassword() {
return whiskProperties.getProperty("invoker.password");
}

public static String getBaseDBHost() {
return getDBHosts().split(",")[0];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package org.apache.openwhisk.operation

import akka.http.scaladsl.Http
import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest, StatusCodes}
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import common._
import common.rest.HttpConnection
import org.apache.openwhisk.core.connector.PrewarmContainerDataList
import org.apache.openwhisk.core.connector.PrewarmContainerDataProtocol._
import org.junit.runner.RunWith
import org.scalatest.Matchers
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.junit.JUnitRunner
import spray.json._
import system.rest.RestUtil

import scala.concurrent.duration._
import scala.util.Random

@RunWith(classOf[JUnitRunner])
class RuntimeConfigurationTests
extends TestHelpers
with RestUtil
with Matchers
with ScalaFutures
with WskActorSystem
with StreamLogging {

implicit val materializer = ActorMaterializer()

val kind = "nodejs:10"
val memory = 128
var count = new Random().nextInt(3) + 1

def getRuntimes = {
s""" {
"runtimes": {
"nodejs": [{
"kind": "${kind}",
"default": true,
"image": {
"prefix": "openwhisk",
"name": "action-nodejs-v10",
"tag": "nightly"
},
"deprecated": false,
"attached": {
"attachmentName": "codefile",
"attachmentType": "text/plain"
},
"stemCells": [{
"count": ${count},
"memory": "${memory} MB"
}]
}]
}
}"""
}

val invokerProtocol = WhiskProperties.getInvokerProtocol
val invokerAddress = WhiskProperties.getBaseInvokerAddress
val invokerUsername = WhiskProperties.getInvokerUsername
val invokerPassword = WhiskProperties.getInvokerPassword
val invokerAuthHeader = Authorization(BasicHttpCredentials(invokerUsername, invokerPassword))

val controllerProtocol = WhiskProperties.getControllerProtocol
val controllerAddress = WhiskProperties.getBaseControllerAddress
val controllerUsername = WhiskProperties.getControllerUsername
val controllerPassword = WhiskProperties.getControllerPassword
val controllerAuthHeader = Authorization(BasicHttpCredentials(controllerUsername, controllerPassword))

val getRuntimeUrl = s"${invokerProtocol}://${invokerAddress}/getRuntime"
val invokerChangeRuntimeUrl = s"${invokerProtocol}://${invokerAddress}/config/runtime"
val controllerChangeRuntimeUrl =
s"${controllerProtocol}://${controllerAddress}/config/runtime"

it should "change assigned invoker node's runtime config directly" in {
//Change config
Http()
.singleRequest(
HttpRequest(
method = HttpMethods.POST,
uri = s"${invokerChangeRuntimeUrl}",
headers = List(invokerAuthHeader),
entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, getRuntimes)),
connectionContext = HttpConnection.getContext(invokerProtocol))
.map { response =>
response.status shouldBe StatusCodes.OK
}

Thread.sleep(5.seconds.toMillis)

//Cal the prewarm container number whether right
Http()
.singleRequest(
HttpRequest(method = HttpMethods.GET, uri = s"${getRuntimeUrl}", headers = List(invokerAuthHeader)),
connectionContext = HttpConnection.getContext(invokerProtocol))
.map { response =>
response.status shouldBe StatusCodes.OK
val prewarmContainerDataList =
Unmarshal(response).to[String].futureValue.parseJson.convertTo[PrewarmContainerDataList]
val nodejs10ContainerData = prewarmContainerDataList.items.filter { prewarmContainerData =>
prewarmContainerData.kind == kind && prewarmContainerData.memory == memory
}
nodejs10ContainerData.head.number shouldBe count
}
}

it should "change all managed invokers's prewarm config via controller" in {
//Change runtime config
Http()
.singleRequest(
HttpRequest(
method = HttpMethods.POST,
uri = s"${controllerChangeRuntimeUrl}",
headers = List(controllerAuthHeader),
entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, getRuntimes)),
connectionContext = HttpConnection.getContext(controllerProtocol))
.map { response =>
response.status shouldBe StatusCodes.OK
}

Thread.sleep(5.seconds.toMillis)

//Cal the prewarm container number whether right
Http()
.singleRequest(
HttpRequest(method = HttpMethods.GET, uri = s"${getRuntimeUrl}", headers = List(invokerAuthHeader)),
connectionContext = HttpConnection.getContext(invokerProtocol))
.map { response =>
response.status shouldBe StatusCodes.OK
val prewarmContainerDataList =
Unmarshal(response).to[String].futureValue.parseJson.convertTo[PrewarmContainerDataList]
val nodejs10ContainerData = prewarmContainerDataList.items.filter { prewarmContainerData =>
prewarmContainerData.kind == kind && prewarmContainerData.memory == memory
}
nodejs10ContainerData.head.number shouldBe count
}
}
}

0 comments on commit 556b5bd

Please sign in to comment.