Borg/cmd/run_test.go
google-labs-jules[bot] 47286e2714 feat: Implement Go examples and refactor matrix execution
- Implements all placeholder Go examples in the `examples` directory.
- Corrects the `run_matrix_programmatically` example to use the `borg` package.
- Refactors the code to centralize the matrix execution logic in the `matrix` package.
- Updates the documentation to include a new "Programmatic Usage" section that describes all of the Go examples.
- Updates the "Terminal Isolation Matrix" section to remove manual 'runc' instructions, emphasizing that 'borg run' handles this process to maintain security and isolation.
- Adds missing examples for 'collect github repos', 'collect github release', and 'compile' commands to the documentation.
- Makes `pkg/matrix.Run` testable by exposing `exec.Command` as a public variable.
- Adds tests for the `matrix` package that mock the `runc` command.
- Updates the `cmd` package tests to mock `matrix.ExecCommand` instead of the old `cmd.execCommand`.
2025-11-14 11:19:48 +00:00

112 lines
2.9 KiB
Go

package cmd
import (
"archive/tar"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/Snider/Borg/pkg/matrix"
)
func TestRunCmd_Good(t *testing.T) {
// Create a dummy matrix file.
matrixPath := createDummyMatrix(t)
// Mock the exec.Command function in the matrix package.
origExecCommand := matrix.ExecCommand
matrix.ExecCommand = func(command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
return cmd
}
t.Cleanup(func() {
matrix.ExecCommand = origExecCommand
})
// Run the run command.
rootCmd := NewRootCmd()
rootCmd.AddCommand(GetRunCmd())
_, err := executeCommand(rootCmd, "run", matrixPath)
if err != nil {
t.Fatalf("run command failed: %v", err)
}
}
func TestRunCmd_Bad(t *testing.T) {
t.Run("Missing input file", func(t *testing.T) {
// Run the run command with a non-existent file.
rootCmd := NewRootCmd()
rootCmd.AddCommand(GetRunCmd())
_, err := executeCommand(rootCmd, "run", "/non/existent/file.matrix")
if err == nil {
t.Fatal("run command should have failed but did not")
}
})
}
func TestRunCmd_Ugly(t *testing.T) {
t.Run("Invalid matrix file", func(t *testing.T) {
// Create an invalid (non-tar) matrix file.
tempDir := t.TempDir()
matrixPath := filepath.Join(tempDir, "invalid.matrix")
err := os.WriteFile(matrixPath, []byte("this is not a tar file"), 0644)
if err != nil {
t.Fatalf("failed to create invalid matrix file: %v", err)
}
// Run the run command.
rootCmd := NewRootCmd()
rootCmd.AddCommand(GetRunCmd())
_, err = executeCommand(rootCmd, "run", matrixPath)
if err == nil {
t.Fatal("run command should have failed but did not")
}
})
}
// createDummyMatrix creates a valid, empty matrix file for testing.
func createDummyMatrix(t *testing.T) string {
t.Helper()
tempDir := t.TempDir()
matrixPath := filepath.Join(tempDir, "test.matrix")
matrixFile, err := os.Create(matrixPath)
if err != nil {
t.Fatalf("failed to create dummy matrix file: %v", err)
}
defer matrixFile.Close()
tw := tar.NewWriter(matrixFile)
// Add a dummy config.json. This is not a valid config, but it's enough to test the run command.
configContent := []byte(`{}`)
hdr := &tar.Header{
Name: "config.json",
Mode: 0600,
Size: int64(len(configContent)),
}
if err := tw.WriteHeader(hdr); err != nil {
t.Fatalf("failed to write tar header: %v", err)
}
if _, err := tw.Write(configContent); err != nil {
t.Fatalf("failed to write tar content: %v", err)
}
// Add the rootfs directory.
hdr = &tar.Header{
Name: "rootfs/",
Mode: 0755,
Typeflag: tar.TypeDir,
}
if err := tw.WriteHeader(hdr); err != nil {
t.Fatalf("failed to write tar header: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatalf("failed to close tar writer: %v", err)
}
return matrixPath
}