Create a dedicated Admin user for use in tests

Instead of relying on the default user which is deprecated.

Signed-off-by: Richard Wall <richard.wall@venafi.com>
This commit is contained in:
Richard Wall 2024-01-11 16:02:06 +00:00
parent 36345fd163
commit 0dcb758119
2 changed files with 23 additions and 3 deletions

View File

@ -71,7 +71,10 @@ type fixture struct {
setupLock sync.Mutex
environment *envtest.Environment
clientset kubernetes.Interface
// An admin user for running kubectl commands against this envtest
// environment.
adminUser *envtest.AuthenticatedUser
clientset kubernetes.Interface
pollInterval time.Duration
propagationLimit time.Duration
@ -114,6 +117,19 @@ func (f *fixture) setup(t *testing.T) func() {
env, stopFunc := apiserver.RunBareControlPlane(t)
f.environment = env
// An admin user instance for running kubectl against this envtest
// environment.
// Derived from the envtest global config which is configured with very high
// QPS and Burst settings for rapid interactions with the API server.
adminUser, err := env.AddUser(envtest.User{
Name: "envtest-admin",
Groups: []string{"system:masters"},
}, env.Config)
if err != nil {
t.Fatalf("unable to provision admin user: %s", err)
}
f.adminUser = adminUser
cl, err := kubernetes.NewForConfig(env.Config)
if err != nil {
t.Fatal(err)

View File

@ -46,6 +46,11 @@ func (f *fixture) setupNamespace(t *testing.T, name string) (string, func()) {
t.Fatalf("error creating test namespace %q: %v", name, err)
}
kubectl, err := f.adminUser.Kubectl()
if err != nil {
t.Fatalf("enable to create kubectl instance: %s", err)
}
if f.kubectlManifestsPath != "" {
if err := filepath.Walk(f.kubectlManifestsPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
@ -61,8 +66,7 @@ func (f *fixture) setupNamespace(t *testing.T, name string) (string, func()) {
t.Logf("skipping file %q with unrecognised extension", path)
return nil
}
_, _, err = f.environment.ControlPlane.KubeCtl().Run("apply", "--namespace", name, "-f", path)
_, _, err = kubectl.Run("apply", "--namespace", name, "-f", path)
if err != nil {
return err
}