forked from kubeflow/kubeflow
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test_harness w/ fixes for the unit tests to pass (kubeflow#50)
* test_harness w/ fixes for the unit tests to pass * update for README.md * update to README.md * added test harness files to generate unit tests
- Loading branch information
1 parent
ad3874e
commit f0a80c4
Showing
23 changed files
with
913 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# gen-test-target will generate a golang testcase using the | ||
# kustomize test-harness that is used from kerbernetes-sigs/kustomize/pkg/target/kusttestharness_test.go | ||
# The unittest compares the collection of resource files with what kustomize build would produce (actual vs expected) | ||
# | ||
source hack/utils.sh | ||
|
||
kebab-case-2-PascalCase() | ||
{ | ||
local a=$1 b='' array | ||
IFS='-' read -r -a array <<< "$a" | ||
for element in "${array[@]}" | ||
do | ||
part="${element^}" | ||
b+=$part | ||
done | ||
echo $b | ||
} | ||
|
||
gen-target-start() | ||
{ | ||
local dir=$(get-target $1) target fname | ||
fname=/manifests${dir##*/manifests} | ||
target=$(kebab-case-2-PascalCase $(get-target-name $1)) | ||
|
||
echo 'package kustomize_test' | ||
echo '' | ||
echo 'import (' | ||
echo ' "testing"' | ||
echo ')' | ||
echo '' | ||
echo 'func write'$target'(th *KustTestHarness) {' | ||
} | ||
|
||
gen-target-end() | ||
{ | ||
echo '}' | ||
} | ||
|
||
gen-target() | ||
{ | ||
local directory=$1 | ||
gen-target-start $1 | ||
for i in $(echo $(cat $directory/kustomization.yaml | grep '^- .*yaml$'|sed 's/^- //') $(cat $directory/kustomization.yaml | grep ' path: ' | sed 's/^.*: \(.*\)$/\1/') params.env kustomization.yaml | sed 's/ /\n/g' | sort | uniq); do | ||
file=$i | ||
if [[ -f $directory/$file ]]; then | ||
case $file in | ||
"kustomization.yaml") | ||
gen-target-kustomization $file $directory | ||
;; | ||
*.yaml) | ||
gen-target-resource $file $directory | ||
;; | ||
params.env) | ||
gen-target-resource $file $directory | ||
;; | ||
*) | ||
;; | ||
esac | ||
fi | ||
done | ||
gen-target-end | ||
} | ||
|
||
gen-target-kustomization() | ||
{ | ||
local file=$1 dir=$2 fname kname | ||
fname=/manifests${dir##*/manifests} | ||
kname=${fname%/kustomization.yaml} | ||
echo ' th.writeK("'$kname'", `' | ||
cat $dir/$file | ||
echo '`)' | ||
|
||
} | ||
|
||
gen-target-resource() | ||
{ | ||
local file=$1 dir=$2 fname | ||
fname=/manifests${dir##*/manifests}/$file | ||
|
||
echo ' th.writeF("'$fname'", `' | ||
cat $dir/$file | ||
echo '`)' | ||
} | ||
|
||
gen-expected-start() | ||
{ | ||
echo ' th.assertActualEqualsExpected(m, `' | ||
} | ||
|
||
gen-expected-end() | ||
{ | ||
echo '`)' | ||
} | ||
|
||
gen-expected() | ||
{ | ||
gen-expected-start | ||
cd $1 | ||
kustomize build | ||
cd - >/dev/null | ||
gen-expected-end | ||
} | ||
|
||
gen-test-case() | ||
{ | ||
local base=$(get-target-name $1) dir=$(get-target $1) target fname | ||
fname=/manifests${dir##*/manifests}/$(get-target-dirname $1) | ||
target=$(kebab-case-2-PascalCase $base) | ||
|
||
gen-target $1 | ||
echo '' | ||
echo 'func Test'$target'(t *testing.T) {' | ||
echo ' th := NewKustTestHarness(t, "'$fname'")' | ||
echo ' write'$target'(th)' | ||
echo ' m, err := th.makeKustTarget().MakeCustomizedResMap()' | ||
echo ' if err != nil {' | ||
echo ' t.Fatalf("Err: %v", err)' | ||
echo ' }' | ||
gen-expected $1 | ||
echo '}' | ||
} | ||
|
||
gen-test-case $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# gen-test-targets will generate units tests under tests/ for all directories that | ||
# have a kustomization.yaml. This script first finds all directories and then calls | ||
# gen-test-target to generate each golang unit test. | ||
# The script is based on kusttestharness_test.go from kubernetes-sigs/kustomize/pkg/target | ||
# | ||
if [[ $(basename $PWD) != "manifests" ]]; then | ||
echo "must be at manifests root directory to run $0" | ||
exit 1 | ||
fi | ||
|
||
source hack/utils.sh | ||
rm -f $(ls tests/*_test.go | grep -v kusttestharness_test.go) | ||
for i in $(find * -type d -exec sh -c '(ls -p "{}"|grep />/dev/null)||echo "{}"' \; | egrep -v 'docs|tests|hack'); do | ||
absdir=$(pwd)/$i | ||
testname=$(get-target-name $absdir)_test.go | ||
echo generating $testname from $absdir | ||
./hack/gen-test-target.sh $absdir > tests/$testname | ||
done |
Oops, something went wrong.