-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathnamespace_apply_test.go
More file actions
184 lines (161 loc) · 3.99 KB
/
namespace_apply_test.go
File metadata and controls
184 lines (161 loc) · 3.99 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
// Copyright IBM Corp. 2015, 2025
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"strings"
"testing"
"github.com/hashicorp/cli"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/shoenig/test/must"
)
func TestNamespaceApplyCommand_Implements(t *testing.T) {
ci.Parallel(t)
var _ cli.Command = &NamespaceApplyCommand{}
}
func TestNamespaceApplyCommand_Fails(t *testing.T) {
ci.Parallel(t)
ui := cli.NewMockUi()
cmd := &NamespaceApplyCommand{Meta: Meta{Ui: ui}}
// Fails on misuse
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
t.Fatalf("expected help output, got: %s", out)
}
ui.ErrorWriter.Reset()
if code := cmd.Run([]string{"-address=nope"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
t.Fatalf("name required error, got: %s", out)
}
ui.ErrorWriter.Reset()
}
func TestNamespaceApplyCommand_Good(t *testing.T) {
ci.Parallel(t)
// Create a server
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := cli.NewMockUi()
cmd := &NamespaceApplyCommand{Meta: Meta{Ui: ui}}
// Create a namespace
name, desc := "foo", "bar"
if code := cmd.Run([]string{"-address=" + url, "-description=" + desc, name}); code != 0 {
t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
}
namespaces, _, err := client.Namespaces().List(nil)
must.NoError(t, err)
must.SliceLen(t, 2, namespaces)
}
func TestNamespaceApplyCommand_parseNamesapceSpec(t *testing.T) {
ci.Parallel(t)
testCases := []struct {
name string
input string
expected *api.Namespace
}{
{
name: "valid namespace",
input: `
name = "test-namespace"
description = "Test namespace"
quota = "test"
capabilities {
enabled_task_drivers = ["exec", "docker"]
disabled_task_drivers = ["raw_exec"]
}
node_pool_config {
default = "dev"
allowed = ["prod*"]
}
vault {
default = "infra"
allowed = ["apps", "infra"]
}
consul {
default = "prod"
allowed = ["prod", "apps*"]
}
meta {
dept = "eng"
}
required_extra_claims {
foo = "${job.namespace}"
}
optional_extra_claims {
bar = "class:${node.class}"
baz = "dc:${node.datacenter}"
}
`,
expected: &api.Namespace{
Name: "test-namespace",
Description: "Test namespace",
Quota: "test",
Capabilities: &api.NamespaceCapabilities{
EnabledTaskDrivers: []string{"exec", "docker"},
DisabledTaskDrivers: []string{"raw_exec"},
},
NodePoolConfiguration: &api.NamespaceNodePoolConfiguration{
Default: "dev",
Allowed: []string{"prod*"},
},
VaultConfiguration: &api.NamespaceVaultConfiguration{
Default: "infra",
Allowed: []string{"apps", "infra"},
},
ConsulConfiguration: &api.NamespaceConsulConfiguration{
Default: "prod",
Allowed: []string{"prod", "apps*"},
},
Meta: map[string]string{
"dept": "eng",
},
RequiredExtraClaims: map[string]string{
"foo": "${job.namespace}",
},
OptionalExtraClaims: map[string]string{
"bar": "class:${node.class}",
"baz": "dc:${node.datacenter}",
},
},
},
{
name: "minimal",
input: `name = "test-small"`,
expected: &api.Namespace{
Name: "test-small",
},
},
{
name: "empty",
input: "",
expected: &api.Namespace{},
},
{
name: "lists in node pool config are nil if not provided",
input: `
name = "nil-lists"
node_pool_config {
default = "default"
}
`,
expected: &api.Namespace{
Name: "nil-lists",
NodePoolConfiguration: &api.NamespaceNodePoolConfiguration{
Default: "default",
Allowed: nil,
Denied: nil,
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := parseNamespaceSpec([]byte(tc.input))
must.NoError(t, err)
must.Eq(t, tc.expected, got)
})
}
}