-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_test.go
More file actions
248 lines (234 loc) · 6.74 KB
/
setup_test.go
File metadata and controls
248 lines (234 loc) · 6.74 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package introspector_enclave
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestParseGitRemoteURL(t *testing.T) {
tests := []struct {
name string
url string
wantOwner string
wantRepo string
wantErr bool
}{
{
name: "SSH",
url: "git@github.com:ArkLabsHQ/introspector-enclave.git",
wantOwner: "ArkLabsHQ",
wantRepo: "introspector-enclave",
},
{
name: "HTTPS with .git",
url: "https://github.com/ArkLabsHQ/introspector-enclave.git",
wantOwner: "ArkLabsHQ",
wantRepo: "introspector-enclave",
},
{
name: "HTTPS without .git",
url: "https://github.com/MyOrg/MyRepo",
wantOwner: "MyOrg",
wantRepo: "MyRepo",
},
{
name: "HTTP",
url: "http://github.com/Owner/Repo.git",
wantOwner: "Owner",
wantRepo: "Repo",
},
{
name: "invalid SSH missing colon",
url: "git@github.com/Owner/Repo",
wantErr: true,
},
{
name: "invalid no path",
url: "https://github.com",
wantErr: true,
},
{
name: "invalid single segment",
url: "https://github.com/onlyone",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
owner, repo, err := parseGitRemoteURL(tt.url)
if tt.wantErr {
if err == nil {
t.Errorf("expected error for %q", tt.url)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if owner != tt.wantOwner {
t.Errorf("owner = %q, want %q", owner, tt.wantOwner)
}
if repo != tt.wantRepo {
t.Errorf("repo = %q, want %q", repo, tt.wantRepo)
}
})
}
}
func TestReplaceYAMLValue(t *testing.T) {
tests := []struct {
name string
content string
key string
newValue string
want string
}{
{
name: "simple replacement",
content: `nix_rev: "oldvalue"`,
key: "nix_rev",
newValue: "newvalue",
want: `nix_rev: "newvalue"`,
},
{
name: "empty to value",
content: `nix_hash: ""`,
key: "nix_hash",
newValue: "sha256-abc",
want: `nix_hash: "sha256-abc"`,
},
{
name: "preserves surrounding content",
content: "name: \"myapp\"\nnix_rev: \"old\"\nregion: \"us-east-1\"",
key: "nix_rev",
newValue: "new",
want: "name: \"myapp\"\nnix_rev: \"new\"\nregion: \"us-east-1\"",
},
{
name: "no match leaves unchanged",
content: `nix_rev: "old"`,
key: "nonexistent",
newValue: "new",
want: `nix_rev: "old"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := replaceYAMLValue(tt.content, tt.key, tt.newValue)
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
})
}
}
// TestComputeNixHash_MonorepoUsesRepoRoot guards against the regression
// where `git archive` was run with cwd set to the enclave.yaml directory.
// In a monorepo layout (git root != enclave config dir) that caused
// `git archive` to archive only the subtree, producing a hash that did
// not match what fetchFromGitHub computes for the full repo tarball.
//
// The fix resolves `git rev-parse --show-toplevel` and uses that as cwd.
//
// This test creates a repo with the enclave config in a subdirectory,
// computes the hash via the function-under-test, and compares it to the
// hash of the full repo archive. They must match.
func TestComputeNixHash_MonorepoUsesRepoRoot(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
if _, err := exec.LookPath("nix"); err != nil {
t.Skip("nix not available")
}
if _, err := exec.LookPath("tar"); err != nil {
t.Skip("tar not available")
}
repo := t.TempDir()
// Minimal repo with a top-level file AND a subdirectory containing
// enclave.yaml (the monorepo shape that previously triggered the bug).
if err := os.WriteFile(filepath.Join(repo, "top.txt"), []byte("repo-root-content\n"), 0644); err != nil {
t.Fatal(err)
}
sub := filepath.Join(repo, "server", "enclave")
if err := os.MkdirAll(sub, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(sub, "enclave.yaml"), []byte("name: test\n"), 0644); err != nil {
t.Fatal(err)
}
runIn := func(dir, name string, args ...string) {
t.Helper()
cmd := exec.Command(name, args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%s %v: %v\n%s", name, args, err, out)
}
}
runIn(repo, "git", "init", "-q")
runIn(repo, "git", "-c", "user.email=t@t", "-c", "user.name=T", "add", ".")
runIn(repo, "git", "-c", "user.email=t@t", "-c", "user.name=T", "commit", "-q", "-m", "init")
revOut, err := exec.Command("git", "-C", repo, "rev-parse", "HEAD").Output()
if err != nil {
t.Fatal(err)
}
rev := strings.TrimSpace(string(revOut))
// Simulate the buggy caller: pass the subdirectory (where enclave.yaml
// lives) as the "root" argument. The fix should still produce the
// full-repo hash by resolving the git top-level internally.
got, err := computeNixHash(filepath.Join(repo, "server"), rev)
if err != nil {
t.Fatalf("computeNixHash: %v", err)
}
// Expected = hash of the full repo via git archive run at the git top.
expTmp := t.TempDir()
archive := exec.Command("git", "archive", "--format=tar.gz", "--prefix=source/", rev)
archive.Dir = repo
extract := exec.Command("tar", "xz", "-C", expTmp)
extract.Stdin, err = archive.StdoutPipe()
if err != nil {
t.Fatal(err)
}
if err := extract.Start(); err != nil {
t.Fatal(err)
}
if err := archive.Run(); err != nil {
t.Fatal(err)
}
if err := extract.Wait(); err != nil {
t.Fatal(err)
}
wantOut, err := exec.Command("nix", "hash", "path", filepath.Join(expTmp, "source")).Output()
if err != nil {
t.Fatalf("nix hash path: %v", err)
}
want := strings.TrimSpace(string(wantOut))
if got != want {
t.Errorf("computeNixHash from subdir = %q, want full-repo hash %q", got, want)
}
// Sanity check: the subtree-only hash must DIFFER from the full-repo hash
// (otherwise this test can't detect the regression).
subTmp := t.TempDir()
subArchive := exec.Command("git", "archive", "--format=tar.gz", "--prefix=source/", rev)
subArchive.Dir = filepath.Join(repo, "server")
subExtract := exec.Command("tar", "xz", "-C", subTmp)
subExtract.Stdin, err = subArchive.StdoutPipe()
if err != nil {
t.Fatal(err)
}
if err := subExtract.Start(); err != nil {
t.Fatal(err)
}
if err := subArchive.Run(); err != nil {
t.Fatal(err)
}
if err := subExtract.Wait(); err != nil {
t.Fatal(err)
}
subHashOut, err := exec.Command("nix", "hash", "path", filepath.Join(subTmp, "source")).Output()
if err != nil {
t.Fatalf("nix hash path (subtree): %v", err)
}
subHash := strings.TrimSpace(string(subHashOut))
if subHash == want {
t.Fatal("test setup is broken: subtree hash equals full-repo hash, cannot detect regression")
}
}