Skip to content

Commit

Permalink
Add local update tests (#234)
Browse files Browse the repository at this point in the history
* Local update test

* Local update test

* Local update test
  • Loading branch information
l0kix2 authored Apr 16, 2024
1 parent 2f777e3 commit c5d431d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
58 changes: 46 additions & 12 deletions controllers/ytsaurus_local_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package controllers_test

import (
"fmt"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -20,8 +22,7 @@ const (
dndsNameOne = "dn-1"
)

func TestYtsaurusFromScratch(t *testing.T) {
namespace := "ytsaurus-from-scratch"
func prepareTest(t *testing.T, namespace string) *testutil.TestHelper {
h := testutil.NewTestHelper(t, namespace, filepath.Join("..", "config", "crd", "bases"))
reconcilerSetup := func(mgr ctrl.Manager) error {
return (&controllers.YtsaurusReconciler{
Expand All @@ -31,6 +32,26 @@ func TestYtsaurusFromScratch(t *testing.T) {
}).SetupWithManager(mgr)
}
h.Start(reconcilerSetup)
return h
}

func waitClusterState(h *testutil.TestHelper, expectedState ytv1.ClusterState) {
h.Logf("[ Wait for YTsaurus %s state ]", expectedState)
testutil.FetchAndCheckEventually(
h,
ytsaurusName,
&ytv1.Ytsaurus{},
fmt.Sprintf("cluster state is %s", expectedState),
func(obj client.Object) bool {
state := obj.(*ytv1.Ytsaurus).Status.State
return state == expectedState
},
)
}

func TestYtsaurusFromScratch(t *testing.T) {
namespace := "ytsaurus-from-scratch"
h := prepareTest(t, namespace)
defer h.Stop()

ytsaurusResource := testutil.BuildMinimalYtsaurus(namespace, ytsaurusName)
Expand Down Expand Up @@ -78,15 +99,28 @@ func TestYtsaurusFromScratch(t *testing.T) {
return len(secret.Data["YT_TOKEN"]) != 0
},
)
waitClusterState(h, ytv1.ClusterStateRunning)
}

testutil.FetchAndCheckEventually(
h,
ytsaurusName,
&ytv1.Ytsaurus{},
"cluster state is running",
func(obj client.Object) bool {
state := obj.(*ytv1.Ytsaurus).Status.State
return state == ytv1.ClusterStateRunning
},
)
func TestYtsaurusUpdateStatelessComponent(t *testing.T) {
namespace := "upd-discovery"
h := prepareTest(t, namespace)
defer h.Stop()

ytsaurusResource := testutil.BuildMinimalYtsaurus(namespace, ytsaurusName)
testutil.DeployObject(h, &ytsaurusResource)

waitClusterState(h, ytv1.ClusterStateRunning)

imageUpdated := testYtsaurusImage + "-updated"
ytsaurusResource.Spec.Discovery.Image = &imageUpdated
t.Log("[ Updating discovery with disabled full update ]")
ytsaurusResource.Spec.EnableFullUpdate = false
testutil.UpdateObject(h, &ytv1.Ytsaurus{}, &ytsaurusResource)

waitClusterState(h, ytv1.ClusterStateRunning)

sts := appsv1.StatefulSet{}
testutil.GetObject(h, "ds", &sts)
require.Equal(t, imageUpdated, sts.Spec.Template.Spec.Containers[0].Image)
}
13 changes: 13 additions & 0 deletions pkg/testutil/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,25 @@ func (h *TestHelper) createNamespace() {
require.NoError(h.t, err)
}

func (h *TestHelper) Log(args ...any) {
h.t.Log(args...)
}

func (h *TestHelper) Logf(format string, args ...any) {
h.t.Logf(format, args...)
}

// helpers
func GetObject(h *TestHelper, key string, emptyObject client.Object) {
k8sCli := h.GetK8sClient()
err := k8sCli.Get(context.Background(), h.GetObjectKey(key), emptyObject)
require.NoError(h.t, err)
}
func ListObjects(h *TestHelper, emptyList client.ObjectList) {
k8sCli := h.GetK8sClient()
err := k8sCli.List(context.Background(), emptyList)
require.NoError(h.t, err)
}
func DeployObject(h *TestHelper, object client.Object) {
k8sCli := h.GetK8sClient()
err := k8sCli.Create(context.Background(), object)
Expand Down

0 comments on commit c5d431d

Please sign in to comment.