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

Travis e2e #135

Merged
merged 3 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
language: go

os:
- linux
- osx

go:
- "1.13"

git:
depth: 3

go_import_path: github.com/kubernetes-sigs/application
# TODO: switch to below when https://github.com/kubernetes-sigs/application/pull/133 is merged
#go_import_path: sigs.k8s.io/application

# Install must be set to prevent default `go get` to run.
# The dependencies have already been vendored by `dep` so
# we don't need to fetch them.
install:
-

script:
# enable golangci-lint check when the code is migrated to kubebuilder v2.0, which is a simplified version
#- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.21.0
#- golangci-lint run --enable=gofmt --enable=goimports --enable=unparam --enable=nakedret
- ./hack/scripts/test.sh
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then PATH=$PATH:$(pwd) ./e2e/test_e2e.sh; fi

# TBD. Suppressing for now.
notifications:
email: false
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ all: test manager

# Run tests
test: generate fmt vet manifests
go test ./pkg/... ./cmd/... -coverprofile cover.out
go test -v ./pkg/... ./cmd/... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![Build Status](https://travis-ci.org/kubernetes-sigs/application.svg?branch=master)](https://travis-ci.org/kubernetes-sigs/application "Travis")
[![Go Report Card](https://goreportcard.com/badge/sigs.k8s.io/application)](https://goreportcard.com/report/sigs.k8s.io/application)

# Kubernetes Applications

> Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.
Expand Down
21 changes: 21 additions & 0 deletions e2e/kind-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
28 changes: 21 additions & 7 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ limitations under the License.
package main

import (
"github.com/kubernetes-sigs/application/pkg/apis"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"log"
"os"
"path"
"testing"

"github.com/kubernetes-sigs/application/pkg/apis"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"

"github.com/kubernetes-sigs/application/e2e/testutil"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/reporters"
Expand Down Expand Up @@ -54,10 +56,20 @@ func getKubeClientOrDie(config *rest.Config, s *runtime.Scheme) client.Client {
return c
}

const (
crdPath = "../config/crds/app_v1beta1_application.yaml"
applicationPath = "../config/samples/app_v1beta1_application.yaml"
)

var _ = Describe("Application CRD should install correctly", func() {
s := scheme.Scheme
apis.AddToScheme(s)

crd, err := testutil.ParseCRDYaml(crdPath)
if err != nil {
log.Fatal("Unable to parse CRD YAML", err)
}

config, err := getClientConfig()
if err != nil {
log.Fatal("Unable to get client configuration", err)
Expand All @@ -69,24 +81,26 @@ var _ = Describe("Application CRD should install correctly", func() {
}

It("should create CRD", func() {
err = testutil.CreateCRD(extClient, "../config/crds/app_v1beta1_application.yaml")
err = testutil.CreateCRD(extClient, crd)
Expect(err).NotTo(HaveOccurred())
err = testutil.WaitForCRDOrDie(extClient, crd.Name)
Expect(err).NotTo(HaveOccurred())
})

It("should register an application", func() {
client := getKubeClientOrDie(config, s) //Make sure to create the client after CRD has been created.
err = testutil.CreateApplication(client, "default", "../config/samples/app_v1beta1_application.yaml")
err = testutil.CreateApplication(client, "default", applicationPath)
Expect(err).NotTo(HaveOccurred())
})

It("should delete application", func() {
client := getKubeClientOrDie(config, s)
err = testutil.DeleteApplication(client, "default", "../config/samples/app_v1beta1_application.yaml")
err = testutil.DeleteApplication(client, "default", applicationPath)
Expect(err).NotTo(HaveOccurred())
})

It("should delete application CRD", func() {
err = testutil.DeleteCRD(extClient, "../config/crds/app_v1beta1_application.yaml")
err = testutil.DeleteCRD(extClient, crd.Name)
Expect(err).NotTo(HaveOccurred())
})
})
41 changes: 41 additions & 0 deletions e2e/test_e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

source ./hack/scripts/common.sh


K8S_VERSION="v1.16.2"

fetch_kb_tools
install_kind
setup_envs

export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
# You can use --image flag to specify the cluster version you want, e.g --image=kindest/node:v1.13.6, the supported version are listed at https://hub.docker.com/r/kindest/node/tags
kind create cluster -v 4 --retain --wait=1m --config e2e/kind-config.yaml --image=kindest/node:$K8S_VERSION

# remove running containers on exit
function cleanup() {
kind delete cluster
}

trap cleanup EXIT

go test -v ./e2e/...
34 changes: 19 additions & 15 deletions e2e/testutil/customresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,31 @@ package testutil
import (
"io"
"os"
"time"

apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextcs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/util/yaml"
)

func CreateCRD(kubeClient apiextcs.Interface, relativePath string) error {
CRD, err := parseCRDYaml(relativePath)
if err != nil {
return err
}
func CreateCRD(kubeClient apiextcs.Interface, crd *apiextensions.CustomResourceDefinition) error {

_, err = kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Get(CRD.Name, metav1.GetOptions{})
_, err := kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Get(crd.Name, metav1.GetOptions{})

if err == nil {
// CustomResourceDefinition already exists -> Update
_, err = kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Update(CRD)
_, err = kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Update(crd)
if err != nil {
return err
}

} else {
// CustomResourceDefinition doesn't exist -> Create
_, err = kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(CRD)
_, err = kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
if err != nil {
return err
}
Expand All @@ -54,20 +52,26 @@ func CreateCRD(kubeClient apiextcs.Interface, relativePath string) error {
return nil
}

func DeleteCRD(kubeClient apiextcs.Interface, relativePath string) error {
CRD, err := parseCRDYaml(relativePath)
if err != nil {
return err
}
func WaitForCRDOrDie(kubeClient apiextcs.Interface, name string) error {
err := wait.PollImmediate(2*time.Second, 20*time.Second, func() (bool, error) {
_, err := kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Get(name, metav1.GetOptions{})
if err != nil {
return false, err
}
return true, nil
})
return err
}

if err := kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(CRD.Name, &metav1.DeleteOptions{}); err != nil {
func DeleteCRD(kubeClient apiextcs.Interface, crdName string) error {
if err := kubeClient.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(crdName, &metav1.DeleteOptions{}); err != nil {
return err
}

return nil
}

func parseCRDYaml(relativePath string) (*apiextensions.CustomResourceDefinition, error) {
func ParseCRDYaml(relativePath string) (*apiextensions.CustomResourceDefinition, error) {
var manifest *os.File
var err error

Expand Down
106 changes: 106 additions & 0 deletions hack/scripts/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env bash
# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

k8s_version=1.11.0
goarch=amd64
goos="unknown"
tmp_root=/tmp

if [[ "$OSTYPE" == "linux-gnu" ]]; then
goos="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
goos="darwin"
fi

if [[ "$goos" == "unknown" ]]; then
echo "OS '$OSTYPE' not supported. Aborting." >&2
exit 1
fi

go_workspace=''
for p in ${GOPATH//:/ }; do
if [[ $PWD/ = $p/* ]]; then
go_workspace=$p
fi
done

if [ -z $go_workspace ]; then
echo 'Current directory is not in $GOPATH' >&2
exit 1
fi

# Turn colors in this script off by setting the NO_COLOR variable in your
# environment to any value:
#
# $ NO_COLOR=1 test.sh
NO_COLOR=${NO_COLOR:-""}
if [ -z "$NO_COLOR" ]; then
header=$'\e[1;33m'
reset=$'\e[0m'
else
header=''
reset=''
fi

function header_text {
echo "$header$*$reset"
}

# fetch k8s API gen tools and make it available under kb_root_dir/bin.
function fetch_kb_tools {
header_text "fetching kb tools"
kb_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz"
kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name"

kb_tools_archive_path="$tmp_root/$kb_tools_archive_name"
if [ ! -f $kb_tools_archive_path ]; then
curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path"
fi
tar -zvxf "$kb_tools_archive_path" -C "$tmp_root/"
}


function setup_envs {
header_text "setting up env vars"

# Setup env vars
export PATH=$tmp_root/kubebuilder/bin:$PATH
export TEST_ASSET_KUBECTL=$tmp_root/kubebuilder/bin/kubectl
export TEST_ASSET_KUBE_APISERVER=$tmp_root/kubebuilder/bin/kube-apiserver
export TEST_ASSET_ETCD=$tmp_root/kubebuilder/bin/etcd
export TEST_DEP=$tmp_root/kubebuilder/init_project
}

function install_kind {
header_text "Checking for kind"
if ! is_installed kind ; then
header_text "Installing kind"
KIND_DIR=$(mktemp -d)
pushd $KIND_DIR
GO111MODULE=on go get sigs.k8s.io/kind@v0.6.0
popd
fi
}

function is_installed {
if command -v $1 &>/dev/null; then
return 0
fi
return 1
}
27 changes: 27 additions & 0 deletions hack/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail


source ./hack/scripts/common.sh

fetch_kb_tools

setup_envs

make test