Add hack/verify-test-code.sh

This script checks coding style for test code.
It is useful to enforce developers to use framework methods for
keeping readable test code for example.
k3s-v1.15.3
Kenichi Omichi 2019-05-15 18:36:56 +00:00
parent a27d3aef30
commit 496a18febc
4 changed files with 54 additions and 8 deletions

47
hack/verify-test-code.sh Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
cd "${KUBE_ROOT}"
mapfile -t all_e2e_files < <(find test/e2e -name '*.go')
errors_expect_no_error=()
for file in "${all_e2e_files[@]}"
do
if grep "Expect(.*)\.NotTo(.*HaveOccurred()" "${file}" > /dev/null
then
errors_expect_no_error+=( "${file}" )
fi
done
if [ ${#errors_expect_no_error[@]} -ne 0 ]; then
{
echo "Errors:"
for err in "${errors_expect_no_error[@]}"; do
echo "$err"
done
echo
echo 'The above files need to use framework.ExpectNoError(err) instead of '
echo 'Expect(err).NotTo(HaveOccurred()) or gomega.Expect(err).NotTo(gomega.HaveOccurred())'
echo
} >&2
exit 1
fi
echo 'Congratulations! All e2e test source files are valid.'

View File

@ -795,7 +795,7 @@ func testBlockingConfigmapDeletion(f *framework.Framework) {
client := f.ClientSet
configmap := nonDeletableConfigmap(f)
_, err := client.CoreV1().ConfigMaps(f.Namespace.Name).Create(configmap)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to create configmap %s in namespace: %s", configmap.Name, f.Namespace.Name)
framework.ExpectNoError(err, "failed to create configmap %s in namespace: %s", configmap.Name, f.Namespace.Name)
ginkgo.By("deleting the configmap should be denied by the webhook")
err = client.CoreV1().ConfigMaps(f.Namespace.Name).Delete(configmap.Name, &metav1.DeleteOptions{})
@ -813,11 +813,11 @@ func testBlockingConfigmapDeletion(f *framework.Framework) {
cm.Data["webhook-e2e-test"] = "webhook-allow"
}
_, err = updateConfigMap(client, f.Namespace.Name, configmap.Name, toCompliantFn)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to update configmap %s in namespace: %s", configmap.Name, f.Namespace.Name)
framework.ExpectNoError(err, "failed to update configmap %s in namespace: %s", configmap.Name, f.Namespace.Name)
ginkgo.By("deleting the updated configmap should be successful")
err = client.CoreV1().ConfigMaps(f.Namespace.Name).Delete(configmap.Name, &metav1.DeleteOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to delete configmap %s in namespace: %s", configmap.Name, f.Namespace.Name)
framework.ExpectNoError(err, "failed to delete configmap %s in namespace: %s", configmap.Name, f.Namespace.Name)
}
func testAttachingPodWebhook(f *framework.Framework) {
@ -1440,7 +1440,7 @@ func testBlockingCustomResourceDeletion(f *framework.Framework, crd *apiextensio
},
}
_, err := customResourceClient.Create(crInstance, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to create custom resource %s in namespace: %s", crInstanceName, f.Namespace.Name)
framework.ExpectNoError(err, "failed to create custom resource %s in namespace: %s", crInstanceName, f.Namespace.Name)
ginkgo.By("Deleting the custom resource should be denied")
err = customResourceClient.Delete(crInstanceName, &metav1.DeleteOptions{})
@ -1459,11 +1459,11 @@ func testBlockingCustomResourceDeletion(f *framework.Framework, crd *apiextensio
data["webhook-e2e-test"] = "webhook-allow"
}
_, err = updateCustomResource(customResourceClient, f.Namespace.Name, crInstanceName, toCompliantFn)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to update custom resource %s in namespace: %s", crInstanceName, f.Namespace.Name)
framework.ExpectNoError(err, "failed to update custom resource %s in namespace: %s", crInstanceName, f.Namespace.Name)
ginkgo.By("Deleting the updated custom resource should be successful")
err = customResourceClient.Delete(crInstanceName, &metav1.DeleteOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to delete custom resource %s in namespace: %s", crInstanceName, f.Namespace.Name)
framework.ExpectNoError(err, "failed to delete custom resource %s in namespace: %s", crInstanceName, f.Namespace.Name)
}

View File

@ -146,7 +146,7 @@ var _ = SIGDescribe("LimitRange", func() {
ginkgo.By("Verifying LimitRange updating is effective")
err = wait.Poll(time.Second*2, time.Second*20, func() (bool, error) {
limitRange, err = f.ClientSet.CoreV1().LimitRanges(f.Namespace.Name).Get(limitRange.Name, metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
framework.ExpectNoError(err)
return reflect.DeepEqual(limitRange.Spec.Limits[0].Min, newMin), nil
})
framework.ExpectNoError(err)

View File

@ -164,7 +164,6 @@ func OnlyAllowNodeZones(f *framework.Framework, zoneCount int, image string) {
// Get the related PV
pv, err := c.CoreV1().PersistentVolumes().Get(claim.Spec.VolumeName, metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
framework.ExpectNoError(err)
pvZone, ok := pv.ObjectMeta.Labels[v1.LabelZoneFailureDomain]