diff --git a/.github/workflows/checks.yaml b/.github/workflows/checks.yaml index 1c331bd8..1ac015d4 100644 --- a/.github/workflows/checks.yaml +++ b/.github/workflows/checks.yaml @@ -76,3 +76,10 @@ jobs: with: name: test-e2e-results path: .output/*.xml + + - name: Upload logs as artifacts + uses: actions/upload-artifact@v4 + if: failure() + with: + name: test-logs + path: ./test/e2e/.output/*.log diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index a02c338b..eb65745a 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -17,6 +17,11 @@ limitations under the License. package e2e import ( + "bytes" + "fmt" + "os" + "os/exec" + "strings" "testing" . "github.com/onsi/ginkgo/v2" @@ -29,6 +34,8 @@ import ( testenv "github.com/kubevirt/ipam-extensions/test/env" ) +const logsDir = ".output" // ./test/e2e/.output + var _ = BeforeSuite(func() { ctrl.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) testenv.Start() @@ -36,6 +43,37 @@ var _ = BeforeSuite(func() { // Run e2e tests using the Ginkgo runner. func TestE2E(t *testing.T) { + os.RemoveAll(logsDir) + if err := os.MkdirAll(logsDir, 0755); err != nil { + panic(fmt.Sprintf("Error creating directory: %v", err)) + } + RegisterFailHandler(Fail) RunSpecs(t, "kubevirt-ipam-controller e2e suite") } + +func logCommand(args []string, fileName string) { + stdout, stderr, err := kubectl(args...) + if err != nil { + fmt.Printf("Error running command kubectl %v, err %v\n", args, err) + return + } + + file, err := os.Create(fileName) + if err != nil { + fmt.Printf("Error running command %v, err %v\n", args, err) + return + } + defer file.Close() + + fmt.Fprintf(file, "kubectl %s\n%s\n%s\n", strings.Join(args, " "), stdout, stderr) +} + +func kubectl(command ...string) (string, string, error) { + var stdout, stderr bytes.Buffer + cmd := exec.Command(os.Getenv("KUBECTL"), command...) + cmd.Stderr = &stderr + cmd.Stdout = &stdout + err := cmd.Run() + return stdout.String(), stderr.String(), err +} diff --git a/test/e2e/persistentips_test.go b/test/e2e/persistentips_test.go index 6d22ac7c..9975ddbf 100644 --- a/test/e2e/persistentips_test.go +++ b/test/e2e/persistentips_test.go @@ -40,7 +40,19 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +var failureCount int = 0 + var _ = Describe("Persistent IPs", func() { + JustAfterEach(func() { + if CurrentSpecReport().Failed() { + failureCount++ + By(fmt.Sprintf("Test failed, collecting logs and artifacts, failure count %d", failureCount)) + + fileName := fmt.Sprintf(logsDir+"/pods_%d.log", failureCount) + logCommand([]string{"get", "pods", "-A"}, fileName) + } + }) + When("network attachment definition created with allowPersistentIPs=true", func() { var ( td testenv.TestData @@ -74,6 +86,9 @@ var _ = Describe("Persistent IPs", func() { WithTimeout(5 * time.Minute). Should(BeTrue()) + // TODO remove + Expect(1).To(Equal(2)) + By("Wait for IPAMClaim to get created") Eventually(testenv.IPAMClaimsFromNamespace(vm.Namespace)). WithTimeout(time.Minute).