-
Notifications
You must be signed in to change notification settings - Fork 157
/
cluster_validation_test.go
167 lines (153 loc) · 4.68 KB
/
cluster_validation_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
package v1alpha1_test
import (
"testing"
"github.com/scylladb/scylla-operator/pkg/api/v1alpha1"
"github.com/scylladb/scylla-operator/pkg/test/unit"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/pointer"
)
func TestCheckValues(t *testing.T) {
validCluster := unit.NewSingleRackCluster(3)
validCluster.Spec.Datacenter.Racks[0].Resources = corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("2Gi"),
},
}
sameName := validCluster.DeepCopy()
sameName.Spec.Datacenter.Racks = append(sameName.Spec.Datacenter.Racks, sameName.Spec.Datacenter.Racks[0])
invalidIntensity := validCluster.DeepCopy()
invalidIntensity.Spec.Repairs = append(invalidIntensity.Spec.Repairs, v1alpha1.RepairTaskSpec{
Intensity: pointer.StringPtr("100Mib"),
})
tests := []struct {
name string
obj *v1alpha1.ScyllaCluster
allowed bool
}{
{
name: "valid",
obj: validCluster,
allowed: true,
},
{
name: "two racks with same name",
obj: sameName,
allowed: false,
},
{
name: "invalid intensity in repair task spec",
obj: invalidIntensity,
allowed: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := v1alpha1.CheckValues(test.obj)
if test.allowed {
require.NoError(t, err, "Wrong value returned from checkValues function. Message: '%s'", err)
} else {
require.Error(t, err, "Wrong value returned from checkValues function. Message: '%s'", err)
}
})
}
}
func TestCheckTransitions(t *testing.T) {
tests := []struct {
name string
old *v1alpha1.ScyllaCluster
new *v1alpha1.ScyllaCluster
allowed bool
}{
{
name: "same as old",
old: unit.NewSingleRackCluster(3),
new: unit.NewSingleRackCluster(3),
allowed: true,
},
{
name: "major version changed",
old: unit.NewSingleRackCluster(3),
new: unit.NewDetailedSingleRackCluster("test-cluster", "test-ns", "repo", "3.3.1", "test-dc", "test-rack", 3),
allowed: false,
},
{
name: "minor version changed",
old: unit.NewSingleRackCluster(3),
new: unit.NewDetailedSingleRackCluster("test-cluster", "test-ns", "repo", "2.4.2", "test-dc", "test-rack", 3),
allowed: false,
},
{
name: "patch version changed",
old: unit.NewSingleRackCluster(3),
new: unit.NewDetailedSingleRackCluster("test-cluster", "test-ns", "repo", "2.3.2", "test-dc", "test-rack", 3),
allowed: true,
},
{
name: "repo changed",
old: unit.NewSingleRackCluster(3),
new: unit.NewDetailedSingleRackCluster("test-cluster", "test-ns", "new-repo", "2.3.2", "test-dc", "test-rack", 3),
allowed: false,
},
{
name: "dcName changed",
old: unit.NewSingleRackCluster(3),
new: unit.NewDetailedSingleRackCluster("test-cluster", "test-ns", "repo", "2.3.1", "new-dc", "test-rack", 3),
allowed: false,
},
{
name: "rackPlacement changed",
old: unit.NewSingleRackCluster(3),
new: placementChanged(unit.NewSingleRackCluster(3)),
allowed: false,
},
{
name: "rackStorage changed",
old: unit.NewSingleRackCluster(3),
new: storageChanged(unit.NewSingleRackCluster(3)),
allowed: false,
},
{
name: "rackResources changed",
old: unit.NewSingleRackCluster(3),
new: resourceChanged(unit.NewSingleRackCluster(3)),
allowed: false,
},
{
name: "rack deleted",
old: unit.NewSingleRackCluster(3),
new: rackDeleted(unit.NewSingleRackCluster(3)),
allowed: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := v1alpha1.CheckTransitions(test.old, test.new)
if test.allowed {
require.NoError(t, err, "Wrong value returned from checkTransitions function. Message: '%s'", err)
} else {
require.Error(t, err, "Wrong value returned from checkTransitions function. Message: '%s'", err)
}
})
}
}
func placementChanged(c *v1alpha1.ScyllaCluster) *v1alpha1.ScyllaCluster {
c.Spec.Datacenter.Racks[0].Placement = &v1alpha1.PlacementSpec{}
return c
}
func resourceChanged(c *v1alpha1.ScyllaCluster) *v1alpha1.ScyllaCluster {
c.Spec.Datacenter.Racks[0].Resources.Requests = map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: *resource.NewMilliQuantity(1000, resource.DecimalSI),
}
return c
}
func rackDeleted(c *v1alpha1.ScyllaCluster) *v1alpha1.ScyllaCluster {
c.Spec.Datacenter.Racks = nil
return c
}
func storageChanged(c *v1alpha1.ScyllaCluster) *v1alpha1.ScyllaCluster {
c.Spec.Datacenter.Racks[0].Storage.Capacity = "15Gi"
return c
}