Exemplo n.º 1
0
func TestListImagesPopulatedList(t *testing.T) {
	mockRegistry := test.NewImageRegistry()
	mockRegistry.Images = &api.ImageList{
		Items: []api.Image{
			{
				JSONBase: kubeapi.JSONBase{
					ID: "foo",
				},
			},
			{
				JSONBase: kubeapi.JSONBase{
					ID: "bar",
				},
			},
		},
	}

	storage := REST{
		registry: mockRegistry,
	}

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

	images := list.(*api.ImageList)

	if e, a := 2, len(images.Items); e != a {
		t.Errorf("Expected %v, got %v", e, a)
	}
}
Exemplo n.º 2
0
func TestCreateImageRepositoryMappingFindError(t *testing.T) {
	imageRegistry := test.NewImageRegistry()
	imageRepositoryRegistry := test.NewImageRepositoryRegistry()
	imageRepositoryRegistry.Err = fmt.Errorf("123")
	storage := &REST{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 {
		t.Fatal("Unexpected nil err")
	}
	if err.Error() != "123" {
		t.Errorf("Expected 'unable to locate' error, got %#v", err)
	}
}
Exemplo n.º 3
0
func TestDeleteImage(t *testing.T) {
	mockRegistry := test.NewImageRegistry()
	storage := REST{registry: mockRegistry}
	channel, err := storage.Delete("foo")
	if channel == nil {
		t.Error("Unexpected nil channel")
	}
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}

	select {
	case result := <-channel:
		status, ok := result.(*kubeapi.Status)
		if !ok {
			t.Errorf("Expected status type, got: %#v", result)
		}
		if status.Status != "success" {
			t.Errorf("Expected status=success, got: %#v", status)
		}
	case <-time.After(50 * time.Millisecond):
		t.Errorf("Timed out waiting for result")
	default:
	}
}
Exemplo n.º 4
0
func TestCreateImageOK(t *testing.T) {
	mockRegistry := test.NewImageRegistry()
	storage := REST{registry: mockRegistry}

	channel, err := storage.Create(&api.Image{
		JSONBase:             kubeapi.JSONBase{ID: "foo"},
		DockerImageReference: "openshift/ruby-19-centos",
	})
	if channel == nil {
		t.Errorf("Expected nil channel, got %v", channel)
	}
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}

	select {
	case result := <-channel:
		image, ok := result.(*api.Image)
		if !ok {
			t.Errorf("Expected image type, got: %#v", result)
		}
		if image.ID != "foo" {
			t.Errorf("Unexpected image: %#v", image)
		}
	case <-time.After(50 * time.Millisecond):
		t.Errorf("Timed out waiting for result")
	default:
	}
}
Exemplo n.º 5
0
func TestCreateRegistrySaveError(t *testing.T) {
	mockRegistry := test.NewImageRegistry()
	mockRegistry.Err = fmt.Errorf("test error")
	storage := REST{registry: mockRegistry}

	channel, err := storage.Create(&api.Image{
		JSONBase:             kubeapi.JSONBase{ID: "foo"},
		DockerImageReference: "openshift/ruby-19-centos",
	})
	if channel == nil {
		t.Errorf("Expected nil channel, got %v", channel)
	}
	if err != nil {
		t.Errorf("Unexpected non-nil error: %#v", err)
	}

	select {
	case result := <-channel:
		status, ok := result.(*kubeapi.Status)
		if !ok {
			t.Errorf("Expected status type, got: %#v", result)
		}
		if status.Status != "failure" || status.Message != "foo" {
			t.Errorf("Expected failure status, got %#V", status)
		}
	case <-time.After(50 * time.Millisecond):
		t.Errorf("Timed out waiting for result")
	default:
	}
}
Exemplo n.º 6
0
func TestGetImageError(t *testing.T) {
	mockRegistry := test.NewImageRegistry()
	mockRegistry.Err = fmt.Errorf("bad")
	storage := REST{registry: mockRegistry}

	image, err := storage.Get("foo")
	if image != nil {
		t.Errorf("Unexpected non-nil image: %#v", image)
	}
	if err != mockRegistry.Err {
		t.Errorf("Expected %#v, got %#v", mockRegistry.Err, err)
	}
}
Exemplo n.º 7
0
func TestCreateImageRepositoryMappingBadObject(t *testing.T) {
	imageRegistry := test.NewImageRegistry()
	imageRepositoryRegistry := test.NewImageRepositoryRegistry()
	storage := &REST{imageRegistry, imageRepositoryRegistry}

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

	channel, err := storage.Update("repo1")
	if channel != nil {
		t.Errorf("Unexpected non-nil channel %#v", channel)
	}
	if err == nil {
		t.Fatal("Unexpected nil err")
	}
	if strings.Index(err.Error(), "ImageRepositoryMappings may not be changed.") == -1 {
		t.Errorf("Expected 'may not be changed' error, got %#v", err)
	}
}
Exemplo n.º 9
0
func TestDeleteImageRepositoryMapping(t *testing.T) {
	imageRegistry := test.NewImageRegistry()
	imageRepositoryRegistry := test.NewImageRepositoryRegistry()
	storage := &REST{imageRegistry, imageRepositoryRegistry}

	channel, err := storage.Delete("repo1")
	if channel != nil {
		t.Errorf("Unexpected non-nil channel %#v", channel)
	}
	if err == nil {
		t.Fatal("Unexpected nil err")
	}
	if !errors.IsNotFound(err) {
		t.Errorf("Expected 'not found' error, got %#v", err)
	}
}
Exemplo n.º 10
0
func TestListImageRepositoryMappings(t *testing.T) {
	imageRegistry := test.NewImageRegistry()
	imageRepositoryRegistry := test.NewImageRepositoryRegistry()
	storage := &REST{imageRegistry, imageRepositoryRegistry}

	list, err := storage.List(labels.Everything())
	if list != nil {
		t.Errorf("Unexpected non-nil list %#v", list)
	}
	if err == nil {
		t.Fatal("Unexpected nil err")
	}
	if !errors.IsNotFound(err) {
		t.Errorf("Expected 'not found' error, got %#v", err)
	}
}
Exemplo n.º 11
0
func TestGetImageRepositoryMapping(t *testing.T) {
	imageRegistry := test.NewImageRegistry()
	imageRepositoryRegistry := test.NewImageRepositoryRegistry()
	storage := &REST{imageRegistry, imageRepositoryRegistry}

	obj, err := storage.Get("foo")
	if obj != nil {
		t.Errorf("Unexpected non-nil object %#v", obj)
	}
	if err == nil {
		t.Fatal("Unexpected nil err")
	}
	if !errors.IsNotFound(err) {
		t.Errorf("Expected 'not found' error, got %#v", err)
	}
}
Exemplo n.º 12
0
func TestListImagesError(t *testing.T) {
	mockRegistry := test.NewImageRegistry()
	mockRegistry.Err = fmt.Errorf("test error")

	storage := REST{
		registry: mockRegistry,
	}

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

	if images != nil {
		t.Errorf("Unexpected non-nil images list: %#v", images)
	}
}
Exemplo n.º 13
0
func TestListImagesEmptyList(t *testing.T) {
	mockRegistry := test.NewImageRegistry()
	mockRegistry.Images = &api.ImageList{
		Items: []api.Image{},
	}

	storage := REST{
		registry: mockRegistry,
	}

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

	if len(images.(*api.ImageList).Items) != 0 {
		t.Errorf("Unexpected non-zero images list: %#v", images)
	}
}
Exemplo n.º 14
0
func TestGetImageOK(t *testing.T) {
	mockRegistry := test.NewImageRegistry()
	mockRegistry.Image = &api.Image{
		JSONBase:             kubeapi.JSONBase{ID: "foo"},
		DockerImageReference: "openshift/ruby-19-centos",
	}
	storage := REST{registry: mockRegistry}

	image, err := storage.Get("foo")
	if image == nil {
		t.Error("Unexpected nil image")
	}
	if err != nil {
		t.Errorf("Unexpected non-nil error", err)
	}
	if image.(*api.Image).ID != "foo" {
		t.Errorf("Unexpected image: %#v", image)
	}
}
Exemplo n.º 15
0
func TestCreateImageRepositoryMappingNotFound(t *testing.T) {
	imageRegistry := test.NewImageRegistry()
	imageRepositoryRegistry := test.NewImageRepositoryRegistry()
	imageRepositoryRegistry.ImageRepositories = &api.ImageRepositoryList{
		Items: []api.ImageRepository{
			{
				JSONBase: kubeapi.JSONBase{
					ID: "repo1",
				},
				DockerImageRepository: "localhost:5000/test/repo",
			},
		},
	}
	storage := &REST{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 {
		t.Fatal("Unexpected nil err")
	}
	if !errors.IsInvalid(err) {
		t.Fatalf("Expected 'invalid' err, got: %#v", err)
	}
}
Exemplo n.º 16
0
func TestCreateImageRepositoryMapping(t *testing.T) {
	imageRegistry := test.NewImageRegistry()
	imageRepositoryRegistry := test.NewImageRepositoryRegistry()
	imageRepositoryRegistry.ImageRepositories = &api.ImageRepositoryList{
		Items: []api.ImageRepository{
			{
				JSONBase: kubeapi.JSONBase{
					ID: "repo1",
				},
				DockerImageRepository: "localhost:5000/someproject/somerepo",
			},
		},
	}
	storage := &REST{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)
	}
}