Skip to content
Snippets Groups Projects
Commit 586a3120 authored by rodric rabbah's avatar rodric rabbah Committed by Justin Berstler
Browse files

Normalize initializer for all execs. (#1938)

* Normalize initializer for all execs.

* Handle empty code for blackbox.

* Do not include code if null.
parent 5e394e20
No related branches found
No related tags found
No related merge requests found
......@@ -46,7 +46,7 @@ class ActionRunner:
# @return True iff binary exists and is executable
def init(self, message):
def prep():
if 'code' in message:
if 'code' in message and message['code'] is not None:
binary = message['binary'] if 'binary' in message else False
if not binary:
return self.initCodeFromString(message)
......
......@@ -60,16 +60,20 @@ def content_from_args(args):
def init(args):
main = args[1] if len(args) == 2 else "main"
args = args[0]
args = args[0] if len(args) > 1 else None
if args.endswith(".zip"):
if args and args.endswith(".zip"):
with open(args, "rb") as fp:
contents = fp.read().encode("base64")
binary = True
else:
elif args:
with(codecs.open(args, "r", "utf-8")) as fp:
contents = fp.read()
binary = False
else:
contents = None
binary = False
r = requests.post("%s/init" % DEST, json = { "value" : { "code": contents, "binary": binary, "main": main } })
print r.text
......
......@@ -54,7 +54,7 @@ trait ActionProxyContainerTestUtils extends FlatSpec with Matchers {
def initPayload(code: String, main: String = "main") = {
JsObject("value" -> JsObject(
"code" -> JsString(code),
"code" -> { if (code != null) JsString(code) else JsNull },
"main" -> JsString(main),
"binary" -> JsBoolean(Exec.isBinaryCode(code))))
}
......
......@@ -122,6 +122,21 @@ class ActionProxyContainerTests extends BasicActionRunnerTests with WskActorSyst
})
}
it should "run sample with 'null' init" in {
val (out, err) = withActionContainer() { c =>
val (initCode, _) = c.init(initPayload(null))
initCode should be(200)
val (runCode, out) = c.run(JsObject())
runCode should be(200)
out should be(Some(JsObject("error" -> JsString("This is a stub action. Replace it with custom logic."))))
}
checkStreams(out, err, {
case (o, _) => o should include("This is a stub action")
})
}
it should "run sample with init that does nothing" in {
val (out, err) = withActionContainer() { c =>
val (initCode, _) = c.init(JsObject())
......
......@@ -38,7 +38,7 @@ class JavaActionContainerTests extends FlatSpec with Matchers with WskActorSyste
"value" -> JsObject(
"name" -> JsString("dummyAction"),
"main" -> JsString(mainClass),
"jar" -> JsString(jar64)))
"code" -> JsString(jar64)))
behavior of "Java action"
......
......@@ -337,14 +337,33 @@ class SchemaTests extends FlatSpec with BeforeAndAfter with Matchers {
val contents = b64.encodeToString("tarball".getBytes)
val json = Seq[JsObject](
JsObject("kind" -> "blackbox".toJson, "image" -> "container1".toJson, "binary" -> false.toJson),
JsObject("kind" -> "blackbox".toJson, "image" -> "container1".toJson, "binary" -> true.toJson, "code" -> contents.toJson))
JsObject("kind" -> "blackbox".toJson, "image" -> "container1".toJson, "binary" -> true.toJson, "code" -> contents.toJson),
JsObject("kind" -> "blackbox".toJson, "image" -> "container1".toJson, "binary" -> true.toJson, "code" -> contents.toJson, "main" -> "naim".toJson))
val execs = json.map { e => Exec.serdes.read(e) }
assert(execs(0) == Exec.bb("container1") && json(0).compactPrint == Exec.bb("container1").toString)
assert(execs(1) == Exec.bb("container1", contents) && json(1).compactPrint == Exec.bb("container1", contents).toString)
assert(execs(0) == Exec.serdes.read(JsObject("kind" -> "blackbox".toJson, "image" -> "container1".toJson, "binary" -> false.toJson, "code" -> " ".toJson)))
assert(execs(0) == Exec.serdes.read(JsObject("kind" -> "blackbox".toJson, "image" -> "container1".toJson, "binary" -> false.toJson, "code" -> "".toJson)))
execs(0) shouldBe Exec.bb("container1")
execs(1) shouldBe Exec.bb("container1", contents)
execs(2) shouldBe Exec.bb("container1", contents, Some("naim"))
json(0).compactPrint shouldBe Exec.bb("container1").toString
json(1).compactPrint shouldBe Exec.bb("container1", contents).toString
json(2).compactPrint shouldBe Exec.bb("container1", contents, Some("naim")).toString
execs(0) shouldBe Exec.serdes.read(JsObject("kind" -> "blackbox".toJson, "image" -> "container1".toJson, "binary" -> false.toJson, "code" -> " ".toJson))
execs(0) shouldBe Exec.serdes.read(JsObject("kind" -> "blackbox".toJson, "image" -> "container1".toJson, "binary" -> false.toJson, "code" -> "".toJson))
}
it should "exclude undefined code in whisk action initializer" in {
WhiskAction(EntityPath("a"), EntityName("b"), Exec.bb("container1")).containerInitializer shouldBe {
Some(JsObject("name" -> "b".toJson, "binary" -> false.toJson, "main" -> "main".toJson))
}
WhiskAction(EntityPath("a"), EntityName("b"), Exec.bb("container1", "xyz")).containerInitializer shouldBe {
Some(JsObject("name" -> "b".toJson, "binary" -> false.toJson, "main" -> "main".toJson, "code" -> "xyz".toJson))
}
WhiskAction(EntityPath("a"), EntityName("b"), Exec.bb("container1", "", Some("naim"))).containerInitializer shouldBe {
Some(JsObject("name" -> "b".toJson, "binary" -> false.toJson, "main" -> "naim".toJson))
}
}
it should "reject malformed JSON" in {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment