Skip to content

Commit 552cb2d

Browse files
authored
Merge pull request #183 from anfernee/fix
Several enhancements on test.sh
2 parents 561e4da + bbd0127 commit 552cb2d

File tree

1 file changed

+62
-46
lines changed

1 file changed

+62
-46
lines changed

test.sh

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ set -e
2020
# environment to any value:
2121
#
2222
# $ TRACE=1 test.sh
23-
TRACE=${TRACE:""}
23+
TRACE=${TRACE:-""}
2424
if [ -n "$TRACE" ]; then
2525
set -x
2626
fi
@@ -59,15 +59,17 @@ fi
5959
# environment to any value:
6060
#
6161
# $ NO_COLOR=1 test.sh
62-
NO_COLOR=${NO_COLOR:""}
63-
header=$'\e[1;33m'
64-
reset=$'\e[0m'
62+
NO_COLOR=${NO_COLOR:-""}
63+
if [ -z "$NO_COLOR" ]; then
64+
header=$'\e[1;33m'
65+
reset=$'\e[0m'
66+
else
67+
header=''
68+
reset=''
69+
fi
70+
6571
function header_text {
66-
if [ -z "$NO_COLOR" ]; then
67-
echo "$header${@}$reset"
68-
else
69-
echo ${@}
70-
fi
72+
echo "$header$*$reset"
7173
}
7274

7375
rc=0
@@ -83,17 +85,17 @@ kb_orig=$(pwd)
8385
#
8486
# If you skip fetching tools, this script will use the tools already on your
8587
# machine, but rebuild the kubebuilder and kubebuilder-bin binaries.
86-
SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:""}
88+
SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""}
8789

8890
function prepare_staging_dir {
8991
header_text "preparing staging dir"
9092

9193
if [ -z "$SKIP_FETCH_TOOLS" ]; then
92-
rm -rf $kb_root_dir
94+
rm -rf "$kb_root_dir"
9395
else
94-
rm -f $kb_root_dir/kubebuilder/bin/kubebuilder
95-
rm -f $kb_root_dir/kubebuilder/bin/kubebuilder-gen
96-
rm -f $kb_root_dir/kubebuilder/bin/vendor.tar.gz
96+
rm -f "$kb_root_dir/kubebuilder/bin/kubebuilder"
97+
rm -f "$kb_root_dir/kubebuilder/bin/kubebuilder-gen"
98+
rm -f "$kb_root_dir/kubebuilder/bin/vendor.tar.gz"
9799
fi
98100
}
99101

@@ -104,38 +106,39 @@ function fetch_tools {
104106
fi
105107

106108
header_text "fetching tools"
107-
kb_tools_archive_name=kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz
109+
kb_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz"
108110
kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name"
109111

110-
kb_tools_archive_path=$tmp_root/$kb_tools_archive_name
112+
kb_tools_archive_path="$tmp_root/$kb_tools_archive_name"
111113
if [ ! -f $kb_tools_archive_path ]; then
112-
curl -sL ${kb_tools_download_url} -o $kb_tools_archive_path
114+
curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path"
113115
fi
114-
tar -zvxf $kb_tools_archive_path -C $tmp_root/
116+
tar -zvxf "$kb_tools_archive_path" -C "$tmp_root/"
115117
}
116118

117119
function build_kb {
118120
header_text "building kubebuilder"
119121

120122
if [ "$INJECT_KB_VERSION" = "unknown" ]; then
121-
go build -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd/kubebuilder
123+
opts=""
122124
else
123-
go build -ldflags "-X github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version.kubeBuilderVersion=$INJECT_KB_VERSION" -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd/kubebuilder
125+
opts=-ldflags "-X github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version.kubeBuilderVersion=$INJECT_KB_VERSION"
124126
fi
125127

128+
go build $opts -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd/kubebuilder
126129
go build -o $tmp_root/kubebuilder/bin/kubebuilder-gen ./cmd/kubebuilder-gen
127130
}
128131

129132
function prepare_testdir_under_gopath {
130133
kb_test_dir=$GOPATH/src/github.com/kubernetes-sigs/kubebuilder-test
131134
header_text "preparing test directory $kb_test_dir"
132-
rm -rf $kb_test_dir && mkdir -p $kb_test_dir && cd $kb_test_dir
135+
rm -rf "$kb_test_dir" && mkdir -p "$kb_test_dir" && cd "$kb_test_dir"
133136
header_text "running kubebuilder commands in test directory $kb_test_dir"
134137
}
135138

136139
function generate_crd_resources {
137140
header_text "generating CRD resources and code"
138-
141+
139142
# Setup env vars
140143
export PATH=/tmp/kubebuilder/bin/:$PATH
141144
export TEST_ASSET_KUBECTL=/tmp/kubebuilder/bin/kubectl
@@ -147,8 +150,13 @@ function generate_crd_resources {
147150
kubebuilder create resource --group insect --version v1beta1 --kind Bee
148151

149152
header_text "editing generated files to simulate a user"
150-
sed -i -e "s|type Bee struct|// +kubebuilder:categories=foo,bar\ntype Bee struct|" pkg/apis/insect/v1beta1/bee_types.go
151-
sed -i -e "s|type BeeController struct {|// +kubebuilder:rbac:groups="",resources=pods,verbs=get;watch;list\ntype BeeController struct {|" pkg/controller/bee/controller.go
153+
sed -i -e '/type Bee struct/ i \
154+
// +kubebuilder:categories=foo,bar
155+
' pkg/apis/insect/v1beta1/bee_types.go
156+
157+
sed -i -e '/type BeeController struct {/ i \
158+
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;watch;list
159+
' pkg/controller/bee/controller.go
152160

153161
header_text "adding a map type to resource"
154162
sed -i -e "s|type BeeSpec struct {|type BeeSpec struct {\n Request map[string]string \`json:\"request,omitempty\"\`|" pkg/apis/insect/v1beta1/bee_types.go
@@ -162,7 +170,7 @@ function generate_crd_resources {
162170
# TODO: this is awkwardly inserted after the first resource created in this
163171
# test because the output order seems nondeterministic and it's preferable to
164172
# avoid introducing a new dependency like yq or complex parsing logic
165-
cat << EOF > expected.yaml
173+
cat << EOF | diff crd.yaml -
166174
apiVersion: apiextensions.k8s.io/v1beta1
167175
kind: CustomResourceDefinition
168176
metadata:
@@ -204,9 +212,8 @@ status:
204212
plural: ""
205213
conditions: null
206214
EOF
207-
diff crd.yaml expected.yaml
208215

209-
cat << EOF > expected.yaml
216+
cat << EOF | diff -B install.yaml -
210217
apiVersion: v1
211218
kind: Namespace
212219
metadata:
@@ -366,7 +373,6 @@ status:
366373
replicas: 0
367374
368375
EOF
369-
diff -B install.yaml expected.yaml
370376

371377

372378
kubebuilder create resource --group insect --version v1beta1 --kind Wasp
@@ -375,7 +381,7 @@ EOF
375381

376382
# Check for ordering of generated YAML
377383
# TODO: make this a more concise test in a follow-up
378-
cat << EOF > expected.yaml
384+
cat << EOF | diff crd.yaml -
379385
apiVersion: apiextensions.k8s.io/v1beta1
380386
kind: CustomResourceDefinition
381387
metadata:
@@ -487,7 +493,6 @@ status:
487493
plural: ""
488494
conditions: null
489495
EOF
490-
diff crd.yaml expected.yaml
491496
}
492497

493498
function test_generated_controller {
@@ -502,45 +507,56 @@ function test_generated_controller {
502507
}
503508

504509
function test_vendor_update {
505-
header_text "performing vendor update"
506-
kubebuilder update vendor
510+
header_text "performing vendor update"
511+
kubebuilder update vendor
507512
}
508513

509514
function test_docs {
510515
header_text "building docs"
511516
kubebuilder docs --docs-copyright "Hello" --title "World" --cleanup=false --brodocs=false
512-
diff docs/reference/includes $kb_orig/test/docs/expected/includes
513-
diff docs/reference/manifest.json $kb_orig/test/docs/expected/manifest.json
514-
diff docs/reference/config.yaml $kb_orig/test/docs/expected/config.yaml
517+
diff docs/reference/includes "$kb_orig/test/docs/expected/includes"
518+
diff docs/reference/manifest.json "$kb_orig/test/docs/expected/manifest.json"
519+
diff docs/reference/config.yaml "$kb_orig/test/docs/expected/config.yaml"
515520

516521
header_text "testing doc annotations"
517-
sed -i -e "s|// +kubebuilder:categories=foo,bar|// +kubebuilder:categories=foo,bar\n// +kubebuilder:doc:note=test notes message annotations\n// +kubebuilder:doc:warning=test warnings message annotations|" pkg/apis/insect/v1beta1/bee_types.go
522+
sed -i -e '/type Bee struct/ i \
523+
// +kubebuilder:doc:note=test notes message annotations\
524+
// +kubebuilder:doc:warning=test warnings message annotations
525+
' pkg/apis/insect/v1beta1/bee_types.go
526+
518527
kubebuilder docs --brodocs=false --cleanup=false
519-
diff docs/reference/config.yaml $kb_orig/test/docs/expected/config-annotated.yaml
528+
diff docs/reference/config.yaml "$kb_orig/test/docs/expected/config-annotated.yaml"
520529
}
521530

522531
function generate_controller {
523532
header_text "creating controller"
524-
kubebuilder create controller --group ant --version v1beta1 --kind Ant
533+
kubebuilder create controller --group ant --version v1beta1 --kind Ant
525534
}
526535

527536
function update_controller_test {
528537
# Update import
529-
sed -i 's!"k8s.io/client-go/kubernetes/typed/apps/v1beta2"!&\n "k8s.io/api/core/v1"!' ./pkg/controller/deployment/controller_test.go
538+
sed -i -e '/"k8s.io\/client-go\/kubernetes\/typed\/apps\/v1beta2"/ a \
539+
"k8s.io/api/core/v1"
540+
' ./pkg/controller/deployment/controller_test.go
530541

531542
# Fill deployment instance
532-
sed -i 's!instance.Name = "instance-1"!&\n instance.Spec.Template.Spec.Containers = []v1.Container{{Name: "name", Image: "someimage"}}\n labels := map[string]string{"foo": "bar"}\n instance.Spec.Template.ObjectMeta.Labels = labels\n instance.Spec.Selector = \&metav1.LabelSelector{MatchLabels: labels}!' ./pkg/controller/deployment/controller_test.go
543+
sed -i -e '/instance.Name = "instance-1"/ a \
544+
instance.Spec.Template.Spec.Containers = []v1.Container{{Name: "name", Image: "someimage"}}\
545+
labels := map[string]string{"foo": "bar"}\
546+
instance.Spec.Template.ObjectMeta.Labels = labels\
547+
instance.Spec.Selector = \&metav1.LabelSelector{MatchLabels: labels}
548+
' ./pkg/controller/deployment/controller_test.go
533549
}
534550

535551
function generate_coretype_controller {
536-
header_text "generating controller for coretype Deployment"
552+
header_text "generating controller for coretype Deployment"
537553

538-
# Run the commands
539-
kubebuilder init repo --domain sample.kubernetes.io --controller-only
540-
kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type
554+
# Run the commands
555+
kubebuilder init repo --domain sample.kubernetes.io --controller-only
556+
kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type
541557

542558
# Fill the required fileds of Deployment object so that the Deployment instance can be successfully created
543-
update_controller_test
559+
update_controller_test
544560
}
545561

546562
function generate_resource_with_coretype_controller {
@@ -551,7 +567,7 @@ function generate_resource_with_coretype_controller {
551567
kubebuilder create resource --group ant --version v1beta1 --kind Ant
552568
kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type
553569

554-
# Fill the required fileds of Deployment object so that the Deployment instance can be successfully created
570+
# Fill the required fields of Deployment object so that the Deployment instance can be successfully created
555571
update_controller_test
556572
}
557573

0 commit comments

Comments
 (0)