Files
fn_registry/functions/infra/write_dockerfile_test.go
T
egutierrez 90693fb32f feat: funciones infra — Docker, deploy, build y health check
Funciones impuras para gestión de contenedores: docker_build_image,
docker_compose_up/down, docker_volume_create/list/remove,
generate_dockerfile, write_dockerfile, go_build_binary, health_check_http,
deploy_app y stop_app. Todas con tests unitarios donde aplica.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 14:24:12 +02:00

79 lines
2.1 KiB
Go

package infra
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestWriteDockerfile(t *testing.T) {
t.Run("escribe Dockerfile en directorio existente", func(t *testing.T) {
tmpDir := t.TempDir()
content := "FROM alpine:latest\nCMD [\"sh\"]\n"
path, err := WriteDockerfile(tmpDir, content)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("cannot read written file: %v", err)
}
if string(data) != content {
t.Errorf("content mismatch: got %q, want %q", string(data), content)
}
})
t.Run("crea directorio si no existe", func(t *testing.T) {
tmpDir := t.TempDir()
newDir := filepath.Join(tmpDir, "subdir", "nested")
content := "FROM scratch\n"
path, err := WriteDockerfile(newDir, content)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, statErr := os.Stat(newDir); os.IsNotExist(statErr) {
t.Error("expected directory to be created")
}
if _, statErr := os.Stat(path); os.IsNotExist(statErr) {
t.Errorf("expected Dockerfile at %s", path)
}
})
t.Run("retorna path absoluto correcto", func(t *testing.T) {
tmpDir := t.TempDir()
content := "FROM ubuntu:22.04\n"
path, err := WriteDockerfile(tmpDir, content)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !filepath.IsAbs(path) {
t.Errorf("expected absolute path, got: %s", path)
}
if !strings.HasSuffix(path, "Dockerfile") {
t.Errorf("expected path to end with Dockerfile, got: %s", path)
}
})
t.Run("error si dir es path invalido", func(t *testing.T) {
// Intentar escribir en un path donde el padre es un archivo (no directorio)
tmpDir := t.TempDir()
// Crear un archivo donde esperamos un directorio
blockerPath := filepath.Join(tmpDir, "blocker")
if err := os.WriteFile(blockerPath, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
// Intentar usar ese archivo como directorio
_, err := WriteDockerfile(filepath.Join(blockerPath, "subdir"), "FROM scratch\n")
if err == nil {
t.Error("expected error when dir path goes through a file, got nil")
}
})
}