Skip to content

Commit

Permalink
Add test for folder renames in applier manager
Browse files Browse the repository at this point in the history
See: aa704c5 ("Also remove stack when directory is renamed")
Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Nov 14, 2024
1 parent aa704c5 commit 80aff81
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/applier/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"
"time"

"github.com/k0sproject/k0s/internal/pkg/file"
"github.com/k0sproject/k0s/internal/testutil"
"github.com/k0sproject/k0s/pkg/applier"
"github.com/k0sproject/k0s/pkg/component/controller/leaderelector"
Expand Down Expand Up @@ -159,6 +160,61 @@ data: {}
}
}

func TestManager_Rename(t *testing.T) {
cf := testutil.NewFakeClientFactory()
manifestsDir := t.TempDir()
leaderElector := leaderelector.Dummy{}

underTest := applier.Manager{
ManifestsDir: manifestsDir,
KubeClientFactory: cf,
LeaderElector: &leaderElector,
}

require.NoError(t, underTest.Init(context.TODO()))
require.NoError(t, underTest.Start(context.TODO()))
defer func() { assert.NoError(t, underTest.Stop()) }()
leaderElector.Leader = true
require.NoError(t, leaderElector.Start(context.TODO()))

theMap, err := yaml.Marshal(&map[string]any{
"apiVersion": "v1", "kind": "ConfigMap",
"metadata": map[string]any{"name": "the-map"},
})
require.NoError(t, err)
require.NoError(t, os.Mkdir(filepath.Join(manifestsDir, "lorem"), 0755))
require.NoError(t, file.AtomicWithTarget(filepath.Join(manifestsDir, "lorem", "the-map.yaml")).Write(theMap))
t.Log("Waiting for stack to be applied")
require.EventuallyWithT(t, func(t *assert.CollectT) {
configMaps, err := cf.Client.CoreV1().ConfigMaps(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if assert.NoError(t, err) && assert.Len(t, configMaps.Items, 1) {
cm := &configMaps.Items[0]
assert.Equal(t, "the-map", cm.Name)
assert.Subset(t, cm.Labels, map[string]string{"k0s.k0sproject.io/stack": "lorem"})
}
}, 20*time.Second, 350*time.Millisecond)

require.NoError(t, os.Rename(filepath.Join(manifestsDir, "lorem"), filepath.Join(manifestsDir, "ipsum")))
t.Log("Waiting for stack to be changed")
require.EventuallyWithT(t, func(t *assert.CollectT) {
configMaps, err := cf.Client.CoreV1().ConfigMaps(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if assert.NoError(t, err) && assert.Len(t, configMaps.Items, 1) {
cm := &configMaps.Items[0]
assert.Equal(t, "the-map", cm.Name)
assert.Subset(t, cm.Labels, map[string]string{"k0s.k0sproject.io/stack": "ipsum"})
}
}, 20*time.Second, 350*time.Millisecond)

require.NoError(t, os.Rename(filepath.Join(manifestsDir, "ipsum"), filepath.Join(t.TempDir(), "moved-away")))
t.Log("Waiting for stack to be deleted")
require.EventuallyWithT(t, func(t *assert.CollectT) {
configMaps, err := cf.Client.CoreV1().ConfigMaps(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if assert.NoError(t, err) {
assert.Empty(t, configMaps.Items)
}
}, 20*time.Second, 350*time.Millisecond)
}

//go:embed testdata/manager_test/*
var managerTestData embed.FS

Expand Down

0 comments on commit 80aff81

Please sign in to comment.