Skip to content

Commit

Permalink
Delete Eventually and add Gomega custom matchers
Browse files Browse the repository at this point in the history
Eventually is not needed when testing for resources in bundles as we get
the bundle from `cli.GetBundleListFromOutput` and the buffer read will be
in its final state once `cli.GetBundleListFromOutput` returns.

Also addind custom Gomega matchers to avoid using helper functions and
to get the exact line when any test assertion fails.

Signed-off-by: Xavi Garcia <[email protected]>
  • Loading branch information
0xavi0 committed Feb 16, 2024
1 parent 6a471d4 commit 57f45d9
Showing 1 changed file with 160 additions and 157 deletions.
317 changes: 160 additions & 157 deletions integrationtests/cli/apply/apply_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package apply

import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gcustom"
"github.com/onsi/gomega/types"
cp "github.com/otiai10/copy"

"github.com/rancher/fleet/integrationtests/cli"
Expand Down Expand Up @@ -280,24 +283,21 @@ var _ = Describe("Fleet apply with helm charts with dependencies", Ordered, func
})

It("creates a Bundle with all the resources, including the dependencies", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// files expected are:
// Chart.yaml + values.yaml + templates/configmap.yaml +
// Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(bundle.Spec.Resources)).To(Equal(5))
files, err := getAllFilesInDir(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(bundle.Spec.Resources)))
for _, file := range files {
presentInBundleResources(file, bundle.Spec.Resources)
}
// explicitly check for dependency files
presentInBundleResources(path.Join(tmpDir, "Chart.lock"), bundle.Spec.Resources)
presentInBundleResources(path.Join(tmpDir, "charts/config-chart-0.1.0.tgz"), bundle.Spec.Resources)
return true
}).Should(BeTrue())
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// files expected are:
// Chart.yaml + values.yaml + templates/configmap.yaml +
// Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(bundle.Spec.Resources)).To(Equal(5))
files, err := getAllFilesInDir(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(bundle.Spec.Resources)))
for _, file := range files {
Expect(file).Should(bePresentInBundleResources(bundle.Spec.Resources))
}
// explicitly check for dependency files
Expect(path.Join(tmpDir, "Chart.lock")).Should(bePresentInBundleResources(bundle.Spec.Resources))
Expect(path.Join(tmpDir, "charts/config-chart-0.1.0.tgz")).Should(bePresentInBundleResources(bundle.Spec.Resources))
})
})

Expand All @@ -307,24 +307,21 @@ var _ = Describe("Fleet apply with helm charts with dependencies", Ordered, func
})

It("creates a Bundle with all the resources, including the dependencies", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// files expected are:
// Chart.yaml + values.yaml + templates/configmap.yaml + fleet.yaml +
// Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(bundle.Spec.Resources)).To(Equal(6))
files, err := getAllFilesInDir(tmpDirRel)
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(bundle.Spec.Resources)))
for _, file := range files {
presentInBundleResources(file, bundle.Spec.Resources)
}
// explicitly check for dependency files
presentInBundleResources(path.Join(tmpDirRel, "Chart.lock"), bundle.Spec.Resources)
presentInBundleResources(path.Join(tmpDirRel, "charts/config-chart-0.1.0.tgz"), bundle.Spec.Resources)
return true
}).Should(BeTrue())
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// files expected are:
// Chart.yaml + values.yaml + templates/configmap.yaml + fleet.yaml +
// Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(bundle.Spec.Resources)).To(Equal(6))
files, err := getAllFilesInDir(tmpDirRel)
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(bundle.Spec.Resources)))
for _, file := range files {
Expect(file).Should(bePresentInBundleResources(bundle.Spec.Resources))
}
// explicitly check for dependency files
Expect(path.Join(tmpDirRel, "Chart.lock")).Should(bePresentInBundleResources(bundle.Spec.Resources))
Expect(path.Join(tmpDirRel, "charts/config-chart-0.1.0.tgz")).Should(bePresentInBundleResources(bundle.Spec.Resources))
})
})

Expand All @@ -334,23 +331,22 @@ var _ = Describe("Fleet apply with helm charts with dependencies", Ordered, func
})

It("creates a Bundle with all the resources, dependencies should not be in the bundle", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// files expected are:
// Chart.yaml + values.yaml + templates/configmap.yaml + fleet.yaml
Expect(len(bundle.Spec.Resources)).To(Equal(4))
files, err := getAllFilesInDir(tmpDirRel)
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(bundle.Spec.Resources)))
for _, file := range files {
presentInBundleResources(file, bundle.Spec.Resources)
}
// explicitly check for dependency files (they should not exist)
notPresentInBundleResources(path.Join(tmpDirRel, "Chart.lock"), bundle.Spec.Resources)
notPresentInBundleResources(path.Join(tmpDirRel, "charts/config-chart-0.1.0.tgz"), bundle.Spec.Resources)
return true
}).Should(BeTrue())
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// files expected are:
// Chart.yaml + values.yaml + templates/configmap.yaml + fleet.yaml
Expect(len(bundle.Spec.Resources)).To(Equal(4))
files, err := getAllFilesInDir(tmpDirRel)
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(bundle.Spec.Resources)))
for _, file := range files {
Expect(file).Should(bePresentInBundleResources(bundle.Spec.Resources))
}
// explicitly check for dependency files (they should not exist in the file system nor in bundle resources)
Expect(path.Join(tmpDirRel, "Chart.lock")).ShouldNot(BeAnExistingFile())
Expect(path.Join(tmpDirRel, "Chart.lock")).ShouldNot(bePresentOnlyInBundleResources(bundle.Spec.Resources))
Expect(path.Join(tmpDirRel, "charts/config-chart-0.1.0.tgz")).ShouldNot(BeAnExistingFile())
Expect(path.Join(tmpDirRel, "charts/config-chart-0.1.0.tgz")).ShouldNot(bePresentOnlyInBundleResources(bundle.Spec.Resources))
})
})

Expand All @@ -360,24 +356,21 @@ var _ = Describe("Fleet apply with helm charts with dependencies", Ordered, func
})

It("creates a Bundle with all the resources, dependencies should be in the bundle", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// expected files are:
// fleet.yaml + Chart.yaml + values.yaml + templates/configmap.yaml +
// Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(bundle.Spec.Resources)).To(Equal(6))
presentInBundleResources(path.Join(tmpDirRel, "fleet.yaml"), bundle.Spec.Resources)
// as files were unpacked from the downloaded chart we can't just
// list the files in the original folder and compare.
// Files are only located in the bundle resources
onlyPresentInBundleResources("Chart.yaml", bundle.Spec.Resources)
onlyPresentInBundleResources("values.yaml", bundle.Spec.Resources)
onlyPresentInBundleResources("templates/configmap.yaml", bundle.Spec.Resources)
onlyPresentInBundleResources("Chart.lock", bundle.Spec.Resources)
onlyPresentInBundleResources("charts/config-chart-0.1.0.tgz", bundle.Spec.Resources)
return true
}).Should(BeTrue())
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// expected files are:
// fleet.yaml + Chart.yaml + values.yaml + templates/configmap.yaml +
// Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(bundle.Spec.Resources)).To(Equal(6))
Expect(path.Join(tmpDirRel, "fleet.yaml")).Should(bePresentInBundleResources(bundle.Spec.Resources))
// as files were unpacked from the downloaded chart we can't just
// list the files in the original folder and compare.
// Files are only located in the bundle resources
Expect("Chart.yaml").Should(bePresentOnlyInBundleResources(bundle.Spec.Resources))
Expect("values.yaml").Should(bePresentOnlyInBundleResources(bundle.Spec.Resources))
Expect("templates/configmap.yaml").Should(bePresentOnlyInBundleResources(bundle.Spec.Resources))
Expect("Chart.lock").Should(bePresentOnlyInBundleResources(bundle.Spec.Resources))
Expect("charts/config-chart-0.1.0.tgz").Should(bePresentOnlyInBundleResources(bundle.Spec.Resources))
})
})

Expand All @@ -387,22 +380,25 @@ var _ = Describe("Fleet apply with helm charts with dependencies", Ordered, func
})

It("creates a Bundle with all the resources, dependencies should not be in the bundle", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// expected files are:
// fleet.yaml +
// Chart.yaml + values.yaml + templates/configmap.yaml
Expect(len(bundle.Spec.Resources)).To(Equal(4))
presentInBundleResources(path.Join(tmpDirRel, "fleet.yaml"), bundle.Spec.Resources)
// as files were unpacked from the downloaded chart we can't just
// list the files in the original folder and compare.
// Files are only located in the bundle resources
onlyPresentInBundleResources("Chart.yaml", bundle.Spec.Resources)
onlyPresentInBundleResources("values.yaml", bundle.Spec.Resources)
onlyPresentInBundleResources("templates/configmap.yaml", bundle.Spec.Resources)
return true
}).Should(BeTrue())
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
// expected files are:
// fleet.yaml +
// Chart.yaml + values.yaml + templates/configmap.yaml
Expect(len(bundle.Spec.Resources)).To(Equal(4))
Expect(path.Join(tmpDirRel, "fleet.yaml")).Should(bePresentInBundleResources(bundle.Spec.Resources))
// as files were unpacked from the downloaded chart we can't just
// list the files in the original folder and compare.
// Files are only located in the bundle resources
Expect("Chart.yaml").Should(bePresentOnlyInBundleResources(bundle.Spec.Resources))
Expect("values.yaml").Should(bePresentOnlyInBundleResources(bundle.Spec.Resources))
Expect("templates/configmap.yaml").Should(bePresentOnlyInBundleResources(bundle.Spec.Resources))

// explicitly check for dependency files (they should not exist in the file system nor in bundle resources)
Expect(path.Join(tmpDirRel, "Chart.lock")).ShouldNot(BeAnExistingFile())
Expect(path.Join(tmpDirRel, "Chart.lock")).ShouldNot(bePresentOnlyInBundleResources(bundle.Spec.Resources))
Expect(path.Join(tmpDirRel, "charts/config-chart-0.1.0.tgz")).ShouldNot(BeAnExistingFile())
Expect(path.Join(tmpDirRel, "charts/config-chart-0.1.0.tgz")).ShouldNot(bePresentOnlyInBundleResources(bundle.Spec.Resources))
})
})

Expand All @@ -412,59 +408,58 @@ var _ = Describe("Fleet apply with helm charts with dependencies", Ordered, func
})

It("creates Bundles with the corresponding resources, depending if they should update dependencies", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleListFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle)).To(Equal(3))
remoteDepl := bundle[0]
simpleDepl := bundle[1]
noDepsDepl := bundle[2]

// remoteDepl corresponds to multi-chart/remote-chart-with-deps
// expected files are:
// fleet.yaml +
// Chart.yaml + values.yaml + templates/configmap.yaml + Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(remoteDepl.Spec.Resources)).To(Equal(6))
presentInBundleResources(path.Join(tmpDirRel, "remote-chart-with-deps", "fleet.yaml"), remoteDepl.Spec.Resources)
// as files were unpacked from the downloaded chart we can't just
// list the files in the original folder and compare.
// Files are only located in the bundle resources
onlyPresentInBundleResources("Chart.yaml", remoteDepl.Spec.Resources)
onlyPresentInBundleResources("values.yaml", remoteDepl.Spec.Resources)
onlyPresentInBundleResources("templates/configmap.yaml", remoteDepl.Spec.Resources)
onlyPresentInBundleResources("Chart.lock", remoteDepl.Spec.Resources)
onlyPresentInBundleResources("charts/config-chart-0.1.0.tgz", remoteDepl.Spec.Resources)

// simpleDepl corresponds to multi-chart/simple-with-fleet-yaml
// expected files are:
// fleet.yaml + Chart.yaml + values.yaml + templates/configmap.yaml +
// Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(simpleDepl.Spec.Resources)).To(Equal(6))
files, err := getAllFilesInDir(path.Join(tmpDirRel, "simple-with-fleet-yaml"))
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(simpleDepl.Spec.Resources)))
for _, file := range files {
presentInBundleResources(file, simpleDepl.Spec.Resources)
}
// explicitly check for dependency files
presentInBundleResources(path.Join(tmpDirRel, "simple-with-fleet-yaml", "Chart.lock"), simpleDepl.Spec.Resources)
presentInBundleResources(path.Join(tmpDirRel, "simple-with-fleet-yaml", "charts/config-chart-0.1.0.tgz"), simpleDepl.Spec.Resources)

// noDepsDepl corresponds to multi-char/simple-with-fleet-yaml-no-deps
// expected files are:
// Chart.yaml + fleet.yaml + values.yaml + templates/configmap.yaml
Expect(len(noDepsDepl.Spec.Resources)).To(Equal(4))
files, err = getAllFilesInDir(path.Join(tmpDirRel, "simple-with-fleet-yaml-no-deps"))
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(noDepsDepl.Spec.Resources)))
for _, file := range files {
presentInBundleResources(file, noDepsDepl.Spec.Resources)
}
// explicitly check for dependency files (they should not exist)
notPresentInBundleResources(path.Join(tmpDirRel, "simple-with-fleet-yaml-no-deps", "Chart.lock"), noDepsDepl.Spec.Resources)
notPresentInBundleResources(path.Join(tmpDirRel, "simple-with-fleet-yaml-no-deps", "charts/config-chart-0.1.0.tgz"), noDepsDepl.Spec.Resources)
return true
}).Should(BeTrue())
bundle, err := cli.GetBundleListFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle)).To(Equal(3))
remoteDepl := bundle[0]
simpleDepl := bundle[1]
noDepsDepl := bundle[2]

// remoteDepl corresponds to multi-chart/remote-chart-with-deps
// expected files are:
// fleet.yaml +
// Chart.yaml + values.yaml + templates/configmap.yaml + Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(remoteDepl.Spec.Resources)).To(Equal(6))
Expect(path.Join(tmpDirRel, "remote-chart-with-deps", "fleet.yaml")).Should(bePresentInBundleResources(remoteDepl.Spec.Resources))
// as files were unpacked from the downloaded chart we can't just
// list the files in the original folder and compare.
// Files are only located in the bundle resources
Expect("Chart.yaml").Should(bePresentOnlyInBundleResources(remoteDepl.Spec.Resources))
Expect("values.yaml").Should(bePresentOnlyInBundleResources(remoteDepl.Spec.Resources))
Expect("templates/configmap.yaml").Should(bePresentOnlyInBundleResources(remoteDepl.Spec.Resources))
Expect("Chart.lock").Should(bePresentOnlyInBundleResources(remoteDepl.Spec.Resources))
Expect("charts/config-chart-0.1.0.tgz").Should(bePresentOnlyInBundleResources(remoteDepl.Spec.Resources))

// simpleDepl corresponds to multi-chart/simple-with-fleet-yaml
// expected files are:
// fleet.yaml + Chart.yaml + values.yaml + templates/configmap.yaml +
// Chart.lock + charts/config-chart-0.1.0.tgz
Expect(len(simpleDepl.Spec.Resources)).To(Equal(6))
files, err := getAllFilesInDir(path.Join(tmpDirRel, "simple-with-fleet-yaml"))
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(simpleDepl.Spec.Resources)))
for _, file := range files {
Expect(file).Should(bePresentInBundleResources(simpleDepl.Spec.Resources))
}
// explicitly check for dependency files
Expect(path.Join(tmpDirRel, "simple-with-fleet-yaml", "Chart.lock")).Should(bePresentInBundleResources(simpleDepl.Spec.Resources))
Expect(path.Join(tmpDirRel, "simple-with-fleet-yaml", "charts/config-chart-0.1.0.tgz")).Should(bePresentInBundleResources(simpleDepl.Spec.Resources))

// noDepsDepl corresponds to multi-char/simple-with-fleet-yaml-no-deps
// expected files are:
// Chart.yaml + fleet.yaml + values.yaml + templates/configmap.yaml
Expect(len(noDepsDepl.Spec.Resources)).To(Equal(4))
files, err = getAllFilesInDir(path.Join(tmpDirRel, "simple-with-fleet-yaml-no-deps"))
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(len(noDepsDepl.Spec.Resources)))
for _, file := range files {
Expect(file).Should(bePresentInBundleResources(noDepsDepl.Spec.Resources))
}
// explicitly check for dependency files (they should not exist in the file system nor in bundle resources)
Expect(path.Join(tmpDirRel, "simple-with-fleet-yaml-no-deps", "Chart.lock")).ShouldNot(BeAnExistingFile())
Expect(path.Join(tmpDirRel, "simple-with-fleet-yaml-no-deps", "Chart.lock")).ShouldNot(bePresentOnlyInBundleResources(noDepsDepl.Spec.Resources))
Expect(path.Join(tmpDirRel, "simple-with-fleet-yaml-no-deps", "charts/config-chart-0.1.0.tgz")).ShouldNot(BeAnExistingFile())
Expect(path.Join(tmpDirRel, "simple-with-fleet-yaml-no-deps", "charts/config-chart-0.1.0.tgz")).ShouldNot(bePresentOnlyInBundleResources(noDepsDepl.Spec.Resources))
})
})

Expand All @@ -474,26 +469,34 @@ var _ = Describe("Fleet apply with helm charts with dependencies", Ordered, func
})
})

func presentInBundleResources(path string, resources []v1alpha1.BundleResource) {
isPresent, err := cli.IsResourcePresentInBundle(path, resources)
Expect(err).NotTo(HaveOccurred())
Expect(isPresent).Should(BeTrue())
}

func onlyPresentInBundleResources(path string, resources []v1alpha1.BundleResource) {
found := false
for _, resource := range resources {
if strings.HasSuffix(resource.Name, path) {
found = true
func bePresentInBundleResources(expected interface{}) types.GomegaMatcher {
return gcustom.MakeMatcher(func(path string) (bool, error) {
resources, ok := expected.([]v1alpha1.BundleResource)
if !ok {
return false, fmt.Errorf("BePresentInBundleResources matcher expects []v1alpha1.BundleResource")
}
}
Expect(found).Should(BeTrue())
isPresent, err := cli.IsResourcePresentInBundle(path, resources)
if err != nil {
return false, fmt.Errorf("Failed to check for path in resources: %s", err.Error())
}
return isPresent, nil
}).WithTemplate("Expected:\n{{.FormattedActual}}\n{{.To}} be present in \n{{format .Data 1}}").WithTemplateData(expected)
}

func notPresentInBundleResources(path string, resources []v1alpha1.BundleResource) {
isPresent, err := cli.IsResourcePresentInBundle(path, resources)
Expect(err).To(HaveOccurred())
Expect(isPresent).Should(BeFalse())
func bePresentOnlyInBundleResources(expected interface{}) types.GomegaMatcher {
return gcustom.MakeMatcher(func(path string) (bool, error) {
resources, ok := expected.([]v1alpha1.BundleResource)
if !ok {
return false, fmt.Errorf("bePresentOnlyInBundleResources matcher expects []v1alpha1.BundleResource")
}
found := false
for _, resource := range resources {
if strings.HasSuffix(resource.Name, path) {
found = true
}
}
return found, nil
}).WithTemplate("Expected:\n{{.FormattedActual}}\n{{.To}} be present in \n{{format .Data 1}}").WithTemplateData(expected)
}

func getAllFilesInDir(chartPath string) ([]string, error) {
Expand Down

0 comments on commit 57f45d9

Please sign in to comment.