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) }
func TestSourcePods(t *testing.T) { fs := testTempFileSource(t) defer os.RemoveAll(string(fs)) podFile := path.Join(string(fs), "test.pod.yaml") terminationPeriod := int64(30) kubePod := &kube.Pod{ ObjectMeta: kube.ObjectMeta{ Name: "example-pod", Namespace: kube.NamespaceDefault, }, TypeMeta: unversioned.TypeMeta{ Kind: "Pod", APIVersion: "v1", }, Spec: kube.PodSpec{ Containers: []kube.Container{ { Name: "wiki", Image: "mediawiki", ImagePullPolicy: kube.PullAlways, TerminationMessagePath: kube.TerminationMessagePathDefault, }, { Name: "db", Image: "postgres", ImagePullPolicy: kube.PullAlways, TerminationMessagePath: kube.TerminationMessagePathDefault, }, }, SecurityContext: &kube.PodSecurityContext{}, RestartPolicy: kube.RestartPolicyAlways, DNSPolicy: kube.DNSDefault, TerminationGracePeriodSeconds: &terminationPeriod, }, } testWriteYAMLToFile(t, podFile, kubePod) pods, err := fs.Entities(entity.EntityPod) assert.NoError(t, err, "should be okay") assert.Len(t, pods, 1, "should have single pod") expectedKubePod := kubePod testClearTypeInfo(expectedKubePod) expected, err := entity.NewPod(expectedKubePod, kube.ObjectMeta{}, podFile) actual := pods[0] testCompareEntity(t, expected, actual) }
func TestInputPod(t *testing.T) { input := testTempFileInput(t) defer os.RemoveAll(input.Path()) kubePod := testKubePod() podFile := fmt.Sprintf("test.%s.%s", PodExtension, testRandomExtension()) podPath := path.Join(input.Path(), podFile) testWriteYAMLToFile(t, podPath, kubePod) testClearTypeInfo(kubePod) objects := testWriteRandomObjects(t, input.Path(), 5) expectedPod, err := entity.NewPod(kubePod, kube.ObjectMeta{}, podPath) assert.NoError(t, err) expected, err := entity.NewApp([]entity.Entity{expectedPod}, kube.ObjectMeta{}, input.Path(), objects...) assert.NoError(t, err) actual, err := input.Build() assert.NoError(t, err, "should have built entity successfully") testCompareEntity(t, expected, actual) }
// pods returns Pods for the rcs in the PodFile func (fs FileSource) pods(objects []deploy.KubeObject) (pods []entity.Entity, err error) { pattern := fmt.Sprintf("%s/*%s", fs, PodExtension) patternExts := withExtensions(pattern) paths, err := multiGlob(patternExts...) if err != nil { return pods, err } for _, filePath := range paths { err = walkPathForObjects(filePath, func(info *resource.Info, err error) error { kubePod, ok := info.Object.(*kube.Pod) if !ok { return fmt.Errorf("expected type `Pod` but found `%s`", info.Object.GetObjectKind().GroupVersionKind().Kind) } pod, err := entity.NewPod(kubePod, kube.ObjectMeta{}, filePath, objects...) if err != nil { return err } pods = append(pods, pod) return nil }) if checkErrPathDoesNotExist(err) { // it's okay if directory doesn't exist err = nil } if err != nil { return } } return }