Skip to content

Commit 147f6ee

Browse files
committed
Add new integration tests
1 parent 671ccad commit 147f6ee

File tree

3 files changed

+100
-9
lines changed

3 files changed

+100
-9
lines changed

controllers/controller_util_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,16 @@ func rbNotExists(ctx context.Context, c client.Client, other *rbacv1.RoleBinding
320320
return apierrors.IsNotFound(err) || (err == nil && rb.DeletionTimestamp != nil)
321321
}
322322

323+
func pvcEqualSize(ctx context.Context, c client.Client, other *corev1.PersistentVolumeClaim, newSize string) bool {
324+
pvc := &corev1.PersistentVolumeClaim{}
325+
key := client.ObjectKeyFromObject(other)
326+
err := c.Get(ctx, key, pvc)
327+
if err != nil {
328+
return false
329+
}
330+
return newSize == pvc.Spec.Resources.Requests.Storage().String()
331+
}
332+
323333
// Only succeeds if the condition is valid for `waitFor` time.
324334
// Checks the condition every `tick`
325335
func consistently(t assert.TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool {

controllers/inplace_controller_test.go

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
batchv1 "k8s.io/api/batch/v1"
1414
corev1 "k8s.io/api/core/v1"
1515
rbacv1 "k8s.io/api/rbac/v1"
16+
storagev1 "k8s.io/api/storage/v1"
1617
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1718
"k8s.io/apimachinery/pkg/runtime"
19+
"k8s.io/utils/pointer"
1820
ctrl "sigs.k8s.io/controller-runtime"
1921
"sigs.k8s.io/controller-runtime/pkg/client"
2022
"sigs.k8s.io/controller-runtime/pkg/envtest"
@@ -32,7 +34,20 @@ func TestInplaceController(t *testing.T) {
3234

3335
t.Run("InplaceE2E", func(t *testing.T) {
3436

35-
t.Run("Don't scale down correct StatfulSets", func(t *testing.T) {
37+
sc := &storagev1.StorageClass{
38+
ObjectMeta: metav1.ObjectMeta{
39+
Name: "mysc",
40+
Annotations: map[string]string{
41+
"storageclass.kubernetes.io/is-default-class": "true",
42+
},
43+
},
44+
Provisioner: "mysc",
45+
AllowVolumeExpansion: pointer.Bool(true),
46+
}
47+
48+
require.NoError(t, c.Create(ctx, sc))
49+
50+
t.Run("Don't touch correct PVCs", func(t *testing.T) {
3651
t.Parallel()
3752
ctx := context.Background()
3853
ns := "e2e1"
@@ -42,21 +57,66 @@ func TestInplaceController(t *testing.T) {
4257
Name: ns,
4358
},
4459
}))
60+
pvcSize := "2G"
61+
sts := newTestStatefulSet(ns, "test", 1, pvcSize)
62+
sts.Labels = map[string]string{
63+
testLabelName: "true",
64+
}
65+
66+
pvc := applyResizablePVC(ctx, "data-test-0", ns, pvcSize, sts, c, require)
67+
68+
require.NoError(c.Create(ctx, sts))
69+
70+
consistently(t, func() bool {
71+
return pvcEqualSize(ctx, c, pvc, pvcSize)
72+
}, duration, interval, "PVCs equal size")
73+
74+
})
75+
t.Run("Ignore STS without the label", func(t *testing.T) {
76+
t.Parallel()
77+
ctx := context.Background()
78+
ns := "e2e2"
79+
require := require.New(t)
80+
require.NoError(c.Create(ctx, &corev1.Namespace{
81+
ObjectMeta: metav1.ObjectMeta{
82+
Name: ns,
83+
},
84+
}))
4585
sts := newTestStatefulSet(ns, "test", 1, "2G")
46-
// sts.Labels[testLabelName] = "true"
47-
require.NoError(c.Create(ctx, newSource(ns, "data-test-0", "2G",
48-
func(pvc *corev1.PersistentVolumeClaim) *corev1.PersistentVolumeClaim {
49-
pvc.Labels = sts.Spec.Selector.MatchLabels
50-
return pvc
51-
})))
86+
87+
pvc := applyResizablePVC(ctx, "data-test-0", ns, "1G", sts, c, require)
88+
5289
require.NoError(c.Create(ctx, sts))
5390

5491
consistently(t, func() bool {
55-
return stsExists(ctx, c, sts)
56-
}, duration, interval, "Sts exists")
92+
return pvcEqualSize(ctx, c, pvc, "1G")
93+
}, duration, interval, "PVCs equal size")
94+
})
95+
t.Run("Change PVCs if they not match", func(t *testing.T) {
96+
t.Parallel()
97+
ctx := context.Background()
98+
ns := "e2e3"
99+
require := require.New(t)
100+
require.NoError(c.Create(ctx, &corev1.Namespace{
101+
ObjectMeta: metav1.ObjectMeta{
102+
Name: ns,
103+
},
104+
}))
105+
sts := newTestStatefulSet(ns, "test", 1, "2G")
106+
sts.Labels = map[string]string{
107+
testLabelName: "true",
108+
}
57109

110+
pvc := applyResizablePVC(ctx, "data-test-0", ns, "1G", sts, c, require)
111+
112+
require.NoError(c.Create(ctx, sts))
113+
114+
consistently(t, func() bool {
115+
return pvcEqualSize(ctx, c, pvc, "2G")
116+
}, duration, interval, "PVCs equal size")
58117
})
59118
})
119+
60120
}
61121

62122
// startInplaceTestReconciler sets up a separate test env and starts the controller
@@ -72,6 +132,7 @@ func startInplaceTestReconciler(t *testing.T, ctx context.Context, crname string
72132
req.NoError(corev1.AddToScheme(s))
73133
req.NoError(batchv1.AddToScheme(s))
74134
req.NoError(rbacv1.AddToScheme(s))
135+
req.NoError(storagev1.AddToScheme(s))
75136

76137
mgr, err := ctrl.NewManager(conf, ctrl.Options{
77138
Scheme: s,
@@ -90,3 +151,19 @@ func startInplaceTestReconciler(t *testing.T, ctx context.Context, crname string
90151

91152
return mgr.GetClient(), testEnv.Stop
92153
}
154+
155+
func applyResizablePVC(ctx context.Context, name, ns, size string, sts *appsv1.StatefulSet, c client.Client, require *require.Assertions) *corev1.PersistentVolumeClaim {
156+
pvc := newSource(ns, name, size,
157+
func(pvc *corev1.PersistentVolumeClaim) *corev1.PersistentVolumeClaim {
158+
pvc.Labels = sts.Spec.Selector.MatchLabels
159+
return pvc
160+
})
161+
162+
pvc.Spec.StorageClassName = pointer.String("mysc")
163+
require.NoError(c.Create(ctx, pvc))
164+
165+
// we need to set the PVC to bound in order for the resize to work
166+
pvc.Status.Phase = corev1.ClaimBound
167+
require.NoError(c.Status().Update(ctx, pvc))
168+
return pvc
169+
}

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
131131
github.com/go-lintpack/lintpack v0.5.2/go.mod h1:NwZuYi2nUHho8XEIZ6SIxihrnPoqBTDqfpXvXAN0sXM=
132132
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
133133
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
134+
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
134135
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
135136
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
136137
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
@@ -328,11 +329,13 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
328329
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
329330
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
330331
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
332+
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
331333
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
332334
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
333335
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
334336
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
335337
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
338+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
336339
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
337340
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
338341
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
@@ -394,6 +397,7 @@ github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:
394397
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
395398
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
396399
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
400+
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
397401
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
398402
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
399403
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=

0 commit comments

Comments
 (0)