func TestCreateImageRepositoryMappingFindError(t *testing.T) {
	imageRegistry := imagetest.NewImageRegistry()
	imageRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	imageRepositoryRegistry.Err = fmt.Errorf("123")
	storage := &ImageRepositoryMappingStorage{imageRegistry, imageRepositoryRegistry}

	mapping := api.ImageRepositoryMapping{
		DockerImageRepository: "localhost:5000/someproject/somerepo",
		Image: api.Image{
			JSONBase: kubeapi.JSONBase{
				ID: "imageID1",
			},
			DockerImageReference: "localhost:5000/someproject/somerepo:imageID1",
		},
		Tag: "latest",
	}

	channel, err := storage.Create(&mapping)
	if channel != nil {
		t.Errorf("Unexpected non-nil channel %#v", channel)
	}
	if err == nil || err.Error() != "123" {
		t.Errorf("Expected 'unable to locate' error, got %#v", err)
	}
}
func TestCreateImageRepositoryMappingNotFound(t *testing.T) {
	imageRegistry := imagetest.NewImageRegistry()
	imageRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	imageRepositoryRegistry.ImageRepositories = &api.ImageRepositoryList{
		Items: []api.ImageRepository{
			{
				JSONBase: kubeapi.JSONBase{
					ID: "repo1",
				},
				DockerImageRepository: "localhost:5000/test/repo",
			},
		},
	}
	storage := &ImageRepositoryMappingStorage{imageRegistry, imageRepositoryRegistry}

	mapping := api.ImageRepositoryMapping{
		DockerImageRepository: "localhost:5000/someproject/somerepo",
		Image: api.Image{
			JSONBase: kubeapi.JSONBase{
				ID: "imageID1",
			},
			DockerImageReference: "localhost:5000/someproject/somerepo:imageID1",
		},
		Tag: "latest",
	}

	channel, err := storage.Create(&mapping)
	if channel != nil {
		t.Errorf("Unexpected non-nil channel %#v", channel)
	}
	if err == nil || strings.Index(err.Error(), "Unable to locate an image repository") == -1 {
		t.Errorf("Expected 'unable to locate' error, got %#v", err)
	}
}
func TestListImageRepositoriesPopulatedList(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	mockRepositoryRegistry.ImageRepositories = &api.ImageRepositoryList{
		Items: []api.ImageRepository{
			{
				JSONBase: kubeapi.JSONBase{
					ID: "foo",
				},
			},
			{
				JSONBase: kubeapi.JSONBase{
					ID: "bar",
				},
			},
		},
	}

	storage := ImageRepositoryStorage{
		registry: mockRepositoryRegistry,
	}

	list, err := storage.List(labels.Everything())
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}

	imageRepositories := list.(*api.ImageRepositoryList)

	if e, a := 2, len(imageRepositories.Items); e != a {
		t.Errorf("Expected %v, got %v", e, a)
	}
}
func TestCreateImageRepositoryMappingBadObject(t *testing.T) {
	imageRegistry := imagetest.NewImageRegistry()
	imageRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	storage := &ImageRepositoryMappingStorage{imageRegistry, imageRepositoryRegistry}

	channel, err := storage.Create("bad object")
	if channel != nil {
		t.Errorf("Unexpected non-nil channel %#v", channel)
	}
	if err == nil || strings.Index(err.Error(), "not an image repository mapping") == -1 {
		t.Errorf("Expected 'not an image repository mapping' error, got %#v", err)
	}
}
func TestUpdateImageRepositoryMapping(t *testing.T) {
	imageRegistry := imagetest.NewImageRegistry()
	imageRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	storage := &ImageRepositoryMappingStorage{imageRegistry, imageRepositoryRegistry}

	channel, err := storage.Update("repo1")
	if channel != nil {
		t.Errorf("Unexpected non-nil channel %#v", channel)
	}
	if err == nil || strings.Index(err.Error(), "not supported") == -1 {
		t.Errorf("Expected 'not supported' error, got %#v", err)
	}
}
func TestListImageRepositoryMappings(t *testing.T) {
	imageRegistry := imagetest.NewImageRegistry()
	imageRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	storage := &ImageRepositoryMappingStorage{imageRegistry, imageRepositoryRegistry}

	list, err := storage.List(labels.Everything())
	if list != nil {
		t.Errorf("Unexpected non-nil list %#v", list)
	}
	if err == nil || strings.Index(err.Error(), "not supported") == -1 {
		t.Errorf("Expected 'not supported' error, got %#v", err)
	}
}
func TestGetImageRepositoryMapping(t *testing.T) {
	imageRegistry := imagetest.NewImageRegistry()
	imageRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	storage := &ImageRepositoryMappingStorage{imageRegistry, imageRepositoryRegistry}

	obj, err := storage.Get("foo")
	if obj != nil {
		t.Errorf("Unexpected non-nil object %#v", obj)
	}
	if err == nil || strings.Index(err.Error(), "not supported") == -1 {
		t.Errorf("Expected 'not supported' error, got %#v", err)
	}
}
func TestGetImageRepositoryError(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	mockRepositoryRegistry.Err = fmt.Errorf("test error")
	storage := ImageRepositoryStorage{registry: mockRepositoryRegistry}

	image, err := storage.Get("image1")
	if image != nil {
		t.Errorf("Unexpected non-nil image: %#v", image)
	}
	if err != mockRepositoryRegistry.Err {
		t.Errorf("Expected %#v, got %#v", mockRepositoryRegistry.Err, err)
	}
}
func TestListImageRepositoriesError(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	mockRepositoryRegistry.Err = fmt.Errorf("test error")

	storage := ImageRepositoryStorage{
		registry: mockRepositoryRegistry,
	}

	imageRepositories, err := storage.List(nil)
	if err != mockRepositoryRegistry.Err {
		t.Errorf("Expected %#v, Got %#v", mockRepositoryRegistry.Err, err)
	}

	if imageRepositories != nil {
		t.Errorf("Unexpected non-nil imageRepositories list: %#v", imageRepositories)
	}
}
func TestDeleteImageRepository(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	storage := ImageRepositoryStorage{registry: mockRepositoryRegistry}

	channel, err := storage.Delete("foo")
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}
	result := <-channel
	status, ok := result.(*kubeapi.Status)
	if !ok {
		t.Errorf("Expected status, got %#v", result)
	}
	if status.Status != "success" {
		t.Errorf("Expected status=success, got %#v", status)
	}
}
func TestCreateImageRepositoryRegistryErrorSaving(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	mockRepositoryRegistry.Err = fmt.Errorf("foo")
	storage := ImageRepositoryStorage{registry: mockRepositoryRegistry}

	channel, err := storage.Create(&api.ImageRepository{})
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}
	result := <-channel
	status, ok := result.(*kubeapi.Status)
	if !ok {
		t.Errorf("Expected status, got %#v", result)
	}
	if status.Status != "failure" || status.Message != "foo" {
		t.Errorf("Expected status=failure, message=foo, got %#v", status)
	}
}
func TestListImageRepositoriesEmptyList(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	mockRepositoryRegistry.ImageRepositories = &api.ImageRepositoryList{
		Items: []api.ImageRepository{},
	}

	storage := ImageRepositoryStorage{
		registry: mockRepositoryRegistry,
	}

	imageRepositories, err := storage.List(labels.Everything())
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}

	if len(imageRepositories.(*api.ImageRepositoryList).Items) != 0 {
		t.Errorf("Unexpected non-zero imageRepositories list: %#v", imageRepositories)
	}
}
func TestGetImageRepositoryOK(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	mockRepositoryRegistry.ImageRepository = &api.ImageRepository{
		JSONBase:              kubeapi.JSONBase{ID: "foo"},
		DockerImageRepository: "openshift/ruby-19-centos",
	}
	storage := ImageRepositoryStorage{registry: mockRepositoryRegistry}

	repo, err := storage.Get("foo")
	if repo == nil {
		t.Errorf("Unexpected nil repo: %#v", repo)
	}
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}
	if e, a := mockRepositoryRegistry.ImageRepository, repo; !reflect.DeepEqual(e, a) {
		t.Errorf("Expected %#v, got %#v", e, a)
	}
}
func TestUpdateImageRepositoryOK(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	storage := ImageRepositoryStorage{registry: mockRepositoryRegistry}

	channel, err := storage.Update(&api.ImageRepository{
		JSONBase: kubeapi.JSONBase{ID: "bar"},
	})
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}
	result := <-channel
	repo, ok := result.(*api.ImageRepository)
	if !ok {
		t.Errorf("Expected image repository, got %#v", result)
	}
	if repo.ID != "bar" {
		t.Errorf("Unexpected repo returned: %#v", repo)
	}
}
func TestCreateImageRepositoryOK(t *testing.T) {
	mockRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	storage := ImageRepositoryStorage{registry: mockRepositoryRegistry}

	channel, err := storage.Create(&api.ImageRepository{})
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}

	result := <-channel
	repo, ok := result.(*api.ImageRepository)
	if !ok {
		t.Errorf("Unexpected result: %#v", result)
	}
	if len(repo.ID) == 0 {
		t.Errorf("Expected repo's ID to be set: %#v", repo)
	}
	if repo.CreationTimestamp.IsZero() {
		t.Error("Unexpected zero CreationTimestamp")
	}
}
func TestCreateImageRepositoryMapping(t *testing.T) {
	imageRegistry := imagetest.NewImageRegistry()
	imageRepositoryRegistry := imagetest.NewImageRepositoryRegistry()
	imageRepositoryRegistry.ImageRepositories = &api.ImageRepositoryList{
		Items: []api.ImageRepository{
			{
				JSONBase: kubeapi.JSONBase{
					ID: "repo1",
				},
				DockerImageRepository: "localhost:5000/someproject/somerepo",
			},
		},
	}
	storage := &ImageRepositoryMappingStorage{imageRegistry, imageRepositoryRegistry}

	mapping := api.ImageRepositoryMapping{
		DockerImageRepository: "localhost:5000/someproject/somerepo",
		Image: api.Image{
			JSONBase: kubeapi.JSONBase{
				ID: "imageID1",
			},
			DockerImageReference: "localhost:5000/someproject/somerepo:imageID1",
			Metadata: docker.Image{
				Config: &docker.Config{
					Cmd:          []string{"ls", "/"},
					Env:          []string{"a=1"},
					ExposedPorts: map[docker.Port]struct{}{"1234/tcp": {}},
					Memory:       1234,
					CpuShares:    99,
					WorkingDir:   "/workingDir",
				},
			},
		},
		Tag: "latest",
	}
	ch, err := storage.Create(&mapping)
	if err != nil {
		t.Errorf("Unexpected error creating mapping: %#v", err)
	}

	out := <-ch
	t.Logf("out = '%#v'", out)

	image, err := imageRegistry.GetImage("imageID1")
	if err != nil {
		t.Errorf("Unexpected error retrieving image: %#v", err)
	}
	if e, a := mapping.Image.DockerImageReference, image.DockerImageReference; e != a {
		t.Errorf("Expected %s, got %s", e, a)
	}
	if !reflect.DeepEqual(mapping.Image.Metadata, image.Metadata) {
		t.Errorf("Expected %#v, got %#v", mapping.Image, image)
	}

	repo, err := imageRepositoryRegistry.GetImageRepository("repo1")
	if err != nil {
		t.Errorf("Unexpected non-nil err: %#v", err)
	}
	if e, a := "imageID1", repo.Tags["latest"]; e != a {
		t.Errorf("Expected %s, got %s", e, a)
	}
}