Skip to content

Commit

Permalink
Add new integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Aug 4, 2023
1 parent 671ccad commit 147f6ee
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
10 changes: 10 additions & 0 deletions controllers/controller_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ func rbNotExists(ctx context.Context, c client.Client, other *rbacv1.RoleBinding
return apierrors.IsNotFound(err) || (err == nil && rb.DeletionTimestamp != nil)
}

func pvcEqualSize(ctx context.Context, c client.Client, other *corev1.PersistentVolumeClaim, newSize string) bool {
pvc := &corev1.PersistentVolumeClaim{}
key := client.ObjectKeyFromObject(other)
err := c.Get(ctx, key, pvc)
if err != nil {
return false
}
return newSize == pvc.Spec.Resources.Requests.Storage().String()
}

// Only succeeds if the condition is valid for `waitFor` time.
// Checks the condition every `tick`
func consistently(t assert.TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool {
Expand Down
95 changes: 86 additions & 9 deletions controllers/inplace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
Expand All @@ -32,7 +34,20 @@ func TestInplaceController(t *testing.T) {

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

t.Run("Don't scale down correct StatfulSets", func(t *testing.T) {
sc := &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "mysc",
Annotations: map[string]string{
"storageclass.kubernetes.io/is-default-class": "true",
},
},
Provisioner: "mysc",
AllowVolumeExpansion: pointer.Bool(true),
}

require.NoError(t, c.Create(ctx, sc))

t.Run("Don't touch correct PVCs", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ns := "e2e1"
Expand All @@ -42,21 +57,66 @@ func TestInplaceController(t *testing.T) {
Name: ns,
},
}))
pvcSize := "2G"
sts := newTestStatefulSet(ns, "test", 1, pvcSize)
sts.Labels = map[string]string{
testLabelName: "true",
}

pvc := applyResizablePVC(ctx, "data-test-0", ns, pvcSize, sts, c, require)

require.NoError(c.Create(ctx, sts))

consistently(t, func() bool {
return pvcEqualSize(ctx, c, pvc, pvcSize)
}, duration, interval, "PVCs equal size")

})
t.Run("Ignore STS without the label", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ns := "e2e2"
require := require.New(t)
require.NoError(c.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
},
}))
sts := newTestStatefulSet(ns, "test", 1, "2G")
// sts.Labels[testLabelName] = "true"
require.NoError(c.Create(ctx, newSource(ns, "data-test-0", "2G",
func(pvc *corev1.PersistentVolumeClaim) *corev1.PersistentVolumeClaim {
pvc.Labels = sts.Spec.Selector.MatchLabels
return pvc
})))

pvc := applyResizablePVC(ctx, "data-test-0", ns, "1G", sts, c, require)

require.NoError(c.Create(ctx, sts))

consistently(t, func() bool {
return stsExists(ctx, c, sts)
}, duration, interval, "Sts exists")
return pvcEqualSize(ctx, c, pvc, "1G")
}, duration, interval, "PVCs equal size")
})
t.Run("Change PVCs if they not match", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ns := "e2e3"
require := require.New(t)
require.NoError(c.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
},
}))
sts := newTestStatefulSet(ns, "test", 1, "2G")
sts.Labels = map[string]string{
testLabelName: "true",
}

pvc := applyResizablePVC(ctx, "data-test-0", ns, "1G", sts, c, require)

require.NoError(c.Create(ctx, sts))

consistently(t, func() bool {
return pvcEqualSize(ctx, c, pvc, "2G")
}, duration, interval, "PVCs equal size")
})
})

}

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

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

return mgr.GetClient(), testEnv.Stop
}

func applyResizablePVC(ctx context.Context, name, ns, size string, sts *appsv1.StatefulSet, c client.Client, require *require.Assertions) *corev1.PersistentVolumeClaim {
pvc := newSource(ns, name, size,
func(pvc *corev1.PersistentVolumeClaim) *corev1.PersistentVolumeClaim {
pvc.Labels = sts.Spec.Selector.MatchLabels
return pvc
})

pvc.Spec.StorageClassName = pointer.String("mysc")
require.NoError(c.Create(ctx, pvc))

// we need to set the PVC to bound in order for the resize to work
pvc.Status.Phase = corev1.ClaimBound
require.NoError(c.Status().Update(ctx, pvc))
return pvc
}
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-lintpack/lintpack v0.5.2/go.mod h1:NwZuYi2nUHho8XEIZ6SIxihrnPoqBTDqfpXvXAN0sXM=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -328,11 +329,13 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down Expand Up @@ -394,6 +397,7 @@ github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
Expand Down

0 comments on commit 147f6ee

Please sign in to comment.