package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
)
func main() {
userCode := `
def two_sum(nums, target):
num_dict = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_dict:
return [num_dict[complement], i]
num_dict[num] = i
return []
`
// Predefined test cases
testCode := `
import unittest
from main import two_sum
class TestTwoSum(unittest.TestCase):
def test_case_1(self):
self.assertEqual(two_sum([2, 7, 11, 15], 9), [0, 1])
def test_case_2(self):
self.assertEqual(two_sum([3, 2, 4], 6), [1, 2])
if __name__ == '__main__':
unittest.main()
`
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
defer cli.Close()
// 1. Create 2 temp files
hostWorkingDir, err := os.MkdirTemp("", "")
check(err)
fmt.Println("Temp dir name:", hostWorkingDir)
defer os.RemoveAll(hostWorkingDir)
codeFilePath := filepath.Join(hostWorkingDir, "main.py")
codeFile, err := os.Create(codeFilePath)
if err != nil {
fmt.Println("failed to create source file", err)
return
}
fmt.Println("Code file name:", codeFile.Name())
defer codeFile.Close()
testFilePath := filepath.Join(hostWorkingDir, "test.py")
testFile, err := os.Create(testFilePath)
if err != nil {
fmt.Println("failed to create test file", err)
return
}
fmt.Println("Test file name:", testFile.Name())
defer testFile.Close()
// 2. Write code into 2 temp files:
_, err = codeFile.WriteString(userCode)
if err != nil {
fmt.Print("failed to write source file")
return
}
_, err = testFile.WriteString(testCode)
if err != nil {
fmt.Print("failed to write test file")
return
}
// 3. Create Container
ctx := context.Background()
workingDir := "/work"
programFileDirectory, codePath := filepath.Split(codeFile.Name())
_, testPath := filepath.Split(testFile.Name())
codePath = filepath.Join(workingDir, codePath)
fmt.Println(codePath)
testPath = filepath.Join(workingDir, testPath)
fmt.Println(testPath)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "docker.io/library/python:3.13-rc-slim",
Cmd: []string{"timeout", "--foreground", "10s", "python", "test.py"},
WorkingDir: workingDir,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
OpenStdin: true,
StdinOnce: true,
}, &container.HostConfig{
Binds: []string{fmt.Sprintf("%s:%s", programFileDirectory, workingDir)},
Resources: container.Resources{
CPUQuota: 1000000,
Memory: int64(256 * 1024 * 1024), /// 256 MB
},
NetworkMode: "none"}, nil, nil, "")
if err != nil {
panic(err)
}
//4. Start Container
if err := cli.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
panic(err)
}
//5. Wait for container to response
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, resp.ID, container.LogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}
stdcopy.StdCopy(os.Stdout, os.Stderr, out)
}
func check(e error) {
if e != nil {
panic(e)
}
}