markusthoemmes commented on a change in pull request #2593: Add BaseWsk class for Wsk and WskRest to inherit URL: https://github.com/apache/incubator-openwhisk/pull/2593#discussion_r138567546
########## File path: tests/src/test/scala/common/BaseWsk.scala ########## @@ -0,0 +1,365 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package common + +import java.io.BufferedWriter +import java.io.File +import java.io.FileWriter +import java.time.Instant + +import scala.concurrent.duration.DurationInt +import scala.collection.mutable.Buffer +import scala.concurrent.duration.Duration +import scala.language.postfixOps +import org.scalatest.Matchers + +import TestUtils._ +import spray.json.JsObject +import spray.json.JsValue +import spray.json.pimpString +import whisk.core.entity.ByteSize + +case class WskProps( + authKey: String = WhiskProperties.readAuthKey(WhiskProperties.getAuthFileForTesting), + cert: String = + WhiskProperties.getFileRelativeToWhiskHome("ansible/roles/nginx/files/openwhisk-client-cert.pem").getAbsolutePath, + key: String = + WhiskProperties.getFileRelativeToWhiskHome("ansible/roles/nginx/files/openwhisk-client-key.pem").getAbsolutePath, + namespace: String = "_", + apiversion: String = "v1", + apihost: String = WhiskProperties.getEdgeHost, + token: String = "") { + def overrides = Seq("-i", "--apihost", apihost, "--apiversion", apiversion) + def writeFile(propsfile: File) = { + val propsStr = + s"NAMESPACE=$namespace\nAPIVERSION=$apiversion\nAUTH=$authKey\nAPIHOST=$apihost\nAPIGW_ACCESS_TOKEN=$token\n" + val bw = new BufferedWriter(new FileWriter(propsfile)) + try { + bw.write(propsStr) + } finally { + bw.close() + } + } +} + +trait WaitFor { + + /** + * Waits up to totalWait seconds for a 'step' to return value. + * Often tests call this routine immediately after starting work. + * Performs an initial wait before entering poll loop. + */ + def waitfor[T](step: () => T, + initialWait: Duration = 1 second, + pollPeriod: Duration = 1 second, + totalWait: Duration = 30 seconds): T = { + Thread.sleep(initialWait.toMillis) + val endTime = System.currentTimeMillis() + totalWait.toMillis + while (System.currentTimeMillis() < endTime) { + val predicate = step() + predicate match { + case (t: Boolean) if t => + return predicate + case (t: Any) if t != null && !t.isInstanceOf[Boolean] => + return predicate + case _ if System.currentTimeMillis() >= endTime => + return predicate + case _ => + Thread.sleep(pollPeriod.toMillis) + } + } + null.asInstanceOf[T] + } +} + +trait BaseWsk extends BaseRunWsk { + val action: BaseAction + val trigger: BaseTrigger + val rule: BaseRule + val activation: BaseActivation + val pkg: BasePackage + val namespace: BaseNamespace + val api: BaseApi +} + +trait FullyQualifiedNames { + + /** + * Fully qualifies the name of an entity with its namespace. + * If the name already starts with the PATHSEP character, then + * it already is fully qualified. Otherwise (package name or + * basic entity name) it is prefixed with the namespace. The + * namespace is derived from the implicit whisk properties. + * + * @param name to fully qualify iff it is not already fully qualified + * @param wp whisk properties + * @return name if it is fully qualified else a name fully qualified for a namespace + */ + def fqn(name: String)(implicit wp: WskProps) = { + val sep = "/" // Namespace.PATHSEP + if (name.startsWith(sep) || name.count(_ == sep(0)) == 2) name + else s"$sep${wp.namespace}$sep$name" + } + + /** + * Resolves a namespace. If argument is defined, it takes precedence. + * else resolve to namespace in implicit WskProps. + * + * @param namespace an optional namespace + * @param wp whisk properties + * @return resolved namespace + */ + def resolve(namespace: Option[String])(implicit wp: WskProps) = { + val sep = "/" // Namespace.PATHSEP + namespace getOrElse s"$sep${wp.namespace}" + } +} + +trait BaseListOrGetFromCollection extends FullyQualifiedNames { + self: BaseRunWsk => Review comment: Do we need the extra qualification to `BaseRunWsk` @rabbah? As `self` is unused, I guess not? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
