-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcompose_cli_test.go
More file actions
130 lines (121 loc) · 4.05 KB
/
Copy pathcompose_cli_test.go
File metadata and controls
130 lines (121 loc) · 4.05 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
package agentbox
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
// Per-workstream test file (NEW; not appended to a shared *_test.go)
// per the Phase-1 lesson. Focused on the dispatcher + envelope shape
// — the underlying compose ops (discoverStacks, systemctlUser, etc.)
// touch real systemd/podman and aren't worth mocking; the existing
// MCP-handler tests in compose_test.go cover the inner-logic edges.
func TestRunComposeCLI_NoArgs(t *testing.T) {
var out, errBuf bytes.Buffer
code := RunComposeCLI(nil, &out, &errBuf)
if code != 2 {
t.Errorf("exit code = %d, want 2 (usage error)", code)
}
if !strings.Contains(errBuf.String(), "usage:") {
t.Errorf("stderr missing usage line: %q", errBuf.String())
}
if out.Len() != 0 {
t.Errorf("stdout should be empty on usage error, got: %q", out.String())
}
}
func TestRunComposeCLI_UnknownVerb(t *testing.T) {
var out, errBuf bytes.Buffer
code := RunComposeCLI([]string{"nonsense"}, &out, &errBuf)
if code != 2 {
t.Errorf("exit code = %d, want 2", code)
}
if !strings.Contains(errBuf.String(), "unknown compose verb") {
t.Errorf("stderr missing 'unknown' diagnosis: %q", errBuf.String())
}
}
func TestRunComposeCLI_EnableRequiresDir(t *testing.T) {
var out, errBuf bytes.Buffer
code := RunComposeCLI([]string{"enable"}, &out, &errBuf)
if code != 1 {
t.Errorf("exit code = %d, want 1 (operation failure)", code)
}
// stdout must be valid JSON with ok:false; that's the daemon-parse
// contract — even on failure, daemon expects JSON it can introspect.
var got map[string]any
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("stdout not valid JSON: %v\nbody: %q", err, out.String())
}
if got["ok"] != false {
t.Errorf("ok field = %v, want false", got["ok"])
}
if msg, _ := got["error"].(string); !strings.Contains(msg, "--dir is required") {
t.Errorf("error message missing 'dir' diagnosis: %q", msg)
}
}
func TestRunComposeCLI_DisableRequiresDir(t *testing.T) {
var out, errBuf bytes.Buffer
code := RunComposeCLI([]string{"disable"}, &out, &errBuf)
if code != 1 {
t.Errorf("exit code = %d, want 1", code)
}
var got map[string]any
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("stdout not valid JSON: %v", err)
}
if got["ok"] != false {
t.Errorf("ok = %v, want false", got["ok"])
}
}
func TestRunComposeCLI_StatusRequiresDir(t *testing.T) {
var out, errBuf bytes.Buffer
code := RunComposeCLI([]string{"status"}, &out, &errBuf)
if code != 1 {
t.Errorf("exit code = %d, want 1", code)
}
var got map[string]any
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("stdout not valid JSON: %v", err)
}
}
func TestRunComposeCLI_StatusMissingComposeFile(t *testing.T) {
tmp := t.TempDir() // no compose file under here
var out, errBuf bytes.Buffer
code := RunComposeCLI([]string{"status", "--dir", tmp}, &out, &errBuf)
if code != 1 {
t.Errorf("exit code = %d, want 1 (no compose file)", code)
}
var got map[string]any
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("stdout not valid JSON: %v", err)
}
if got["ok"] != false {
t.Errorf("ok = %v, want false", got["ok"])
}
if msg, _ := got["error"].(string); !strings.Contains(msg, "no compose file") {
t.Errorf("error missing 'no compose file': %q", msg)
}
}
func TestRunComposeCLI_DiscoverEmptyRoot(t *testing.T) {
// Empty tmp dir → discover returns empty stack list, ok=true.
tmp := t.TempDir()
var out, errBuf bytes.Buffer
code := RunComposeCLI([]string{"discover", "--root", tmp}, &out, &errBuf)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr: %q", code, errBuf.String())
}
var got map[string]any
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("stdout not valid JSON: %v", err)
}
if got["ok"] != true {
t.Errorf("ok = %v, want true", got["ok"])
}
// result.stacks must be empty or absent
result, _ := got["result"].(map[string]any)
if result == nil {
t.Fatal("result missing")
}
if stacks, ok := result["stacks"].([]any); ok && len(stacks) != 0 {
t.Errorf("expected empty stacks, got %d", len(stacks))
}
}