~james/core

core/spindle/engines/microvm/debug_test.go -rw-r--r-- 4.0 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//go:build linux

package microvm

import (
	"strings"
	"testing"

	"tangled.org/core/spindle/models"
	"tangled.org/core/spindle/secrets"
)

func TestDebugSSHCommandUsesJumpHost(t *testing.T) {
	got := debugSSHCommand("0.0.0.0:2224", "executor-a.internal", "executor-a", "spindle.example", "job-1")
	want := "ssh -tt -J spindle.example -p 2224 job-1@executor-a"
	if got != want {
		t.Fatalf("debug ssh command = %q, want %q", got, want)
	}
}

func TestDebugSSHCommandUsesExecutorHostWithoutJump(t *testing.T) {
	got := debugSSHCommand("0.0.0.0:22", "executor-a.example", "", "", "job-1")
	want := "ssh -tt job-1@executor-a.example"
	if got != want {
		t.Fatalf("debug ssh command = %q, want %q", got, want)
	}
}

func TestStepEnvironmentPreservesFailedStepContext(t *testing.T) {
	workflow := &models.Workflow{
		Environment: map[string]string{
			"CI":       "true",
			"OVERRIDE": "workflow",
		},
	}
	step := Step{
		environment: map[string]string{
			"OVERRIDE":  "step",
			"STEP_ONLY": "yes",
		},
	}
	unlocked := []secrets.UnlockedSecret{{
		Key:   "SECRET",
		Value: "value",
	}}

	got := make(map[string]string)
	for _, value := range stepEnvironment(workflow, step, unlocked) {
		key, value, ok := strings.Cut(value, "=")
		if ok {
			got[key] = value
		}
	}

	want := map[string]string{
		"HOME":      "/workspace",
		"LOGNAME":   guestWorkflowUser,
		"PATH":      guestBasePATH,
		"USER":      guestWorkflowUser,
		"CI":        "true",
		"OVERRIDE":  "step",
		"STEP_ONLY": "yes",
		"SECRET":    "value",
	}
	for key, value := range want {
		if got[key] != value {
			t.Errorf("%s = %q, want %q", key, got[key], value)
		}
	}
}

func heldTarget(repo, actor string) debugTarget {
	return debugTarget{
		holdRepo:  repo,
		actor:     actor,
		connected: make(chan struct{}),
		released:  make(chan struct{}),
		evicted:   make(chan struct{}),
	}
}

func holdWid(name string) models.WorkflowId {
	return models.WorkflowId{PipelineId: models.PipelineId("p"), Name: name}
}

func TestNewerFailureReplacesAnUnconnectedHold(t *testing.T) {
	e := &Engine{debug: make(map[string]debugTarget)}
	first := heldTarget("did:plc:repo", "did:plc:alice")
	if !e.claimDebugHold(holdWid("first"), first) {
		t.Fatal("first failure was not held")
	}
	// another repo, but the same pusher
	if !e.claimDebugHold(holdWid("second"), heldTarget("did:plc:other", "did:plc:alice")) {
		t.Fatal("newer failure was not held")
	}
	if !isClosed(first.evicted) {
		t.Fatal("the older hold was not told to tear down")
	}
	if _, ok := e.lookupDebugTarget(newDebugHandle(holdWid("first"))); ok {
		t.Fatal("the older hold still accepts debug shells")
	}
}

func TestConnectedHoldIsNeverReplaced(t *testing.T) {
	e := &Engine{debug: make(map[string]debugTarget)}
	first := heldTarget("did:plc:repo", "did:plc:alice")
	e.claimDebugHold(holdWid("first"), first)
	if !e.markDebugConnected(newDebugHandle(holdWid("first"))) {
		t.Fatal("connecting to the hold failed")
	}

	if e.claimDebugHold(holdWid("second"), heldTarget("did:plc:repo", "did:plc:bob")) {
		t.Fatal("a failure in the same repo was held while someone is debugging")
	}
	if isClosed(first.evicted) {
		t.Fatal("the connected hold was torn down")
	}
	if !e.claimDebugHold(holdWid("third"), heldTarget("did:plc:other", "did:plc:bob")) {
		t.Fatal("an unrelated repo and pusher should still get a hold")
	}
}

func TestHoldsWithoutAnActorOnlyShareByRepo(t *testing.T) {
	e := &Engine{debug: make(map[string]debugTarget)}
	first := heldTarget("did:plc:repo-a", "")
	e.claimDebugHold(holdWid("first"), first)
	e.claimDebugHold(holdWid("second"), heldTarget("did:plc:repo-b", ""))
	if isClosed(first.evicted) {
		t.Fatal("two repos with no recorded pusher replaced each other")
	}
}

func TestReplacedHoldRefusesAShellThatWasStillOpening(t *testing.T) {
	e := &Engine{debug: make(map[string]debugTarget)}
	e.claimDebugHold(holdWid("first"), heldTarget("did:plc:repo", "did:plc:alice"))
	e.claimDebugHold(holdWid("second"), heldTarget("did:plc:repo", "did:plc:alice"))
	if e.markDebugConnected(newDebugHandle(holdWid("first"))) {
		t.Fatal("a shell connected to a replaced hold")
	}
}