|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + kapi "k8s.io/kubernetes/pkg/api" |
| 8 | + "k8s.io/kubernetes/pkg/api/unversioned" |
| 9 | + "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" |
| 10 | + "k8s.io/kubernetes/pkg/client/testing/core" |
| 11 | + "k8s.io/kubernetes/pkg/runtime" |
| 12 | + "k8s.io/kubernetes/pkg/watch" |
| 13 | + |
| 14 | + deployapi "github.com/openshift/origin/pkg/deploy/api" |
| 15 | +) |
| 16 | + |
| 17 | +func TestWaitForRunningDeploymentSuccess(t *testing.T) { |
| 18 | + fakeController := &kapi.ReplicationController{} |
| 19 | + fakeController.Name = "test-1" |
| 20 | + fakeController.Namespace = "test" |
| 21 | + |
| 22 | + kubeclient := fake.NewSimpleClientset([]runtime.Object{fakeController}...) |
| 23 | + fakeWatch := watch.NewFake() |
| 24 | + kubeclient.PrependWatchReactor("replicationcontrollers", core.DefaultWatchReactor(fakeWatch, nil)) |
| 25 | + stopChan := make(chan struct{}) |
| 26 | + |
| 27 | + go func() { |
| 28 | + defer close(stopChan) |
| 29 | + rc, ok, err := WaitForRunningDeployment(kubeclient.Core(), fakeController, 10*time.Second) |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("unexpected error: %v", err) |
| 32 | + } |
| 33 | + if !ok { |
| 34 | + t.Errorf("expected to return success") |
| 35 | + } |
| 36 | + if rc == nil { |
| 37 | + t.Errorf("expected returned replication controller to not be nil") |
| 38 | + } |
| 39 | + }() |
| 40 | + |
| 41 | + fakeController.Annotations = map[string]string{deployapi.DeploymentStatusAnnotation: string(deployapi.DeploymentStatusRunning)} |
| 42 | + fakeWatch.Modify(fakeController) |
| 43 | + <-stopChan |
| 44 | +} |
| 45 | + |
| 46 | +func TestWaitForRunningDeploymentRestartWatch(t *testing.T) { |
| 47 | + fakeController := &kapi.ReplicationController{} |
| 48 | + fakeController.Name = "test-1" |
| 49 | + fakeController.Namespace = "test" |
| 50 | + |
| 51 | + kubeclient := fake.NewSimpleClientset([]runtime.Object{fakeController}...) |
| 52 | + fakeWatch := watch.NewFake() |
| 53 | + |
| 54 | + watchCalledChan := make(chan struct{}) |
| 55 | + kubeclient.PrependWatchReactor("replicationcontrollers", func(action core.Action) (bool, watch.Interface, error) { |
| 56 | + fakeWatch.Reset() |
| 57 | + watchCalledChan <- struct{}{} |
| 58 | + return core.DefaultWatchReactor(fakeWatch, nil)(action) |
| 59 | + }) |
| 60 | + |
| 61 | + getReceivedChan := make(chan struct{}) |
| 62 | + kubeclient.PrependReactor("get", "replicationcontrollers", func(action core.Action) (bool, runtime.Object, error) { |
| 63 | + close(getReceivedChan) |
| 64 | + return true, fakeController, nil |
| 65 | + }) |
| 66 | + |
| 67 | + stopChan := make(chan struct{}) |
| 68 | + go func() { |
| 69 | + defer close(stopChan) |
| 70 | + rc, ok, err := WaitForRunningDeployment(kubeclient.Core(), fakeController, 10*time.Second) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("unexpected error: %v", err) |
| 73 | + } |
| 74 | + if !ok { |
| 75 | + t.Errorf("expected to return success") |
| 76 | + } |
| 77 | + if rc == nil { |
| 78 | + t.Errorf("expected returned replication controller to not be nil") |
| 79 | + } |
| 80 | + }() |
| 81 | + |
| 82 | + select { |
| 83 | + case <-watchCalledChan: |
| 84 | + case <-time.After(time.Second * 5): |
| 85 | + t.Fatalf("timeout waiting for the watch to start") |
| 86 | + } |
| 87 | + |
| 88 | + // Send the StatusReasonGone error to watcher which should trigger the watch restart. |
| 89 | + goneError := &unversioned.Status{Reason: unversioned.StatusReasonGone} |
| 90 | + fakeWatch.Error(goneError) |
| 91 | + |
| 92 | + // Make sure we observed the "get" action on replication controller, so the watch gets |
| 93 | + // the latest resourceVersion. |
| 94 | + select { |
| 95 | + case <-getReceivedChan: |
| 96 | + case <-time.After(time.Second * 5): |
| 97 | + t.Fatalf("timeout waiting for get on replication controllers") |
| 98 | + } |
| 99 | + |
| 100 | + // Wait for the watcher to restart and then transition the replication controller to |
| 101 | + // running state. |
| 102 | + select { |
| 103 | + case <-watchCalledChan: |
| 104 | + fakeController.Annotations = map[string]string{deployapi.DeploymentStatusAnnotation: string(deployapi.DeploymentStatusRunning)} |
| 105 | + fakeWatch.Modify(fakeController) |
| 106 | + <-stopChan |
| 107 | + case <-time.After(time.Second * 5): |
| 108 | + t.Fatalf("timeout waiting for the watch restart") |
| 109 | + } |
| 110 | +} |
0 commit comments