Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand authored and ewrenn8 committed Jun 11, 2021
1 parent 2bac30d commit a2d8444
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 85 deletions.
82 changes: 8 additions & 74 deletions pkg/pkgrepository/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/fetch"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/reftracker"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/template"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sfake "k8s.io/client-go/kubernetes/fake"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -39,17 +38,16 @@ func Test_SecretRefs_RetrievesAllSecretRefs(t *testing.T) {
},
Spec: v1alpha1.AppSpec{
Fetch: []v1alpha1.AppFetch{
v1alpha1.AppFetch{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{SecretRef: &v1alpha1.AppFetchInlineSourceRef{"", v1.LocalObjectReference{Name: "s"}}}}}},
v1alpha1.AppFetch{Image: &v1alpha1.AppFetchImage{SecretRef: &v1alpha1.AppFetchLocalRef{v1.LocalObjectReference{Name: "s1"}}}},
v1alpha1.AppFetch{HTTP: &v1alpha1.AppFetchHTTP{SecretRef: &v1alpha1.AppFetchLocalRef{v1.LocalObjectReference{Name: "s2"}}}},
v1alpha1.AppFetch{Git: &v1alpha1.AppFetchGit{SecretRef: &v1alpha1.AppFetchLocalRef{v1.LocalObjectReference{Name: "s3"}}}},
v1alpha1.AppFetch{HelmChart: &v1alpha1.AppFetchHelmChart{Repository: &v1alpha1.AppFetchHelmChartRepo{SecretRef: &v1alpha1.AppFetchLocalRef{v1.LocalObjectReference{Name: "s4"}}}}},
v1alpha1.AppFetch{ImgpkgBundle: &v1alpha1.AppFetchImgpkgBundle{SecretRef: &v1alpha1.AppFetchLocalRef{v1.LocalObjectReference{Name: "s5"}}}},
v1alpha1.AppFetch{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{SecretRef: &v1alpha1.AppFetchInlineSourceRef{"", "s"}}}}},
v1alpha1.AppFetch{Image: &v1alpha1.AppFetchImage{SecretRef: &v1alpha1.AppFetchLocalRef{"s1"}}},
v1alpha1.AppFetch{HTTP: &v1alpha1.AppFetchHTTP{SecretRef: &v1alpha1.AppFetchLocalRef{"s2"}}},
v1alpha1.AppFetch{Git: &v1alpha1.AppFetchGit{SecretRef: &v1alpha1.AppFetchLocalRef{"s3"}}},
v1alpha1.AppFetch{HelmChart: &v1alpha1.AppFetchHelmChart{Repository: &v1alpha1.AppFetchHelmChartRepo{SecretRef: &v1alpha1.AppFetchLocalRef{"s4"}}}},
v1alpha1.AppFetch{ImgpkgBundle: &v1alpha1.AppFetchImgpkgBundle{SecretRef: &v1alpha1.AppFetchLocalRef{"s5"}}},
},
Template: []v1alpha1.AppTemplate{
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{SecretRef: &v1alpha1.AppFetchInlineSourceRef{"", v1.LocalObjectReference{Name: "s6"}}}}}}},
v1alpha1.AppTemplate{HelmTemplate: &v1alpha1.AppTemplateHelmTemplate{ValuesFrom: []v1alpha1.AppTemplateHelmTemplateValuesSource{{SecretRef: &v1alpha1.AppTemplateHelmTemplateValuesSourceRef{v1.LocalObjectReference{Name: "s7"}}}}}},
},
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{SecretRef: &v1alpha1.AppFetchInlineSourceRef{"", "s6"}}}}}},
v1alpha1.AppTemplate{HelmTemplate: &v1alpha1.AppTemplateHelmTemplate{ValuesFrom: []v1alpha1.AppTemplateValuesSource{{SecretRef: &v1alpha1.AppTemplateValuesSourceRef{"s7"}}}}}},
},
}

Expand Down Expand Up @@ -91,67 +89,3 @@ func Test_SecretRefs_RetrievesNoSecretRefs_WhenNonePresent(t *testing.T) {
t.Fatalf("\n Expected: %s\nGot: %s\n", "No SecretRefs to be returned", out)
}
}

func Test_ConfigMapRefs_RetrievesAllConfigMapRefs(t *testing.T) {
log := logf.Log.WithName("kc")

expected := map[reftracker.RefKey]struct{}{
reftracker.NewConfigMapKey("c", "default"): struct{}{},
reftracker.NewConfigMapKey("c1", "default"): struct{}{},
reftracker.NewConfigMapKey("c2", "default"): struct{}{},
}

appWithRefs := v1alpha1.App{
ObjectMeta: metav1.ObjectMeta{
Name: "simple-app",
Namespace: "default",
},
Spec: v1alpha1.AppSpec{
Fetch: []v1alpha1.AppFetch{
v1alpha1.AppFetch{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{ConfigMapRef: &v1alpha1.AppFetchInlineSourceRef{"", v1.LocalObjectReference{Name: "c"}}}}}},
},
Template: []v1alpha1.AppTemplate{
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{ConfigMapRef: &v1alpha1.AppFetchInlineSourceRef{"", v1.LocalObjectReference{Name: "c1"}}}}}}},
v1alpha1.AppTemplate{HelmTemplate: &v1alpha1.AppTemplateHelmTemplate{ValuesFrom: []v1alpha1.AppTemplateHelmTemplateValuesSource{{ConfigMapRef: &v1alpha1.AppTemplateHelmTemplateValuesSourceRef{v1.LocalObjectReference{Name: "c2"}}}}}},
},
},
}

k8scs := k8sfake.NewSimpleClientset()
fetchFac := fetch.NewFactory(k8scs, nil)
tmpFac := template.NewFactory(k8scs, fetchFac)
deployFac := deploy.NewFactory(k8scs)

app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log)

out := app.ConfigMapRefs()
if !reflect.DeepEqual(out, expected) {
t.Fatalf("\n Expected: %s\nGot: %s\n", expected, out)
}
}

func Test_ConfigMapRefs_RetrievesNoConfigMapRefs_WhenNonePresent(t *testing.T) {
log := logf.Log.WithName("kc")

appEmpty := v1alpha1.App{
ObjectMeta: metav1.ObjectMeta{
Name: "simple-app",
},
Spec: v1alpha1.AppSpec{
Fetch: []v1alpha1.AppFetch{},
Template: []v1alpha1.AppTemplate{},
},
}

k8scs := k8sfake.NewSimpleClientset()
fetchFac := fetch.NewFactory(k8scs, nil)
tmpFac := template.NewFactory(k8scs, fetchFac)
deployFac := deploy.NewFactory(k8scs)

app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log)

out := app.ConfigMapRefs()
if len(out) != 0 {
t.Fatalf("\n Expected: %s\nGot: %s\n", "No ConfigMapRefs to be returned", out)
}
}
68 changes: 57 additions & 11 deletions test/e2e/package_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// TODO: Right now the implementation of the package repo reconciler needs improvement
Expand All @@ -35,17 +36,22 @@ spec:
imgpkgBundle:
image: k8slt/i-dont-exist`

expectedStatus := kcv1alpha1.AppStatus {
expectedStatus := v1alpha1.PackageRepositoryStatus{
Fetch: &kcv1alpha1.AppStatusFetch{
ExitCode: 1,
Error: "Fetching resources: Error (see .status.usefulErrorMessage for details)",
},
GenericStatus: kcv1alpha1.GenericStatus{
Conditions: []kcv1alpha1.AppCondition{{
Type: kcv1alpha1.ReconcileFailed,
Status: corev1.ConditionTrue,
Message: "Syncing packages: (see .status.usefulErrorMessage for details)",
Message: "Fetching resources: Error (see .status.usefulErrorMessage for details)",
}},
ObservedGeneration: 1,
FriendlyDescription: "Reconcile failed: Syncing packages: (see .status.usefulErrorMessage for details)",
FriendlyDescription: "Reconcile failed: Fetching resources: Error (see .status.usefulErrorMessage for details)",
UsefulErrorMessage: "Error: Syncing directory '0': Syncing directory '.' with imgpkgBundle contents: Imgpkg: exit status 1 (stderr: Error: Checking if image is bundle: Collecting images: Working with index.docker.io/k8slt/i-dont-exist:latest: GET https://index.docker.io/v2/k8slt/i-dont-exist/manifests/latest: UNAUTHORIZED: authentication required; [map[Action:pull Class: Name:k8slt/i-dont-exist Type:repository]]\n)\n",
},
ConsecutiveReconcileFailures: 4,
}

cleanup := func() {
Expand All @@ -70,6 +76,8 @@ spec:
return fmt.Errorf("failed to unmarshal: %s", err)
}

cleanupStatusForAssertion(&cr)

// assert on expectedStatus
if !reflect.DeepEqual(expectedStatus, cr.Status) {
return fmt.Errorf("\nstatus is not same:\nExpected:\n%#v\nGot:\n%#v", expectedStatus, cr.Status)
Expand Down Expand Up @@ -107,7 +115,19 @@ spec:
RunOpts{StdinReader: strings.NewReader(repoYml)})
})

expectedStatus := kcv1alpha1.AppStatus{
expectedStatus := v1alpha1.PackageRepositoryStatus{
Fetch: &kcv1alpha1.AppStatusFetch{
ExitCode: 0,
},
Template: &kcv1alpha1.AppStatusTemplate{
ExitCode: 0,
},
Deploy: &kcv1alpha1.AppStatusDeploy{
ExitCode: 0,
Finished: true,
},
ConsecutiveReconcileSuccesses: 1,
ConsecutiveReconcileFailures: 0,
GenericStatus: kcv1alpha1.GenericStatus{
Conditions: []kcv1alpha1.AppCondition{{
Type: kcv1alpha1.ReconcileSucceeded,
Expand All @@ -130,6 +150,8 @@ spec:
return fmt.Errorf("failed to unmarshal: %s", err)
}

cleanupStatusForAssertion(&cr)

// assert on expectedStatus
if !reflect.DeepEqual(expectedStatus, cr.Status) {
return fmt.Errorf("\nstatus is not same:\nExpected:\n%#v\nGot:\n%#v", expectedStatus, cr.Status)
Expand Down Expand Up @@ -199,9 +221,9 @@ spec:
packageNames := []string{"pkg.test.carvel.dev.1.0.0", "pkg.test.carvel.dev.2.0.0"}

cleanUp := func() {
kctl.RunWithOpts([]string{"delete", "pkgr/basic.test.carvel.dev"}, RunOpts{NoNamespace: true, AllowError: true})
kctl.RunWithOpts([]string{"delete", "pkgr/basic.test.carvel.dev"}, RunOpts{AllowError: true})
for _, name := range packageNames {
kctl.RunWithOpts([]string{"delete", fmt.Sprintf("package/%s", name)}, RunOpts{NoNamespace: true, AllowError: true})
kctl.RunWithOpts([]string{"delete", fmt.Sprintf("package/%s", name)}, RunOpts{AllowError: true})
}
}
defer cleanUp()
Expand All @@ -218,12 +240,12 @@ spec:
return fmt.Errorf("Expected to find pkg pkg.test.carvel.dev, but couldn't: %v", err)
}

_, err = kctl.RunWithOpts([]string{"get", "packageversion/pkg.test.carvel.dev.1.0.0"}, RunOpts{AllowError: true, NoNamespace: true})
_, err = kctl.RunWithOpts([]string{"get", "packageversion/pkg.test.carvel.dev.1.0.0"}, RunOpts{AllowError: true})
if err != nil {
return fmt.Errorf("Expected to find package pkg.test.carvel.dev.1.0.0: %v", err)
}

_, err = kctl.RunWithOpts([]string{"get", "packageversion/pkg.test.carvel.dev.2.0.0"}, RunOpts{AllowError: true, NoNamespace: true})
_, err = kctl.RunWithOpts([]string{"get", "packageversion/pkg.test.carvel.dev.2.0.0"}, RunOpts{AllowError: true})
if err != nil {
return fmt.Errorf("Expected to find package pkg.test.carvel.dev.2.0.0: %v", err)
}
Expand All @@ -237,17 +259,17 @@ spec:

logger.Section("check packages are deleted too", func() {
retry(t, 10*time.Second, func() error {
_, err := kctl.RunWithOpts([]string{"get", "package/pkg.test.carvel.dev"}, RunOpts{NoNamespace: true, AllowError: true})
_, err := kctl.RunWithOpts([]string{"get", "package/pkg.test.carvel.dev"}, RunOpts{AllowError: true})
if err == nil || !strings.Contains(err.Error(), "\"pkg.test.carvel.dev\" not found") {
return fmt.Errorf("Expected not to find pkg pkg.test.carvel.dev, but did: %v", err)
}

_, err = kctl.RunWithOpts([]string{"get", "packageversion/pkg.test.carvel.dev.1.0.0"}, RunOpts{AllowError: true, NoNamespace: true})
_, err = kctl.RunWithOpts([]string{"get", "packageversion/pkg.test.carvel.dev.1.0.0"}, RunOpts{AllowError: true})
if err == nil || !strings.Contains(err.Error(), "\"pkg.test.carvel.dev.1.0.0\" not found") {
return fmt.Errorf("Expected not to find package pkg.test.carvel.dev.1.0.0, but did")
}

_, err = kctl.RunWithOpts([]string{"get", "packageversion/pkg.test.carvel.dev.2.0.0"}, RunOpts{AllowError: true, NoNamespace: true})
_, err = kctl.RunWithOpts([]string{"get", "packageversion/pkg.test.carvel.dev.2.0.0"}, RunOpts{AllowError: true})
if err == nil || !strings.Contains(err.Error(), "\"pkg.test.carvel.dev.2.0.0\" not found") {
return fmt.Errorf("Expected no to find package pkg.test.carvel.dev.2.0.0, but did")
}
Expand Down Expand Up @@ -309,3 +331,27 @@ func retry(t *testing.T, timeout time.Duration, f func() error) {
time.Sleep(1 * time.Second)
}
}

func cleanupStatusForAssertion(pkgr *v1alpha1.PackageRepository) {
// fetch
if pkgr.Status.Fetch != nil {
pkgr.Status.Fetch.StartedAt = metav1.Time{}
pkgr.Status.Fetch.UpdatedAt = metav1.Time{}
pkgr.Status.Fetch.Stdout = ""
pkgr.Status.Fetch.Stderr = ""
}

// template
if pkgr.Status.Template != nil {
pkgr.Status.Template.UpdatedAt = metav1.Time{}
pkgr.Status.Template.Stderr = ""
}

// deploy
if pkgr.Status.Deploy != nil {
pkgr.Status.Deploy.StartedAt = metav1.Time{}
pkgr.Status.Deploy.UpdatedAt = metav1.Time{}
pkgr.Status.Deploy.Stdout = ""
pkgr.Status.Deploy.Stderr = ""
}
}

0 comments on commit a2d8444

Please sign in to comment.