-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathWskConsoleTests.scala
More file actions
134 lines (118 loc) · 5.71 KB
/
WskConsoleTests.scala
File metadata and controls
134 lines (118 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* 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 system.basic;
import java.time.Instant
import scala.concurrent.duration.Duration
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.MILLISECONDS
import scala.language.postfixOps
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import common.TestHelpers
import common.TestCLIUtils
import common.BaseWsk
import common.WskProps
import common.WskTestHelpers
import spray.json.DefaultJsonProtocol.IntJsonFormat
import spray.json.DefaultJsonProtocol.StringJsonFormat
import spray.json.pimpAny
/**
* Tests of the text console
*/
@RunWith(classOf[JUnitRunner])
abstract class WskConsoleTests extends TestHelpers with WskTestHelpers {
implicit val wskprops = WskProps()
val wsk: BaseWsk
val guestNamespace = wskprops.namespace
/**
* Append the current timestamp in ms
*/
def withTimestamp(text: String) = s"${text}-${System.currentTimeMillis}"
behavior of "Wsk Activation Console"
it should "show an activation log message for hello world" in withAssetCleaner(wskprops) { (wp, assetHelper) =>
val packageName = withTimestamp("samples")
val actionName = withTimestamp("helloWorld")
val fullActionName = s"/$guestNamespace/$packageName/$actionName"
assetHelper.withCleaner(wsk.pkg, packageName) { (pkg, _) =>
pkg.create(packageName, shared = Some(true))
}
assetHelper.withCleaner(wsk.action, fullActionName) { (action, _) =>
action.create(fullActionName, Some(TestCLIUtils.getTestActionFilename("hello.js")))
}
// Some contingency to make query more robust
// Account for time differences between controller and invoker
val start = Instant.now.minusSeconds(5)
val payload = new String("from the console!".getBytes, "UTF-8")
val run = wsk.action.invoke(fullActionName, Map("payload" -> payload.toJson))
withActivation(wsk.activation, run, totalWait = 30.seconds) { activation =>
// Time recorded by invoker, some contingency to make query more robust
val queryTime = activation.start.minusMillis(500)
// since: poll for activations since specified point in time (absolute)
val activations =
wsk.activation.pollFor(N = 1, Some(s"$packageName/$actionName"), since = Some(queryTime), retries = 80).length
withClue(
s"expected activations of action '$fullActionName' since $queryTime, initial activation ${activation.activationId}:") {
activations should be(1)
}
val duration = Duration(Instant.now.minusMillis(start.toEpochMilli).toEpochMilli, MILLISECONDS)
val pollTime = 10 seconds
// since: poll for activations since specified number of seconds ago (relative)
val console = wsk.activation.console(pollTime, since = Some(duration))
withClue(s"Polled since ${duration.toSeconds} seconds, did not find expected result:") {
console.stdout should include(payload)
}
}
}
it should "show repeated activations" in withAssetCleaner(wskprops) { (wp, assetHelper) =>
val name = withTimestamp("countdown")
assetHelper.withCleaner(wsk.action, name) { (action, _) =>
action.create(name, Some(TestCLIUtils.getTestActionFilename("countdown.js")))
}
val count = 3
// Some contingency to make query more robust
// Account for time differences between controller and invoker
val start = Instant.now.minusSeconds(5)
val run = wsk.action.invoke(name, Map("n" -> count.toJson))
withActivation(wsk.activation, run) { activation =>
// Time recorded by invoker, some contingency to make query more robust
val queryTime = activation.start.minusMillis(500)
// since: poll for activations since specified point in time (absolute)
val activations = wsk.activation.pollFor(N = 4, Some(name), since = Some(queryTime), retries = 80).length
withClue(
s"expected activations of action '$name' since $queryTime, initial activation ${activation.activationId}:") {
activations should be(count + 1)
}
val duration = Duration(Instant.now.minusMillis(start.toEpochMilli).toEpochMilli, MILLISECONDS)
val pollTime = 10 seconds
// since: poll for activations since specified number of seconds ago (relative)
val console = wsk.activation.console(pollTime, since = Some(duration))
withClue(s"Polled for ${duration.toSeconds} seconds, did not find expected result:") {
console.stdout should include("Happy New Year")
}
}
}
it should "invoke an action and poll activations and verify that action names are quoted." in withAssetCleaner(wskprops) {
(wp, assetHelper) =>
val name = "helloRunActivationPoll"
assetHelper.withCleaner(wsk.action, name) { (action, _) =>
action.create(name, Some(TestCLIUtils.getTestActionFilename("hello.js")))
}
val activationId = wsk.action.invoke(name)
val pollResults = wsk.activation.console(duration = 5.second, actionName = Some(name))
pollResults.stdout should include (s"""Activation: '${name}'""")
}
}