Skip to content

Commit

Permalink
e2e: Collect data in case of a failure
Browse files Browse the repository at this point in the history
Signed-off-by: Or Shoval <[email protected]>
  • Loading branch information
oshoval committed Aug 14, 2024
1 parent 7e23b1d commit aeb29d4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 38 additions & 0 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ limitations under the License.
package e2e

import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -29,13 +34,46 @@ 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()
})

// 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
}
15 changes: 15 additions & 0 deletions test/e2e/persistentips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit aeb29d4

Please sign in to comment.