Skip to content

Commit

Permalink
chore: update controllers/extensions/addon_controller_test.go (apeclo…
Browse files Browse the repository at this point in the history
  • Loading branch information
nashtsai authored Jun 11, 2023
1 parent 86bb1b2 commit fe8a45a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apis/extensions/v1alpha1/addon_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func (r *Addon) GetExtraNames() []string {
}
switch r.Spec.Type {
case HelmType:
if r.Spec.Helm == nil {
if r.Spec.Helm == nil || len(r.Spec.Helm.ValuesMapping.ExtraItems) == 0 {
return nil
}
// r.Spec.DefaultInstallValues has minItem=1 constraint
Expand Down
41 changes: 41 additions & 0 deletions apis/extensions/v1alpha1/addon_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func TestHelmInstallSpecBuildMergedValues(t *testing.T) {
InstallValues: HelmInstallValues{
SetValues: []string{},
SetJSONValues: []string{},
URLs: []string{"https://kubeblock.io/test"},
},
InstallOptions: HelmInstallOptions{
"--atomic": "true",
},
ValuesMapping: HelmValuesMapping{
HelmValuesMappingItem: buildHelmValuesMappingItem("primary"),
Expand Down Expand Up @@ -340,6 +344,9 @@ func TestHelmInstallSpecBuildMergedValues(t *testing.T) {
mergedValues = helmValues.BuildMergedValues(&installSpec)
g.Expect(fmt.Sprintf("%s=null",
mappingName("primary", sc))).Should(BeElementOf(mergedValues.SetValues))

helmContainer := corev1.Container{}
g.Expect(helmValues.BuildContainerArgs(&helmContainer, mergedValues)).ShouldNot(HaveOccurred())
}

func TestAddonMisc(t *testing.T) {
Expand All @@ -350,6 +357,27 @@ func TestAddonMisc(t *testing.T) {
g.Expect(addon.Spec.InstallSpec.GetEnabled()).Should(BeFalse())
g.Expect(addon.Spec.Helm.BuildMergedValues(nil)).Should(BeEquivalentTo(HelmInstallValues{}))

addon.Spec.Type = HelmType
g.Expect(addon.GetExtraNames()).Should(BeEmpty())

addon.Spec.Helm = &HelmTypeInstallSpec{}
g.Expect(addon.GetExtraNames()).Should(BeEmpty())

addon.Spec.Helm = &HelmTypeInstallSpec{
ValuesMapping: HelmValuesMapping{
ExtraItems: []HelmValuesMappingExtraItem{
{
Name: "extra",
},
},
},
}
en := addon.GetExtraNames()
g.Expect(en).ShouldNot(BeEmpty())
for i, n := range addon.Spec.Helm.ValuesMapping.ExtraItems {
g.Expect(en[i]).Should(Equal(n.Name))
}

addon.Spec.InstallSpec = &AddonInstallSpec{
Enabled: true,
AddonInstallSpecItem: NewAddonInstallSpecItem(),
Expand All @@ -376,8 +404,21 @@ func TestAddonMisc(t *testing.T) {
},
}

for _, di := range addon.Spec.DefaultInstallValues {
ss := di.GetSelectorsStrings()
if len(di.Selectors) > 0 {
g.Expect(ss[0]).ShouldNot(BeEmpty())
} else {
g.Expect(ss).Should(BeEmpty())
}
}

di := addon.Spec.GetSortedDefaultInstallValues()
g.Expect(di).Should(HaveLen(2))

addon.Spec.Installable = &InstallableSpec{}
ss := addon.Spec.Installable.GetSelectorsStrings()
g.Expect(ss).Should(BeEmpty())
}

func TestAddonInstallHasSetValues(t *testing.T) {
Expand Down
32 changes: 4 additions & 28 deletions controllers/extensions/addon_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ var _ = Describe("Addon controller", func() {
client.HasLabels{
constant.AddonNameLabelKey,
})
testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, intctrlutil.JobSignature, true, inNS,
client.HasLabels{
constant.AppManagedByLabelKey,
})

// delete rest mocked objects
testapps.ClearResources(&testCtx, intctrlutil.ConfigMapSignature, inNS, ml)
Expand Down Expand Up @@ -610,34 +614,6 @@ var _ = Describe("Addon controller", func() {
addonStatusPhaseCheck(2, extensionsv1alpha1.AddonFailed, nil)
})
})
})

var _ = Describe("Addon controller manager", func() {

cleanEnv := func() {
// must wait till resources deleted and no longer existed before the testcases start,
// otherwise if later it needs to create some new resource objects with the same name,
// in race conditions, it will find the existence of old objects, resulting failure to
// create the new objects.
By("clean resources")
// non-namespaced
ml := client.HasLabels{testCtx.TestObjLabelKey}
testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, intctrlutil.AddonSignature, true, ml)

inNS := client.InNamespace(viper.GetString(constant.CfgKeyCtrlrMgrNS))
testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, intctrlutil.JobSignature, true, inNS,
client.HasLabels{
constant.AddonNameLabelKey,
})
testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, intctrlutil.JobSignature, true, inNS,
client.HasLabels{
constant.AppManagedByLabelKey,
})
}

BeforeEach(func() {
cleanEnv()
})

Context("Addon controller SetupWithManager", func() {
It("Do controller SetupWithManager init. flow", func() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ require (
google.golang.org/protobuf v1.28.1
gopkg.in/inf.v0 v0.9.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.11.1
k8s.io/api v0.26.1
k8s.io/apiextensions-apiserver v0.26.1
Expand Down Expand Up @@ -375,6 +374,7 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6 // indirect
k8s.io/apiserver v0.26.1 // indirect
k8s.io/component-helpers v0.26.0 // indirect
Expand Down

0 comments on commit fe8a45a

Please sign in to comment.