Skip to content

Commit

Permalink
Add code coverage for flow aggregator
Browse files Browse the repository at this point in the history
Add code coverage for flow aggregator binary when
running end-to-end tests on different platforms
(kind, jenkins, VMC etc.)
  • Loading branch information
srikartati committed Feb 27, 2021
1 parent ad3a9c9 commit 47335d6
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 36 deletions.
54 changes: 27 additions & 27 deletions .github/workflows/kind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ jobs:
path: antrea-ubuntu.tar
retention-days: 1 # minimum value, in case artifact deletion by 'artifact-cleanup' job fails

build-flow-aggregator-image:
build-flow-aggregator-coverage-image:
name: Build Flow Aggregator image to be used for Kind e2e tests
needs: check-changes
if: ${{ needs.check-changes.outputs.has_changes == 'yes' }}
runs-on: [ ubuntu-latest ]
steps:
- uses: actions/checkout@v2
- run: make flow-aggregator-ubuntu
- run: make flow-aggregator-ubuntu-coverage
- name: Save Flow Aggregator image to tarball
run: docker save -o flow-aggregator.tar projects.registry.vmware.com/antrea/flow-aggregator:latest
run: docker save -o flow-aggregator.tar antrea/flow-aggregator-coverage:latest
- name: Upload Flow Aggregator image for subsequent jobs
uses: actions/upload-artifact@v2
with:
name: flow-aggregator
name: flow-aggregator-cov
path: flow-aggregator.tar
retention-days: 1 # minimum value, in case artifact deletion by 'artifact-cleanup' job fails

test-e2e-encap:
name: E2e tests on a Kind cluster on Linux
needs: [build-antrea-coverage-image, build-flow-aggregator-image]
needs: [build-antrea-coverage-image, build-flow-aggregator-coverage-image]
runs-on: [ubuntu-latest]
steps:
- name: Free disk space
Expand All @@ -84,11 +84,11 @@ jobs:
- name: Download Flow Aggregator image from previous job
uses: actions/download-artifact@v1
with:
name: flow-aggregator
name: flow-aggregator-cov
- name: Load Antrea image
run: |
docker load -i antrea-ubuntu-cov/antrea-ubuntu.tar
docker load -i flow-aggregator/flow-aggregator.tar
docker load -i flow-aggregator-cov/flow-aggregator.tar
- name: Install Kind
run: |
curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-$(uname)-amd64
Expand All @@ -111,7 +111,7 @@ jobs:
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: '*antrea*'
file: '*.cov.out*'
flags: kind-e2e-tests
name: codecov-test-e2e-encap
directory: test-e2e-encap-coverage
Expand All @@ -128,7 +128,7 @@ jobs:

test-e2e-encap-no-proxy:
name: E2e tests on a Kind cluster on Linux with AntreaProxy disabled
needs: [build-antrea-coverage-image, build-flow-aggregator-image]
needs: [build-antrea-coverage-image, build-flow-aggregator-coverage-image]
runs-on: [ubuntu-latest]
steps:
- name: Free disk space
Expand All @@ -147,11 +147,11 @@ jobs:
- name: Download Flow Aggregator image from previous job
uses: actions/download-artifact@v1
with:
name: flow-aggregator
name: flow-aggregator-cov
- name: Load Antrea image
run: |
docker load -i antrea-ubuntu-cov/antrea-ubuntu.tar
docker load -i flow-aggregator/flow-aggregator.tar
docker load -i flow-aggregator-cov/flow-aggregator.tar
- name: Install Kind
run: |
curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-$(uname)-amd64
Expand All @@ -174,7 +174,7 @@ jobs:
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: '*antrea*'
file: '*.cov.out*'
flags: kind-e2e-tests
name: codecov-test-e2e-encap-no-proxy
directory: test-e2e-encap-no-proxy-coverage
Expand All @@ -191,7 +191,7 @@ jobs:

test-e2e-noencap:
name: E2e tests on a Kind cluster on Linux (noEncap)
needs: [build-antrea-coverage-image, build-flow-aggregator-image]
needs: [build-antrea-coverage-image, build-flow-aggregator-coverage-image]
runs-on: [ubuntu-latest]
steps:
- name: Free disk space
Expand All @@ -210,11 +210,11 @@ jobs:
- name: Download Flow Aggregator image from previous job
uses: actions/download-artifact@v1
with:
name: flow-aggregator
name: flow-aggregator-cov
- name: Load Antrea image
run: |
docker load -i antrea-ubuntu-cov/antrea-ubuntu.tar
docker load -i flow-aggregator/flow-aggregator.tar
docker load -i flow-aggregator-cov/flow-aggregator.tar
- name: Install Kind
run: |
curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-$(uname)-amd64
Expand All @@ -237,7 +237,7 @@ jobs:
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: '*antrea*'
file: '*.cov.out*'
flags: kind-e2e-tests
name: codecov-test-e2e-noencap
directory: test-e2e-noencap-coverage
Expand All @@ -254,7 +254,7 @@ jobs:

test-e2e-hybrid:
name: E2e tests on a Kind cluster on Linux (hybrid)
needs: [build-antrea-coverage-image, build-flow-aggregator-image]
needs: [build-antrea-coverage-image, build-flow-aggregator-coverage-image]
runs-on: [ubuntu-latest]
steps:
- name: Free disk space
Expand All @@ -273,11 +273,11 @@ jobs:
- name: Download Flow Aggregator image from previous job
uses: actions/download-artifact@v1
with:
name: flow-aggregator
name: flow-aggregator-cov
- name: Load Antrea image
run: |
docker load -i antrea-ubuntu-cov/antrea-ubuntu.tar
docker load -i flow-aggregator/flow-aggregator.tar
docker load -i flow-aggregator-cov/flow-aggregator.tar
- name: Install Kind
run: |
curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-$(uname)-amd64
Expand All @@ -300,7 +300,7 @@ jobs:
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: '*antrea*'
file: '*.cov.out*'
flags: kind-e2e-tests
name: codecov-test-e2e-hybrid
directory: test-e2e-hybrid-coverage
Expand All @@ -317,7 +317,7 @@ jobs:

test-e2e-encap-np:
name: E2e tests on a Kind cluster on Linux with Antrea NetworkPolicies enabled
needs: [build-antrea-coverage-image, build-flow-aggregator-image]
needs: [build-antrea-coverage-image, build-flow-aggregator-coverage-image]
runs-on: [ubuntu-latest]
steps:
- name: Free disk space
Expand All @@ -336,11 +336,11 @@ jobs:
- name: Download Flow Aggregator image from previous job
uses: actions/download-artifact@v1
with:
name: flow-aggregator
name: flow-aggregator-cov
- name: Load Antrea image
run: |
docker load -i antrea-ubuntu-cov/antrea-ubuntu.tar
docker load -i flow-aggregator/flow-aggregator.tar
docker load -i flow-aggregator-cov/flow-aggregator.tar
- name: Install Kind
run: |
curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-$(uname)-amd64
Expand All @@ -363,7 +363,7 @@ jobs:
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: '*antrea*'
file: '*.cov.out*'
flags: kind-e2e-tests
name: codecov-test-e2e-np-encap
directory: test-e2e-encap-np-coverage
Expand Down Expand Up @@ -462,7 +462,7 @@ jobs:
# yet.
artifact-cleanup:
name: Delete uploaded images
needs: [build-antrea-coverage-image, build-flow-aggregator-image, build-antrea-image, test-e2e-encap, test-e2e-encap-no-proxy, test-e2e-noencap, test-e2e-hybrid, test-e2e-encap-np, test-netpol-tmp, validate-prometheus-metrics-doc]
needs: [build-antrea-coverage-image, build-flow-aggregator-coverage-image, build-antrea-image, test-e2e-encap, test-e2e-encap-no-proxy, test-e2e-noencap, test-e2e-hybrid, test-e2e-encap-np, test-netpol-tmp, validate-prometheus-metrics-doc]
if: ${{ always() }}
runs-on: [ubuntu-latest]
steps:
Expand All @@ -472,10 +472,10 @@ jobs:
with:
name: antrea-ubuntu-cov
- name: Delete flow-aggregator
if: ${{ needs.build-flow-aggregator-image.result == 'success' }}
if: ${{ needs.build-flow-aggregator-coverage-image.result == 'success' }}
uses: geekyeggo/delete-artifact@v1
with:
name: flow-aggregator
name: flow-aggregator-cov
- name: Delete antrea-ubuntu
if: ${{ needs.build-antrea-image.result == 'success' }}
uses: geekyeggo/delete-artifact@v1
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ flow-aggregator:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' github.com/vmware-tanzu/antrea/cmd/flow-aggregator

.PHONY: flow-aggregator-instr-binary
flow-aggregator-instr-binary:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) test -tags testbincover -covermode count -coverpkg=github.com/vmware-tanzu/antrea/pkg/... -c -o $(BINDIR)/flow-aggregator-coverage $(GOFLAGS) -ldflags '$(LDFLAGS)' github.com/vmware-tanzu/antrea/cmd/flow-aggregator

.PHONY: test-unit test-integration
ifeq ($(UNAME_S),Linux)
test-unit: .linux-test-unit
Expand Down Expand Up @@ -332,6 +337,7 @@ manifest-scale:
manifest-coverage:
$(CURDIR)/hack/generate-manifest.sh --mode dev --coverage > build/yamls/antrea-coverage.yml
$(CURDIR)/hack/generate-manifest.sh --mode dev --ipsec --coverage > build/yamls/antrea-ipsec-coverage.yml
$(CURDIR)/hack/generate-manifest-flow-aggregator.sh --mode dev --coverage > build/yamls/flow-aggregator-coverage.yml

.PHONY: octant-antrea-ubuntu
octant-antrea-ubuntu:
Expand All @@ -353,6 +359,16 @@ endif
docker tag antrea/flow-aggregator:$(DOCKER_IMG_VERSION) projects.registry.vmware.com/antrea/flow-aggregator
docker tag antrea/flow-aggregator:$(DOCKER_IMG_VERSION) projects.registry.vmware.com/antrea/flow-aggregator:$(DOCKER_IMG_VERSION)

.PHONY: flow-aggregator-ubuntu-coverage
flow-aggregator-ubuntu-coverage:
@echo "===> Building antrea/flow-aggregator-coverage Docker image <==="
ifneq ($(DOCKER_REGISTRY),"")
docker build -t antrea/flow-aggregator-coverage:$(DOCKER_IMG_VERSION) -f build/images/flow-aggregator/Dockerfile.coverage .
else
docker build --pull -t antrea/flow-aggregator-coverage:$(DOCKER_IMG_VERSION) -f build/images/flow-aggregator/Dockerfile.coverage .
endif
docker tag antrea/flow-aggregator-coverage:$(DOCKER_IMG_VERSION) antrea/flow-aggregator-coverage

.PHONY: verify
verify:
@echo "===> Verifying spellings <==="
Expand Down
17 changes: 17 additions & 0 deletions build/images/flow-aggregator/Dockerfile.coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.15 as flow-aggregator-build

WORKDIR /antrea

COPY . /antrea

RUN make flow-aggregator flow-aggregator-instr-binary

FROM antrea/base-ubuntu:2.14.0

LABEL maintainer="Antrea <projectantrea-dev@googlegroups.com>"
LABEL description="The docker image for the flow aggregator with code coverage measurement enabled for testing purposes."

USER root

COPY --from=flow-aggregator-build /antrea/bin/flow-aggregator* /usr/local/bin/
COPY --from=flow-aggregator-build /antrea/test/e2e/coverage/flow-aggregator-arg-file /
13 changes: 13 additions & 0 deletions build/yamls/flow-aggregator/patches/coverage/startFlowAggCov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: flow-aggregator
namespace: flow-aggregator
spec:
template:
spec:
containers:
- name: flow-aggregator
image: antrea/flow-aggregator-coverage:latest
command: [ "/bin/sh" ]
args: [ "-c", "sleep 2; flow-aggregator-coverage -test.run=TestBincoverRunMain -test.coverprofile=flow-aggregator.cov.out -args-file=/flow-aggregator-arg-file; while true; do sleep 5 & wait $!; done" ]
13 changes: 10 additions & 3 deletions ci/jenkins/test-vmc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ function deliver_antrea {
VERSION="$CLUSTER" DOCKER_REGISTRY="${DOCKER_REGISTRY}" make && break
fi
done
VERSION="$CLUSTER" DOCKER_REGISTRY="${DOCKER_REGISTRY}" make flow-aggregator-ubuntu
if [[ "$COVERAGE" == true ]]; then
VERSION="$CLUSTER" DOCKER_REGISTRY="${DOCKER_REGISTRY}" make flow-aggregator-ubuntu-coverage
else
VERSION="$CLUSTER" DOCKER_REGISTRY="${DOCKER_REGISTRY}" make flow-aggregator-ubuntu
fi
cd ci/jenkins

if [ "$?" -ne "0" ]; then
Expand Down Expand Up @@ -332,10 +336,12 @@ function deliver_antrea {

if [[ "$COVERAGE" == true ]]; then
docker save -o antrea-ubuntu-coverage.tar antrea/antrea-ubuntu-coverage:${DOCKER_IMG_VERSION}
docker save -o flow-aggregator-coverage.tar antrea/flow-aggregator-coverage:${DOCKER_IMG_VERSION}
else
docker save -o antrea-ubuntu.tar projects.registry.vmware.com/antrea/antrea-ubuntu:${DOCKER_IMG_VERSION}
docker save -o flow-aggregator.tar projects.registry.vmware.com/antrea/flow-aggregator:${DOCKER_IMG_VERSION}
fi
docker save -o flow-aggregator.tar projects.registry.vmware.com/antrea/flow-aggregator:${DOCKER_IMG_VERSION}


kubectl get nodes -o wide --no-headers=true | awk -v role="$CONTROL_PLANE_NODE_ROLE" '$3 == role {print $6}' | while read control_plane_ip; do
scp -q -o StrictHostKeyChecking=no -i ${GIT_CHECKOUT_DIR}/jenkins/key/antrea-ci-key $GIT_CHECKOUT_DIR/build/yamls/*.yml capv@${control_plane_ip}:~
Expand All @@ -347,10 +353,11 @@ function deliver_antrea {
ssh-keygen -f "/var/lib/jenkins/.ssh/known_hosts" -R ${IPs[$i]}
if [[ "$COVERAGE" == true ]]; then
copy_image antrea-ubuntu-coverage.tar docker.io/antrea/antrea-ubuntu-coverage ${IPs[$i]} ${DOCKER_IMG_VERSION} true
copy_image flow-aggregator-coverage.tar docker.io/antrea/flow-aggregator-coverage ${IPs[$i]} ${DOCKER_IMG_VERSION} true
else
copy_image antrea-ubuntu.tar projects.registry.vmware.com/antrea/antrea-ubuntu ${IPs[$i]} ${DOCKER_IMG_VERSION} true
copy_image flow-aggregator.tar projects.registry.vmware.com/antrea/flow-aggregator ${IPs[$i]} ${DOCKER_IMG_VERSION} true
fi
copy_image flow-aggregator.tar projects.registry.vmware.com/antrea/flow-aggregator ${IPs[$i]} ${DOCKER_IMG_VERSION} true
done

if [[ -z $OLD_ANTREA_VERSION ]]; then
Expand Down
9 changes: 6 additions & 3 deletions ci/kind/test-e2e-kind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ done
if $coverage; then
manifest_args="$manifest_args --coverage"
COMMON_IMAGES_LIST+=("antrea/antrea-ubuntu-coverage:latest")
COMMON_IMAGES_LIST+=("antrea/flow-aggregator-coverage:latest")
else
COMMON_IMAGES_LIST+=("projects.registry.vmware.com/antrea/antrea-ubuntu:latest")
COMMON_IMAGES_LIST+=("projects.registry.vmware.com/antrea/flow-aggregator:latest")
fi
COMMON_IMAGES_LIST+=("projects.registry.vmware.com/antrea/flow-aggregator:latest")

printf -v COMMON_IMAGES "%s " "${COMMON_IMAGES_LIST[@]}"

function run_test {
Expand All @@ -122,11 +124,12 @@ function run_test {
eval "timeout 600 $TESTBED_CMD create kind --antrea-cni false $args"

if $coverage; then
$YML_CMD --kind --encap-mode $current_mode $manifest_args | docker exec -i kind-control-plane dd of=/root/antrea-coverage.yml
$YML_CMD --kind --encap-mode $current_mode $manifest_args | docker exec -i kind-control-plane dd of=/root/antrea-coverage.yml
$FLOWAGGREGATOR_YML_CMD --coverage | docker exec -i kind-control-plane dd of=/root/flow-aggregator-coverage.yml
else
$YML_CMD --kind --encap-mode $current_mode $manifest_args | docker exec -i kind-control-plane dd of=/root/antrea.yml
$FLOWAGGREGATOR_YML_CMD | docker exec -i kind-control-plane dd of=/root/flow-aggregator.yml
fi
$FLOWAGGREGATOR_YML_CMD | docker exec -i kind-control-plane dd of=/root/flow-aggregator.yml
sleep 1
if $coverage; then
go test -v -timeout=40m github.com/vmware-tanzu/antrea/test/e2e -provider=kind --logs-export-dir=$ANTREA_LOG_DIR --coverage --coverage-dir $ANTREA_COV_DIR
Expand Down
13 changes: 13 additions & 0 deletions cmd/flow-aggregator/bincover_run_main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build testbincover

package main

import (
"testing"

"github.com/confluentinc/bincover"
)

func TestBincoverRunMain(t *testing.T) {
bincover.RunTest(main)
}
Loading

0 comments on commit 47335d6

Please sign in to comment.