|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + |
| 16 | + infrav1 "sigs.k8s.io/cluster-api-provider-kubevirt/api/v1alpha1" |
| 17 | +) |
| 18 | + |
| 19 | +func dump(ctx context.Context, kubeconfig, artifactsSuffix string) { |
| 20 | + cmd := exec.CommandContext(ctx, DumpPath, "--kubeconfig", kubeconfig) |
| 21 | + |
| 22 | + failureLocation := CurrentSpecReport().Failure.Location |
| 23 | + artifactsPath := filepath.Join(os.Getenv("ARTIFACTS"), fmt.Sprintf("%s:%d", filepath.Base(failureLocation.FileName), failureLocation.LineNumber), artifactsSuffix) |
| 24 | + cmd.Env = append(cmd.Env, fmt.Sprintf("ARTIFACTS=%s", artifactsPath)) |
| 25 | + |
| 26 | + By(fmt.Sprintf("dumping k8s artifacts to %s", artifactsPath)) |
| 27 | + output, err := cmd.CombinedOutput() |
| 28 | + Expect(err).To(Succeed(), string(output)) |
| 29 | + |
| 30 | + By(fmt.Sprintf("dumping CAPK artifacts to %s", artifactsPath)) |
| 31 | + dumpCAPKResources(ctx, artifactsPath) |
| 32 | +} |
| 33 | + |
| 34 | +func dumpCAPKResources(ctx context.Context, artifactsDir string) { |
| 35 | + GinkgoHelper() |
| 36 | + |
| 37 | + By("dump KubevirtClusters") |
| 38 | + kvClusterList := &infrav1.KubevirtClusterList{} |
| 39 | + Expect(k8sclient.List(ctx, kvClusterList, &client.ListOptions{})).To(Succeed()) |
| 40 | + for i := range kvClusterList.Items { |
| 41 | + item := &kvClusterList.Items[i] |
| 42 | + item.SetManagedFields(nil) |
| 43 | + } |
| 44 | + |
| 45 | + Expect( |
| 46 | + dumpJsonFile(kvClusterList, filepath.Join(artifactsDir, "0_kubevirtclusters.json")), |
| 47 | + ).To(Succeed()) |
| 48 | + |
| 49 | + By("dump KubevirtClusterTemplates") |
| 50 | + kvClusterTmpltList := &infrav1.KubevirtClusterTemplateList{} |
| 51 | + Expect(k8sclient.List(ctx, kvClusterTmpltList, &client.ListOptions{})).To(Succeed()) |
| 52 | + for i := range kvClusterTmpltList.Items { |
| 53 | + item := &kvClusterTmpltList.Items[i] |
| 54 | + item.SetManagedFields(nil) |
| 55 | + } |
| 56 | + Expect( |
| 57 | + dumpJsonFile(kvClusterTmpltList, filepath.Join(artifactsDir, "0_kubevirtclustertemplates.json")), |
| 58 | + ).To(Succeed()) |
| 59 | + |
| 60 | + By("dump KubevirtMachines") |
| 61 | + kvMachineList := &infrav1.KubevirtMachineList{} |
| 62 | + Expect(k8sclient.List(ctx, kvMachineList, &client.ListOptions{})).To(Succeed()) |
| 63 | + for i := range kvMachineList.Items { |
| 64 | + item := &kvMachineList.Items[i] |
| 65 | + item.SetManagedFields(nil) |
| 66 | + } |
| 67 | + Expect( |
| 68 | + dumpJsonFile(kvMachineList, filepath.Join(artifactsDir, "0_kubevirtmachines.json")), |
| 69 | + ).To(Succeed()) |
| 70 | + |
| 71 | + By("dump KubevirtMachineTemplates") |
| 72 | + kvMachineTmpltList := &infrav1.KubevirtMachineTemplateList{} |
| 73 | + Expect(k8sclient.List(ctx, kvMachineTmpltList, &client.ListOptions{})).To(Succeed()) |
| 74 | + for i := range kvMachineTmpltList.Items { |
| 75 | + item := &kvMachineTmpltList.Items[i] |
| 76 | + item.SetManagedFields(nil) |
| 77 | + } |
| 78 | + Expect( |
| 79 | + dumpJsonFile(kvMachineTmpltList, filepath.Join(artifactsDir, "0_kubevirtmachinetemplates.json")), |
| 80 | + ).To(Succeed()) |
| 81 | +} |
| 82 | + |
| 83 | +func dumpJsonFile(objList metav1.ListInterface, artifactsFilePath string) error { |
| 84 | + file, err := os.OpenFile(artifactsFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + defer file.Close() |
| 89 | + |
| 90 | + encoder := json.NewEncoder(file) |
| 91 | + encoder.SetIndent("", " ") |
| 92 | + |
| 93 | + return encoder.Encode(objList) |
| 94 | +} |
0 commit comments