|
| 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.controller.test |
| 19 | + |
| 20 | +import org.junit.runner.RunWith |
| 21 | +import org.scalatest.junit.JUnitRunner |
| 22 | +import akka.http.scaladsl.model.StatusCodes.{BadRequest, MethodNotAllowed, OK} |
| 23 | +import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport.sprayJsonUnmarshaller |
| 24 | +import akka.http.scaladsl.server.Route |
| 25 | +import org.apache.openwhisk.core.controller.WhiskLimitsApi |
| 26 | +import org.apache.openwhisk.core.entity.{EntityPath, UserLimits} |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests Packages API. |
| 30 | + * |
| 31 | + * Unit tests of the controller service as a standalone component. |
| 32 | + * These tests exercise a fresh instance of the service object in memory -- these |
| 33 | + * tests do NOT communication with a whisk deployment. |
| 34 | + * |
| 35 | + * @Idioglossia |
| 36 | + * "using Specification DSL to write unit tests, as in should, must, not, be" |
| 37 | + * "using Specs2RouteTest DSL to chain HTTP requests for unit testing, as in ~>" |
| 38 | + */ |
| 39 | +@RunWith(classOf[JUnitRunner]) |
| 40 | +class LimitsApiTests extends ControllerTestCommon with WhiskLimitsApi { |
| 41 | + |
| 42 | + /** Limits API tests */ |
| 43 | + behavior of "Limits API" |
| 44 | + |
| 45 | + // test namespace limit configurations |
| 46 | + val testInvokesPerMinute = 100 |
| 47 | + val testConcurrent = 200 |
| 48 | + val testFiresPerMinute = 300 |
| 49 | + val testAllowedKinds = Set("java:8") |
| 50 | + val testStoreActivations = false |
| 51 | + |
| 52 | + val creds = WhiskAuthHelpers.newIdentity() |
| 53 | + val credsWithSetLimits = WhiskAuthHelpers |
| 54 | + .newIdentity() |
| 55 | + .copy( |
| 56 | + limits = UserLimits( |
| 57 | + Some(testInvokesPerMinute), |
| 58 | + Some(testConcurrent), |
| 59 | + Some(testFiresPerMinute), |
| 60 | + Some(testAllowedKinds), |
| 61 | + Some(testStoreActivations))) |
| 62 | + val namespace = EntityPath(creds.subject.asString) |
| 63 | + val collectionPath = s"/${EntityPath.DEFAULT}/${collection.path}" |
| 64 | + |
| 65 | + //// GET /limits |
| 66 | + it should "list default system limits if no namespace limits are set" in { |
| 67 | + implicit val tid = transid() |
| 68 | + Seq("", "/").foreach { p => |
| 69 | + Get(collectionPath + p) ~> Route.seal(routes(creds)) ~> check { |
| 70 | + status should be(OK) |
| 71 | + responseAs[UserLimits].invocationsPerMinute shouldBe Some(whiskConfig.actionInvokePerMinuteLimit.toInt) |
| 72 | + responseAs[UserLimits].concurrentInvocations shouldBe Some(whiskConfig.actionInvokeConcurrentLimit.toInt) |
| 73 | + responseAs[UserLimits].firesPerMinute shouldBe Some(whiskConfig.triggerFirePerMinuteLimit.toInt) |
| 74 | + responseAs[UserLimits].allowedKinds shouldBe None |
| 75 | + responseAs[UserLimits].storeActivations shouldBe None |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + it should "list set limits if limits have been set for the namespace" in { |
| 81 | + implicit val tid = transid() |
| 82 | + Seq("", "/").foreach { p => |
| 83 | + Get(collectionPath + p) ~> Route.seal(routes(credsWithSetLimits)) ~> check { |
| 84 | + status should be(OK) |
| 85 | + responseAs[UserLimits].invocationsPerMinute shouldBe Some(testInvokesPerMinute) |
| 86 | + responseAs[UserLimits].concurrentInvocations shouldBe Some(testConcurrent) |
| 87 | + responseAs[UserLimits].firesPerMinute shouldBe Some(testFiresPerMinute) |
| 88 | + responseAs[UserLimits].allowedKinds shouldBe Some(testAllowedKinds) |
| 89 | + responseAs[UserLimits].storeActivations shouldBe Some(testStoreActivations) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + it should "reject requests for unsupported methods" in { |
| 95 | + implicit val tid = transid() |
| 96 | + Seq(Put, Post, Delete).foreach { m => |
| 97 | + m(collectionPath) ~> Route.seal(routes(creds)) ~> check { |
| 98 | + status should be(MethodNotAllowed) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + it should "reject all methods for entity level request" in { |
| 104 | + implicit val tid = transid() |
| 105 | + Seq(Put, Post, Delete).foreach { m => |
| 106 | + m(s"$collectionPath/limitsEntity") ~> Route.seal(routes(creds)) ~> check { |
| 107 | + status should be(MethodNotAllowed) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + Seq(Get).foreach { m => |
| 112 | + m(s"$collectionPath/limitsEntity") ~> Route.seal(routes(creds)) ~> check { |
| 113 | + status should be(BadRequest) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments