Skip to content

Commit 62b5196

Browse files
committed
chore: Add tests for config and app
Signed-off-by: imusmanmalik <[email protected]>
1 parent dc4cf0f commit 62b5196

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

pkg/config/config_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ func Test_NewConfig_ReturnsSecret_WhenBothConfigMapAndSecretExist(t *testing.T)
4545
}, config.ProxyOpts())
4646
}
4747

48+
func Test_NewConfig_PackageInstallDefaultSyncPeriod(t *testing.T) {
49+
t.Run("with empty config value, returns 10m", func(t *testing.T) {
50+
secret := &v1.Secret{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Name: "kapp-controller-config",
53+
Namespace: "default",
54+
},
55+
Data: map[string][]byte{},
56+
}
57+
config, err := kcconfig.NewConfig(k8sfake.NewSimpleClientset(secret))
58+
assert.NoError(t, err)
59+
assert.Equal(t, 10*time.Minute, config.PackageInstallDefaultSyncPeriod())
60+
})
61+
62+
t.Run("with value, returns 80s", func(t *testing.T) {
63+
secret := &v1.Secret{
64+
ObjectMeta: metav1.ObjectMeta{
65+
Name: "kapp-controller-config",
66+
Namespace: "default",
67+
},
68+
Data: map[string][]byte{
69+
"packageInstallDefaultSyncPeriod": []byte("80s"),
70+
},
71+
}
72+
config, err := kcconfig.NewConfig(k8sfake.NewSimpleClientset(secret))
73+
assert.NoError(t, err)
74+
assert.Equal(t, 80*time.Second, config.PackageInstallDefaultSyncPeriod())
75+
})
76+
}
77+
4878
func Test_NewConfig_AppDefaultSyncPeriod(t *testing.T) {
4979
t.Run("with empty config value, returns 30s", func(t *testing.T) {
5080
secret := &v1.Secret{

pkg/packageinstall/app_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,78 @@ func TestAppCustomFetchSecretNames(t *testing.T) {
563563
require.Equal(t, expectedApp, app, "App does not match expected app")
564564
}
565565

566+
// TestAppPackageIntallDefaultSyncPeriod tests the creation of an App and expects syncPeriod is set to the default value.
567+
func TestAppPackageIntallDefaultSyncPeriod(t *testing.T) {
568+
ipkg := &pkgingv1alpha1.PackageInstall{
569+
ObjectMeta: metav1.ObjectMeta{
570+
Name: "app",
571+
Namespace: "default",
572+
},
573+
}
574+
575+
pkgVersion := datapkgingv1alpha1.Package{
576+
Spec: datapkgingv1alpha1.PackageSpec{
577+
RefName: "expec-pkg",
578+
Version: "1.5.0",
579+
Template: datapkgingv1alpha1.AppTemplateSpec{
580+
Spec: &kcv1alpha1.AppSpec{},
581+
},
582+
},
583+
}
584+
585+
app, err := packageinstall.NewApp(&kcv1alpha1.App{}, ipkg, pkgVersion, packageinstall.Opts{})
586+
require.NoError(t, err)
587+
588+
// Define the expected app object, with the sync period attribute set to the default value
589+
expectedApp := &kcv1alpha1.App{
590+
Spec: kcv1alpha1.AppSpec{
591+
SyncPeriod: packageinstall.DefaultSyncPeriod,
592+
},
593+
}
594+
595+
// Not interesting in metadata in this test
596+
app.ObjectMeta = metav1.ObjectMeta{}
597+
598+
require.Equal(t, expectedApp, app, "App does not match expected app")
599+
}
600+
601+
// TestAppCustomPackageIntallSyncPeriod tests the creation of an App when using PackageInstall with a defined syncPeriod.
602+
func TestAppCustomPackageIntallSyncPeriod(t *testing.T) {
603+
ipkg := &pkgingv1alpha1.PackageInstall{
604+
ObjectMeta: metav1.ObjectMeta{
605+
Name: "app",
606+
Namespace: "default",
607+
},
608+
Spec: pkgingv1alpha1.PackageInstallSpec{
609+
SyncPeriod: &metav1.Duration{Duration: 100 * time.Second},
610+
},
611+
}
612+
613+
pkgVersion := datapkgingv1alpha1.Package{
614+
Spec: datapkgingv1alpha1.PackageSpec{
615+
RefName: "expec-pkg",
616+
Version: "1.5.0",
617+
Template: datapkgingv1alpha1.AppTemplateSpec{
618+
Spec: &kcv1alpha1.AppSpec{},
619+
},
620+
},
621+
}
622+
623+
app, err := packageinstall.NewApp(&kcv1alpha1.App{}, ipkg, pkgVersion, packageinstall.Opts{})
624+
require.NoError(t, err)
625+
626+
expectedApp := &kcv1alpha1.App{
627+
Spec: kcv1alpha1.AppSpec{
628+
SyncPeriod: &metav1.Duration{Duration: 100 * time.Second},
629+
},
630+
}
631+
632+
// Not interesting in metadata in this test
633+
app.ObjectMeta = metav1.ObjectMeta{}
634+
635+
require.Equal(t, expectedApp, app, "App does not match expected app")
636+
}
637+
566638
func TestAppPackageDetailsAnnotations(t *testing.T) {
567639
ipkg := &pkgingv1alpha1.PackageInstall{
568640
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)