Skip to content

Commit

Permalink
Merge pull request #135 from nan-yu/travis-e2e
Browse files Browse the repository at this point in the history
Travis e2e
  • Loading branch information
k8s-ci-robot committed Jan 28, 2020
2 parents 63f78d5 + a5f6c2d commit e61f8e4
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 80 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ script:
#- 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:
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
}
59 changes: 1 addition & 58 deletions hack/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,7 @@ 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

# 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
}

source ./hack/scripts/common.sh

fetch_kb_tools

Expand Down

0 comments on commit e61f8e4

Please sign in to comment.