|
| 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