Ejemplo n.º 1
0
func TestCreateController(t *testing.T) {
	mockRegistry := registrytest.ControllerRegistry{}
	mockPodRegistry := registrytest.PodRegistry{
		Pods: []api.Pod{
			{
				JSONBase: api.JSONBase{ID: "foo"},
				Labels:   map[string]string{"a": "b"},
			},
		},
	}
	storage := RegistryStorage{
		registry:    &mockRegistry,
		podRegistry: &mockPodRegistry,
		pollPeriod:  time.Millisecond * 1,
	}
	controller := &api.ReplicationController{
		JSONBase: api.JSONBase{ID: "test"},
		DesiredState: api.ReplicationControllerState{
			Replicas:        2,
			ReplicaSelector: map[string]string{"a": "b"},
			PodTemplate:     validPodTemplate,
		},
	}
	channel, err := storage.Create(controller)
	if err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	select {
	case <-time.After(time.Millisecond * 100):
		// Do nothing, this is expected.
	case <-channel:
		t.Error("Unexpected read from async channel")
	}

	mockPodRegistry.Lock()
	mockPodRegistry.Pods = []api.Pod{
		{
			JSONBase: api.JSONBase{ID: "foo"},
			Labels:   map[string]string{"a": "b"},
		},
		{
			JSONBase: api.JSONBase{ID: "bar"},
			Labels:   map[string]string{"a": "b"},
		},
	}
	mockPodRegistry.Unlock()

	select {
	case <-time.After(time.Second * 1):
		t.Error("Unexpected timeout")
	case <-channel:
		// Do nothing, this is expected
	}
}