dockerbuild/main_test.go
Ondrej Vlach 5b9e142770
All checks were successful
Go Test / test (push) Successful in 1m44s
initial commit
2025-05-30 19:51:25 +02:00

137 lines
3.6 KiB
Go

package main
import (
"os"
"os/exec"
"strings"
"testing"
)
// Test parseSecrets with various input formats
func TestParseSecrets(t *testing.T) {
input := `
SECRET1=foo
SECRET2="bar"
# This is a comment
SECRET3= baz
`
expected := map[string]string{
"SECRET1": "foo",
"SECRET2": "bar",
"SECRET3": "baz",
}
result := parseSecrets(input)
if len(result) != len(expected) {
t.Fatalf("expected %d secrets, got %d", len(expected), len(result))
}
for k, v := range expected {
if result[k] != v {
t.Errorf("expected %s=%s, got %s", k, v, result[k])
}
}
}
// Integration-like test for writing secrets to file
func TestWriteSecretsFile(t *testing.T) {
secrets := map[string]string{
"FOO": "bar",
"BAR": "baz",
}
f, err := os.CreateTemp("", "secrets-test")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(f.Name())
defer f.Close()
for k, v := range secrets {
_, err := f.WriteString(k + "=" + v + "\n")
if err != nil {
t.Fatalf("failed to write secret: %v", err)
}
}
f.Sync()
// Read back and check
content, err := os.ReadFile(f.Name())
if err != nil {
t.Fatalf("failed to read secrets file: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(content)), "\n")
if len(lines) != len(secrets) {
t.Fatalf("expected %d lines, got %d", len(secrets), len(lines))
}
}
// Test getShortHash (requires git repo)
func TestGetShortHash(t *testing.T) {
cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
out, err := cmd.Output()
if err != nil {
t.Skip("Skipping getShortHash test: not a git repo", err)
}
expected := strings.TrimSpace(string(out))
got := getShortHash()
if got != expected {
t.Errorf("expected hash %s, got %s", expected, got)
}
}
// Test buildDockerCommand returns a valid *exec.Cmd with context and dockerfile
func TestBuildDockerCommandWithContextAndDockerfile(t *testing.T) {
secrets := map[string]string{
"FOO": "bar",
}
context := "./foo"
dockerfile := "Dockerfile"
tagLatest := "test:latest"
tagNightly := "test:nightly"
cmd := buildDockerCommand(context, dockerfile, secrets, tagLatest, tagNightly)
if cmd == nil {
t.Fatal("expected non-nil *exec.Cmd")
}
args := strings.Join(cmd.Args, " ")
if !strings.Contains(args, context) {
t.Errorf("docker build args missing context: %v", cmd.Args)
}
if !strings.Contains(args, "-f") || !strings.Contains(args, dockerfile) {
t.Errorf("docker build args missing dockerfile: %v", cmd.Args)
}
if !strings.Contains(args, tagLatest) || !strings.Contains(args, tagNightly) {
t.Errorf("docker build args missing tags: %v", cmd.Args)
}
if !strings.Contains(args, "--secret") {
t.Errorf("docker build args missing --secret: %v", cmd.Args)
}
}
// Test buildDockerCommand returns a valid *exec.Cmd without secrets
func TestBuildDockerCommandNoSecrets(t *testing.T) {
secrets := map[string]string{}
context := "."
dockerfile := "Dockerfile"
tagLatest := "test:latest"
tagNightly := "test:nightly"
cmd := buildDockerCommand(context, dockerfile, secrets, tagLatest, tagNightly)
if cmd == nil {
t.Fatal("expected non-nil *exec.Cmd")
}
args := strings.Join(cmd.Args, " ")
if strings.Contains(args, "--secret") {
t.Errorf("docker build args should not contain --secret when no secrets are provided: %v", cmd.Args)
}
}
// Test pushDockerCommand returns a valid *exec.Cmd
func TestPushDockerCommand(t *testing.T) {
tag := "test:latest"
cmd := pushDockerCommand(tag)
if cmd == nil {
t.Fatal("expected non-nil *exec.Cmd")
}
args := strings.Join(cmd.Args, " ")
if !strings.Contains(args, "push") || !strings.Contains(args, tag) {
t.Errorf("docker push args missing: %v", cmd.Args)
}
}