-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
80 lines (69 loc) · 2.65 KB
/
Copy patherrors_test.go
File metadata and controls
80 lines (69 loc) · 2.65 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
package xshellz
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
)
func TestAPIErrorFormatting(t *testing.T) {
withCode := &APIError{StatusCode: 403, Code: "verification_required", Message: "Verify your account."}
if got, want := withCode.Error(), "xshellz: API error 403 (verification_required): Verify your account."; got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
noCode := &APIError{StatusCode: 401, Message: "Unauthenticated."}
if got, want := noCode.Error(), "xshellz: API error 401: Unauthenticated."; got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
// Message falls back to the raw body (non-JSON gateway errors).
bodyOnly := &APIError{StatusCode: 502, Body: "<html>bad gateway</html>"}
if got, want := bodyOnly.Error(), "xshellz: API error 502: <html>bad gateway</html>"; got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
}
func TestBoxfileHelpersRequireAPIKey(t *testing.T) {
clearEnv(t)
if _, err := GetBoxfile(context.Background(), nil); !errors.Is(err, ErrNoAPIKey) {
t.Errorf("GetBoxfile err = %v, want ErrNoAPIKey", err)
}
if _, err := SetBoxfile(context.Background(), "apt: git", nil); !errors.Is(err, ErrNoAPIKey) {
t.Errorf("SetBoxfile err = %v, want ErrNoAPIKey", err)
}
if _, err := GetOrCreate(context.Background(), "box", nil); !errors.Is(err, ErrNoAPIKey) {
t.Errorf("GetOrCreate err = %v, want ErrNoAPIKey", err)
}
}
func TestConnectRejectsGarbageKey(t *testing.T) {
clearEnv(t)
_, err := Connect(context.Background(), "uuid-1", []byte("not a key"), &ConnectOptions{APIKey: "pat", APIURL: "http://127.0.0.1:1"})
if err == nil {
t.Fatal("expected a key parse error")
}
}
func TestUploadMissingLocalFile(t *testing.T) {
sbx := runningSandbox(&fakeRunner{})
err := sbx.Upload(context.Background(), filepath.Join(t.TempDir(), "nope.txt"), "/tmp/x")
if err == nil {
t.Fatal("expected an error for a missing local file")
}
}
func TestDownloadPropagatesRemoteReadFailure(t *testing.T) {
fake := &fakeRunner{exitCode: 1, stderr: []byte("cat: no such file\n")}
sbx := runningSandbox(fake)
if err := sbx.Download(context.Background(), "/tmp/nope", filepath.Join(t.TempDir(), "out")); err == nil {
t.Fatal("expected the remote read failure to propagate")
}
}
func TestSaveKeystoreKeyFailsOnUnwritableDir(t *testing.T) {
if os.Getuid() == 0 {
t.Skip("root ignores directory permissions")
}
parent := t.TempDir()
locked := filepath.Join(parent, "locked")
if err := os.MkdirAll(locked, 0o500); err != nil {
t.Fatal(err)
}
if err := saveKeystoreKey(filepath.Join(locked, "keys"), "box", []byte("pem")); err == nil {
t.Fatal("expected a mkdir error under an unwritable parent")
}
}