|
| 1 | +//go:build integration && windows |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/y3owk1n/neru/internal/derrors" |
| 12 | +) |
| 13 | + |
| 14 | +// testTaskPath is a task name no real install uses, so the round trip below |
| 15 | +// never touches a \Neru task the machine already has. |
| 16 | +func testTaskPath(t *testing.T) string { |
| 17 | + t.Helper() |
| 18 | + |
| 19 | + return fmt.Sprintf(`\NeruTest-%d`, os.Getpid()) |
| 20 | +} |
| 21 | + |
| 22 | +// requireTaskScheduler skips when the scheduler cannot be reached at all, |
| 23 | +// which a locked-down session reports as an error on the very first call. |
| 24 | +func requireTaskScheduler(t *testing.T, path string) { |
| 25 | + t.Helper() |
| 26 | + |
| 27 | + status := statusServiceTask(path) |
| 28 | + if strings.HasPrefix(status, "Service status unavailable") { |
| 29 | + t.Skipf("skipping: %s", status) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// TestServiceTask_RoundTrip registers, inspects, drives and deletes a task on |
| 34 | +// the real scheduler, which is the only reader whose opinion of the XML counts. |
| 35 | +// |
| 36 | +// The action is this test binary with `launch` as its argument, which exits at |
| 37 | +// once, so the task's state after Run may already be back to ready; the |
| 38 | +// assertion is that Run was accepted, and that status reports an installed |
| 39 | +// task, not that a daemon stays up. |
| 40 | +func TestServiceTask_RoundTrip(t *testing.T) { |
| 41 | + path := testTaskPath(t) |
| 42 | + requireTaskScheduler(t, path) |
| 43 | + |
| 44 | + t.Cleanup(func() { _ = uninstallServiceTask(path) }) |
| 45 | + |
| 46 | + if status := statusServiceTask(path); !strings.Contains(status, "not installed") { |
| 47 | + t.Fatalf("statusServiceTask() before install = %q, want not installed", status) |
| 48 | + } |
| 49 | + |
| 50 | + err := installServiceTask(path) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("installServiceTask() error = %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + err = installServiceTask(path) |
| 56 | + if !derrors.IsCode(err, derrors.CodeInvalidInput) { |
| 57 | + t.Errorf("second installServiceTask() error = %v, want %v", err, derrors.CodeInvalidInput) |
| 58 | + } |
| 59 | + |
| 60 | + status := statusServiceTask(path) |
| 61 | + if !strings.HasPrefix(status, "Service installed: ") || |
| 62 | + !strings.Contains(status, "enabled at login") { |
| 63 | + t.Errorf("statusServiceTask() after install = %q, want installed and enabled", status) |
| 64 | + } |
| 65 | + |
| 66 | + for _, step := range []struct { |
| 67 | + name string |
| 68 | + call func(string) error |
| 69 | + }{ |
| 70 | + {name: "stop", call: func(p string) error { return driveServiceTask("stop", p, stopTask) }}, |
| 71 | + {name: "start", call: func(p string) error { return driveServiceTask("start", p, runTask) }}, |
| 72 | + {name: "restart", call: func(p string) error { return driveServiceTask("restart", p, restartTask) }}, |
| 73 | + } { |
| 74 | + err = step.call(path) |
| 75 | + if err != nil { |
| 76 | + t.Errorf("%s error = %v", step.name, err) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + err = uninstallServiceTask(path) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("uninstallServiceTask() error = %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + if status := statusServiceTask(path); !strings.Contains(status, "not installed") { |
| 86 | + t.Errorf("statusServiceTask() after uninstall = %q, want not installed", status) |
| 87 | + } |
| 88 | + |
| 89 | + err = uninstallServiceTask(path) |
| 90 | + if err != nil { |
| 91 | + t.Errorf("uninstallServiceTask() on nothing = %v, want nil", err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// TestDriveServiceTask_RefusesWhenNothingIsInstalled pins that start, stop and |
| 96 | +// restart on a machine with no task say so rather than reporting success. |
| 97 | +func TestDriveServiceTask_RefusesWhenNothingIsInstalled(t *testing.T) { |
| 98 | + path := testTaskPath(t) |
| 99 | + requireTaskScheduler(t, path) |
| 100 | + |
| 101 | + err := driveServiceTask("start", path, runTask) |
| 102 | + if !derrors.IsCode(err, derrors.CodeInvalidInput) { |
| 103 | + t.Fatalf("driveServiceTask() error = %v, want %v", err, derrors.CodeInvalidInput) |
| 104 | + } |
| 105 | + |
| 106 | + if !strings.Contains(err.Error(), "neru services install") { |
| 107 | + t.Errorf("driveServiceTask() message = %q, want it to name the next step", err.Error()) |
| 108 | + } |
| 109 | +} |
0 commit comments