-
Notifications
You must be signed in to change notification settings - Fork 516
/
gen-minimal-test.sh
executable file
·105 lines (87 loc) · 2.51 KB
/
gen-minimal-test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
set -o nounset
set -o pipefail
if ! command -v yq > /dev/null; then
echo "Can't find 'yq' tool in PATH, please install from https://github.com/mikefarah/yq"
exit 1
fi
if [[ "$#" -lt 2 ]]; then
echo "${0} <folder> <version>: generate minimal tests"
echo
echo " Examples:"
echo " $0 operator/v1 v1 # Generates minimal tests for operator/v1 folder, and version v1 specifically."
echo
exit 2
fi
FOLDER=$1
VERSION=$2
for crdDir in ${FOLDER}/zz_generated.featuregated-crd-manifests/*; do
if [ ! -d ${crdDir} ]; then
# It's likely the bash expansion didn't find any yaml files.
continue
fi
for file in ${crdDir}/*.yaml; do
if [ $(yq eval '.apiVersion' $file) != "apiextensions.k8s.io/v1" ]; then
continue
fi
if [ $(yq eval '.kind' $file) != "CustomResourceDefinition" ]; then
continue
fi
CRD_NAME=$(echo $file | sed s:"${FOLDER}/":: )
GROUP=$(yq eval '.spec.group' $file)
KIND=$(yq eval '.spec.names.kind' $file)
PLURAL=$(yq eval '.spec.names.plural' $file)
crdDirName=$(basename $(dirname $file))
testFileName=$(basename $file)
SUITE_FILE=${FOLDER}/tests/${crdDirName}/${testFileName}
if [ -f ${SUITE_FILE} ]; then
continue
fi
mkdir -p $(dirname ${SUITE_FILE})
featureGateName="${testFileName%.*}"
if [ ${featureGateName} == "AAA_ungated" ]; then
cat > ${SUITE_FILE} <<EOF
apiVersion: apiextensions.k8s.io/v1 # Hack because controller-gen complains if we don't have this
name: "${KIND}"
crdName: ${PLURAL}.${GROUP}
tests:
onCreate:
- name: Should be able to create a minimal ${KIND}
initial: |
apiVersion: ${GROUP}/${VERSION}
kind: ${KIND}
spec: {} # No spec is required for a ${KIND}
expected: |
apiVersion: ${GROUP}/${VERSION}
kind: ${KIND}
spec: {}
EOF
else
cat > ${SUITE_FILE} <<EOF
apiVersion: apiextensions.k8s.io/v1 # Hack because controller-gen complains if we don't have this
name: "${KIND}"
crdName: ${PLURAL}.${GROUP}
featureGate: ${featureGateName}
tests:
onCreate:
- name: Should be able to create a minimal ${KIND}
initial: |
apiVersion: ${GROUP}/${VERSION}
kind: ${KIND}
spec: {} # No spec is required for a ${KIND}
expected: |
apiVersion: ${GROUP}/${VERSION}
kind: ${KIND}
spec: {}
EOF
fi
done
MAKEFILE=${FOLDER}/Makefile
if [ ! -f ${MAKEFILE} ]; then
cat > ${MAKEFILE} <<EOF
.PHONY: test
test:
make -C ../../tests test GINKGO_EXTRA_ARGS=--focus="${GROUP}/${VERSION}"
EOF
fi
done