|
| 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.core.scheduler |
| 19 | + |
| 20 | +import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ |
| 21 | +import akka.http.scaladsl.model.StatusCodes._ |
| 22 | +import akka.http.scaladsl.model.headers.BasicHttpCredentials |
| 23 | +import akka.http.scaladsl.server.Route |
| 24 | +import akka.http.scaladsl.testkit.ScalatestRouteTest |
| 25 | +import common.StreamLogging |
| 26 | +import org.apache.openwhisk.common.TransactionId |
| 27 | +import org.apache.openwhisk.core.connector.StatusData |
| 28 | +import org.apache.openwhisk.core.entity.SchedulerInstanceId |
| 29 | +import org.junit.runner.RunWith |
| 30 | +import org.scalamock.scalatest.MockFactory |
| 31 | +import org.scalatest.junit.JUnitRunner |
| 32 | +import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FlatSpec, Matchers} |
| 33 | +import spray.json.DefaultJsonProtocol._ |
| 34 | +import spray.json._ |
| 35 | + |
| 36 | +import scala.concurrent.Future |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests SchedulerServer API. |
| 40 | + */ |
| 41 | +@RunWith(classOf[JUnitRunner]) |
| 42 | +class FPCSchedulerServerTests |
| 43 | + extends FlatSpec |
| 44 | + with BeforeAndAfterEach |
| 45 | + with BeforeAndAfterAll |
| 46 | + with ScalatestRouteTest |
| 47 | + with Matchers |
| 48 | + with StreamLogging |
| 49 | + with MockFactory { |
| 50 | + |
| 51 | + def transid() = TransactionId("tid") |
| 52 | + |
| 53 | + val systemUsername = "username" |
| 54 | + val systemPassword = "password" |
| 55 | + |
| 56 | + val queues = List((SchedulerInstanceId("0"), 2), (SchedulerInstanceId("1"), 3)) |
| 57 | + val creationCount = 1 |
| 58 | + val testQueueSize = 2 |
| 59 | + val statusDatas = List( |
| 60 | + StatusData("testns1", "testaction1", 10, "Running", "RunningData"), |
| 61 | + StatusData("testns2", "testaction2", 5, "Running", "RunningData")) |
| 62 | + |
| 63 | + // Create scheduler |
| 64 | + val scheduler = new TestScheduler(queues, creationCount, testQueueSize, statusDatas) |
| 65 | + val server = new FPCSchedulerServer(scheduler, systemUsername, systemPassword) |
| 66 | + |
| 67 | + override protected def afterEach(): Unit = scheduler.reset() |
| 68 | + |
| 69 | + /** FPCSchedulerServer API tests */ |
| 70 | + behavior of "FPCSchedulerServer API" |
| 71 | + |
| 72 | + // POST /disable |
| 73 | + it should "disable scheduler" in { |
| 74 | + implicit val tid = transid() |
| 75 | + val validCredentials = BasicHttpCredentials(systemUsername, systemPassword) |
| 76 | + Post(s"/disable") ~> addCredentials(validCredentials) ~> Route.seal(server.routes(tid)) ~> check { |
| 77 | + status should be(OK) |
| 78 | + scheduler.shutdownCount shouldBe 1 |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // GET /state |
| 83 | + it should "get scheduler state" in { |
| 84 | + implicit val tid = transid() |
| 85 | + val validCredentials = BasicHttpCredentials(systemUsername, systemPassword) |
| 86 | + Get(s"/state") ~> addCredentials(validCredentials) ~> Route.seal(server.routes(tid)) ~> check { |
| 87 | + status should be(OK) |
| 88 | + responseAs[JsObject] shouldBe (queues.map(s => s._1.asString -> s._2.toString).toMap ++ Map( |
| 89 | + "creationCount" -> creationCount.toString)).toJson |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // GET /queue/total |
| 94 | + it should "get total queue" in { |
| 95 | + implicit val tid = transid() |
| 96 | + val validCredentials = BasicHttpCredentials(systemUsername, systemPassword) |
| 97 | + Get(s"/queue/total") ~> addCredentials(validCredentials) ~> Route.seal(server.routes(tid)) ~> check { |
| 98 | + status should be(OK) |
| 99 | + responseAs[String] shouldBe testQueueSize.toString |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // GET /queue/status |
| 104 | + it should "get all queue status" in { |
| 105 | + implicit val tid = transid() |
| 106 | + val validCredentials = BasicHttpCredentials(systemUsername, systemPassword) |
| 107 | + Get(s"/queue/status") ~> addCredentials(validCredentials) ~> Route.seal(server.routes(tid)) ~> check { |
| 108 | + status should be(OK) |
| 109 | + responseAs[List[JsObject]] shouldBe statusDatas.map(_.toJson) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // POST /disable with invalid credential |
| 114 | + it should "not call scheduler api with invalid credential" in { |
| 115 | + implicit val tid = transid() |
| 116 | + val invalidCredentials = BasicHttpCredentials("invaliduser", "invalidpass") |
| 117 | + Post(s"/disable") ~> addCredentials(invalidCredentials) ~> Route.seal(server.routes(tid)) ~> check { |
| 118 | + status should be(Unauthorized) |
| 119 | + scheduler.shutdownCount shouldBe 0 |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // POST /disable with empty credential |
| 124 | + it should "not call scheduler api with empty credential" in { |
| 125 | + implicit val tid = transid() |
| 126 | + Post(s"/disable") ~> Route.seal(server.routes(tid)) ~> check { |
| 127 | + status should be(Unauthorized) |
| 128 | + scheduler.shutdownCount shouldBe 0 |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +class TestScheduler(schedulerStates: List[(SchedulerInstanceId, Int)], |
| 135 | + creationCount: Int, |
| 136 | + queueSize: Int, |
| 137 | + statusDatas: List[StatusData]) |
| 138 | + extends SchedulerCore { |
| 139 | + var shutdownCount = 0 |
| 140 | + |
| 141 | + override def getState: Future[(List[(SchedulerInstanceId, Int)], Int)] = |
| 142 | + Future.successful(schedulerStates, creationCount) |
| 143 | + |
| 144 | + override def getQueueSize: Future[Int] = Future.successful(queueSize) |
| 145 | + |
| 146 | + override def getQueueStatusData: Future[List[StatusData]] = Future.successful(statusDatas) |
| 147 | + |
| 148 | + override def disable(): Unit = shutdownCount += 1 |
| 149 | + |
| 150 | + def reset(): Unit = { |
| 151 | + shutdownCount = 0 |
| 152 | + } |
| 153 | +} |
0 commit comments