Skip to content

Commit 8d52e26

Browse files
author
Mengqi Yu
committed
basic test for v1
1 parent a316f5e commit 8d52e26

File tree

6 files changed

+250
-137
lines changed

6 files changed

+250
-137
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ install:
2222
-
2323

2424
script:
25-
- TRACE=1 ./test.sh
25+
- TRACE=1 ./testv0.sh
2626
- ./test_existing_projects.sh
27+
- TRACE=1 ./testv1.sh
2728

2829
# TBD. Suppressing for now.
2930
notifications:

cmd/kubebuilder/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import (
2525

2626
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/initproject"
2727
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
28-
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version"
2928
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/v0"
3029
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/v1"
30+
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version"
3131
"github.com/spf13/cobra"
3232
)
3333

cmd/kubebuilder/v1/api.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import (
3535
)
3636

3737
type apiOptions struct {
38-
r *resource.Resource
39-
resourceFlag, controllerFlag *flag.Flag
38+
r *resource.Resource
39+
resourceFlag, controllerFlag *flag.Flag
4040
doResource, doController, doMake bool
4141
}
4242

@@ -84,7 +84,7 @@ func (o *apiOptions) RunAddAPI() {
8484
if o.doController {
8585
fmt.Println(filepath.Join("pkg", "controller", strings.ToLower(r.Kind),
8686
fmt.Sprintf("%s_controller.go", strings.ToLower(r.Kind))))
87-
fmt.Println(filepath.Join("pkg", "apis", strings.ToLower(r.Kind),
87+
fmt.Println(filepath.Join("pkg", "controller", strings.ToLower(r.Kind),
8888
fmt.Sprintf("%s_controller_test.go", strings.ToLower(r.Kind))))
8989

9090
err := (&scaffold.Scaffold{}).Execute(input.Options{},
@@ -142,7 +142,7 @@ After the scaffold is written, api will run make on the project.
142142
make run
143143
`,
144144
Run: func(cmd *cobra.Command, args []string) {
145-
o.RunAddAPI()
145+
o.RunAddAPI()
146146
},
147147
}
148148

@@ -166,12 +166,11 @@ func dieIfNoProject() {
166166
}
167167
}
168168

169-
170169
// ResourceForFlags registers flags for Resource fields and returns the Resource
171170
func ResourceForFlags(f *flag.FlagSet) *resource.Resource {
172171
r := &resource.Resource{}
173172
f.StringVar(&r.Kind, "kind", "", "resource Kind")
174173
f.StringVar(&r.Group, "group", "", "resource Group")
175174
f.StringVar(&r.Version, "version", "", "resource Version")
176175
return r
177-
}
176+
}

common.sh

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
}

test.sh renamed to testv0.sh

Lines changed: 1 addition & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -17,135 +17,7 @@ set -o errexit
1717
set -o nounset
1818
set -o pipefail
1919

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-
opts=-ldflags "-X github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version.kubeBuilderVersion=$INJECT_KB_VERSION"
127-
fi
128-
129-
go build $opts -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd/kubebuilder
130-
go build -o $tmp_root/kubebuilder/bin/kubebuilder-gen ./cmd/kubebuilder-gen
131-
}
132-
133-
function prepare_testdir_under_gopath {
134-
kb_test_dir=$GOPATH/src/github.com/kubernetes-sigs/kubebuilder-test
135-
header_text "preparing test directory $kb_test_dir"
136-
rm -rf "$kb_test_dir" && mkdir -p "$kb_test_dir" && cd "$kb_test_dir"
137-
header_text "running kubebuilder commands in test directory $kb_test_dir"
138-
}
139-
140-
function setup_envs {
141-
header_text "setting up env vars"
142-
143-
# Setup env vars
144-
export PATH=/tmp/kubebuilder/bin/:$PATH
145-
export TEST_ASSET_KUBECTL=/tmp/kubebuilder/bin/kubectl
146-
export TEST_ASSET_KUBE_APISERVER=/tmp/kubebuilder/bin/kube-apiserver
147-
export TEST_ASSET_ETCD=/tmp/kubebuilder/bin/etcd
148-
}
20+
source common.sh
14921

15022
function generate_crd_resources {
15123
header_text "generating CRD resources and code"

0 commit comments

Comments
 (0)