From 2e25a33c0cc7316911de8229a597d872b1c6c6b0 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Fri, 13 Oct 2023 16:37:07 +0300 Subject: [PATCH] some improvements to sim --- api.w | 2 +- containers.w | 2 ++ sim/workload.w | 20 ++++++++++++-------- test/local-build.test.w | 4 ++++ test/my-app/Dockerfile | 2 +- test/simple.test.w | 16 ++++++++++++++-- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/api.w b/api.w index 4d6f50a..bb13003 100644 --- a/api.w +++ b/api.w @@ -1,4 +1,4 @@ -interface IWorkload { +interface IWorkload extends std.IResource { /** starts the container */ inflight start(): void; diff --git a/containers.w b/containers.w index 055ecba..233253f 100644 --- a/containers.w +++ b/containers.w @@ -17,6 +17,8 @@ class Workload impl api.IWorkload { } else { throw "unsupported target ${target}"; } + + std.Node.of(this.inner).hidden = true; } pub inflight start() { diff --git a/sim/workload.w b/sim/workload.w index fe5de8b..6fd4f4c 100644 --- a/sim/workload.w +++ b/sim/workload.w @@ -35,19 +35,22 @@ class Workload impl api.IWorkload { } pub inflight start(): void { - log("starting container"); - log("appdir=${this.appDir}"); + log("starting workload..."); let opts = this.props; - let image = opts.image; - // if this a reference to a local directory, build the image from a docker file - log("image: ${image}"); - if image.startsWith("./") { - log("building locally from ${image} and tagging ${this.imageTag}..."); - utils.shell("docker", ["build", "-t", this.imageTag, image], this.appDir); + if opts.image.startsWith("./") { + // check if the image is already built + try { + utils.shell("docker", ["inspect", this.imageTag]); + log("image ${this.imageTag} already exists"); + } catch { + log("building locally from ${opts.image} and tagging ${this.imageTag}..."); + utils.shell("docker", ["build", "-t", this.imageTag, opts.image], this.appDir); + } } else { + log("pulling ${opts.image}"); utils.shell("docker", ["pull", opts.image], this.appDir); } @@ -83,6 +86,7 @@ class Workload impl api.IWorkload { } } + log("starting container ${this.containerId}"); utils.shell("docker", dockerRun.copy()); let out = Json.parse(utils.shell("docker", ["inspect", this.containerId])); diff --git a/test/local-build.test.w b/test/local-build.test.w index ab4a394..9a1b014 100644 --- a/test/local-build.test.w +++ b/test/local-build.test.w @@ -9,5 +9,9 @@ let app = new containers.Workload( test "can access container" { let response = http.get("${app.url()}"); + if let body = response.body { + log(body); + } + assert((response.body ?? "") == "Hello, Wingnuts!"); } \ No newline at end of file diff --git a/test/my-app/Dockerfile b/test/my-app/Dockerfile index 000947d..686c128 100644 --- a/test/my-app/Dockerfile +++ b/test/my-app/Dockerfile @@ -1,4 +1,4 @@ FROM node:20.8.0-alpine - +EXPOSE 3000 ADD index.js /app/index.js ENTRYPOINT [ "/app/index.js" ] \ No newline at end of file diff --git a/test/simple.test.w b/test/simple.test.w index 88643cb..2770d04 100644 --- a/test/simple.test.w +++ b/test/simple.test.w @@ -1,9 +1,21 @@ +bring cloud; +bring http; bring "../containers.w" as containers; -new containers.Workload( +let app = new containers.Workload( image: "hashicorp/http-echo", port: 5678, public: true, replicas: 2, args: ["-text=bang_bang"], -); +) as "http-echo"; + +test "http get" { + if let url = app.url() { + let response = http.get(url); + log(response.body ?? ""); + if let body = response.body { + assert(body.contains("bang_bang")); + } + } +}