From 41c2fd3f5ceae7507f385c6cf74549d768769acb Mon Sep 17 00:00:00 2001 From: connorwalsh Date: Wed, 14 Mar 2018 12:05:42 -0400 Subject: [PATCH] [cw|#5] mv smoke test into main binary, begin configuring shared vol Major Changes: * move langtest code into a smoke test in compilebox main.go * begin setting up shared volumes between server api and compilebox TODO: * doesn't run yet, need to figure out shared volumes * delete langtest cmd --- .env | 3 ++ Dockerfile-server | 7 +++- cmd/compilebox/main.go | 77 ++++++++++++++++++++++++++++++++++++++++-- docker-compose.yml | 5 +++ 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/.env b/.env index dcbb816..7039924 100644 --- a/.env +++ b/.env @@ -1,2 +1,5 @@ # API server vars COMPILEBOX_API_SERVER_PORT=31337 + +# results vars +COMPILEBOX_RESULTS_DIR=/results/ diff --git a/Dockerfile-server b/Dockerfile-server index ce33281..c9bf535 100644 --- a/Dockerfile-server +++ b/Dockerfile-server @@ -1,3 +1,4 @@ +# --- multi-stage build 1/2: build api server from src --- FROM golang:1.9-alpine as server # GOPATH = /go in the golang image @@ -11,6 +12,7 @@ COPY . . # compile and install server binary within container RUN go install ./cmd/compilebox +# --- multi-stage build 2/2: install binaries onto clean image --- FROM alpine WORKDIR /bin/ @@ -21,8 +23,11 @@ WORKDIR /bin/ RUN apk update && \ apk add docker -# copy over single binary from build stage --^ +# copy over single binary and supporting scripts from build stage --^ +# TODO once compilebox uses the Docker native API we most likely won't need these supporting scritps COPY --from=server /go/bin/compilebox . +COPY --from=server /go/src/github.com/frenata/compilebox/Payload ./Payload +COPY --from=server /go/src/github.com/frenata/compilebox/DockerTimeout.sh . # copy compilers data COPY ./data/compilers.json ./data/compilers.json diff --git a/cmd/compilebox/main.go b/cmd/compilebox/main.go index 4ecfd1c..4843fc4 100644 --- a/cmd/compilebox/main.go +++ b/cmd/compilebox/main.go @@ -3,10 +3,11 @@ package main import ( "encoding/json" "fmt" - "github.com/frenata/compilebox" "log" "net/http" "os" + + "github.com/frenata/compilebox" ) type CodeSubmission struct { @@ -24,6 +25,10 @@ type ExecutionResult struct { Message compilebox.Message `json:"message"` } +const ( + CompilersFile = "data/compilers.json" +) + // type LanguagesResponse struct { // Languages map[string]compilebox.Language `json:"languages"` // } @@ -31,9 +36,19 @@ type ExecutionResult struct { var box compilebox.Interface func main() { + var ( + err error + ) + + // on spinup, run a smoke test against the compilebox Docker container + err = runSmokeTest() + if err != nil { + panic(err) + } + port := getEnv("COMPILEBOX_API_SERVER_PORT", "31337") - box = compilebox.New("data/compilers.json") + box = compilebox.New(CompilersFile) http.HandleFunc("/languages/", getLangs) http.HandleFunc("/eval/", evalCode) @@ -104,3 +119,61 @@ func getLangs(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write(buf) } + +// executes a hello world program for all supported languages inside the compilerbox +// Docker container. +func runSmokeTest() error { + testBox := compilebox.New(CompilersFile) + + // currently passing: + compilerTests := make(map[string]string) + compilerTests["C++"] = "#include \nusing namespace std;\n\nint main() {\n\tcout<<\"Hello\";\n\treturn 0;\n}" + compilerTests["Java"] = "\n\nimport java.io.*;\n\nclass myCode\n{\n\tpublic static void main (String[] args) throws java.lang.Exception\n\t{\n\t\t\n\t\tSystem.out.println(\"Hello\");\n\t}\n}" + compilerTests["C#"] = "using System;\n\npublic class Challenge\n{\n\tpublic static void Main()\n\t{\n\t\t\tConsole.WriteLine(\"Hello\");\n\t}\n}" + compilerTests["Clojure"] = "(println \"Hello\")" + compilerTests["Perl"] = "use strict;\nuse warnings\n;use v5.14; say 'Hello';" + compilerTests["Golang"] = "package main\nimport \"fmt\"\n\nfunc main(){\n \n\tfmt.Printf(\"Hello\")\n}" + compilerTests["JavaScript"] = "console.log(\"Hello\");" + compilerTests["Python"] = "print(\"Hello\")" + compilerTests["Ruby"] = "puts \"Hello\"" + compilerTests["Bash"] = "echo 'Hello' " + compilerTests["PHP"] = " %s\n", lang, result) + } + + return nil +} diff --git a/docker-compose.yml b/docker-compose.yml index 6295a50..33372f6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,7 @@ services: env_file: .env volumes: - /var/run/docker.sock:/var/run/docker.sock # use host Docker daemon + - "results:${COMPILEBOX_RESULTS_DIR}" ports: - "${COMPILEBOX_API_SERVER_PORT}:${COMPILEBOX_API_SERVER_PORT}" depends_on: @@ -17,3 +18,7 @@ services: context: . dockerfile: Dockerfile-vm command: echo "compilebox build complete" + volumes: + - "results:${COMPILEBOX_RESULTS_DIR}" +volumes: + results: