-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstackpack_validate_test.go
More file actions
214 lines (166 loc) · 7.2 KB
/
stackpack_validate_test.go
File metadata and controls
214 lines (166 loc) · 7.2 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
package stackpack
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/internal/config"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// setupValidateCmd creates a test command with API context
func setupValidateCmd(t *testing.T) (*di.MockDeps, *cobra.Command) {
cli := di.NewMockDeps(t)
cfg := &config.Config{
CurrentContext: "test-context",
Contexts: []*config.NamedContext{
{
Name: "test-context",
Context: &config.StsContext{
URL: "https://test-server.example.com",
APIPath: "/api",
},
},
},
}
cli.ConfigPath = filepath.Join(t.TempDir(), "config.yaml")
err := config.WriteConfig(cli.ConfigPath, cfg)
require.NoError(t, err)
cmd := StackpackValidateCommand(&cli.Deps)
return &cli, cmd
}
// createTestStackpackDir creates a minimal stackpack directory with required items
func createTestStackpackDir(t *testing.T, dir string, name string, version string) {
require.NoError(t, os.MkdirAll(filepath.Join(dir, "settings"), 0755))
require.NoError(t, os.MkdirAll(filepath.Join(dir, "resources"), 0755))
stackpackConf := fmt.Sprintf(`name: "%s"
version: "%s"
`, name, version)
require.NoError(t, os.WriteFile(filepath.Join(dir, "stackpack.yaml"), []byte(stackpackConf), 0644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "README.md"), []byte("# Test Stackpack"), 0644))
}
// ===== Tests =====
func TestValidate_WithDirectory_AutoPackages(t *testing.T) {
cli, cmd := setupValidateCmd(t)
tempDir, err := os.MkdirTemp("", "stackpack-validate-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
stackpackDir := filepath.Join(tempDir, "test-stackpack")
require.NoError(t, os.MkdirAll(stackpackDir, 0755))
createTestStackpackDir(t, stackpackDir, "test-stackpack", "1.0.0")
_, err = di.ExecuteCommandWithContext(&cli.Deps, cmd, "--stackpack-directory", stackpackDir)
require.NoError(t, err)
// Verify success message
require.NotEmpty(t, *cli.MockPrinter.SuccessCalls)
successCall := (*cli.MockPrinter.SuccessCalls)[0]
assert.Contains(t, successCall, "validation successful")
}
func TestValidate_WithDirectory_InvalidStackpack(t *testing.T) {
cli, cmd := setupValidateCmd(t)
tempDir, err := os.MkdirTemp("", "stackpack-validate-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// Create directory with missing required items
stackpackDir := filepath.Join(tempDir, "invalid-stackpack")
require.NoError(t, os.MkdirAll(stackpackDir, 0755))
_, err = di.ExecuteCommandWithContext(&cli.Deps, cmd, "--stackpack-directory", stackpackDir)
require.Error(t, err)
assert.Contains(t, err.Error(), "required stackpack item not found")
}
func TestValidate_WithDirectory_MissingStackpackYaml(t *testing.T) {
cli, cmd := setupValidateCmd(t)
tempDir, err := os.MkdirTemp("", "stackpack-validate-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
stackpackDir := filepath.Join(tempDir, "test-stackpack")
require.NoError(t, os.MkdirAll(filepath.Join(stackpackDir, "settings"), 0755))
require.NoError(t, os.MkdirAll(filepath.Join(stackpackDir, "resources"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(stackpackDir, "README.md"), []byte("test"), 0644))
_, err = di.ExecuteCommandWithContext(&cli.Deps, cmd, "--stackpack-directory", stackpackDir)
require.Error(t, err)
assert.Contains(t, err.Error(), "required stackpack item not found")
}
func TestValidate_WithPrePackagedFile(t *testing.T) {
cli, cmd := setupValidateCmd(t)
tempDir, err := os.MkdirTemp("", "stackpack-validate-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// Create a pre-packaged .sts file
stackpackFile := filepath.Join(tempDir, "test.sts")
require.NoError(t, os.WriteFile(stackpackFile, []byte("test stackpack content"), 0644))
_, err = di.ExecuteCommandWithContext(&cli.Deps, cmd, "--stackpack-file", stackpackFile)
require.NoError(t, err)
// Verify success message
require.NotEmpty(t, *cli.MockPrinter.SuccessCalls)
successCall := (*cli.MockPrinter.SuccessCalls)[0]
assert.Contains(t, successCall, "validation successful")
}
func TestValidate_JSONOutput(t *testing.T) {
cli, cmd := setupValidateCmd(t)
tempDir, err := os.MkdirTemp("", "stackpack-validate-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
stackpackFile := filepath.Join(tempDir, "test.sts")
require.NoError(t, os.WriteFile(stackpackFile, []byte("test content"), 0644))
_, err = di.ExecuteCommandWithContext(&cli.Deps, cmd, "--stackpack-file", stackpackFile, "-o", "json")
require.NoError(t, err)
// Verify JSON was called
require.Len(t, *cli.MockPrinter.PrintJsonCalls, 1)
jsonOutput := (*cli.MockPrinter.PrintJsonCalls)[0]
assert.Equal(t, true, jsonOutput["success"])
}
func TestValidate_MissingPath(t *testing.T) {
cli, cmd := setupValidateCmd(t)
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd)
require.Error(t, err)
assert.Contains(t, err.Error(), "exactly one of")
}
func TestValidate_MutuallyExclusive(t *testing.T) {
cli, cmd := setupValidateCmd(t)
tempDir, err := os.MkdirTemp("", "stackpack-validate-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
stackpackDir := filepath.Join(tempDir, "stackpack")
require.NoError(t, os.MkdirAll(stackpackDir, 0755))
stackpackFile := filepath.Join(tempDir, "test.sts")
require.NoError(t, os.WriteFile(stackpackFile, []byte("test"), 0644))
_, err = di.ExecuteCommandWithContext(&cli.Deps, cmd,
"-d", stackpackDir,
"-f", stackpackFile)
require.Error(t, err)
assert.Contains(t, err.Error(), "exactly one of")
}
func TestValidate_NonexistentFile(t *testing.T) {
cli, cmd := setupValidateCmd(t)
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--stackpack-file", "/nonexistent/path/file.sts")
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to access stackpack file")
}
func TestValidate_WithDirectory_IncludingOptionalItems(t *testing.T) {
cli, cmd := setupValidateCmd(t)
tempDir, err := os.MkdirTemp("", "stackpack-validate-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
stackpackDir := filepath.Join(tempDir, "test-stackpack")
require.NoError(t, os.MkdirAll(stackpackDir, 0755))
createTestStackpackDir(t, stackpackDir, "test-stackpack", "1.0.0")
// Add optional items
require.NoError(t, os.MkdirAll(filepath.Join(stackpackDir, "icons"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(stackpackDir, "icons", "icon.png"), []byte("fake png"), 0644))
require.NoError(t, os.MkdirAll(filepath.Join(stackpackDir, "includes"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(stackpackDir, "includes", "include.txt"), []byte("include data"), 0644))
_, err = di.ExecuteCommandWithContext(&cli.Deps, cmd, "--stackpack-directory", stackpackDir)
require.NoError(t, err)
// Verify success message
require.NotEmpty(t, *cli.MockPrinter.SuccessCalls)
successCall := (*cli.MockPrinter.SuccessCalls)[0]
assert.Contains(t, successCall, "validation successful")
}
func TestValidate_NonexistentDirectory(t *testing.T) {
cli, cmd := setupValidateCmd(t)
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--stackpack-directory", "/nonexistent/stackpack/dir")
require.Error(t, err)
assert.Contains(t, err.Error(), "required stackpack item not found")
}