Files
Archduino/internal/modules/hello/hello_test.go

86 lines
1.7 KiB
Go

package hello
import (
"context"
"os"
"path/filepath"
"testing"
"gitlab.hive-brain.de/MDietermann/Archduino/internal/engine"
)
func TestHelloFullLifecycle(t *testing.T) {
path := filepath.Join(t.TempDir(), "hello.ok")
m := Module{Path: path}
ctx := context.Background()
st, err := m.Detect(ctx)
if err != nil {
t.Fatal(err)
}
if st.(state).Present {
t.Fatal("marker should be absent on fresh tmpdir")
}
plan, err := m.Plan(ctx, st)
if err != nil {
t.Fatal(err)
}
if plan.IsEmpty() {
t.Fatal("plan should have one action")
}
if err := m.Apply(ctx, plan, engine.NewBufferedReporter()); err != nil {
t.Fatal(err)
}
if err := m.Verify(ctx); err != nil {
t.Fatal(err)
}
st2, _ := m.Detect(ctx)
if !st2.(state).Present {
t.Fatal("marker should be present after apply")
}
plan2, _ := m.Plan(ctx, st2)
if !plan2.IsEmpty() {
t.Fatal("second plan should be empty (idempotent)")
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(data) != markerContent {
t.Fatalf("content = %q", data)
}
}
func TestHelloPlanForceNonEmpty(t *testing.T) {
path := filepath.Join(t.TempDir(), "hello.ok")
m := Module{Path: path}
// Even after marker is present, force plan must run.
if err := os.WriteFile(path, []byte(markerContent), 0o644); err != nil {
t.Fatal(err)
}
plan, err := m.PlanForce(context.Background())
if err != nil {
t.Fatalf("PlanForce: %v", err)
}
if plan.IsEmpty() {
t.Fatal("force plan must not be empty")
}
for _, a := range plan.Actions {
if err := a.Exec(context.Background()); err != nil {
t.Fatalf("Exec: %v", err)
}
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(data) != markerContent {
t.Fatalf("content after force = %q", data)
}
}