1+ #! /usr/bin/env bash
2+ # Copyright 2018 The Kubernetes Authors.
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License");
5+ # you may not use this file except in compliance with the License.
6+ # You may obtain a copy of the License at
7+ #
8+ # http://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS,
12+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # See the License for the specific language governing permissions and
14+ # limitations under the License.
15+
16+ set -o errexit
17+ set -o nounset
18+ set -o pipefail
19+
20+ # Enable tracing in this script off by setting the TRACE variable in your
21+ # environment to any value:
22+ #
23+ # $ TRACE=1 test.sh
24+ TRACE=${TRACE:- " " }
25+ if [ -n " $TRACE " ]; then
26+ set -x
27+ fi
28+
29+ # By setting INJECT_KB_VERSION variable in your environment, KB will be compiled
30+ # with this version. This is to assist testing functionality which depends on
31+ # version .e.g gopkg.toml generation.
32+ #
33+ # $ INJECT_KB_VERSION=0.1.7 test.sh
34+ INJECT_KB_VERSION=${INJECT_KB_VERSION:- unknown}
35+
36+ # Make sure, we run in the root of the repo and
37+ # therefore run the tests on all packages
38+ base_dir=" $( cd " $( dirname " $0 " ) /" && pwd ) "
39+ cd " $base_dir " || {
40+ echo " Cannot cd to '$base_dir '. Aborting." >&2
41+ exit 1
42+ }
43+
44+ k8s_version=1.10.1
45+ goarch=amd64
46+ goos=" unknown"
47+
48+ if [[ " $OSTYPE " == " linux-gnu" ]]; then
49+ goos=" linux"
50+ elif [[ " $OSTYPE " == " darwin" * ]]; then
51+ goos=" darwin"
52+ fi
53+
54+ if [[ " $goos " == " unknown" ]]; then
55+ echo " OS '$OSTYPE ' not supported. Aborting." >&2
56+ exit 1
57+ fi
58+
59+ # Turn colors in this script off by setting the NO_COLOR variable in your
60+ # environment to any value:
61+ #
62+ # $ NO_COLOR=1 test.sh
63+ NO_COLOR=${NO_COLOR:- " " }
64+ if [ -z " $NO_COLOR " ]; then
65+ header=$' \e [1;33m'
66+ reset=$' \e [0m'
67+ else
68+ header=' '
69+ reset=' '
70+ fi
71+
72+ function header_text {
73+ echo " $header $* $reset "
74+ }
75+
76+ rc=0
77+ tmp_root=/tmp
78+
79+ kb_root_dir=$tmp_root /kubebuilder
80+ kb_orig=$( pwd)
81+
82+ # Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable
83+ # in your environment to any value:
84+ #
85+ # $ SKIP_FETCH_TOOLS=1 ./test.sh
86+ #
87+ # If you skip fetching tools, this script will use the tools already on your
88+ # machine, but rebuild the kubebuilder and kubebuilder-bin binaries.
89+ SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:- " " }
90+
91+ function prepare_staging_dir {
92+ header_text " preparing staging dir"
93+
94+ if [ -z " $SKIP_FETCH_TOOLS " ]; then
95+ rm -rf " $kb_root_dir "
96+ else
97+ rm -f " $kb_root_dir /kubebuilder/bin/kubebuilder"
98+ rm -f " $kb_root_dir /kubebuilder/bin/kubebuilder-gen"
99+ rm -f " $kb_root_dir /kubebuilder/bin/vendor.tar.gz"
100+ fi
101+ }
102+
103+ # fetch k8s API gen tools and make it available under kb_root_dir/bin.
104+ function fetch_tools {
105+ if [ -n " $SKIP_FETCH_TOOLS " ]; then
106+ return 0
107+ fi
108+
109+ header_text " fetching tools"
110+ kb_tools_archive_name=" kubebuilder-tools-$k8s_version -$goos -$goarch .tar.gz"
111+ kb_tools_download_url=" https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name "
112+
113+ kb_tools_archive_path=" $tmp_root /$kb_tools_archive_name "
114+ if [ ! -f $kb_tools_archive_path ]; then
115+ curl -sL ${kb_tools_download_url} -o " $kb_tools_archive_path "
116+ fi
117+ tar -zvxf " $kb_tools_archive_path " -C " $tmp_root /"
118+ }
119+
120+ function build_kb {
121+ header_text " building kubebuilder"
122+
123+ if [ " $INJECT_KB_VERSION " = " unknown" ]; then
124+ opts=" "
125+ else
126+ # TODO: what does this thing do.
127+ opts=-ldflags " -X github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version.kubeBuilderVersion=$INJECT_KB_VERSION "
128+ fi
129+
130+ go build $opts -o $tmp_root /kubebuilder/bin/kubebuilder ./cmd/kubebuilder
131+ go build $opts -o $tmp_root /kubebuilder/bin/kubebuilder-gen ./cmd/kubebuilder-gen
132+ }
133+
134+ function prepare_testdir_under_gopath {
135+ kb_test_dir=$GOPATH /src/github.com/kubernetes-sigs/kubebuilder-test
136+ header_text " preparing test directory $kb_test_dir "
137+ rm -rf " $kb_test_dir " && mkdir -p " $kb_test_dir " && cd " $kb_test_dir "
138+ header_text " running kubebuilder commands in test directory $kb_test_dir "
139+ }
140+
141+ function setup_envs {
142+ header_text " setting up env vars"
143+
144+ # Setup env vars
145+ export PATH=/tmp/kubebuilder/bin/:$PATH
146+ export TEST_ASSET_KUBECTL=/tmp/kubebuilder/bin/kubectl
147+ export TEST_ASSET_KUBE_APISERVER=/tmp/kubebuilder/bin/kube-apiserver
148+ export TEST_ASSET_ETCD=/tmp/kubebuilder/bin/etcd
149+ }
0 commit comments