Skip to content

Commit

Permalink
[BPF] fv test for prog cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastigera committed Nov 18, 2022
1 parent d00a4f1 commit 27b1ace
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
2 changes: 2 additions & 0 deletions felix/docker-image/calico-felix-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ while true; do
done
fi

source /extra-env.sh

echo "calico-felix-wrapper: Starting calico-felix"
calico-felix &
pid=$!
Expand Down
37 changes: 29 additions & 8 deletions felix/fv/bpf_attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var _ = infrastructure.DatastoreDescribe("_BPF-SAFE_ Felix bpf reattach object",
}

var (
infra infrastructure.DatastoreInfra
felixes []*infrastructure.Felix
infra infrastructure.DatastoreInfra
felix *infrastructure.Felix
)

BeforeEach(func() {
Expand All @@ -51,7 +51,8 @@ var _ = infrastructure.DatastoreDescribe("_BPF-SAFE_ Felix bpf reattach object",
},
}

felixes, _ = infrastructure.StartNNodeTopology(1, opts, infra)
felixes, _ := infrastructure.StartNNodeTopology(1, opts, infra)
felix = felixes[0]

err := infra.AddAllowToDatastore("host-endpoint=='true'")
Expect(err).NotTo(HaveOccurred())
Expand All @@ -62,15 +63,11 @@ var _ = infrastructure.DatastoreDescribe("_BPF-SAFE_ Felix bpf reattach object",
infra.DumpErrorData()
}

for _, felix := range felixes {
felix.Stop()
}

felix.Stop()
infra.Stop()
})

It("should not reattach bpf programs", func() {
felix := felixes[0]

// This should not happen at initial execution of felix, since there is no program attached
firstRunBase := felix.WatchStdoutFor(regexp.MustCompile("Program already attached, skip reattaching"))
Expand All @@ -94,4 +91,28 @@ var _ = infrastructure.DatastoreDescribe("_BPF-SAFE_ Felix bpf reattach object",
Eventually(secondRunProg2, "10s", "100ms").Should(BeClosed())
Expect(secondRunBase).NotTo(BeClosed())
})

It("should clean up programs when BPFDataIfacePattern changes", func() {
By("Starting Felix")
felix.TriggerDelayedStart()

By("Checking that eth0 has a program")

Eventually(func() string {
out, _ := felix.ExecOutput("bpftool", "-jp", "net")
return out
}, "15s", "1s").Should(ContainSubstring("eth0"))

By("Changing env and restarting felix")

felix.SetEvn(map[string]string{"FELIX_BPFDataIfacePattern": "eth1"})
felix.Restart()

By("Checking that eth0 does not have a program anymore")

Eventually(func() string {
out, _ := felix.ExecOutput("bpftool", "-jp", "net")
return out
}, "15s", "1s").ShouldNot(ContainSubstring("eth0"))
})
})
20 changes: 20 additions & 0 deletions felix/fv/infrastructure/felix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package infrastructure

import (
"bufio"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -229,6 +230,25 @@ func (f *Felix) Restart() {
Eventually(f.GetFelixPID, "10s", "100ms").ShouldNot(Equal(oldPID))
}

func (f *Felix) SetEvn(env map[string]string) {
fn := "extra-env.sh"

file, err := os.OpenFile("./"+fn, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
Expect(err).NotTo(HaveOccurred())

fw := bufio.NewWriter(file)

for k, v := range env {
fmt.Fprintf(fw, "export %s=%v\n", k, v)
}

fw.Flush()
file.Close()

err = f.CopyFileIntoContainer("./"+fn, "/"+fn)
Expect(err).NotTo(HaveOccurred())
}

// AttachTCPDump returns tcpdump attached to the container
func (f *Felix) AttachTCPDump(iface string) *tcpdump.TCPDump {
return tcpdump.Attach(f.Container.Name, "", iface)
Expand Down

0 comments on commit 27b1ace

Please sign in to comment.