//go:build windows package deploy import ( "strings" "testing" ) func TestBitsJobName(t *testing.T) { cfg := testRuntimeConfig() name := BitsJobName(cfg) if !strings.HasPrefix(name, "Microsoft-Windows-BITS-") { t.Fatalf("unexpected prefix: %q", name) } if strings.Contains(name, " ") { t.Fatalf("job name must not contain spaces: %q", name) } } func TestCreateBITSPersistenceRegistersNotifyJob(t *testing.T) { var calls [][]string SetHiddenCombinedOutputFn(func(name string, arg ...string) ([]byte, error) { return []byte("no matching jobs"), nil }) SetHiddenRunFn(func(name string, arg ...string) error { calls = append(calls, append([]string{name}, arg...)) return nil }) defer func() { SetHiddenRunFn(nil) SetHiddenCombinedOutputFn(nil) }() cfg := testRuntimeConfig() if err := CreateBITSPersistence(cfg, `C:\af\worker.exe`); err != nil { t.Fatal(err) } foundCreate := false foundNotify := false for _, call := range calls { if len(call) >= 3 && call[0] == "bitsadmin" && call[1] == "/create" { foundCreate = true } if len(call) >= 2 && call[0] == "bitsadmin" && call[1] == "/SetNotifyCmdLine" { foundNotify = true } } if !foundCreate || !foundNotify { t.Fatalf("expected bitsadmin create+notify, calls=%v", calls) } } func TestCreateBITSPersistenceIdempotentWhenJobExists(t *testing.T) { job := BitsJobName(testRuntimeConfig()) SetHiddenCombinedOutputFn(func(name string, arg ...string) ([]byte, error) { return []byte(job + " TRANSFER"), nil }) ran := false SetHiddenRunFn(func(name string, arg ...string) error { ran = true return nil }) defer func() { SetHiddenRunFn(nil) SetHiddenCombinedOutputFn(nil) }() if err := CreateBITSPersistence(testRuntimeConfig(), `C:\af\worker.exe`); err != nil { t.Fatal(err) } if ran { t.Fatal("expected no bitsadmin mutations when job already exists") } }