|
| 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.apache.openwhisk.common.TransactionId |
| 21 | +import org.apache.openwhisk.core.controller.RejectRequest |
| 22 | +import org.apache.openwhisk.core.entitlement.{EntitlementProvider, FPCEntitlementProvider, Privilege, Resource} |
| 23 | +import org.apache.openwhisk.core.entitlement.Privilege.{ACTIVATE, DELETE, PUT, READ, REJECT} |
| 24 | +import org.apache.openwhisk.core.entity.{EntityName, EntityPath, FullyQualifiedEntityName} |
| 25 | +import org.apache.openwhisk.core.loadBalancer.LoadBalancer |
| 26 | +import org.junit.runner.RunWith |
| 27 | +import org.scalamock.scalatest.MockFactory |
| 28 | +import org.scalatest.concurrent.ScalaFutures |
| 29 | +import org.scalatest.junit.JUnitRunner |
| 30 | + |
| 31 | +@RunWith(classOf[JUnitRunner]) |
| 32 | +class FPCEntitlementProviderTests extends ControllerTestCommon with ScalaFutures with MockFactory { |
| 33 | + |
| 34 | + implicit val transactionId = TransactionId.testing |
| 35 | + |
| 36 | + it should "get throttle flag from loadBalancer" in { |
| 37 | + val someUser = WhiskAuthHelpers.newIdentity() |
| 38 | + val action = FullyQualifiedEntityName(EntityPath("testns"), EntityName("action")) |
| 39 | + val loadBalancer = mock[LoadBalancer] |
| 40 | + (loadBalancer |
| 41 | + .checkThrottle(_: EntityPath, _: String)) |
| 42 | + .expects(someUser.namespace.name.toPath, action.fullPath.asString) |
| 43 | + .returning(true) |
| 44 | + val resources = Set(Resource(action.path, ACTIONS, Some(action.name.name))) |
| 45 | + |
| 46 | + val entitlementProvider: EntitlementProvider = new FPCEntitlementProvider(whiskConfig, loadBalancer, instance) |
| 47 | + entitlementProvider.checkThrottles(someUser, ACTIVATE, resources).failed.futureValue shouldBe a[RejectRequest] |
| 48 | + |
| 49 | + Seq[Privilege](READ, PUT, DELETE, REJECT).foreach(OP => { |
| 50 | + noException shouldBe thrownBy(entitlementProvider.checkThrottles(someUser, OP, resources).futureValue) |
| 51 | + }) |
| 52 | + |
| 53 | + val action2 = FullyQualifiedEntityName(EntityPath("testns2"), EntityName("action2")) |
| 54 | + val resources2 = Set(Resource(action2.path, ACTIONS, Some(action2.name.name))) |
| 55 | + (loadBalancer |
| 56 | + .checkThrottle(_: EntityPath, _: String)) |
| 57 | + .expects(someUser.namespace.name.toPath, action2.fullPath.asString) |
| 58 | + .returning(false) |
| 59 | + noException shouldBe thrownBy(entitlementProvider.checkThrottles(someUser, ACTIVATE, resources2).futureValue) |
| 60 | + } |
| 61 | +} |
0 commit comments