-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.go
129 lines (111 loc) · 2.96 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package container
import (
"bytes"
"context"
"fmt"
"regexp"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
)
func runContainer(ctx context.Context, cli *client.Client, ID string) (string, error) {
if err := cli.ContainerStart(ctx, ID, types.ContainerStartOptions{}); err != nil {
return "", err
}
statusCh, errCh := cli.ContainerWait(ctx, ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
return "", err
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
buf.ReadFrom(out)
res := buf.String()
cli.ContainerRemove(ctx, ID, types.ContainerRemoveOptions{})
return res, err
}
// Execute executes the test program against gives test cases
func Execute(ctx context.Context, cli *client.Client, image string, lang string,
path string, count int, time int, mem int64) error {
eval := ""
switch lang {
case "c":
eval = fmt.Sprintf("/tests/evaluate %v %v %v", count, time, mem)
case "cpp":
eval = fmt.Sprintf("/tests/evaluate %v %v %v", count, time, mem)
case "java":
program := "java -cp /tests/data/ Main"
eval = fmt.Sprintf("/tests/evaluate %v %v %v %v", count, time, mem, program)
case "py":
program := "python3 /tests/data/a.py 2>&1"
eval = fmt.Sprintf("/tests/evaluate %v %v %v %v", count, time, mem, program)
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: image,
Cmd: []string{"/bin/bash", "-c", eval},
}, &container.HostConfig{
Mounts: []mount.Mount{
mount.Mount{
Type: mount.TypeBind,
Source: path,
Target: "/tests/data",
},
mount.Mount{
Type: mount.TypeBind,
Source: "/sys/fs/cgroup",
Target: "/sys/fs/cgroup",
},
},
Privileged: true,
CapDrop: []string{"all"},
}, nil, nil, "")
if err != nil {
return err
}
_, err = runContainer(ctx, cli, resp.ID)
return err
}
// Compile compiles the program and generates the executable
func Compile(ctx context.Context, cli *client.Client, image string, lang string,
path string) (int, string) {
eval := ""
switch lang {
case "c":
eval = "gcc /tests/data/a.c -o /tests/data/a.out 2>&1"
case "cpp":
eval = "g++ -w -O2 /tests/data/a.cpp -o /tests/data/a.out 2>&1"
case "java":
eval = "javac -d /tests/data/ -cp /tests/data/ /tests/data/Main.java 2>&1"
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: image,
Cmd: []string{"/bin/bash", "-c", eval},
}, &container.HostConfig{
Mounts: []mount.Mount{
mount.Mount{
Type: mount.TypeBind,
Source: path,
Target: "/tests/data",
},
},
}, nil, nil, "")
if err != nil {
return 2, err.Error()
}
out, err := runContainer(ctx, cli, resp.ID)
if err != nil {
return 2, err.Error()
}
r, _ := regexp.Compile("error")
if r.MatchString(out) {
return 0, out
}
return 1, ""
}