Beispiel #1
0
func TestInputRCAndPod(t *testing.T) {
	input := testTempFileInput(t)
	defer os.RemoveAll(input.Path())

	// setup pod
	kubePod := testKubePod()

	podFile := fmt.Sprintf("test.%s.%s", PodExtension, testRandomExtension())
	podPath := path.Join(input.Path(), podFile)
	testWriteYAMLToFile(t, podPath, kubePod)
	testClearTypeInfo(kubePod)
	pod, err := entity.NewPod(kubePod, kube.ObjectMeta{}, podPath)
	assert.NoError(t, err)

	// setup rc
	objects := testWriteRandomObjects(t, input.Path(), 5)
	kubeRC := &kube.ReplicationController{
		ObjectMeta: kube.ObjectMeta{
			Name:      "spread",
			Namespace: kube.NamespaceDefault,
		},
		TypeMeta: unversioned.TypeMeta{
			Kind:       "ReplicationController",
			APIVersion: "v1",
		},
		Spec: kube.ReplicationControllerSpec{
			Selector: map[string]string{"valid": "selector"},
			Template: &kube.PodTemplateSpec{
				Spec: kubePod.Spec,
			},
		},
	}

	rcFile := fmt.Sprintf("test.%s.%s", RCExtension, testRandomExtension())
	rcPath := path.Join(input.Path(), rcFile)
	testWriteYAMLToFile(t, rcPath, kubeRC)
	testClearTypeInfo(kubeRC)
	rc, err := entity.NewReplicationController(kubeRC, kube.ObjectMeta{}, rcPath)
	assert.NoError(t, err)

	expected, err := entity.NewApp(nil, kube.ObjectMeta{}, input.Path(), objects...)
	assert.NoError(t, err)

	// attach rc and pod
	err = expected.Attach(rc)
	assert.NoError(t, err)
	err = expected.Attach(pod)
	assert.NoError(t, err)

	// generate entity
	actual, err := input.Build()
	assert.NoError(t, err, "should have built entity successfully")

	testCompareEntity(t, expected, actual)
}
Beispiel #2
0
// rcs returns entities for the rcs in the RCFile
func (fs FileSource) rcs(objects []deploy.KubeObject) (rcs []entity.Entity, err error) {
	pattern := fmt.Sprintf("%s/*%s", fs, RCExtension)

	patternExts := withExtensions(pattern)
	paths, err := multiGlob(patternExts...)
	if err != nil {
		return rcs, err
	}

	for _, filePath := range paths {
		err = walkPathForObjects(filePath, func(info *resource.Info, err error) error {
			kubeRC, ok := info.Object.(*kube.ReplicationController)
			if !ok {
				return fmt.Errorf("expected type `ReplicationController` but found `%s`", info.Object.GetObjectKind().GroupVersionKind().Kind)
			}

			rc, err := entity.NewReplicationController(kubeRC, kube.ObjectMeta{}, info.Source, objects...)
			if err != nil {
				return err
			}

			rcs = append(rcs, rc)
			return nil
		})

		if checkErrPathDoesNotExist(err) {
			// it's okay if directory doesn't exit
			err = nil
		}

		if err != nil {
			return
		}
	}
	return
}
Beispiel #3
0
func TestSourceRCs(t *testing.T) {
	fs := testTempFileSource(t)
	defer os.RemoveAll(string(fs))

	rcFile := path.Join(string(fs), "test.rc.json")

	selector := map[string]string{"app": "example"}

	terminationPeriod := int64(30)
	kubeRC := &kube.ReplicationController{
		ObjectMeta: kube.ObjectMeta{
			Name:      "example-rc",
			Namespace: kube.NamespaceDefault,
		},
		TypeMeta: unversioned.TypeMeta{
			Kind:       "ReplicationController",
			APIVersion: "v1",
		},
		Spec: kube.ReplicationControllerSpec{
			Selector: selector,
			Replicas: 2,
			Template: &kube.PodTemplateSpec{
				ObjectMeta: kube.ObjectMeta{
					Labels: selector,
				},
				Spec: kube.PodSpec{
					Containers: []kube.Container{
						{
							Name:                   "example",
							Image:                  "hello-world",
							ImagePullPolicy:        kube.PullAlways,
							TerminationMessagePath: kube.TerminationMessagePathDefault,
						},
						{
							Name:                   "cache",
							Image:                  "redis",
							ImagePullPolicy:        kube.PullAlways,
							TerminationMessagePath: kube.TerminationMessagePathDefault,
						},
					},
					SecurityContext:               &kube.PodSecurityContext{},
					RestartPolicy:                 kube.RestartPolicyAlways,
					DNSPolicy:                     kube.DNSDefault,
					TerminationGracePeriodSeconds: &terminationPeriod,
				},
			},
		},
	}

	testWriteYAMLToFile(t, rcFile, kubeRC)

	rcs, err := fs.Entities(entity.EntityReplicationController)
	assert.NoError(t, err, "should be okay")
	assert.Len(t, rcs, 1, "should have single rc")

	expectedKubeRC := kubeRC
	testClearTypeInfo(expectedKubeRC)
	expectedKubeRC.Labels = selector
	expected, err := entity.NewReplicationController(expectedKubeRC, kube.ObjectMeta{}, rcFile)

	actual := rcs[0]

	testCompareEntity(t, expected, actual)
}