Third review of mesh-catalog #147. A unit's name can now come from a setting (a template's instance), so every systemctl call that names a unit passes `--` before it, and a name can never be read as an option. On removal, a unit is started or enabled again only when it is exactly the stopped and disabled the mesh held it to. A masked unit, or one somebody started or enabled since, is left as it is, and the report says why. A restore that fails (the start refused, or the unit not running after it) puts back what this attempt changed and is reported as waiting, so no apply is made fatal by it and the next one tries again. The tests' fake service managers read systemctl's arguments in the shape they were written for (oldShape); internal/system's own test holds the `--`.
38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Third review of mesh-catalog #147: a unit's name reaches systemctl as an argument, and since a setting
|
|
// may now name a template's instance, a name beginning with '-' must never be read as an option. Every
|
|
// call that names a unit passes `--` before it.
|
|
func TestEverySystemctlCallPassesDashDashBeforeTheUnit(t *testing.T) {
|
|
var asked [][]string
|
|
run := func(_ context.Context, name string, args ...string) (string, error) {
|
|
if name == "systemctl" {
|
|
asked = append(asked, args)
|
|
}
|
|
return "LoadState=loaded\nActiveState=active\nType=simple\nFragmentPath=/x\n", nil
|
|
}
|
|
ctx := context.Background()
|
|
a := arch{}
|
|
unit := "-x@y.timer"
|
|
a.ServiceState(ctx, run, unit)
|
|
a.SetServiceState(ctx, run, unit, "running")
|
|
a.SetServiceState(ctx, run, unit, "stopped")
|
|
a.ServiceBoot(ctx, run, unit)
|
|
a.SetServiceBoot(ctx, run, unit, "enabled")
|
|
a.SetServiceBoot(ctx, run, unit, "disabled")
|
|
if len(asked) != 6 {
|
|
t.Fatalf("asked %d times: %v", len(asked), asked)
|
|
}
|
|
for _, args := range asked {
|
|
if len(args) < 2 || args[len(args)-1] != unit || args[len(args)-2] != "--" {
|
|
t.Errorf("systemctl %s", strings.Join(args, " "))
|
|
}
|
|
}
|
|
}
|