Skip to content

Commit

Permalink
create a temporary kubeconfig file for the test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
atheo89 committed May 20, 2024
1 parent 29d9b9e commit cc7097e
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions components/odh-notebook-controller/controllers/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*
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
Expand All @@ -19,7 +18,9 @@ import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -49,11 +50,6 @@ import (
//+kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

// +kubebuilder:docs-gen:collapse=Imports

var (
cfg *rest.Config
cli client.Client
Expand Down Expand Up @@ -83,6 +79,36 @@ var _ = BeforeSuite(func() {
}
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseFlagOptions(&opts)))

// Create a temporary kubeconfig file
kubeconfigContent := []byte(`
apiVersion: v1
clusters:
- cluster:
server: https://localhost:6443
name: local
contexts:
- context:
cluster: local
user: user
name: local
current-context: local
kind: Config
preferences: {}
users:
- name: user
user:
token: fake-token
`)
tmpDir, err := ioutil.TempDir("", "kubeconfig")
Expect(err).NotTo(HaveOccurred())

kubeconfigPath := filepath.Join(tmpDir, "config")
err = ioutil.WriteFile(kubeconfigPath, kubeconfigContent, 0644)
Expect(err).NotTo(HaveOccurred())

// Set the KUBECONFIG environment variable
os.Setenv("KUBECONFIG", kubeconfigPath)

// Initiliaze test environment:
// https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest#Environment.Start
By("Bootstrapping test environment")
Expand All @@ -98,7 +124,7 @@ var _ = BeforeSuite(func() {
},
}

cfg, err := envTest.Start()
cfg, err = envTest.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

Expand Down

0 comments on commit cc7097e

Please sign in to comment.