Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run kubernetes integration tests inside of a pod and use kind to setup a kubernetes cluster #17656

Merged
merged 22 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fe4f8b2
Use kind to bring up a local kubernetes cluster and then run the inte…
blakerouse Apr 9, 2020
59f8c95
Run mage update.
blakerouse Apr 9, 2020
39ad334
Add more tests.
blakerouse Apr 10, 2020
c529a0e
Fix more tests, install kind in prepare-tests.
blakerouse Apr 12, 2020
89f8364
Switch to running kubernetes integration tests inside of Kubernetes.
blakerouse Apr 16, 2020
a3ef81a
Use golang 1.13.9.
blakerouse Apr 16, 2020
4b284a5
Fix for other beats.
blakerouse Apr 16, 2020
1460711
Run mage fmt.
blakerouse Apr 16, 2020
cea2b4b
Don't run kubernetes integration tests if not inside kubernetes envir…
blakerouse Apr 16, 2020
3f193a1
Fix metricbeat to use mage and the makefile shim for mage.
blakerouse Apr 16, 2020
fa1ef81
Improve the error message when kind or kubectl is not available.
blakerouse Apr 16, 2020
4b766ae
Refactor the integration tests into a more module system.
blakerouse Apr 24, 2020
80b5b34
Fix go vet.
blakerouse Apr 24, 2020
53e514d
Setup travis to use kind.
blakerouse Apr 27, 2020
17137ca
Run kubernetes integration tests in Jenkins.
blakerouse Apr 27, 2020
bcfe456
Fix filebeat magefile.
blakerouse Apr 27, 2020
85ed3c3
Fix travis and Jenkins.
blakerouse Apr 27, 2020
539e3b2
Check requirements of the test runner before actually running the tests.
blakerouse Apr 28, 2020
92fcb56
Add return on parsebool error.
blakerouse Apr 28, 2020
fbea221
Don't return err on missing requirements for tester.
blakerouse Apr 28, 2020
431afe2
Run make update.
blakerouse Apr 28, 2020
2f42f18
Move the kubernetes items to its own module, import that module only …
blakerouse Apr 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .ci/scripts/install-kind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -exuo pipefail

MSG="parameter missing."
DEFAULT_HOME="/usr/local"
KIND_VERSION=${KIND_VERSION:?$MSG}
HOME=${HOME:?$DEFAULT_HOME}
KIND_CMD="${HOME}/bin/kind"

mkdir -p "${HOME}/bin"

curl -sSLo "${KIND_CMD}" "https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-linux-amd64"
chmod +x "${KIND_CMD}"
14 changes: 14 additions & 0 deletions .ci/scripts/install-kubectl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -exuo pipefail

MSG="parameter missing."
DEFAULT_HOME="/usr/local"
K8S_VERSION=${K8S_VERSION:?$MSG}
HOME=${HOME:?$DEFAULT_HOME}
KUBECTL_CMD="${HOME}/bin/kubectl"

mkdir -p "${HOME}/bin"

curl -sSLo "${KUBECTL_CMD}" "https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/linux/amd64/kubectl"
chmod +x "${KUBECTL_CMD}"

13 changes: 0 additions & 13 deletions .ci/scripts/kind-setup.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
#!/usr/bin/env bash
set -exuo pipefail

MSG="parameter missing."
K8S_VERSION=${K8S_VERSION:?$MSG}
HOME=${HOME:?$MSG}
KBC_CMD="${HOME}/bin/kubectl"

mkdir -p "${HOME}/bin"

curl -sSLo "${KBC_CMD}" "https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/linux/amd64/kubectl"
chmod +x "${KBC_CMD}"

GO111MODULE="on" go get sigs.k8s.io/[email protected]
kind create cluster --image kindest/node:${K8S_VERSION}

export KUBECONFIG="$(kind get kubeconfig-path)"
kubectl cluster-info
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ jobs:
stage: test
- os: linux
before_install: .ci/scripts/travis_has_changes.sh metricbeat libbeat || travis_terminate 0
env: TARGETS="-C metricbeat integration-tests"
install:
- .ci/scripts/install-kind.sh
- .ci/scripts/install-kubectl.sh
env:
- TARGETS="-C metricbeat integration-tests"
- K8S_VERSION=v1.17.2
- KIND_VERSION=v0.7.0
go: $TRAVIS_GO_VERSION
stage: test
- os: linux
Expand Down
14 changes: 7 additions & 7 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ pipeline {
}
}
steps {
k8sTest(["v1.16.2","v1.15.3","v1.14.6","v1.13.10","v1.12.10","v1.11.10"])
k8sTest(["v1.18.2","v1.17.2","v1.16.4","v1.15.7","v1.14.10"])
}
}
}
Expand Down Expand Up @@ -806,14 +806,14 @@ def dumpFilteredEnvironment(){
def k8sTest(versions){
versions.each{ v ->
stage("k8s ${v}"){
withEnv(["K8S_VERSION=${v}"]){
withEnv(["K8S_VERSION=${v}", "KIND_VERSION=v0.7.0", "KUBECONFIG=${env.WORKSPACE}/kubecfg"]){
withGithubNotify(context: "K8s ${v}") {
withBeatsEnv(false) {
sh(label: "Install k8s", script: """
eval "\$(gvm use ${GO_VERSION} --format=bash)"
.ci/scripts/kind-setup.sh
""")
sh(label: "Kubernetes Kind",script: "make KUBECONFIG=\"\$(kind get kubeconfig-path)\" -C deploy/kubernetes test")
sh(label: "Install kind", script: ".ci/scripts/install-kind.sh")
sh(label: "Install kubectl", script: ".ci/scripts/install-kubectl.sh")
sh(label: "Integration tests", script: "MODULE=kubernetes make -C metricbeat integration-tests")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use mage instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used make because the PWD is the root of beats, so using make allows the make -C metricbeat to easily go into that directory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mage -d works like make -C.

sh(label: "Setup kind", script: ".ci/scripts/kind-setup.sh")
sh(label: "Deploy to kubernetes",script: "make -C deploy/kubernetes test")
sh(label: 'Delete cluster', script: 'kind delete cluster')
}
}
Expand Down
9 changes: 9 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,15 @@ License type (autodetected): Apache-2.0
Apache License 2.0


--------------------------------------------------------------------
Dependency: github.com/docker/spdystream
Revision: 449fdfce4d96
License type (autodetected): Apache-2.0
./vendor/github.com/docker/spdystream/LICENSE:
--------------------------------------------------------------------
Apache License 2.0


--------------------------------------------------------------------
Dependency: github.com/dop251/goja
Overwrite: github.com/andrewkroh/goja
Expand Down
14 changes: 13 additions & 1 deletion dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,19 @@ func dockerInfo() (*DockerInfo, error) {
// PATH.
func HaveDockerCompose() error {
_, err := exec.LookPath("docker-compose")
return errors.Wrap(err, "docker-compose was not found on the PATH")
if err != nil {
return fmt.Errorf("docker-compose is not available")
}
return nil
}

// HaveKubectl returns an error if kind is not found on the PATH.
func HaveKubectl() error {
_, err := exec.LookPath("kubectl")
if err != nil {
return fmt.Errorf("kubectl is not available")
}
return nil
}

// FindReplace reads a file, performs a find/replace operation, then writes the
Expand Down
53 changes: 34 additions & 19 deletions dev-tools/mage/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"sort"
Expand Down Expand Up @@ -137,33 +138,47 @@ func DefaultTestBinaryArgs() TestBinaryArgs {
// Use RACE_DETECTOR=true to enable the race detector.
// Use MODULE=module to run only tests for `module`.
func GoTestIntegrationForModule(ctx context.Context) error {
return RunIntegTest("goIntegTest", func() error {
module := EnvOr("MODULE", "")
if module != "" {
err := GoTest(ctx, GoTestIntegrationArgsForModule(module))
return errors.Wrapf(err, "integration tests failed for module %s", module)
module := EnvOr("MODULE", "")
modulesFileInfo, err := ioutil.ReadDir("./module")
if err != nil {
return err
}

foundModule := false
failedModules := []string{}
for _, fi := range modulesFileInfo {
if !fi.IsDir() {
continue
}
if module != "" && module != fi.Name() {
continue
}
foundModule = true

modulesFileInfo, err := ioutil.ReadDir("./module")
// Set MODULE because only want that modules tests to run inside the testing environment.
runners, err := NewIntegrationRunners(path.Join("./module", fi.Name()), map[string]string{"MODULE": fi.Name()})
if err != nil {
return err
return errors.Wrapf(err, "test setup failed for module %s", fi.Name())
}

var failed bool
for _, fi := range modulesFileInfo {
if !fi.IsDir() {
continue
}
err = runners.Test("goIntegTest", func() error {
err := GoTest(ctx, GoTestIntegrationArgsForModule(fi.Name()))
if err != nil {
failed = true
return err
}
return nil
})
if err != nil {
// err will already be report to stdout, collect failed module to report at end
failedModules = append(failedModules, fi.Name())
}
if failed {
return errors.New("integration tests failed")
}
return nil
})
}
if module != "" && !foundModule {
return fmt.Errorf("no module %s", module)
}
if len(failedModules) > 0 {
return fmt.Errorf("failed modules: %s", strings.Join(failedModules, ", "))
}
return nil
}

// GoTest invokes "go test" and reports the results to stdout. It returns an
Expand Down
Loading