package fleet import ( "context" "testing" "time" ) // scriptRunner repond la ligne A fournie pour chaque mise a jour. type scriptRunner struct{ line string } func (r *scriptRunner) Run(_ context.Context, _ string, _ int, _ string, _ []string, out func(string)) error { out(r.line) return nil } // Un echec de mise a jour est compte (Summary.Failed), garde par l'analyse // tant que la stack n'est pas a jour, efface par un « deja a jour » ou par une // analyse qui trouve la stack a jour ; le message global nomme les stacks. func TestApplyFailureVisibility(t *testing.T) { hostPoll = 5 * time.Millisecond ctx, cancel := context.WithCancel(context.Background()) defer cancel() run := &scriptRunner{line: "A\tfail\tup -d : port is already allocated"} m, err := Open(ctx, t.TempDir(), run, 1) if err != nil { t.Fatal(err) } srv, _ := m.Add(Server{Name: "s1", Host: "h1", Enabled: true, Targets: []Target{{User: "root"}}}) tid := srv.Targets[0].ID m.scans[Key(srv.ID, tid)] = &Scan{Stacks: []Stack{{Dir: "/home/a", Name: "a", Status: StatusUpdates}}} ok, _, msg := m.applyAndWait(srv.ID, tid, "/home/a", AutoHostBy) if ok || msg != "up -d : port is already allocated" { t.Fatalf("echec attendu : %v %q", ok, msg) } if s := m.Summary(); s.Failed != 1 { t.Fatalf("Failed = %d", s.Failed) } if _, msg := tally([]HostStackResult{{Name: "a", OK: false}, {Name: "b", OK: true, Skipped: true}}); msg != "2 stack(s) : 0 relancee(s), 1 sans changement, 1 en echec : a" { t.Fatalf("message : %q", msg) } // Analyse : la stack a toujours une mise a jour -> l'echec reste. scan := func(state string) { m.mu.Lock() p := newScanParser(m.scans[Key(srv.ID, tid)]) for _, l := range []string{"S\t/home/a\tcompose.yml", "V\t/home/a\tweb\tnginx\taaa\tbbb\t" + state, "D\t/home/a"} { p.Line(l) } p.Finish() m.mu.Unlock() } scan("update") if s := m.Summary(); s.Failed != 1 { t.Fatalf("echec perdu a l'analyse : %d", s.Failed) } // « deja a jour » efface l'echec. run.line = "A\tskip\tdeja a jour : aucune nouvelle image" if ok, skipped, _ := m.applyAndWait(srv.ID, tid, "/home/a", AutoHostBy); !ok || !skipped { t.Fatal("skip attendu") } if s := m.Summary(); s.Failed != 0 { t.Fatalf("echec non efface par skip : %d", s.Failed) } // Nouvel echec, puis analyse qui trouve la stack a jour : efface. run.line = "A\tfail\tpull : denied" m.applyAndWait(srv.ID, tid, "/home/a", AutoHostBy) scan("ok") if s := m.Summary(); s.Failed != 0 { t.Fatalf("echec non efface par l'analyse : %d", s.Failed) } }