Skip to content

Commit 64340cb

Browse files
committed
Moved Swift3 test, fixed formatting issues and extraneous white space in Swift code. Fixed up the CLI comments. Tweaked the Swagger docs more but these are a mess we need to fix more systematically.
Refactored Swift tests so that swift:3 extends swift tests. Rewrite Swift CLI tests in Scala and make Swift:3 test actually run the action. Organize Docker to hit in cache on python layer.
1 parent fde7e33 commit 64340cb

9 files changed

Lines changed: 136 additions & 243 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ build/
3737
node_modules
3838

3939
# Vagrant
40-
/tools/vagrant/.vagrant
40+
/tools/vagrant/.vagrant*

catalog/samples/httpGet.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
1-
/*
2-
Sample code using the experimental Swift 3 runtime
3-
with links to KituraNet and GCD
4-
*/
1+
/**
2+
* Sample code using the experimental Swift 3 runtime
3+
* with links to KituraNet and GCD
4+
*/
55

66
import KituraNet
77
import Dispatch
88
import Foundation
99

1010
func main(args:[String:Any]) -> [String:Any] {
11-
12-
11+
1312
// Force KituraNet call to run synchronously on a global queue
1413
var str = "No response"
1514
dispatch_sync(dispatch_get_global_queue(0, 0)) {
16-
15+
1716
Http.get("https://httpbin.org/get") { response in
18-
17+
1918
do {
20-
str = try response!.readString()!
19+
str = try response!.readString()!
2120
} catch {
2221
print("Error \(error)")
2322
}
24-
23+
2524
}
26-
2725
}
28-
26+
2927
// Assume string is JSON
3028
print("Got string \(str)")
3129
var result:[String:Any]?
32-
30+
3331
// Convert to NSData
3432
let data = str.bridge().dataUsingEncoding(NSUTF8StringEncoding)!
3533
do {
3634
result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
3735
} catch {
3836
print("Error \(error)")
3937
}
40-
38+
4139
// return, which should be a dictionary
4240
print("Result is \(result!)")
4341
return result!
44-
}
45-
42+
}

tests/src/actionContainers/Swift3ActionContainerTests.scala

Lines changed: 4 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,13 @@ import ActionContainer.withContainer
2828
import scala.util.Random
2929

3030
@RunWith(classOf[JUnitRunner])
31-
class Swift3ActionContainerTests extends FlatSpec
32-
with Matchers
33-
with BeforeAndAfter {
31+
class Swift3ActionContainerTests extends SwiftActionContainerTests {
3432

35-
// Helpers specific to swiftaction
36-
def withSwiftContainer(code: ActionContainer => Unit) = withContainer("whisk/swift3action")(code)
37-
def initPayload(code: String) = JsObject(
38-
"value" -> JsObject(
39-
"name" -> JsString("someSwiftAction"),
40-
"code" -> JsString(code)))
41-
def runPayload(args: JsValue) = JsObject("value" -> args)
33+
override val checkStdOutEmpty = false
34+
override val swiftContainerImageName = "whisk/swift3action"
4235

4336
behavior of "whisk/swift3action"
4437

45-
it should "support valid flows" in {
46-
val (out, err) = withSwiftContainer { c =>
47-
val code = """
48-
| func main(args: [String: Any]) -> [String: Any] {
49-
| return args
50-
| }
51-
""".stripMargin
52-
53-
val (initCode, _) = c.init(initPayload(code))
54-
55-
initCode should be(200)
56-
57-
val argss = List(
58-
JsObject("greeting" -> JsString("hi!")),
59-
JsObject("numbers" -> JsArray(List(JsNumber(42), JsNumber(1)))))
60-
61-
for (args <- argss) {
62-
val (runCode, out) = c.run(runPayload(args))
63-
runCode should be(200)
64-
out should be(Some(args))
65-
}
66-
}
67-
68-
// note: "out" will likely not be empty as swift build likes
69-
// to print status messages and there doesn't seem to be a way to quiet them
70-
err.trim shouldBe empty
71-
}
72-
7338
it should "properly use KituraNet and Dispatch" in {
7439
val (out, err) = withSwiftContainer { c =>
7540
val code = """
@@ -117,85 +82,8 @@ class Swift3ActionContainerTests extends FlatSpec
11782
}
11883
}
11984

120-
// note: "out" will likely not be empty as swift build likes
121-
// to print status messages and there doesn't seem to be a way to quiet them
85+
if (checkStdOutEmpty) out.trim shouldBe empty
12286
err.trim shouldBe empty
12387
}
12488

125-
it should "return some error on action error" in {
126-
withSwiftContainer { c =>
127-
val code = """
128-
| // You need an indirection, or swiftc detects the div/0
129-
| // at compile-time. Smart.
130-
| func div(x: Int, _ y: Int) -> Int {
131-
| return x/y
132-
| }
133-
| func main(args: [String: Any]) -> [String: Any] {
134-
| return [ "divBy0": div(5,0) ]
135-
| }
136-
""".stripMargin
137-
138-
val (initCode, _) = c.init(initPayload(code))
139-
initCode should be(200)
140-
141-
val (runCode, runRes) = c.run(runPayload(JsObject()))
142-
runCode should be(502)
143-
144-
runRes shouldBe defined
145-
runRes.get.fields.get("error") shouldBe defined
146-
}
147-
}
148-
149-
it should "log compilation errors" in {
150-
val (_, err) = withSwiftContainer { c =>
151-
val code = """
152-
| 10 PRINT "Hello!"
153-
| 20 GOTO 10
154-
""".stripMargin
155-
156-
val (initCode, _) = c.init(initPayload(code))
157-
initCode should not be(200)
158-
159-
val (runCode, runRes) = c.run(runPayload(JsObject("basic" -> JsString("forever"))))
160-
runCode should be(502)
161-
}
162-
err.toLowerCase should include("error")
163-
}
164-
165-
166-
it should "support application errors" in {
167-
withSwiftContainer { c =>
168-
val code = """
169-
| func main(args: [String: Any]) -> [String: Any] {
170-
| return [ "error": "sorry" ]
171-
| }
172-
""".stripMargin
173-
174-
val (initCode, _) = c.init(initPayload(code))
175-
initCode should be(200)
176-
177-
val (runCode, runRes) = c.run(runPayload(JsObject()))
178-
runCode should be(200) // action writer returning an error is OK
179-
180-
runRes shouldBe defined
181-
runRes.get.fields.get("error") shouldBe defined
182-
}
183-
}
184-
185-
it should "enforce that the user returns an object" in {
186-
withSwiftContainer { c =>
187-
val code = """
188-
| func main(args: [String: Any]) -> String {
189-
| return "rebel, rebel"
190-
| }
191-
""".stripMargin
192-
193-
val (initCode, _) = c.init(initPayload(code))
194-
initCode should be(200) // This could change if the action wrapper has strong type checks for `main`.
195-
196-
val (runCode, runRes) = c.run(runPayload(JsObject()))
197-
runCode should be(502)
198-
runRes.get.fields.get("error") shouldBe defined
199-
}
200-
}
20189
}

tests/src/actionContainers/SwiftActionContainerTests.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ class SwiftActionContainerTests extends FlatSpec
3232
with Matchers
3333
with BeforeAndAfter {
3434

35+
// note: "out" will likely not be empty in some swift build as the compiler
36+
// prints status messages and there doesn't seem to be a way to quiet them
37+
val checkStdOutEmpty = true
38+
val swiftContainerImageName = "whisk/swiftaction"
39+
3540
// Helpers specific to swiftaction
36-
def withSwiftContainer(code: ActionContainer => Unit) = withContainer("whisk/swiftaction")(code)
41+
def withSwiftContainer(code: ActionContainer => Unit) = withContainer(swiftContainerImageName)(code)
3742
def initPayload(code: String) = JsObject(
3843
"value" -> JsObject(
3944
"name" -> JsString("someSwiftAction"),
@@ -65,7 +70,7 @@ class SwiftActionContainerTests extends FlatSpec
6570
}
6671
}
6772

68-
out.trim shouldBe empty
73+
if (checkStdOutEmpty) out.trim shouldBe empty
6974
err.trim shouldBe empty
7075
}
7176

@@ -101,15 +106,14 @@ class SwiftActionContainerTests extends FlatSpec
101106
""".stripMargin
102107

103108
val (initCode, _) = c.init(initPayload(code))
104-
initCode should not be(200)
109+
initCode should not be (200)
105110

106111
val (runCode, runRes) = c.run(runPayload(JsObject("basic" -> JsString("forever"))))
107112
runCode should be(502)
108113
}
109114
err.toLowerCase should include("error")
110115
}
111116

112-
113117
it should "support application errors" in {
114118
withSwiftContainer { c =>
115119
val code = """

tests/src/common/Wsk.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class WskAction()
194194
def create(
195195
name: String,
196196
artifact: Option[String],
197-
kind: Option[String] = None, // one of docker, copy, sequence or none for node/swift auto select
197+
kind: Option[String] = None, // one of docker, copy, sequence or none for autoselect else an explicit type
198198
parameters: Map[String, JsValue] = Map(),
199199
annotations: Map[String, JsValue] = Map(),
200200
timeout: Option[Duration] = None,
@@ -205,7 +205,12 @@ class WskAction()
205205
implicit wp: WskProps): RunResult = {
206206
val params = Seq(noun, if (!update) "create" else "update", "--auth", wp.authKey, fqn(name)) ++
207207
{ artifact map { Seq(_) } getOrElse Seq() } ++
208-
{ kind map { k => Seq(s"--$k") } getOrElse Seq() } ++
208+
{
209+
kind map { k =>
210+
if (k == "docker" || k == "sequence" || k == "copy") Seq(s"--$k")
211+
else Seq("--kind", k)
212+
} getOrElse Seq()
213+
} ++
209214
{ parameters flatMap { p => Seq("-p", p._1, p._2.compactPrint) } } ++
210215
{ annotations flatMap { p => Seq("-p", p._1, p._2.compactPrint) } } ++
211216
{ timeout map { t => Seq("-t", t.toMillis.toString) } getOrElse Seq() } ++

tests/src/common/WskCli.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public String delete(int expectedExitCode, Item item, String name) throws IOExce
262262
}
263263

264264
public String copyAction(String name, String existingAction) throws IOException {
265-
return createAction(SUCCESS_EXIT, name, existingAction, null, null, false, true, true, false, 0, null);
265+
return createAction(SUCCESS_EXIT, name, existingAction, null, null, false, true, true, false, 0);
266266
}
267267

268268
public String createAction(String name, String file) throws IOException {
@@ -279,52 +279,48 @@ public String createAction(int expectedCode, String name, String file) throws IO
279279

280280
public String createAction(String name, String[] actions) throws IOException {
281281
String csv = String.join(",", actions);
282-
return createAction(SUCCESS_EXIT, name, csv, null, null, true, false, false, true, 0, null);
282+
return createAction(SUCCESS_EXIT, name, csv, null, null, true, false, false, true, 0);
283283
}
284284

285285
public String createAction(String name, String[] actions, int timeoutMillis) throws IOException {
286286
String csv = String.join(",", actions);
287-
return createAction(SUCCESS_EXIT, name, csv, null, null, true, false, false, true, timeoutMillis, null);
287+
return createAction(SUCCESS_EXIT, name, csv, null, null, true, false, false, true, timeoutMillis);
288288
}
289289

290290
public String createAction(String name, String file, Map<String, String> params) throws IOException {
291291
return createAction(name, file, params, false, false);
292292
}
293293

294294
public String createAction(String name, String file, String library) throws IOException {
295-
return createAction(SUCCESS_EXIT, name, file, library, null, false, false, false, false, 0, null);
295+
return createAction(SUCCESS_EXIT, name, file, library, null, false, false, false, false, 0);
296296
}
297297

298298
public String createAction(String name, String file, String library, Map<String, String> params) throws IOException {
299-
return createAction(SUCCESS_EXIT, name, file, library, params, false, false, false, false, 0, null);
299+
return createAction(SUCCESS_EXIT, name, file, library, params, false, false, false, false, 0);
300300
}
301301

302302
public String createAction(String name, String file, boolean update, boolean shared) throws IOException {
303-
return createAction(SUCCESS_EXIT, name, file, null, null, false, update, false, shared, 0, null);
303+
return createAction(SUCCESS_EXIT, name, file, null, null, false, update, false, shared, 0);
304304
}
305305

306306
public String createAction(String name, String file, Map<String, String> params, boolean update, boolean shared) throws IOException {
307-
return createAction(SUCCESS_EXIT, name, file, null, params, false, update, false, shared, 0, null);
307+
return createAction(SUCCESS_EXIT, name, file, null, params, false, update, false, shared, 0);
308308
}
309309

310310
public String createAction(String name, String file, Map<String, String> params, boolean update, boolean shared, int timeoutMillis) throws IOException {
311-
return createAction(SUCCESS_EXIT, name, file, null, params, false, update, false, shared, timeoutMillis, null);
311+
return createAction(SUCCESS_EXIT, name, file, null, params, false, update, false, shared, timeoutMillis);
312312
}
313313

314314
public String updateAction(String name, boolean shared) throws IOException {
315-
return createAction(SUCCESS_EXIT, name, null, null, null, false, true, false, shared, 0, null);
315+
return createAction(SUCCESS_EXIT, name, null, null, null, false, true, false, shared, 0);
316316
}
317317

318318
public String createAction(int expectedCode, String name, String file, Map<String, String> params, boolean update, boolean shared) throws IOException {
319-
return createAction(expectedCode, name, file, null, params, false, update, false, shared, 0, null);
319+
return createAction(expectedCode, name, file, null, params, false, update, false, shared, 0);
320320

321321
}
322322

323-
public String createActionWithKind(String name, String file, String kind) throws IOException {
324-
return createAction(SUCCESS_EXIT, name, file, null, null, false, false, false, false, 0, kind);
325-
}
326-
327-
public String createAction(int expectedCode, String name, String artifact, String library, Map<String, String> params, boolean sequence, boolean update, boolean copy, boolean shared, int timeoutMillis, String kind) throws IOException {
323+
public String createAction(int expectedCode, String name, String artifact, String library, Map<String, String> params, boolean sequence, boolean update, boolean copy, boolean shared, int timeoutMillis) throws IOException {
328324
String[] cmd1 = { "action", "update", "--auth", authKey, name };
329325
String[] cmd2 = { "action", "create", "--auth", authKey, name, artifact };
330326
String[] cmd = update ? cmd1 : cmd2;
@@ -333,10 +329,6 @@ public String createAction(int expectedCode, String name, String artifact, Strin
333329
cmd = Util.concat(cmd, new String[] { artifact });
334330
}
335331

336-
if (kind != null) {
337-
cmd = Util.concat(cmd, new String[] { "--kind", kind });
338-
}
339-
340332
if (params != null) {
341333
for (String key : params.keySet()) {
342334
String value = params.get(key);

tests/src/system/basic/CLIActionTests.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,22 +319,6 @@ public void updateAction() throws Exception {
319319
}
320320
}
321321

322-
@Test(timeout=120*1000)
323-
public void createActionWithKind() throws Exception {
324-
String action = "updateActionWithKind";
325-
String publishFalseStr = "\"publish\": false";
326-
String publishTrueStr = "\"publish\": true";
327-
try {
328-
wsk.sanitize(Action, action);
329-
// create public action then update it to private
330-
wsk.createActionWithKind(action, TestUtils.getCatalogFilename("samples/httpGet.swift"), "swift:3");
331-
String item = wsk.get(Action, action);
332-
assertTrue("Expect action to not be shared", item.contains(publishFalseStr));
333-
} finally {
334-
wsk.delete(Action, action);
335-
}
336-
}
337-
338322
@Test(timeout=120*1000)
339323
public void invokeActionWithSpace() throws Exception {
340324
String name = "WORD COUNT";

0 commit comments

Comments
 (0)