Skip to content

Commit

Permalink
test(api): fix e2e cleanup operations
Browse files Browse the repository at this point in the history
There is a situation where tests fail when deleting a namespace and namespaced resources with one kustomization command.

Signed-off-by: Roman Sysoev <[email protected]>
  • Loading branch information
Roman Sysoev committed Jan 28, 2025
1 parent 6c82174 commit e45a038
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
13 changes: 3 additions & 10 deletions tests/e2e/affinity_toleration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,9 @@ var _ = Describe("Virtual machine affinity and toleration", ginkgoutil.CommonE2E
})
})

Context("When test is complited:", func() {
It("tries to delete used resources", func() {
kustimizationFile := fmt.Sprintf("%s/%s", conf.TestData.AffinityToleration, "kustomization.yaml")
err := kustomize.ExcludeResource(kustimizationFile, "ns.yaml")
Expect(err).NotTo(HaveOccurred(), "cannot exclude namespace from clean up operation:\n%s", err)
res := kubectl.Delete(kc.DeleteOptions{
Filename: []string{conf.TestData.AffinityToleration},
FilenameOption: kc.Kustomize,
})
Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr())
Context("When test is completed", func() {
It("deletes test case resources", func() {
DeleteTestCaseResources(ResourcesToDelete{KustomizationDir: conf.TestData.AffinityToleration})
})
})
})
58 changes: 31 additions & 27 deletions tests/e2e/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func GetObject(resource kc.Resource, name string, object client.Object, opts kc.
}
cmd := kubectl.GetResource(resource, name, cmdOpts)
if cmd.Error() != nil {
return fmt.Errorf(cmd.StdErr())
return fmt.Errorf("%s", cmd.StdErr())
}
err := json.Unmarshal(cmd.StdOutBytes(), object)
if err != nil {
Expand Down Expand Up @@ -302,7 +302,7 @@ func GetDefaultStorageClass() (*storagev1.StorageClass, error) {
var scList storagev1.StorageClassList
res := kubectl.List(kc.ResourceStorageClass, kc.GetOptions{Output: "json"})
if !res.WasSuccess() {
return nil, fmt.Errorf(res.StdErr())
return nil, fmt.Errorf("%s", res.StdErr())
}

err := json.Unmarshal([]byte(res.StdOut()), &scList)
Expand Down Expand Up @@ -459,31 +459,35 @@ type ResourcesToDelete struct {

// This function checks that all resources in test case can be deleted correctly.
func DeleteTestCaseResources(resources ResourcesToDelete) {
By("Response on deletion request should be successful")
const errMessage = "cannot delete test case resources"

if resources.KustomizationDir != "" {
res := kubectl.Delete(kc.DeleteOptions{
Filename: []string{resources.KustomizationDir},
FilenameOption: kc.Kustomize,
})
Expect(res.Error()).NotTo(HaveOccurred(), fmt.Sprintf("%s\nkustomizationDir: %s\ncmd: %s\nstderr: %s", errMessage, resources.KustomizationDir, res.GetCmd(), res.StdErr()))
}
By("Response on deletion request should be successful", func() {
const errMessage = "cannot delete test case resources"

if resources.KustomizationDir != "" {
kustimizationFile := fmt.Sprintf("%s/%s", resources.KustomizationDir, "kustomization.yaml")
err := kustomize.ExcludeResource(kustimizationFile, "ns.yaml")
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("%s\nkustomizationDir: %s\nstderr: %s", errMessage, resources.KustomizationDir, err))
res := kubectl.Delete(kc.DeleteOptions{
Filename: []string{resources.KustomizationDir},
FilenameOption: kc.Kustomize,
})
Expect(res.Error()).NotTo(HaveOccurred(), fmt.Sprintf("%s\nkustomizationDir: %s\ncmd: %s\nstderr: %s", errMessage, resources.KustomizationDir, res.GetCmd(), res.StdErr()))
}

for _, r := range resources.AdditionalResources {
res := kubectl.Delete(kc.DeleteOptions{
Labels: r.Labels,
Namespace: conf.Namespace,
Resource: r.Resource,
})
Expect(res.Error()).NotTo(HaveOccurred(), fmt.Sprintf("%s\ncmd: %s\nstderr: %s", errMessage, res.GetCmd(), res.StdErr()))
}
for _, r := range resources.AdditionalResources {
res := kubectl.Delete(kc.DeleteOptions{
Labels: r.Labels,
Namespace: conf.Namespace,
Resource: r.Resource,
})
Expect(res.Error()).NotTo(HaveOccurred(), fmt.Sprintf("%s\ncmd: %s\nstderr: %s", errMessage, res.GetCmd(), res.StdErr()))
}

if len(resources.Files) != 0 {
res := kubectl.Delete(kc.DeleteOptions{
Filename: resources.Files,
FilenameOption: kc.Filename,
})
Expect(res.Error()).NotTo(HaveOccurred(), fmt.Sprintf("%s\ncmd: %s\nstderr: %s", errMessage, res.GetCmd(), res.StdErr()))
}
if len(resources.Files) != 0 {
res := kubectl.Delete(kc.DeleteOptions{
Filename: resources.Files,
FilenameOption: kc.Filename,
})
Expect(res.Error()).NotTo(HaveOccurred(), fmt.Sprintf("%s\ncmd: %s\nstderr: %s", errMessage, res.GetCmd(), res.StdErr()))
}
})
}
3 changes: 1 addition & 2 deletions tests/e2e/vm_disk_resizing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"k8s.io/apimachinery/pkg/api/resource"

virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/tests/e2e/config"
cfg "github.com/deckhouse/virtualization/tests/e2e/config"
d8 "github.com/deckhouse/virtualization/tests/e2e/d8"
"github.com/deckhouse/virtualization/tests/e2e/ginkgoutil"
Expand Down Expand Up @@ -162,7 +161,7 @@ func GetVirtualMachineDisks(vmName string, config *cfg.Config) (VirtualMachineDi

var _ = Describe("Virtual disk resizing", ginkgoutil.CommonE2ETestDecorators(), func() {
BeforeEach(func() {
if config.IsReusable() {
if cfg.IsReusable() {
Skip("Test not available in REUSABLE mode: not supported yet.")
}
})
Expand Down

0 comments on commit e45a038

Please sign in to comment.