-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
agent_customization_test.go
232 lines (201 loc) · 6.42 KB
/
agent_customization_test.go
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
//go:build validation
package rke2
import (
"testing"
"github.com/rancher/rancher/tests/v2/validation/provisioning/permutations"
"github.com/rancher/shepherd/clients/rancher"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/clusters"
"github.com/rancher/shepherd/extensions/machinepools"
"github.com/rancher/shepherd/extensions/provisioning"
"github.com/rancher/shepherd/extensions/provisioninginput"
"github.com/rancher/shepherd/extensions/users"
password "github.com/rancher/shepherd/extensions/users/passwordgenerator"
"github.com/rancher/shepherd/pkg/config"
namegen "github.com/rancher/shepherd/pkg/namegenerator"
"github.com/rancher/shepherd/pkg/session"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type RKE2AgentCustomizationTestSuite struct {
suite.Suite
client *rancher.Client
session *session.Session
standardUserClient *rancher.Client
provisioningConfig *provisioninginput.Config
}
func (r *RKE2AgentCustomizationTestSuite) TearDownSuite() {
r.session.Cleanup()
}
func (r *RKE2AgentCustomizationTestSuite) SetupSuite() {
testSession := session.NewSession()
r.session = testSession
r.provisioningConfig = new(provisioninginput.Config)
config.LoadConfig(provisioninginput.ConfigurationFileKey, r.provisioningConfig)
client, err := rancher.NewClient("", testSession)
require.NoError(r.T(), err)
r.client = client
enabled := true
var testuser = namegen.AppendRandomString("testuser-")
var testpassword = password.GenerateUserPassword("testpass-")
user := &management.User{
Username: testuser,
Password: testpassword,
Name: testuser,
Enabled: &enabled,
}
newUser, err := users.CreateUserWithRole(client, user, "user")
require.NoError(r.T(), err)
newUser.Password = user.Password
standardUserClient, err := client.AsUser(newUser)
require.NoError(r.T(), err)
r.standardUserClient = standardUserClient
}
func (r *RKE2AgentCustomizationTestSuite) TestProvisioningRKE2ClusterAgentCustomization() {
productionPool := []provisioninginput.MachinePools{
{
NodeRoles: machinepools.NodeRoles{
ControlPlane: true,
Quantity: 2,
},
},
{
NodeRoles: machinepools.NodeRoles{
Etcd: true,
Quantity: 3,
},
},
{
NodeRoles: machinepools.NodeRoles{
Worker: true,
Quantity: 2,
},
},
}
agentCustomization := management.AgentDeploymentCustomization{
AppendTolerations: []management.Toleration{
{
Key: "TestKeyToleration",
Value: "TestValueToleration",
},
},
OverrideResourceRequirements: &management.ResourceRequirements{
Limits: map[string]string{
"cpu": "750m",
"mem": "500Mi",
},
Requests: map[string]string{
"cpu": "250m",
},
},
OverrideAffinity: &management.Affinity{
NodeAffinity: &management.NodeAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []management.PreferredSchedulingTerm{
{
Preference: &management.NodeSelectorTerm{
MatchExpressions: []management.NodeSelectorRequirement{
{
Key: "testAffinityKey",
Operator: "In",
Values: []string{"true"},
},
},
},
Weight: 100,
},
},
},
},
}
customAgents := []string{"fleet-agent", "cluster-agent"}
tests := []struct {
name string
machinePools []provisioninginput.MachinePools
client *rancher.Client
agent string
}{
{"Custom Fleet Agent - Standard User", productionPool, r.standardUserClient, customAgents[0]},
{"Custom Cluster Agent - Standard User", productionPool, r.standardUserClient, customAgents[1]},
}
for _, tt := range tests {
subSession := r.session.NewSession()
defer subSession.Cleanup()
client, err := tt.client.WithSession(subSession)
require.NoError(r.T(), err)
r.provisioningConfig.MachinePools = tt.machinePools
if tt.agent == "fleet-agent" {
r.provisioningConfig.FleetAgent = &agentCustomization
r.provisioningConfig.ClusterAgent = nil
}
if tt.agent == "cluster-agent" {
r.provisioningConfig.ClusterAgent = &agentCustomization
r.provisioningConfig.FleetAgent = nil
}
permutations.RunTestPermutations(&r.Suite, tt.name, client, r.provisioningConfig, permutations.RKE2ProvisionCluster, nil, nil)
}
}
func (r *RKE2AgentCustomizationTestSuite) TestFailureProvisioningRKE2ClusterAgentCustomization() {
productionPool := []provisioninginput.MachinePools{
{
NodeRoles: machinepools.NodeRoles{
ControlPlane: true,
Quantity: 2,
},
},
{
NodeRoles: machinepools.NodeRoles{
Etcd: true,
Quantity: 3,
},
},
{
NodeRoles: machinepools.NodeRoles{
Worker: true,
Quantity: 2,
},
},
}
agentCustomization := management.AgentDeploymentCustomization{
AppendTolerations: []management.Toleration{
{
Key: "BadLabel",
Value: "123\"[];'{}-+=",
},
},
OverrideAffinity: &management.Affinity{},
OverrideResourceRequirements: &management.ResourceRequirements{},
}
customAgents := []string{"fleet-agent", "cluster-agent"}
tests := []struct {
name string
machinePools []provisioninginput.MachinePools
client *rancher.Client
agent string
}{
{"Invalid Custom Fleet Agent - Standard User", productionPool, r.standardUserClient, customAgents[0]},
{"Invalid Custom Cluster Agent - Standard User", productionPool, r.standardUserClient, customAgents[1]},
}
for _, tt := range tests {
subSession := r.session.NewSession()
defer subSession.Cleanup()
client, err := tt.client.WithSession(subSession)
require.NoError(r.T(), err)
r.provisioningConfig.MachinePools = tt.machinePools
if tt.agent == "fleet-agent" {
r.provisioningConfig.FleetAgent = &agentCustomization
r.provisioningConfig.ClusterAgent = nil
}
if tt.agent == "cluster-agent" {
r.provisioningConfig.ClusterAgent = &agentCustomization
r.provisioningConfig.FleetAgent = nil
}
rke2Provider, _, _, kubeVersions := permutations.GetClusterProvider(permutations.RKE2ProvisionCluster, r.provisioningConfig.Providers[0], r.provisioningConfig)
testClusterConfig := clusters.ConvertConfigToClusterConfig(r.provisioningConfig)
testClusterConfig.KubernetesVersion = kubeVersions[0]
_, err = provisioning.CreateProvisioningCluster(client, *rke2Provider, testClusterConfig, nil)
require.Error(r.T(), err)
}
}
func TestRKE2AgentCustomizationTestSuite(t *testing.T) {
suite.Run(t, new(RKE2AgentCustomizationTestSuite))
}