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 18, 2024
1 parent 7e23b1d commit ce71787
Show file tree
Hide file tree
Showing 3 changed files with 87 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
66 changes: 66 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,74 @@ 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, topic string, failureCount int) {
stdout, stderr, err := kubectl(args...)
if err != nil {
fmt.Printf("Error running command kubectl %v, err %v, stderr %s\n", args, err, stderr)
return
}

fileName := fmt.Sprintf(logsDir+"/%d_%s.log", failureCount, topic)
file, err := os.Create(fileName)
if err != nil {
fmt.Printf("Error running command %v, err %v\n", args, err)
return
}
defer file.Close()

fmt.Fprint(file, fmt.Sprintf("kubectl %s\n%s\n", strings.Join(args, " "), stdout))
}

func logOvnPods(failureCount int) {
args := []string{"get", "pods", "-n", "ovn-kubernetes", "--no-headers", "-o=custom-columns=NAME:.metadata.name"}
ovnK8sPods, stderr, err := kubectl(args...)
if err != nil {
fmt.Printf("Error running command kubectl %v, stderr %s, err %v\n", args, stderr, err)
return
}

podContainers := []struct {
PodPrefix string
Containers []string
}{
{PodPrefix: "ovnkube-control-plane", Containers: []string{"ovnkube-cluster-manager"}},
{PodPrefix: "ovnkube-node", Containers: []string{"ovnkube-controller"}},
}

for _, pod := range strings.Split(ovnK8sPods, "\n") {
for _, pc := range podContainers {
if strings.HasPrefix(pod, pc.PodPrefix) {
for _, container := range pc.Containers {
logCommand([]string{"logs", "-n", "ovn-kubernetes", pod, "-c", container}, pod+"_"+container, failureCount)
}
}
}
}
}

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
}
14 changes: 14 additions & 0 deletions test/e2e/persistentips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ import (
)

var _ = Describe("Persistent IPs", func() {
var failureCount int = 0
JustAfterEach(func() {
if CurrentSpecReport().Failed() {
failureCount++
By(fmt.Sprintf("Test failed, collecting logs and artifacts, failure count %d", failureCount))

logCommand([]string{"get", "pods", "-A"}, "pods", failureCount)
logCommand([]string{"get", "vm", "-A", "-oyaml"}, "vms", failureCount)
logCommand([]string{"get", "vmi", "-A", "-oyaml"}, "vmis", failureCount)
logCommand([]string{"get", "ipamclaims", "-A", "-oyaml"}, "ipamclaims", failureCount)
logOvnPods(failureCount)
}
})

When("network attachment definition created with allowPersistentIPs=true", func() {
var (
td testenv.TestData
Expand Down

0 comments on commit ce71787

Please sign in to comment.