Skip to content

Commit bbd0127

Browse files
Anfernee Yongkun GuiYongkun Gui
Anfernee Yongkun Gui
authored and
Yongkun Gui
committed
Several enhancements on test.sh
- It doesn't work on MacOS, mainly because BSD sed is different from GNU sed. However, Multi-line addition should use `i`/`a` instead of `s` anyway. New sed command makes it clearer. - Double quote path to avoid possible damage caused by space in path - Consistent indent of 2 spaces - Others that increase readability
1 parent a81a1cc commit bbd0127

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 "generating and testing CRD definition"
154162
kubebuilder create config --crds --output crd.yaml
@@ -159,7 +167,7 @@ function generate_crd_resources {
159167
# TODO: this is awkwardly inserted after the first resource created in this
160168
# test because the output order seems nondeterministic and it's preferable to
161169
# avoid introducing a new dependency like yq or complex parsing logic
162-
cat << EOF > expected.yaml
170+
cat << EOF | diff crd.yaml -
163171
apiVersion: apiextensions.k8s.io/v1beta1
164172
kind: CustomResourceDefinition
165173
metadata:
@@ -198,9 +206,8 @@ status:
198206
plural: ""
199207
conditions: null
200208
EOF
201-
diff crd.yaml expected.yaml
202209

203-
cat << EOF > expected.yaml
210+
cat << EOF | diff -B install.yaml -
204211
apiVersion: v1
205212
kind: Namespace
206213
metadata:
@@ -357,7 +364,6 @@ status:
357364
replicas: 0
358365
359366
EOF
360-
diff -B install.yaml expected.yaml
361367

362368

363369
kubebuilder create resource --group insect --version v1beta1 --kind Wasp
@@ -366,7 +372,7 @@ EOF
366372

367373
# Check for ordering of generated YAML
368374
# TODO: make this a more concise test in a follow-up
369-
cat << EOF > expected.yaml
375+
cat << EOF | diff crd.yaml -
370376
apiVersion: apiextensions.k8s.io/v1beta1
371377
kind: CustomResourceDefinition
372378
metadata:
@@ -475,7 +481,6 @@ status:
475481
plural: ""
476482
conditions: null
477483
EOF
478-
diff crd.yaml expected.yaml
479484
}
480485

481486
function test_generated_controller {
@@ -490,45 +495,56 @@ function test_generated_controller {
490495
}
491496

492497
function test_vendor_update {
493-
header_text "performing vendor update"
494-
kubebuilder update vendor
498+
header_text "performing vendor update"
499+
kubebuilder update vendor
495500
}
496501

497502
function test_docs {
498503
header_text "building docs"
499504
kubebuilder docs --docs-copyright "Hello" --title "World" --cleanup=false --brodocs=false
500-
diff docs/reference/includes $kb_orig/test/docs/expected/includes
501-
diff docs/reference/manifest.json $kb_orig/test/docs/expected/manifest.json
502-
diff docs/reference/config.yaml $kb_orig/test/docs/expected/config.yaml
505+
diff docs/reference/includes "$kb_orig/test/docs/expected/includes"
506+
diff docs/reference/manifest.json "$kb_orig/test/docs/expected/manifest.json"
507+
diff docs/reference/config.yaml "$kb_orig/test/docs/expected/config.yaml"
503508

504509
header_text "testing doc annotations"
505-
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
510+
sed -i -e '/type Bee struct/ i \
511+
// +kubebuilder:doc:note=test notes message annotations\
512+
// +kubebuilder:doc:warning=test warnings message annotations
513+
' pkg/apis/insect/v1beta1/bee_types.go
514+
506515
kubebuilder docs --brodocs=false --cleanup=false
507-
diff docs/reference/config.yaml $kb_orig/test/docs/expected/config-annotated.yaml
516+
diff docs/reference/config.yaml "$kb_orig/test/docs/expected/config-annotated.yaml"
508517
}
509518

510519
function generate_controller {
511520
header_text "creating controller"
512-
kubebuilder create controller --group ant --version v1beta1 --kind Ant
521+
kubebuilder create controller --group ant --version v1beta1 --kind Ant
513522
}
514523

515524
function update_controller_test {
516525
# Update import
517-
sed -i 's!"k8s.io/client-go/kubernetes/typed/apps/v1beta2"!&\n "k8s.io/api/core/v1"!' ./pkg/controller/deployment/controller_test.go
526+
sed -i -e '/"k8s.io\/client-go\/kubernetes\/typed\/apps\/v1beta2"/ a \
527+
"k8s.io/api/core/v1"
528+
' ./pkg/controller/deployment/controller_test.go
518529

519530
# Fill deployment instance
520-
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
531+
sed -i -e '/instance.Name = "instance-1"/ a \
532+
instance.Spec.Template.Spec.Containers = []v1.Container{{Name: "name", Image: "someimage"}}\
533+
labels := map[string]string{"foo": "bar"}\
534+
instance.Spec.Template.ObjectMeta.Labels = labels\
535+
instance.Spec.Selector = \&metav1.LabelSelector{MatchLabels: labels}
536+
' ./pkg/controller/deployment/controller_test.go
521537
}
522538

523539
function generate_coretype_controller {
524-
header_text "generating controller for coretype Deployment"
540+
header_text "generating controller for coretype Deployment"
525541

526-
# Run the commands
527-
kubebuilder init repo --domain sample.kubernetes.io --controller-only
528-
kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type
542+
# Run the commands
543+
kubebuilder init repo --domain sample.kubernetes.io --controller-only
544+
kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type
529545

530546
# Fill the required fileds of Deployment object so that the Deployment instance can be successfully created
531-
update_controller_test
547+
update_controller_test
532548
}
533549

534550
function generate_resource_with_coretype_controller {
@@ -539,7 +555,7 @@ function generate_resource_with_coretype_controller {
539555
kubebuilder create resource --group ant --version v1beta1 --kind Ant
540556
kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type
541557

542-
# Fill the required fileds of Deployment object so that the Deployment instance can be successfully created
558+
# Fill the required fields of Deployment object so that the Deployment instance can be successfully created
543559
update_controller_test
544560
}
545561

0 commit comments

Comments
 (0)