Test: add tests for outputs from vela addon list

when an addon with the same as registry one is locally installed

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
This commit is contained in:
Charlie Chiang
2022-05-09 13:47:41 +00:00
parent 0c1e347106
commit e6b3bb024c
3 changed files with 123 additions and 10 deletions
+8 -9
View File
@@ -31,7 +31,6 @@ import (
"helm.sh/helm/v3/pkg/strvals"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/pkg/oam"
"k8s.io/client-go/rest"
@@ -109,10 +108,11 @@ func NewAddonListCommand(c common.Args) *cobra.Command {
if err != nil {
return err
}
err = listAddons(context.Background(), k8sClient, "")
table, err := listAddons(context.Background(), k8sClient, "")
if err != nil {
return err
}
fmt.Println(table.String())
return nil
},
}
@@ -443,13 +443,13 @@ func generateAddonInfo(name string, status pkgaddon.Status) string {
return res
}
func listAddons(ctx context.Context, clt client.Client, registry string) error {
func listAddons(ctx context.Context, clt client.Client, registry string) (*uitable.Table, error) {
var addons []*pkgaddon.UIData
var err error
registryDS := pkgaddon.NewRegistryDataStore(clt)
registries, err := registryDS.ListRegistries(ctx)
if err != nil {
return err
return nil, err
}
for _, r := range registries {
@@ -482,9 +482,9 @@ func listAddons(ctx context.Context, clt client.Client, registry string) error {
// get locally installed addons first
locallyInstalledAddons := map[string]bool{}
appList := v1alpha2.ApplicationList{}
appList := v1beta1.ApplicationList{}
if err := clt.List(ctx, &appList, client.MatchingLabels{oam.LabelAddonRegistry: pkgaddon.LocalAddonRegistryName}); err != nil {
return err
return table, err
}
for _, app := range appList.Items {
labels := app.GetLabels()
@@ -502,7 +502,7 @@ func listAddons(ctx context.Context, clt client.Client, registry string) error {
}
status, err := pkgaddon.GetAddonStatus(ctx, clt, addon.Name)
if err != nil {
return err
return table, err
}
statusRow := status.AddonPhase
if len(status.InstalledVersion) != 0 {
@@ -511,8 +511,7 @@ func listAddons(ctx context.Context, clt client.Client, registry string) error {
table.AddRow(addon.Name, addon.RegistryName, addon.Description, genAvailableVersionInfo(addon.AvailableVersions, status.InstalledVersion), statusRow)
}
fmt.Println(table.String())
return nil
return table, nil
}
func waitApplicationRunning(k8sClient client.Client, addonName string) error {
+112
View File
@@ -0,0 +1,112 @@
package cli
import (
"context"
"fmt"
"time"
"sigs.k8s.io/yaml"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/oam/util"
"github.com/fatih/color"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pkgaddon "github.com/oam-dev/kubevela/pkg/addon"
"github.com/gosuri/uitable"
)
var _ = Describe("Output of listing addons tests", func() {
// Output of function listAddons to test
var actualTable *uitable.Table
// getRowsByName extracts every rows with its NAME matching name
getRowsByName := func(name string) []*uitable.Row {
matchedRows := []*uitable.Row{}
for _, row := range actualTable.Rows {
// Check column NAME(0) = name
if row.Cells[0].Data == name {
matchedRows = append(matchedRows, row)
}
}
return matchedRows
}
BeforeEach(func() {
// Prepare KubeVela registry
reg := &pkgaddon.Registry{
Name: "KubeVela",
Helm: &pkgaddon.HelmSource{
URL: "https://addons.kubevela.net",
},
}
ds := pkgaddon.NewRegistryDataStore(k8sClient)
Expect(ds.AddRegistry(context.Background(), *reg)).To(Succeed())
})
JustBeforeEach(func() {
// Print addon list to table for later comparison
ret, err := listAddons(context.Background(), k8sClient, "")
Expect(err).Should(BeNil())
actualTable = ret
})
When("there is no addons installed", func() {
It("should not have any enabled addon", func() {
Expect(len(actualTable.Rows) > 1).To(BeTrue())
for idx, row := range actualTable.Rows {
// Skip header
if idx == 0 {
continue
}
// Check column STATUS(4) = disabled
Expect(row.Cells[4].Data).To(Equal("disabled"))
}
})
})
When("there is locally installed addons", func() {
BeforeEach(func() {
// Install fluxcd locally
fluxcd := v1beta1.Application{}
err := yaml.Unmarshal([]byte(fluxcdYaml), &fluxcd)
Expect(err).Should(BeNil())
Expect(k8sClient.Create(context.Background(), &fluxcd)).Should(SatisfyAny(BeNil(), util.AlreadyExistMatcher{}))
})
It("should print fluxcd addon as local", func() {
matchedRows := getRowsByName("fluxcd")
Expect(len(matchedRows) > 0).To(BeTrue())
// Only use first row (local first), check column REGISTRY(1) = local
Expect(matchedRows[0].Cells[1].Data).To(Equal("local"))
Eventually(func() error {
matchedRows = getRowsByName("fluxcd")
// Check column STATUS(4) = enabled
if matchedRows[0].Cells[4].Data != "enabled" {
return fmt.Errorf("fluxcd is not enabled yet")
}
// Check column AVAILABLE-VERSIONS(3) = 1.1.0
if versionString := matchedRows[0].Cells[3].Data; versionString != fmt.Sprintf("[%s]", color.New(color.Bold, color.FgGreen).Sprintf("1.1.0")) {
return fmt.Errorf("fluxcd version string is incorrect: %s", versionString)
}
return nil
}, 30*time.Second, 300*time.Millisecond).Should(BeNil())
})
It("should print fluxcd in the registry as disabled", func() {
matchedRows := getRowsByName("fluxcd")
for idx, row := range matchedRows {
// Skip local addon
if idx == 0 {
continue
}
Expect(row.Cells[1].Data).To(Equal("KubeVela"))
Expect(row.Cells[4].Data).To(Equal("disabled"))
}
})
})
})
+3 -1
View File
@@ -70,7 +70,9 @@ metadata:
name: addon-fluxcd
namespace: vela-system
labels:
addons.oam.dev/name: fluxcd
addons.oam.dev/name: fluxcd
addons.oam.dev/registry: local
addons.oam.dev/version: 1.1.0
spec:
components:
- name: ns-flux-system