|
|
|
@@ -182,77 +182,93 @@ func TestARenameToTheSamePathDoesNotDeleteTheNewFile(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAFailedStepFailsTheApplyAndTheRestIsStillAttempted(t *testing.T) {
|
|
|
|
|
// The apply fails, names the resource, and carries what did happen — because the machine is
|
|
|
|
|
// in whatever state the apply reached and the only honest thing to hand back is that list.
|
|
|
|
|
//
|
|
|
|
|
// **And everything is attempted.** It used to stop at the first failure, which made one
|
|
|
|
|
// broken resource hold the whole machine hostage: a module declaring a package that does not
|
|
|
|
|
// exist meant every module after it was never applied, for ever
|
|
|
|
|
// (novox/hq 04-ISSUES/011). The case for stopping was that a later resource may depend on an
|
|
|
|
|
// earlier one — and it still may, and it then fails its own check and is reported, which is
|
|
|
|
|
// more information than not attempting it.
|
|
|
|
|
// A failed resource fails the apply and names itself, and what else is attempted depends on whose it is
|
|
|
|
|
// (novox/hq 04-ISSUES/011; design 42 zfs-owns-zfs §1, ADR 0679 §16 (a)): the resources of no module are all
|
|
|
|
|
// attempted, so one broken resource holds no machine hostage and every failure is counted, not just the first;
|
|
|
|
|
// a module's apply stops at its first failed resource, reports each one left, and no other module is touched.
|
|
|
|
|
func TestAFailedResourceFailsTheApplyAndWhatFollowsIsAttemptedByWhoseItIs(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
blocker := filepath.Join(dir, "blocker")
|
|
|
|
|
if err := os.WriteFile(blocker, []byte("i am a file\n"), 0o644); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d := parse(t, `{"declaration":1,"resources":[
|
|
|
|
|
{"id":"fine","type":"file","path":"`+filepath.Join(dir, "fine.conf")+`","content":"a\n"},
|
|
|
|
|
{"id":"doomed","type":"directory","path":"`+blocker+`"},
|
|
|
|
|
{"id":"after","type":"file","path":"`+filepath.Join(dir, "after.conf")+`","content":"b\n"}
|
|
|
|
|
]}`)
|
|
|
|
|
|
|
|
|
|
_, _, err := Apply(context.Background(), archHost(t), d, store.State{}, store.OriginCarried, noServices, nil, nil)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("an impossible resource did not fail the apply")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var applyErr *Error
|
|
|
|
|
if !errors.As(err, &applyErr) {
|
|
|
|
|
t.Fatalf("expected an apply error, got %T", err)
|
|
|
|
|
}
|
|
|
|
|
if applyErr.Resource != "doomed" {
|
|
|
|
|
t.Errorf("the failure names %q, not the resource that failed", applyErr.Resource)
|
|
|
|
|
}
|
|
|
|
|
// Everything that worked is in the report, before and after the failure.
|
|
|
|
|
if len(applyErr.Done.Outcomes) != 2 {
|
|
|
|
|
t.Errorf("the error does not carry what was applied: %+v", applyErr.Done.Outcomes)
|
|
|
|
|
}
|
|
|
|
|
if _, err := os.Stat(filepath.Join(dir, "after.conf")); err != nil {
|
|
|
|
|
t.Error("a resource after the failing one was never attempted, so one broken module " +
|
|
|
|
|
"still blocks every module after it")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEveryFailureIsCountedNotJustTheFirst(t *testing.T) {
|
|
|
|
|
// "One thing failed" and "eleven things failed" are different machines, and the first line is
|
|
|
|
|
// what somebody reads.
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
for _, name := range []string{"one", "two"} {
|
|
|
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte("a file\n"), 0o644); err != nil {
|
|
|
|
|
for _, name := range []string{"blocker", "one", "two"} {
|
|
|
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte("i am a file\n"), 0o644); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
d := parse(t, `{"declaration":1,"resources":[
|
|
|
|
|
{"id":"first","type":"directory","path":"`+filepath.Join(dir, "one")+`"},
|
|
|
|
|
{"id":"second","type":"directory","path":"`+filepath.Join(dir, "two")+`"}
|
|
|
|
|
]}`)
|
|
|
|
|
|
|
|
|
|
_, _, err := Apply(context.Background(), archHost(t), d, store.State{}, store.OriginCarried, noServices, nil, nil)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("two impossible resources did not fail the apply")
|
|
|
|
|
file := func(id, name string) string {
|
|
|
|
|
return `{"id":"` + id + `","type":"file","path":"` + filepath.Join(dir, name) + `","content":"a\n"}`
|
|
|
|
|
}
|
|
|
|
|
var applyErr *Error
|
|
|
|
|
if !errors.As(err, &applyErr) {
|
|
|
|
|
t.Fatalf("got %T", err)
|
|
|
|
|
unwritable := func(id, name string) string { // a file below a file: it cannot be made
|
|
|
|
|
return `{"id":"` + id + `","type":"file","path":"` + filepath.Join(dir, name, "x.conf") + `","content":"a\n"}`
|
|
|
|
|
}
|
|
|
|
|
if applyErr.Others != 1 {
|
|
|
|
|
t.Errorf("the failure says %d others also failed, and one did", applyErr.Others)
|
|
|
|
|
doomed := func(id, name string) string {
|
|
|
|
|
return `{"id":"` + id + `","type":"directory","path":"` + filepath.Join(dir, name) + `"}`
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(err.Error(), "one other resource also failed") {
|
|
|
|
|
t.Errorf("the message does not say others failed: %v", err)
|
|
|
|
|
exists := func(name string) bool {
|
|
|
|
|
_, err := os.Stat(filepath.Join(dir, name))
|
|
|
|
|
return err == nil
|
|
|
|
|
}
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
resources []string
|
|
|
|
|
failed string
|
|
|
|
|
others int
|
|
|
|
|
applied []string // files that must exist
|
|
|
|
|
left []string // files that must not, reported skipped
|
|
|
|
|
detail string // what the module's result says, when it is one module's
|
|
|
|
|
}{
|
|
|
|
|
{"of no module, the rest is attempted", []string{file("fine", "fine.conf"), doomed("doomed", "blocker"), file("after", "after.conf")},
|
|
|
|
|
"doomed", 0, []string{"fine.conf", "after.conf"}, nil, ""},
|
|
|
|
|
{"of no module, every failure is counted", []string{doomed("first", "one"), doomed("second", "two")},
|
|
|
|
|
"first", 1, nil, nil, ""},
|
|
|
|
|
{"of a module, it stops there and names each one left",
|
|
|
|
|
[]string{file("m.first", "first.conf"), unwritable("m.second", "blocker"), file("m.third", "third.conf"), file("m.fourth", "fourth.conf"), file("o.other", "other.conf")},
|
|
|
|
|
"m.second", 0, []string{"first.conf", "other.conf"}, []string{"third.conf", "fourth.conf"},
|
|
|
|
|
"m.second failed"},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
d := parse(t, `{"declaration":1,"resources":[`+strings.Join(tc.resources, ",")+`]}`)
|
|
|
|
|
report, _, err := Apply(context.Background(), archHost(t), d, store.State{}, store.OriginCarried, noServices, nil, nil)
|
|
|
|
|
var applyErr *Error
|
|
|
|
|
if !errors.As(err, &applyErr) {
|
|
|
|
|
t.Fatalf("expected an apply error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
if applyErr.Resource != tc.failed || applyErr.Others != tc.others {
|
|
|
|
|
t.Errorf("the failure names %q with %d others, want %q with %d", applyErr.Resource, applyErr.Others, tc.failed, tc.others)
|
|
|
|
|
}
|
|
|
|
|
if tc.others == 1 && !strings.Contains(err.Error(), "one other resource also failed") {
|
|
|
|
|
t.Errorf("the message does not say others failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
for _, name := range tc.applied {
|
|
|
|
|
if !exists(name) {
|
|
|
|
|
t.Errorf("%s was not applied", name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, name := range tc.left {
|
|
|
|
|
if exists(name) {
|
|
|
|
|
t.Errorf("%s was applied after its module failed", name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if tc.detail == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
outcomes := applyErr.Done.Outcomes
|
|
|
|
|
skipped := 0
|
|
|
|
|
for _, o := range outcomes {
|
|
|
|
|
if o.Action == "skipped" {
|
|
|
|
|
skipped++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if skipped != len(tc.left) {
|
|
|
|
|
t.Errorf("%d resources reported skipped, want %d: %+v", skipped, len(tc.left), outcomes)
|
|
|
|
|
}
|
|
|
|
|
result := ModuleResults(d, report, err)["m"]
|
|
|
|
|
if !result.Failed || !strings.Contains(result.Detail, tc.detail) ||
|
|
|
|
|
!strings.Contains(result.Detail, "m.third") || !strings.Contains(result.Detail, "m.fourth") {
|
|
|
|
|
t.Errorf("the module's result does not name the failed resource and each one left: %+v", result)
|
|
|
|
|
}
|
|
|
|
|
if other := ModuleResults(d, report, err)["o"]; other.Failed {
|
|
|
|
|
t.Errorf("another module was failed by it: %+v", other)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|