103 lines
3.6 KiB
Go
103 lines
3.6 KiB
Go
package buildstep
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/delivery"
|
|
)
|
|
|
|
// scanOf is the steps a keys double holds, last value per key.
|
|
func scanOf(k *keys) Scan {
|
|
return Scan{
|
|
Keys: func() ([]string, error) {
|
|
k.mu.Lock()
|
|
defer k.mu.Unlock()
|
|
var out []string
|
|
for key := range k.puts {
|
|
out = append(out, key)
|
|
}
|
|
return out, nil
|
|
},
|
|
Get: func(key string) (json.RawMessage, error) {
|
|
k.mu.Lock()
|
|
defer k.mu.Unlock()
|
|
s, ok := k.puts[key]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
raw, err := delivery.EncodeStep(s[len(s)-1])
|
|
return raw, err
|
|
},
|
|
}
|
|
}
|
|
|
|
// 1890: EndStale ends only this node's steps left running before the process began; EndOne ends one by hand.
|
|
func TestEndStaleAndEndOne(t *testing.T) {
|
|
k := newKeys()
|
|
boot := time.Now()
|
|
put := func(node, id, state string, started time.Time) {
|
|
key, _ := delivery.StepKey(node, id)
|
|
_, _ = k.put(key, json.RawMessage(mustStep(t, delivery.Step{ID: id, Tool: "check ask", Node: node, State: state, Started: started})))
|
|
}
|
|
put("ace", "old1", delivery.StepRunning, boot.Add(-time.Hour))
|
|
put("ace", "new1", delivery.StepRunning, boot.Add(time.Second))
|
|
put("ace", "done1", delivery.StepPassed, boot.Add(-time.Hour))
|
|
put("g14", "other1", delivery.StepRunning, boot.Add(-time.Hour))
|
|
scan := scanOf(k)
|
|
ended, err := EndStale(scan, k.put, "ace", boot, nil)
|
|
if err != nil || len(ended) != 1 || ended[0] != "old1" {
|
|
t.Fatalf("ended %v, %v", ended, err)
|
|
}
|
|
if s := k.last("ace.old1"); s.State != delivery.StepFailed || s.Reason != RestartReason || s.Ended.IsZero() {
|
|
t.Errorf("old1 = %+v", s)
|
|
}
|
|
for key, state := range map[string]string{"ace.new1": delivery.StepRunning, "ace.done1": delivery.StepPassed, "g14.other1": delivery.StepRunning} {
|
|
if s := k.last(key); s.State != state {
|
|
t.Errorf("%s = %s, want %s", key, s.State, state)
|
|
}
|
|
}
|
|
// EndOne: refuses without why, a held step, a step not running and another node's; ends the rest.
|
|
if _, err := EndOne(scan, k.put, "ace", "new1", "", nil); err == nil {
|
|
t.Error("ended with no why")
|
|
}
|
|
if _, err := EndOne(scan, k.put, "ace", "new1", "stuck", func(string) bool { return true }); err == nil || !strings.Contains(err.Error(), "running in this process") {
|
|
t.Errorf("held step: %v", err)
|
|
}
|
|
if _, err := EndOne(scan, k.put, "ace", "done1", "x", nil); err == nil {
|
|
t.Error("ended a passed step")
|
|
}
|
|
if _, err := EndOne(scan, k.put, "ace", "g14.other1", "x", nil); err == nil {
|
|
t.Error("ended another node's step")
|
|
}
|
|
if _, err := EndOne(scan, k.put, "ace", "ace.new1", "stuck for good", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s := k.last("ace.new1"); s.State != delivery.StepFailed || s.Reason != "stuck for good" {
|
|
t.Errorf("new1 = %+v", s)
|
|
}
|
|
}
|
|
|
|
func mustStep(t *testing.T, s delivery.Step) []byte {
|
|
t.Helper()
|
|
raw, err := delivery.EncodeStep(s)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return raw
|
|
}
|
|
|
|
// 1890: AskWhat names the repository, head and levels of a check ask and the module of a build ask.
|
|
func TestAskWhat(t *testing.T) {
|
|
check := delivery.StepAsk{Kind: "check", Check: &delivery.CheckJob{Fetch: map[string]any{"repository": "novox/mesh-sdk", "commit": "0123456789abcdef0123"}, Levels: []string{"unit", "integration"}}}
|
|
if got, want := AskWhat(check), "check ask, novox/mesh-sdk@0123456789ab, levels unit,integration"; got != want {
|
|
t.Errorf("check = %q", got)
|
|
}
|
|
build := delivery.StepAsk{Kind: "delivery", Build: &delivery.BuildJob{Module: "mesh-x", FP: "abcdef0123456789"}}
|
|
if got, want := AskWhat(build), "delivery ask, module mesh-x, fp abcdef012345"; got != want {
|
|
t.Errorf("build = %q", got)
|
|
}
|
|
}
|