func TestStaticPodStatus(t *testing.T) { staticPod := *testPod staticPod.Annotations = map[string]string{kubetypes.ConfigSourceAnnotationKey: "file"} mirrorPod := *testPod mirrorPod.UID = "mirror-12345678" mirrorPod.Annotations = map[string]string{ kubetypes.ConfigSourceAnnotationKey: "api", kubetypes.ConfigMirrorAnnotationKey: "mirror", } client := testclient.NewSimpleFake(&mirrorPod) m := newTestManager(client) m.podManager.AddPod(&staticPod) m.podManager.AddPod(&mirrorPod) // Verify setup. assert.True(t, kubepod.IsStaticPod(&staticPod), "SetUp error: staticPod") assert.True(t, kubepod.IsMirrorPod(&mirrorPod), "SetUp error: mirrorPod") assert.Equal(t, m.podManager.TranslatePodUID(mirrorPod.UID), staticPod.UID) status := getRandomPodStatus() now := unversioned.Now() status.StartTime = &now m.SetPodStatus(&staticPod, status) retrievedStatus := expectPodStatus(t, m, &staticPod) assert.True(t, isStatusEqual(&status, &retrievedStatus), "Expected: %+v, Got: %+v", status, retrievedStatus) retrievedStatus, _ = m.GetPodStatus(mirrorPod.UID) assert.True(t, isStatusEqual(&status, &retrievedStatus), "Expected: %+v, Got: %+v", status, retrievedStatus) // Should translate mirrorPod / staticPod UID. m.testSyncBatch() verifyActions(t, m.kubeClient, []testclient.Action{ testclient.GetActionImpl{ActionImpl: testclient.ActionImpl{Verb: "get", Resource: "pods"}}, testclient.UpdateActionImpl{ActionImpl: testclient.ActionImpl{Verb: "update", Resource: "pods", Subresource: "status"}}, }) updateAction := client.Actions()[1].(testclient.UpdateActionImpl) updatedPod := updateAction.Object.(*api.Pod) assert.Equal(t, mirrorPod.UID, updatedPod.UID, "Expected mirrorPod (%q), but got %q", mirrorPod.UID, updatedPod.UID) assert.True(t, isStatusEqual(&status, &updatedPod.Status), "Expected: %+v, Got: %+v", status, updatedPod.Status) client.ClearActions() // No changes. m.testSyncBatch() verifyActions(t, m.kubeClient, []testclient.Action{}) // Mirror pod identity changes. m.podManager.DeletePod(&mirrorPod) mirrorPod.UID = "new-mirror-pod" mirrorPod.Status = api.PodStatus{} m.podManager.AddPod(&mirrorPod) // Expect update to new mirrorPod. m.testSyncBatch() verifyActions(t, m.kubeClient, []testclient.Action{ testclient.GetActionImpl{ActionImpl: testclient.ActionImpl{Verb: "get", Resource: "pods"}}, testclient.UpdateActionImpl{ActionImpl: testclient.ActionImpl{Verb: "update", Resource: "pods", Subresource: "status"}}, }) updateAction = client.Actions()[1].(testclient.UpdateActionImpl) updatedPod = updateAction.Object.(*api.Pod) assert.Equal(t, mirrorPod.UID, updatedPod.UID, "Expected mirrorPod (%q), but got %q", mirrorPod.UID, updatedPod.UID) assert.True(t, isStatusEqual(&status, &updatedPod.Status), "Expected: %+v, Got: %+v", status, updatedPod.Status) }
func TestStaticPodStatus(t *testing.T) { staticPod := *testPod staticPod.Annotations = map[string]string{kubetypes.ConfigSourceAnnotationKey: "file"} mirrorPod := *testPod mirrorPod.UID = "mirror-12345678" mirrorPod.Annotations = map[string]string{ kubetypes.ConfigSourceAnnotationKey: "api", kubetypes.ConfigMirrorAnnotationKey: "mirror", } client := testclient.NewSimpleFake(&mirrorPod) m := newTestManager(client) m.podManager.AddPod(&staticPod) m.podManager.AddPod(&mirrorPod) // Verify setup. assert.True(t, kubepod.IsStaticPod(&staticPod), "SetUp error: staticPod") assert.True(t, kubepod.IsMirrorPod(&mirrorPod), "SetUp error: mirrorPod") assert.Equal(t, m.podManager.TranslatePodUID(mirrorPod.UID), staticPod.UID) status := getRandomPodStatus() now := unversioned.Now() status.StartTime = &now m.SetPodStatus(&staticPod, status) retrievedStatus, _ := m.GetPodStatus(staticPod.UID) assert.True(t, isStatusEqual(&status, &retrievedStatus), "Expected: %+v, Got: %+v", status, retrievedStatus) retrievedStatus, _ = m.GetPodStatus(mirrorPod.UID) assert.True(t, isStatusEqual(&status, &retrievedStatus), "Expected: %+v, Got: %+v", status, retrievedStatus) // Should translate mirrorPod / staticPod UID. m.syncBatch() verifyActions(t, m.kubeClient, []testclient.Action{ testclient.GetActionImpl{ActionImpl: testclient.ActionImpl{Verb: "get", Resource: "pods"}}, testclient.UpdateActionImpl{ActionImpl: testclient.ActionImpl{Verb: "update", Resource: "pods", Subresource: "status"}}, }) updateAction := client.Actions()[1].(testclient.UpdateActionImpl) updatedPod := updateAction.Object.(*api.Pod) assert.Equal(t, mirrorPod.UID, updatedPod.UID, "Expected mirrorPod (%q), but got %q", mirrorPod.UID, updatedPod.UID) assert.True(t, isStatusEqual(&status, &updatedPod.Status), "Expected: %+v, Got: %+v", status, updatedPod.Status) client.ClearActions() otherPod := &api.Pod{ ObjectMeta: api.ObjectMeta{ UID: "other-87654321", Name: "other", Namespace: "new", }, } m.podManager.AddPod(otherPod) m.SetPodStatus(otherPod, getRandomPodStatus()) m.syncBatch() verifyActions(t, m.kubeClient, []testclient.Action{ testclient.GetActionImpl{ActionImpl: testclient.ActionImpl{Verb: "get", Resource: "pods"}}, }) _, found := m.GetPodStatus(otherPod.UID) assert.False(t, found, "otherPod status should have been deleted") }
func TestTokenDeletion(t *testing.T) { dockercfgSecretFieldSelector := fields.OneTermEqualSelector(client.SecretType, string(api.SecretTypeDockercfg)) testcases := map[string]struct { ClientObjects []runtime.Object DeletedSecret *api.Secret ExpectedActions []testclient.Action }{ "deleted token secret without serviceaccount": { ClientObjects: []runtime.Object{serviceAccount(addTokenSecretReference(tokenSecretReferences()), imagePullSecretReferences()), createdDockercfgSecret()}, DeletedSecret: serviceAccountTokenSecret(), ExpectedActions: []testclient.Action{ testclient.NewListAction("secrets", "default", labels.Everything(), dockercfgSecretFieldSelector), testclient.NewDeleteAction("secrets", "default", "default-dockercfg-fplln"), }, }, "deleted token secret with serviceaccount with reference": { ClientObjects: []runtime.Object{serviceAccount(addTokenSecretReference(tokenSecretReferences()), imagePullSecretReferences()), createdDockercfgSecret()}, DeletedSecret: serviceAccountTokenSecret(), ExpectedActions: []testclient.Action{ testclient.NewListAction("secrets", "default", labels.Everything(), dockercfgSecretFieldSelector), testclient.NewDeleteAction("secrets", "default", "default-dockercfg-fplln"), }, }, "deleted token secret with serviceaccount without reference": { ClientObjects: []runtime.Object{serviceAccount(addTokenSecretReference(tokenSecretReferences()), imagePullSecretReferences()), createdDockercfgSecret()}, DeletedSecret: serviceAccountTokenSecret(), ExpectedActions: []testclient.Action{ testclient.NewListAction("secrets", "default", labels.Everything(), dockercfgSecretFieldSelector), testclient.NewDeleteAction("secrets", "default", "default-dockercfg-fplln"), }, }, } for k, tc := range testcases { // Re-seed to reset name generation rand.Seed(1) client := testclient.NewSimpleFake(tc.ClientObjects...) controller := NewDockercfgTokenDeletedController(client, DockercfgTokenDeletedControllerOptions{}) if tc.DeletedSecret != nil { controller.secretDeleted(tc.DeletedSecret) } for i, action := range client.Actions() { if len(tc.ExpectedActions) < i+1 { t.Errorf("%s: %d unexpected actions: %+v", k, len(client.Actions())-len(tc.ExpectedActions), client.Actions()[i:]) break } expectedAction := tc.ExpectedActions[i] if !reflect.DeepEqual(expectedAction, action) { t.Errorf("%s: Expected %v, got %v", k, expectedAction, action) continue } } if len(tc.ExpectedActions) > len(client.Actions()) { t.Errorf("%s: %d additional expected actions:%+v", k, len(tc.ExpectedActions)-len(client.Actions()), tc.ExpectedActions[len(client.Actions()):]) } } }