Skip to content

Commit

Permalink
Fix panic, run collectLogArtifacts during AfterSuite
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Squizzato <[email protected]>
  • Loading branch information
squizzi committed Sep 24, 2024
1 parent 0302f58 commit fea83d7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
57 changes: 36 additions & 21 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ var _ = BeforeSuite(func() {
})

var _ = AfterSuite(func() {
By("removing the controller-manager")
cmd := exec.Command("make", "dev-destroy")
_, err := utils.Run(cmd)
Expect(err).NotTo(HaveOccurred())
if !noCleanup() {
if CurrentSpecReport().Failed() {
By("collecting failure logs from local controllers")
kc := kubeclient.NewFromLocal(internalutils.DefaultSystemNamespace)
collectLogArtifacts(kc, "")
}

By("removing the controller-manager")
cmd := exec.Command("make", "dev-destroy")
_, err := utils.Run(cmd)
Expect(err).NotTo(HaveOccurred())
}
})

// verifyControllersUp validates that controllers for the given providers list
Expand Down Expand Up @@ -141,10 +149,12 @@ func templateBy(t managedcluster.Template, description string) {
By(fmt.Sprintf("[%s] %s", t, description))
}

// CollectLogArtfiacts collects log output from each the HMC controller,
// collectLogArtifacts collects log output from each the HMC controller,
// CAPI controller and the provider controller(s) as well as output from clusterctl
// and stores them in the test/e2e directory as artifacts. If it fails it
// produces a warning message to the GinkgoWriter, but does not fail the test.
// and stores them in the test/e2e directory as artifacts. clusterName can be
// optionally provided, passing an empty string will prevent clusterctl output
// from being fetched. If collectLogArtifacts fails it produces a warning
// message to the GinkgoWriter, but does not fail the test.
func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, providerTypes ...managedcluster.ProviderType) {
GinkgoHelper()

Expand All @@ -158,8 +168,12 @@ func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, provider
host = strings.ReplaceAll(hostURL.Host, ":", "_")
}

for _, providerType := range providerTypes {
filterLabels = append(filterLabels, managedcluster.GetProviderLabel(providerType))
if providerTypes == nil {
filterLabels = managedcluster.FilterAllProviders()
} else {
for _, providerType := range providerTypes {
filterLabels = append(filterLabels, managedcluster.GetProviderLabel(providerType))
}
}

for _, label := range filterLabels {
Expand Down Expand Up @@ -193,24 +207,25 @@ func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, provider
}
}

cmd := exec.Command("./bin/clusterctl",
"describe", "cluster", clusterName, "--namespace", internalutils.DefaultSystemNamespace, "--show-conditions=all")
output, err := utils.Run(cmd)
if err != nil {
utils.WarnError(fmt.Errorf("failed to get clusterctl log: %w", err))
return
}

err = os.WriteFile(filepath.Join("test/e2e", host+"-"+"clusterctl.log"), output, 0644)
if err != nil {
utils.WarnError(fmt.Errorf("failed to write clusterctl log: %w", err))
if clusterName != "" {
cmd := exec.Command("./bin/clusterctl",
"describe", "cluster", clusterName, "--namespace", internalutils.DefaultSystemNamespace, "--show-conditions=all")
output, err := utils.Run(cmd)
if err != nil {
utils.WarnError(fmt.Errorf("failed to get clusterctl log: %w", err))
return
}
err = os.WriteFile(filepath.Join("test/e2e", host+"-"+"clusterctl.log"), output, 0644)
if err != nil {
utils.WarnError(fmt.Errorf("failed to write clusterctl log: %w", err))
}
}
}

func noCleanup() bool {
noCleanup := os.Getenv(managedcluster.EnvVarNoCleanup)
if noCleanup != "" {
By(fmt.Sprintf("skipping After nodes as %s is set", managedcluster.EnvVarNoCleanup))
By(fmt.Sprintf("skipping After node as %s is set", managedcluster.EnvVarNoCleanup))
}

return noCleanup != ""
Expand Down
7 changes: 2 additions & 5 deletions test/e2e/provider_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("AWS Templates", Label("provider:cloud", "provider:aws"), Order

By("verifying CAPA controller is running and ready")
Eventually(func() error {
err := verifyControllersUp(standaloneClient, managedcluster.ProviderCAPI, managedcluster.ProviderAWS)
err := verifyControllersUp(kc, managedcluster.ProviderCAPI, managedcluster.ProviderAWS)
if err != nil {
_, _ = fmt.Fprintf(
GinkgoWriter, "[%s] controller validation failed: %v\n",
Expand All @@ -62,11 +62,8 @@ var _ = Describe("AWS Templates", Label("provider:cloud", "provider:aws"), Order
// If we failed collect logs from each of the affiliated controllers
// as well as the output of clusterctl to store as artifacts.
if CurrentSpecReport().Failed() && !noCleanup() {
By("collecting failure logs from controllers")
if kc != nil {
collectLogArtifacts(kc, clusterName, managedcluster.ProviderAWS, managedcluster.ProviderCAPI)
}
if standaloneClient != nil {
By("collecting failure logs from hosted controllers")
collectLogArtifacts(standaloneClient, clusterName, managedcluster.ProviderAWS, managedcluster.ProviderCAPI)
}

Expand Down
11 changes: 11 additions & 0 deletions test/managedcluster/managedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"strings"

"github.com/Mirantis/hmc/test/utils"
"github.com/a8m/envsubst"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -60,6 +61,16 @@ var vsphereStandaloneCPManagedClusterTemplateBytes []byte
//go:embed resources/vsphere-hosted-cp.yaml.tpl
var vsphereHostedCPManagedClusterTemplateBytes []byte

func FilterAllProviders() []string {
return []string{
utils.HMCControllerLabel,
GetProviderLabel(ProviderAWS),
GetProviderLabel(ProviderAzure),
GetProviderLabel(ProviderCAPI),
GetProviderLabel(ProviderVSphere),
}
}

func GetProviderLabel(provider ProviderType) string {
return fmt.Sprintf("%s=%s", providerLabel, provider)
}
Expand Down

0 comments on commit fea83d7

Please sign in to comment.