-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration_test.go
More file actions
687 lines (564 loc) · 17.8 KB
/
integration_test.go
File metadata and controls
687 lines (564 loc) · 17.8 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package preflight_test
import (
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"slices"
"strings"
"testing"
"time"
"github.com/vertti/preflight/pkg/check"
"github.com/vertti/preflight/pkg/cmdcheck"
"github.com/vertti/preflight/pkg/envcheck"
"github.com/vertti/preflight/pkg/filecheck"
"github.com/vertti/preflight/pkg/gitcheck"
"github.com/vertti/preflight/pkg/hashcheck"
"github.com/vertti/preflight/pkg/httpcheck"
"github.com/vertti/preflight/pkg/jsoncheck"
"github.com/vertti/preflight/pkg/resourcecheck"
"github.com/vertti/preflight/pkg/syscheck"
"github.com/vertti/preflight/pkg/tcpcheck"
"github.com/vertti/preflight/pkg/usercheck"
)
// Integration tests verify Real* implementations work with actual system resources.
// Unit tests in each package cover edge cases; these tests verify end-to-end integration.
func TestIntegration_Cmd(t *testing.T) {
c := cmdcheck.Check{
Name: "bash", // bash --version is universally available
Runner: &cmdcheck.RealCmdRunner{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_Env(t *testing.T) {
t.Setenv("PREFLIGHT_TEST_VAR", "test-value")
c := envcheck.Check{
Name: "PREFLIGHT_TEST_VAR",
Getter: &envcheck.RealEnvGetter{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_File(t *testing.T) {
tmpFile, err := os.CreateTemp("", "preflight-integration-*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func() { _ = os.Remove(tmpFile.Name()) }()
_, err = tmpFile.WriteString("test content")
if err != nil {
t.Fatalf("failed to write to temp file: %v", err)
}
_ = tmpFile.Close()
c := filecheck.Check{
Path: tmpFile.Name(),
NotEmpty: true,
Owner: -1, // Don't check owner
FS: &filecheck.RealFileSystem{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_FileOwner(t *testing.T) {
tmpFile, err := os.CreateTemp("", "preflight-owner-*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func() { _ = os.Remove(tmpFile.Name()) }()
_ = tmpFile.Close()
// Get current user's UID
uid := os.Getuid()
// Test that owner matches current user
c := filecheck.Check{
Path: tmpFile.Name(),
Owner: uid,
FS: &filecheck.RealFileSystem{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Test owner mismatch (use impossible UID)
c = filecheck.Check{
Path: tmpFile.Name(),
Owner: 99999,
FS: &filecheck.RealFileSystem{},
}
result = c.Run()
if result.Status != check.StatusFail {
t.Errorf("Status = %v, want Fail (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_FileSocket(t *testing.T) {
// Create a temporary Unix socket
tmpDir, err := os.MkdirTemp("", "preflight-socket-test")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer func() { _ = os.RemoveAll(tmpDir) }()
socketPath := tmpDir + "/test.sock"
listener, err := net.Listen("unix", socketPath)
if err != nil {
t.Fatalf("failed to create unix socket: %v", err)
}
defer func() { _ = listener.Close() }()
// Test that --socket flag works
c := filecheck.Check{
Path: socketPath,
ExpectSocket: true,
Owner: -1, // Don't check owner
FS: &filecheck.RealFileSystem{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Verify we see the socket type in details
foundSocketType := slices.Contains(result.Details, "type: socket")
if !foundSocketType {
t.Errorf("Details = %v, want to contain 'type: socket'", result.Details)
}
}
func TestIntegration_FileSocketFail(t *testing.T) {
// Create a regular file, not a socket
tmpFile, err := os.CreateTemp("", "preflight-not-socket-*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func() { _ = os.Remove(tmpFile.Name()) }()
_ = tmpFile.Close()
// Test that --socket fails on a regular file
c := filecheck.Check{
Path: tmpFile.Name(),
ExpectSocket: true,
Owner: -1, // Don't check owner
FS: &filecheck.RealFileSystem{},
}
result := c.Run()
if result.Status != check.StatusFail {
t.Errorf("Status = %v, want Fail (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_FileSymlink(t *testing.T) {
// Create a temp file and a symlink to it
tmpFile, err := os.CreateTemp("", "preflight-symlink-target-*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func() { _ = os.Remove(tmpFile.Name()) }()
_ = tmpFile.Close()
symlinkPath := tmpFile.Name() + ".link"
if err := os.Symlink(tmpFile.Name(), symlinkPath); err != nil {
t.Fatalf("failed to create symlink: %v", err)
}
defer func() { _ = os.Remove(symlinkPath) }()
// Test that --symlink flag works
c := filecheck.Check{
Path: symlinkPath,
ExpectSymlink: true,
Owner: -1,
FS: &filecheck.RealFileSystem{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Verify we see the symlink type in details
foundSymlinkType := slices.Contains(result.Details, "type: symlink")
if !foundSymlinkType {
t.Errorf("Details = %v, want to contain 'type: symlink'", result.Details)
}
// Test --symlink-target
c = filecheck.Check{
Path: symlinkPath,
ExpectSymlink: true,
SymlinkTarget: tmpFile.Name(),
Owner: -1,
FS: &filecheck.RealFileSystem{},
}
result = c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Test wrong target fails
c = filecheck.Check{
Path: symlinkPath,
ExpectSymlink: true,
SymlinkTarget: "/nonexistent",
Owner: -1,
FS: &filecheck.RealFileSystem{},
}
result = c.Run()
if result.Status != check.StatusFail {
t.Errorf("Status = %v, want Fail (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_JSON(t *testing.T) {
tmpFile, err := os.CreateTemp("", "preflight-integration-*.json")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func() { _ = os.Remove(tmpFile.Name()) }()
_, err = tmpFile.WriteString(`{"name": "test", "version": "1.2.3", "db": {"host": "localhost"}}`)
if err != nil {
t.Fatalf("failed to write to temp file: %v", err)
}
_ = tmpFile.Close()
// Test basic validation
c := jsoncheck.Check{
File: tmpFile.Name(),
FS: &jsoncheck.RealFileSystem{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Test --has-key with nested path
c = jsoncheck.Check{
File: tmpFile.Name(),
HasKey: "db.host",
FS: &jsoncheck.RealFileSystem{},
}
result = c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Test --key with --exact
c = jsoncheck.Check{
File: tmpFile.Name(),
Key: "version",
Exact: "1.2.3",
FS: &jsoncheck.RealFileSystem{},
}
result = c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Test --key with --match
c = jsoncheck.Check{
File: tmpFile.Name(),
Key: "version",
Match: `^1\.`,
FS: &jsoncheck.RealFileSystem{},
}
result = c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_JSONInvalid(t *testing.T) {
tmpFile, err := os.CreateTemp("", "preflight-invalid-*.json")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func() { _ = os.Remove(tmpFile.Name()) }()
_, err = tmpFile.WriteString(`{invalid json}`)
if err != nil {
t.Fatalf("failed to write to temp file: %v", err)
}
_ = tmpFile.Close()
c := jsoncheck.Check{
File: tmpFile.Name(),
FS: &jsoncheck.RealFileSystem{},
}
result := c.Run()
if result.Status != check.StatusFail {
t.Errorf("Status = %v, want Fail (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_Hash(t *testing.T) {
tmpFile, err := os.CreateTemp("", "preflight-integration-*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func() { _ = os.Remove(tmpFile.Name()) }()
_, err = tmpFile.WriteString("test content")
if err != nil {
t.Fatalf("failed to write to temp file: %v", err)
}
_ = tmpFile.Close()
// SHA256 of "test content"
expectedHash := "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72"
c := hashcheck.Check{
File: tmpFile.Name(),
ExpectedHash: expectedHash,
Algorithm: hashcheck.AlgorithmSHA256,
// Opener is nil - uses RealFileOpener
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_HTTP(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
c := httpcheck.Check{
URL: server.URL,
Client: &httpcheck.RealHTTPClient{Timeout: 5 * time.Second},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_TCP(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("failed to create listener: %v", err)
}
defer func() { _ = listener.Close() }()
addr := listener.Addr().String()
c := tcpcheck.Check{
Address: addr,
Timeout: 5 * time.Second,
Dialer: &tcpcheck.RealTCPDialer{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_User(t *testing.T) {
username := os.Getenv("USER")
if username == "" {
t.Skip("USER environment variable not set")
}
c := usercheck.Check{
Username: username,
Lookup: &usercheck.RealUserLookup{},
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_Sys(t *testing.T) {
// Get the actual OS from RealSysInfo and verify it matches
info := &syscheck.RealSysInfo{}
c := syscheck.Check{
ExpectedOS: info.OS(), // Use actual OS so test always passes
Info: info,
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_Git(t *testing.T) {
// This test runs in the preflight repo itself, which is a git repository
runner := &gitcheck.RealGitRunner{}
// Verify we're in a git repo
isRepo, err := runner.IsGitRepo()
if err != nil {
t.Fatalf("IsGitRepo() error = %v", err)
}
if !isRepo {
t.Skip("not running in a git repository")
}
// Test that CurrentBranch works
branch, err := runner.CurrentBranch()
if err != nil {
t.Errorf("CurrentBranch() error = %v", err)
}
if branch == "" {
t.Error("CurrentBranch() returned empty string")
}
// Test that Status works (output varies, just verify no error)
_, err = runner.Status()
if err != nil {
t.Errorf("Status() error = %v", err)
}
// Test that TagsAtHead works (may return empty, just verify no error)
_, err = runner.TagsAtHead()
if err != nil {
t.Errorf("TagsAtHead() error = %v", err)
}
// Test full check with branch verification
c := gitcheck.Check{
Branch: branch, // Use actual branch so test passes
Runner: runner,
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_Resource(t *testing.T) {
checker := &resourcecheck.RealResourceChecker{}
// Test disk space (should have at least 1MB free)
c := resourcecheck.Check{
MinDisk: 1 * 1024 * 1024, // 1MB
Checker: checker,
}
result := c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Test memory (should have at least 100MB)
c = resourcecheck.Check{
MinMemory: 100 * 1024 * 1024, // 100MB
Checker: checker,
}
result = c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
// Test CPUs (should have at least 1)
c = resourcecheck.Check{
MinCPUs: 1,
Checker: checker,
}
result = c.Run()
if result.Status != check.StatusOK {
t.Errorf("Status = %v, want OK (details: %v)", result.Status, result.Details)
}
}
func TestIntegration_Examples(t *testing.T) {
// Verify example files exist and contain expected preflight commands
// This catches CLI drift where docs/examples get out of sync with the tool
examples := []struct {
path string
expectedContains []string
}{
{
path: "examples/multistage-dockerfile/Dockerfile",
expectedContains: []string{
"preflight cmd",
"preflight file",
"ghcr.io/vertti/preflight",
},
},
{
path: "examples/runtime-checks-with-entrypoint/Dockerfile",
expectedContains: []string{
"ghcr.io/vertti/preflight",
"entrypoint.sh",
},
},
{
path: "examples/runtime-checks-with-entrypoint/entrypoint.sh",
expectedContains: []string{
"#!/bin/sh",
"set -e",
"preflight tcp",
"preflight env",
"preflight file",
"exec \"$@\"",
},
},
}
for _, ex := range examples {
t.Run(ex.path, func(t *testing.T) {
content, err := os.ReadFile(ex.path)
if err != nil {
t.Fatalf("failed to read %s: %v", ex.path, err)
}
for _, expected := range ex.expectedContains {
if !strings.Contains(string(content), expected) {
t.Errorf("%s: expected to contain %q", ex.path, expected)
}
}
})
}
}
func TestIntegration_ExamplesShellSyntax(t *testing.T) {
// Verify shell scripts have valid syntax
shellScripts := []string{
"examples/runtime-checks-with-entrypoint/entrypoint.sh",
}
for _, script := range shellScripts {
t.Run(script, func(t *testing.T) {
cmd := exec.Command("sh", "-n", script) //nolint:gosec // intentional: validating shell syntax of example scripts
if err := cmd.Run(); err != nil {
t.Errorf("shell syntax error in %s: %v", script, err)
}
})
}
}
func TestIntegration_ExecMode(t *testing.T) {
// Build preflight binary for testing
tmpDir := t.TempDir()
binaryPath := tmpDir + "/preflight"
buildCmd := exec.Command("go", "build", "-o", binaryPath, "./cmd/preflight") //nolint:gosec // intentional: building test binary
if output, err := buildCmd.CombinedOutput(); err != nil {
t.Fatalf("failed to build preflight: %v\n%s", err, output)
}
t.Run("exec after successful check", func(t *testing.T) {
// Run: preflight env PATH -- echo "success"
cmd := exec.Command(binaryPath, "env", "PATH", "--", "echo", "exec-success-marker") //nolint:gosec // intentional: testing exec mode
cmd.Env = append(os.Environ(), "NO_COLOR=1")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("command failed: %v\n%s", err, output)
}
// Should see both the check output and the exec'd command output
outputStr := string(output)
if !strings.Contains(outputStr, "[OK] env: PATH") {
t.Errorf("expected check output, got: %s", outputStr)
}
if !strings.Contains(outputStr, "exec-success-marker") {
t.Errorf("expected exec'd command output, got: %s", outputStr)
}
})
t.Run("no exec after failed check", func(t *testing.T) {
// Run: preflight env NONEXISTENT_VAR_12345 -- echo "should-not-print"
cmd := exec.Command(binaryPath, "env", "NONEXISTENT_VAR_12345", "--", "echo", "should-not-print") //nolint:gosec // intentional: testing exec mode
cmd.Env = append(os.Environ(), "NO_COLOR=1")
output, err := cmd.CombinedOutput()
// Command should fail
if err == nil {
t.Fatal("expected command to fail")
}
// Should see the failure output but NOT the exec'd command
outputStr := string(output)
if !strings.Contains(outputStr, "[FAIL]") {
t.Errorf("expected failure output, got: %s", outputStr)
}
if strings.Contains(outputStr, "should-not-print") {
t.Errorf("exec'd command should not have run, got: %s", outputStr)
}
})
t.Run("exec with arguments", func(t *testing.T) {
// Run: preflight env PATH -- echo arg1 arg2 arg3
cmd := exec.Command(binaryPath, "env", "PATH", "--", "echo", "arg1", "arg2", "arg3") //nolint:gosec // intentional: testing exec mode
cmd.Env = append(os.Environ(), "NO_COLOR=1")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("command failed: %v\n%s", err, output)
}
// Should see all arguments passed through
outputStr := string(output)
if !strings.Contains(outputStr, "arg1 arg2 arg3") {
t.Errorf("expected arguments to be passed through, got: %s", outputStr)
}
})
t.Run("exec command not found", func(t *testing.T) {
// Run: preflight env PATH -- nonexistent-command-xyz-12345
cmd := exec.Command(binaryPath, "env", "PATH", "--", "nonexistent-command-xyz-12345") //nolint:gosec // intentional: testing exec mode
cmd.Env = append(os.Environ(), "NO_COLOR=1")
output, err := cmd.CombinedOutput()
// Command should fail because exec'd command doesn't exist
if err == nil {
t.Fatal("expected command to fail")
}
// Should see the check pass but exec fail
outputStr := string(output)
if !strings.Contains(outputStr, "[OK] env: PATH") {
t.Errorf("expected check to pass, got: %s", outputStr)
}
if !strings.Contains(outputStr, "exec:") {
t.Errorf("expected exec error message, got: %s", outputStr)
}
})
}