Skip to content

Commit

Permalink
[cw|frenata#5] mv smoke test into main binary, begin configuring shar…
Browse files Browse the repository at this point in the history
…ed 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
  • Loading branch information
connorwalsh committed Mar 14, 2018
1 parent 5123e0b commit 41c2fd3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# API server vars
COMPILEBOX_API_SERVER_PORT=31337

# results vars
COMPILEBOX_RESULTS_DIR=/results/
7 changes: 6 additions & 1 deletion Dockerfile-server
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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/
Expand All @@ -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
Expand Down
77 changes: 75 additions & 2 deletions cmd/compilebox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,16 +25,30 @@ type ExecutionResult struct {
Message compilebox.Message `json:"message"`
}

const (
CompilersFile = "data/compilers.json"
)

// type LanguagesResponse struct {
// Languages map[string]compilebox.Language `json:"languages"`
// }

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)
Expand Down Expand Up @@ -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 <iostream>\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"] = "<?php\n$ho = fopen('php://stdout', \"w\");\n\nfwrite($ho, \"Hello\");\n\n\nfclose($ho);\n"

// currently broken:
// Haskell ghc missing, maybe need to rebuild docker file
// compilerTests["Haskell"] = "module Main where\nmain = putStrLn \"Hello\""
//
// Scala: don't understand the error this generates
// compilerTests["Scala"] = "object HelloWorld {def main(args: Array[String]) = println(\"Hello\")}"
//
// Rust seems to be missing and there's a problem setting environment variables
// compilerTests["Rust"] = "fn main() {\n\tprintln!(\"Hello\");\n}"

stdin := ""
expected := "Hello"
langResults := make(map[string]string)

// run tests for each language
for lang, code := range compilerTests {
stdouts, msg := testBox.EvalWithStdins(lang, code, []string{stdin})

log.Println(stdouts[0], msg)

if stdouts[0] == expected {
log.Printf("%s passed 'Hello' test.", lang)
langResults[lang] = "Pass"
} else {
log.Println(stdouts)
log.Printf("%s failed 'Hello' test.", lang)
langResults[lang] = "Fail"
}

fmt.Println("-----------------------------------------------------")
}

for lang, result := range langResults {
fmt.Printf("%s -> %s\n", lang, result)
}

return nil
}
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -17,3 +18,7 @@ services:
context: .
dockerfile: Dockerfile-vm
command: echo "compilebox build complete"
volumes:
- "results:${COMPILEBOX_RESULTS_DIR}"
volumes:
results:

0 comments on commit 41c2fd3

Please sign in to comment.