Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srujangit123 committed Dec 26, 2024
1 parent 5d93f4c commit 6aa87b9
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/container/dockerclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package codecontainer

import (
"remote-code-engine/pkg/config"
"testing"
)

func TestGetContainerCommand(t *testing.T) {
tests := []struct {
name string
code *Code
codeFileName string
inputFileName string
expectedCommand []string
}{
{
name: "golang command",
code: &Code{
Language: "golang",
LanguageConfig: config.LanguageConfig{
Extension: ".go",
Command: "go run {{FILE}} < {{INPUT}}",
},
},
codeFileName: "main.go",
inputFileName: "input.txt",
expectedCommand: []string{
"sh", "-c",
"go run /container/code/main.go < /container/code/input.txt",
},
},
{
name: "cpp command",
code: &Code{
Language: "cpp",
LanguageConfig: config.LanguageConfig{
Extension: ".cpp",
Command: "g++ {{FILE}} -o a.out && a.out < {{INPUT}}",
},
},
codeFileName: "main.cpp",
inputFileName: "input.txt",
expectedCommand: []string{
"sh", "-c",
"g++ /container/code/main.cpp -o a.out && a.out < /container/code/input.txt",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
command := getContainerCommand(tt.code, tt.codeFileName, tt.inputFileName)
if len(command) != len(tt.expectedCommand) {
t.Errorf("expected command length %d, got %d", len(tt.expectedCommand), len(command))
}
for i := range command {
if command[i] != tt.expectedCommand[i] {
t.Errorf("expected command '%s', got '%s'", tt.expectedCommand[i], command[i])
}
}
})
}
}
106 changes: 106 additions & 0 deletions pkg/container/file-helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package codecontainer

import (
"encoding/base64"
"os"
"path/filepath"
"remote-code-engine/pkg/config"
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestCreateFile(t *testing.T) {
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(os.Stdout),
zapcore.DebugLevel,
))

tempDir := t.TempDir()
filePath := filepath.Join(tempDir, "testfile.txt")
content := "Hello, World!"
encodedContent := base64.StdEncoding.EncodeToString([]byte(content))

fileName, err := createFile(filePath, encodedContent, logger)
if err != nil {
t.Fatalf("failed to create file: %v", err)
}

if fileName != "testfile.txt" {
t.Errorf("expected file name 'testfile.txt', got '%s'", fileName)
}

data, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}

if string(data) != content {
t.Errorf("expected file content '%s', got '%s'", content, string(data))
}
}

func TestCreateFileInvalidBase64(t *testing.T) {
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(os.Stdout),
zapcore.DebugLevel,
))

tempDir := t.TempDir()
filePath := filepath.Join(tempDir, "testfile.txt")
invalidEncodedContent := "invalid_base64_content"

_, err := createFile(filePath, invalidEncodedContent, logger)
if err == nil {
t.Fatal("expected an error due to invalid base64 content, but got none")
}
}
func TestGetCodeAndInputFilePathsHost(t *testing.T) {
code := &Code{
Language: "golang",
LanguageConfig: config.LanguageConfig{
Extension: ".go",
},
}

codeFilePath, inputFilePath := getCodeAndInputFilePathsHost(code)

expectedCodeDir := config.GetHostLanguageCodePath(code.Language)
expectedCodeFilePath := filepath.Join(expectedCodeDir, filepath.Base(codeFilePath))
expectedInputFilePath := filepath.Join(expectedCodeDir, filepath.Base(inputFilePath))

if codeFilePath != expectedCodeFilePath {
t.Errorf("expected code file path '%s', got '%s'", expectedCodeFilePath, codeFilePath)
}

if inputFilePath != expectedInputFilePath {
t.Errorf("expected input file path '%s', got '%s'", expectedInputFilePath, inputFilePath)
}
}

func TestGetFilePathHost(t *testing.T) {
hostCodeDirectoryPath := "/tmp/code"
fileName := "testfile.go"
expectedFilePath := "/tmp/code/testfile.go"

result := getFilePathHost(hostCodeDirectoryPath, fileName)

if result != expectedFilePath {
t.Errorf("expected file path '%s', got '%s'", expectedFilePath, result)
}
}

func TestGetFilePathContainer(t *testing.T) {
mountPath := "/mnt/container"
fileName := "testfile.txt"
expectedFilePath := "/mnt/container/testfile.txt"

result := getFilePathContainer(mountPath, fileName)

if result != expectedFilePath {
t.Errorf("expected file path '%s', got '%s'", expectedFilePath, result)
}
}

0 comments on commit 6aa87b9

Please sign in to comment.