Skip to content

Commit 6aa87b9

Browse files
committed
add unit tests
1 parent 5d93f4c commit 6aa87b9

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

pkg/container/dockerclient_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package codecontainer
2+
3+
import (
4+
"remote-code-engine/pkg/config"
5+
"testing"
6+
)
7+
8+
func TestGetContainerCommand(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
code *Code
12+
codeFileName string
13+
inputFileName string
14+
expectedCommand []string
15+
}{
16+
{
17+
name: "golang command",
18+
code: &Code{
19+
Language: "golang",
20+
LanguageConfig: config.LanguageConfig{
21+
Extension: ".go",
22+
Command: "go run {{FILE}} < {{INPUT}}",
23+
},
24+
},
25+
codeFileName: "main.go",
26+
inputFileName: "input.txt",
27+
expectedCommand: []string{
28+
"sh", "-c",
29+
"go run /container/code/main.go < /container/code/input.txt",
30+
},
31+
},
32+
{
33+
name: "cpp command",
34+
code: &Code{
35+
Language: "cpp",
36+
LanguageConfig: config.LanguageConfig{
37+
Extension: ".cpp",
38+
Command: "g++ {{FILE}} -o a.out && a.out < {{INPUT}}",
39+
},
40+
},
41+
codeFileName: "main.cpp",
42+
inputFileName: "input.txt",
43+
expectedCommand: []string{
44+
"sh", "-c",
45+
"g++ /container/code/main.cpp -o a.out && a.out < /container/code/input.txt",
46+
},
47+
},
48+
}
49+
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
command := getContainerCommand(tt.code, tt.codeFileName, tt.inputFileName)
53+
if len(command) != len(tt.expectedCommand) {
54+
t.Errorf("expected command length %d, got %d", len(tt.expectedCommand), len(command))
55+
}
56+
for i := range command {
57+
if command[i] != tt.expectedCommand[i] {
58+
t.Errorf("expected command '%s', got '%s'", tt.expectedCommand[i], command[i])
59+
}
60+
}
61+
})
62+
}
63+
}

pkg/container/file-helper_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package codecontainer
2+
3+
import (
4+
"encoding/base64"
5+
"os"
6+
"path/filepath"
7+
"remote-code-engine/pkg/config"
8+
"testing"
9+
10+
"go.uber.org/zap"
11+
"go.uber.org/zap/zapcore"
12+
)
13+
14+
func TestCreateFile(t *testing.T) {
15+
logger := zap.New(zapcore.NewCore(
16+
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
17+
zapcore.AddSync(os.Stdout),
18+
zapcore.DebugLevel,
19+
))
20+
21+
tempDir := t.TempDir()
22+
filePath := filepath.Join(tempDir, "testfile.txt")
23+
content := "Hello, World!"
24+
encodedContent := base64.StdEncoding.EncodeToString([]byte(content))
25+
26+
fileName, err := createFile(filePath, encodedContent, logger)
27+
if err != nil {
28+
t.Fatalf("failed to create file: %v", err)
29+
}
30+
31+
if fileName != "testfile.txt" {
32+
t.Errorf("expected file name 'testfile.txt', got '%s'", fileName)
33+
}
34+
35+
data, err := os.ReadFile(filePath)
36+
if err != nil {
37+
t.Fatalf("failed to read file: %v", err)
38+
}
39+
40+
if string(data) != content {
41+
t.Errorf("expected file content '%s', got '%s'", content, string(data))
42+
}
43+
}
44+
45+
func TestCreateFileInvalidBase64(t *testing.T) {
46+
logger := zap.New(zapcore.NewCore(
47+
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
48+
zapcore.AddSync(os.Stdout),
49+
zapcore.DebugLevel,
50+
))
51+
52+
tempDir := t.TempDir()
53+
filePath := filepath.Join(tempDir, "testfile.txt")
54+
invalidEncodedContent := "invalid_base64_content"
55+
56+
_, err := createFile(filePath, invalidEncodedContent, logger)
57+
if err == nil {
58+
t.Fatal("expected an error due to invalid base64 content, but got none")
59+
}
60+
}
61+
func TestGetCodeAndInputFilePathsHost(t *testing.T) {
62+
code := &Code{
63+
Language: "golang",
64+
LanguageConfig: config.LanguageConfig{
65+
Extension: ".go",
66+
},
67+
}
68+
69+
codeFilePath, inputFilePath := getCodeAndInputFilePathsHost(code)
70+
71+
expectedCodeDir := config.GetHostLanguageCodePath(code.Language)
72+
expectedCodeFilePath := filepath.Join(expectedCodeDir, filepath.Base(codeFilePath))
73+
expectedInputFilePath := filepath.Join(expectedCodeDir, filepath.Base(inputFilePath))
74+
75+
if codeFilePath != expectedCodeFilePath {
76+
t.Errorf("expected code file path '%s', got '%s'", expectedCodeFilePath, codeFilePath)
77+
}
78+
79+
if inputFilePath != expectedInputFilePath {
80+
t.Errorf("expected input file path '%s', got '%s'", expectedInputFilePath, inputFilePath)
81+
}
82+
}
83+
84+
func TestGetFilePathHost(t *testing.T) {
85+
hostCodeDirectoryPath := "/tmp/code"
86+
fileName := "testfile.go"
87+
expectedFilePath := "/tmp/code/testfile.go"
88+
89+
result := getFilePathHost(hostCodeDirectoryPath, fileName)
90+
91+
if result != expectedFilePath {
92+
t.Errorf("expected file path '%s', got '%s'", expectedFilePath, result)
93+
}
94+
}
95+
96+
func TestGetFilePathContainer(t *testing.T) {
97+
mountPath := "/mnt/container"
98+
fileName := "testfile.txt"
99+
expectedFilePath := "/mnt/container/testfile.txt"
100+
101+
result := getFilePathContainer(mountPath, fileName)
102+
103+
if result != expectedFilePath {
104+
t.Errorf("expected file path '%s', got '%s'", expectedFilePath, result)
105+
}
106+
}

0 commit comments

Comments
 (0)