diff --git a/pkg/applier/manager_test.go b/pkg/applier/manager_test.go index db17e6277987..7c335868ea98 100644 --- a/pkg/applier/manager_test.go +++ b/pkg/applier/manager_test.go @@ -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" @@ -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