mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-09-04 13:36:35 +02:00
cmd/cli: stop the replacement before rolling its binary back
Rollback ran os.Remove(bin) while the replacement service was still running from that image. Windows locks a running executable, so the remove failed with "Access is denied" - and it was fatal, so the os.Rename that restores the previous binary never ran. The upgrade ended with the broken replacement still installed and the working binary stranded at its _previous name. Readiness failing is not evidence the process exited: the service manager can report a started service whose process never became operational. So rollback now stops the service and waits until the manager reports it stopped before touching the executable, then cleans up DNS the way the restart path's Cleanup task does. Restoring is now conditional on the previous binary reporting a version, since a _previous file that exists but produces no version output would trade a service that starts and hangs for one that cannot start at all. When it is unusable, rollback keeps it for inspection, leaves the installed binary alone, and says so instead of pressing on. The --version probe is bounded by a timeout so a binary that hangs cannot hang the upgrade. Remaining failures are reported rather than fatal, so each one says what state the host was left in. os.Remove is retried while the path stays locked, since Windows releases an image lock asynchronously after the process exits. The helpers live in a new file rather than in commands.go, and the rollback is extracted into rollbackToPreviousBinary() so it can be covered: the stop happens while the executable is still present, an unusable previous binary is kept without swapping or restarting, and a failed stop aborts before anything is modified. Reversing the stop and the remove fails these tests. The version probe is called through a variable so those tests do not have to stage a runnable executable. Staging one is not portable: oldBin is bin+"_previous", so a fixture named "ctrld" yields the extension-less "ctrld_previous", which Windows refuses to execute, and a symlink to the test binary needs a privilege Windows does not grant by default. The probe itself is still covered against the real test binary. Production is unaffected: ctrld.exe _previous does have an extension, and os/exec only appends PATHEXT entries when a path has none at all - noted at binaryVersion so the suffix is not renamed into something extension-less by accident.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -11,7 +12,27 @@ import (
|
||||
|
||||
var logOutput strings.Builder
|
||||
|
||||
// envFakeVersionOutput makes this test binary impersonate a ctrld executable: when
|
||||
// set, the process writes the value to stdout and exits without running any test, so
|
||||
// binaryVersion() can be exercised on every platform without building or shipping a
|
||||
// fixture binary. The value envFakeVersionSilent produces no output at all, which
|
||||
// reproduces a ctrld.exe_previous that exists but reports no version.
|
||||
//
|
||||
// This must be handled before m.Run(), which is what parses the test flags: the child
|
||||
// is invoked as "<binary> --version" and would otherwise die on an unknown flag.
|
||||
const (
|
||||
envFakeVersionOutput = "CTRLD_TEST_FAKE_VERSION_OUTPUT"
|
||||
envFakeVersionSilent = "<silent>"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if out := os.Getenv(envFakeVersionOutput); out != "" {
|
||||
if out != envFakeVersionSilent {
|
||||
fmt.Println(out)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
l := zerolog.New(&logOutput)
|
||||
mainLog.Store(&l)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user