Merge pull request #143 from enj/enj/i/cache_mutation_detector_unit

Enable cache mutation detector in unit tests
This commit is contained in:
Matt Moyer 2020-12-17 10:09:02 -06:00 committed by GitHub
commit cc5cb394e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -42,13 +42,21 @@ function with_modules() {
local cmd_function="${1}" local cmd_function="${1}"
cmd="$(${cmd_function})" cmd="$(${cmd_function})"
# start the cache mutation detector by default so that cache mutators will be found
local kube_cache_mutation_detector="${KUBE_CACHE_MUTATION_DETECTOR:-true}"
# panic the server on watch decode errors since they are considered coder mistakes
local kube_panic_watch_decode_error="${KUBE_PANIC_WATCH_DECODE_ERROR:-true}"
env_vars="KUBE_CACHE_MUTATION_DETECTOR=${kube_cache_mutation_detector} KUBE_PANIC_WATCH_DECODE_ERROR=${kube_panic_watch_decode_error}"
pushd "${ROOT}" >/dev/null pushd "${ROOT}" >/dev/null
for mod_file in $(find . -maxdepth 4 -not -path "./generated/*" -name go.mod | sort); do for mod_file in $(find . -maxdepth 4 -not -path "./generated/*" -name go.mod | sort); do
mod_dir="$(dirname "${mod_file}")" mod_dir="$(dirname "${mod_file}")"
( (
echo "=> " echo "=> "
echo " cd ${mod_dir} && ${cmd}" echo " cd ${mod_dir} && ${env_vars} ${cmd}"
cd "${mod_dir}" && ${cmd} cd "${mod_dir}" && env ${env_vars} ${cmd}
) )
done done
popd >/dev/null popd >/dev/null

View File

@ -0,0 +1,27 @@
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package controller
import (
"testing"
"github.com/stretchr/testify/require"
"k8s.io/client-go/tools/cache"
)
func TestCacheMutationDetectorEnabled(t *testing.T) {
// this is a bit of simplistic test to check if we have a real cache mutation detector.
// if we actually start mutating an informer cache in this test, the test will almost
// always fail because the go race detector will see the mutation.
// the cache mutation detector will certainly make certain races more common and thus
// easily detected by the race detector, but its real use is against a compiled binary
// such as pinniped-server running in a pod - that binary has no race detector at runtime.
c := cache.NewCacheMutationDetector("test pinniped")
type realCacheMutationDetector interface {
CompareObjects() // this is brittle, but this function name has never changed...
}
require.Implementsf(t, (*realCacheMutationDetector)(nil), c, "%T is not a real cache mutation detector", c)
}