Borg/cmd/compile_test.go
google-labs-jules[bot] 5711b6c13c feat: Add compile and run commands for RUNC matrices
This commit introduces two new commands to the `borg` CLI:

- `borg compile`: Compiles a `Borgfile` into a "Terminal Isolation
  Matrix" (`.matrix` file). The `Borgfile` format is a simple text file
  with `ADD <src> <dest>` instructions.

- `borg run`: Executes a `.matrix` file using `runc`. The command
  unpacks the matrix into a temporary directory and then executes `runc
  run`.

This commit also adds comprehensive tests for the new commands and the
`pkg/matrix` package, significantly increasing the overall test
coverage. The tests for the `run` command use a mocking pattern to
avoid environment dependencies.
2025-11-13 19:21:35 +00:00

119 lines
3.1 KiB
Go

package cmd
import (
"archive/tar"
"io"
"os"
"path/filepath"
"testing"
)
func TestCompileCmd_Good(t *testing.T) {
tempDir := t.TempDir()
borgfilePath := filepath.Join(tempDir, "Borgfile")
outputMatrixPath := filepath.Join(tempDir, "test.matrix")
fileToAddPath := filepath.Join(tempDir, "test.txt")
// Create a dummy file to add to the matrix.
err := os.WriteFile(fileToAddPath, []byte("hello world"), 0644)
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
// Create a dummy Borgfile.
borgfileContent := "ADD " + fileToAddPath + " /test.txt"
err = os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
if err != nil {
t.Fatalf("failed to create Borgfile: %v", err)
}
// Run the compile command.
rootCmd := NewRootCmd()
rootCmd.AddCommand(compileCmd)
_, err = executeCommand(rootCmd, "compile", "-f", borgfilePath, "-o", outputMatrixPath)
if err != nil {
t.Fatalf("compile command failed: %v", err)
}
// Verify the output matrix file.
matrixFile, err := os.Open(outputMatrixPath)
if err != nil {
t.Fatalf("failed to open output matrix file: %v", err)
}
defer matrixFile.Close()
tr := tar.NewReader(matrixFile)
foundConfig := false
foundRootFS := false
foundTestFile := false
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("failed to read tar header: %v", err)
}
switch header.Name {
case "config.json":
foundConfig = true
case "rootfs/":
foundRootFS = true
case "rootfs/test.txt":
foundTestFile = true
}
}
if !foundConfig {
t.Error("config.json not found in matrix")
}
if !foundRootFS {
t.Error("rootfs/ not found in matrix")
}
if !foundTestFile {
t.Error("rootfs/test.txt not found in matrix")
}
}
func TestCompileCmd_Bad_InvalidBorgfile(t *testing.T) {
tempDir := t.TempDir()
borgfilePath := filepath.Join(tempDir, "Borgfile")
outputMatrixPath := filepath.Join(tempDir, "test.matrix")
// Create a dummy Borgfile with an invalid instruction.
borgfileContent := "INVALID_INSTRUCTION"
err := os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
if err != nil {
t.Fatalf("failed to create Borgfile: %v", err)
}
// Run the compile command.
rootCmd := NewRootCmd()
rootCmd.AddCommand(compileCmd)
_, err = executeCommand(rootCmd, "compile", "-f", borgfilePath, "-o", outputMatrixPath)
if err == nil {
t.Fatal("compile command should have failed but did not")
}
}
func TestCompileCmd_Bad_MissingInputFile(t *testing.T) {
tempDir := t.TempDir()
borgfilePath := filepath.Join(tempDir, "Borgfile")
outputMatrixPath := filepath.Join(tempDir, "test.matrix")
// Create a dummy Borgfile that references a non-existent file.
borgfileContent := "ADD /non/existent/file /test.txt"
err := os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
if err != nil {
t.Fatalf("failed to create Borgfile: %v", err)
}
// Run the compile command.
rootCmd := NewRootCmd()
rootCmd.AddCommand(compileCmd)
_, err = executeCommand(rootCmd, "compile", "-f", borgfilePath, "-o", outputMatrixPath)
if err == nil {
t.Fatal("compile command should have failed but did not")
}
}