示例#1
0
// NewContainer creates a new Entity for the provided kube.Container. Container must be valid.
func NewContainer(container kube.Container, defaults kube.ObjectMeta, source string, objects ...deploy.KubeObject) (*Container, error) {
	err := validateContainer(container)
	if err != nil {
		return nil, fmt.Errorf("could not create Container from `%s`: %v", source, err)
	}

	base, err := newBase(EntityContainer, defaults, source, objects)
	if err != nil {
		return nil, err
	}

	newContainer := Container{base: base}
	if len(container.Image) != 0 {
		image, err := image.FromString(container.Image)
		if err != nil {
			return nil, err
		}

		newContainer.image, err = NewImage(image, defaults, source)
		if err != nil {
			return nil, err
		}
		container.Image = "placeholder"
	}

	newContainer.container = container
	return &newContainer, nil
}
示例#2
0
func newDockerImage(t *testing.T, imageName string) *image.Image {
	simple, err := image.FromString(imageName)
	if err != nil {
		t.Fatalf("Could not create image.Image: %v", err)
	}
	return simple
}
示例#3
0
func TestPodAttachImage(t *testing.T) {
	podObjects := testRandomObjects(3)
	kubePod := testNewKubePod("containerless")
	pod, err := NewPod(kubePod, kube.ObjectMeta{}, "pod", podObjects...)
	assert.NoError(t, err, "valid")

	imageObjects := testRandomObjects(3)
	kubeImage, err := image.FromString("bprashanth/nginxhttps:1.0")
	assert.NoError(t, err, "image should be valid")

	image, err := NewImage(kubeImage, kube.ObjectMeta{}, "image", imageObjects...)
	assert.NoError(t, err, "valid")

	err = pod.Attach(image)
	assert.NoError(t, err, "should be attachable")

	kubePod.Namespace = kube.NamespaceDefault
	kubePod.Spec.Containers = []kube.Container{
		{
			Name:            "nginxhttps",
			Image:           "bprashanth/nginxhttps:1.0",
			ImagePullPolicy: kube.PullAlways,
		},
	}

	objects := append(podObjects, imageObjects...)

	expected := new(deploy.Deployment)
	err = expected.Add(kubePod)
	assert.NoError(t, err, "valid")

	for _, obj := range objects {
		assert.NoError(t, expected.Add(obj))
	}

	actual, err := pod.Deployment()
	assert.NoError(t, err, "deployment should be ok")
	testDeploymentEqual(t, expected, actual)
}
func TestRCAttachImage(t *testing.T) {
	imageName := "rc-attach-image"
	rcName := "image-test"
	selector := map[string]string{
		"app": "cache",
	}

	// create ReplicationController
	rcObjects := testRandomObjects(10)
	rc := testNewRC(t, rcName, selector, rcObjects)

	// create Image
	imageObjects := testRandomObjects(10)
	imageEntity := testNewImage(t, imageName, kube.ObjectMeta{}, "", imageObjects)

	// Attach image to RC
	// Should assume defaults up tree creating necessary components
	err := rc.Attach(imageEntity)
	assert.NoError(t, err, "attachment should be allowed")

	// Compare internal elements
	assert.NotNil(t, rc.pod, "should of created template")

	// Create struct representation for expected
	expectedRC := &kube.ReplicationController{
		ObjectMeta: kube.ObjectMeta{
			Name:      rcName,
			Namespace: kube.NamespaceDefault,
		},
		Spec: kube.ReplicationControllerSpec{
			Selector: selector,
			Template: &kube.PodTemplateSpec{
				ObjectMeta: kube.ObjectMeta{
					Namespace: kube.NamespaceDefault,
					Labels:    selector,
				},
				Spec: kube.PodSpec{
					Containers: []kube.Container{
						{
							Name:            imageName,
							Image:           imageName,
							ImagePullPolicy: kube.PullAlways,
						},
					},
					RestartPolicy: kube.RestartPolicyAlways,
					DNSPolicy:     kube.DNSDefault,
				},
			},
		},
	}

	// Insert into Deployment
	expected := new(deploy.Deployment)
	err = expected.Add(expectedRC)
	assert.NoError(t, err, "should be valid RC")

	// add objects to deployment
	expected.AddDeployment(imageEntity.objects)
	expected.AddDeployment(rc.objects)

	// Create Deployment from RC
	actual, err := rc.Deployment()
	assert.NoError(t, err, "should produce valid deployment")

	// Compare deployments
	testDeploymentEqual(t, expected, actual)

	// check images
	images := rc.Images()
	expectedImage, _ := image.FromString(imageName)
	assert.Len(t, images, 1)
	for _, v := range images {
		assert.EqualValues(t, expectedImage, v, "should match original image")
	}
}