-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9429154
commit 3d8c2bb
Showing
1 changed file
with
8 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,19 +21,11 @@ import ( | |
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/rest" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
log "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
) | ||
|
||
var ( | ||
cfg *rest.Config | ||
k8sClient client.Client // You'll be using this client in your tests. | ||
testEnv *envtest.Environment | ||
err error | ||
) | ||
|
||
func logger() { | ||
// Get log file path from environment variable or use a default path | ||
logFilePath := os.Getenv("LOG_FILE_PATH") | ||
|
@@ -55,45 +47,37 @@ func logger() { | |
log.SetLogger(logger) | ||
} | ||
|
||
func startEnvTest(t *testing.T) { | ||
func startEnvTest(t *testing.T) (*envtest.Environment, *rest.Config) { | ||
// to redirect all functional test related logs to separate logfile ~ default (in functional-tests directory), can be changed using environment variable LOG_FILE_PATH=/path_to_logfile | ||
logger() | ||
|
||
//specify testEnv configuration | ||
testEnv = &envtest.Environment{ | ||
testEnv := &envtest.Environment{ | ||
CRDDirectoryPaths: []string{ | ||
filepath.Join(build.Default.GOPATH, "pkg", "mod", "github.com", "project-codeflare", "[email protected]", "config", "crd", "bases"), | ||
filepath.Join(build.Default.GOPATH, "pkg", "mod", "github.com", "openshift", "[email protected]", "machine", "v1beta1"), | ||
}, | ||
ErrorIfCRDPathMissing: true, | ||
} | ||
test := WithConfig(t, testEnv.Config) | ||
cfg, err = testEnv.Start() | ||
cfg, err := testEnv.Start() | ||
test.Expect(err).NotTo(HaveOccurred()) | ||
|
||
t.Cleanup(func() { | ||
teardownTestEnv(t, testEnv) | ||
}) | ||
|
||
establishClient(t, testEnv) // setup client required for test to interact with kubernetes machine API | ||
return testEnv, cfg | ||
} | ||
|
||
func establishClient(t *testing.T, testEnv *envtest.Environment) { | ||
// Create a test instance with the provided configuration. | ||
test := WithConfig(t, cfg) | ||
|
||
func startInstascaleController(test Test, testEnv *envtest.Environment, cfg *rest.Config) { | ||
// Add custom resource schemes to the global scheme. | ||
err = mcadv1beta1.AddToScheme(scheme.Scheme) | ||
err := mcadv1beta1.AddToScheme(scheme.Scheme) | ||
test.Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = machinev1beta1.AddToScheme(scheme.Scheme) | ||
test.Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Create a Kubernetes client using the provided configuration. | ||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
test.Expect(err).NotTo(HaveOccurred()) | ||
test.Expect(k8sClient).NotTo(BeNil()) | ||
|
||
// Create a controller manager for managing controllers. | ||
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ | ||
Scheme: scheme.Scheme, | ||
|
@@ -177,9 +161,10 @@ func MachineSetReplicas(machineSet *machinev1beta1.MachineSet) *int32 { | |
|
||
func TestReconciler(t *testing.T) { | ||
// initiate setting up the EnvTest environment | ||
startEnvTest(t) | ||
testEnv, cfg := startEnvTest(t) | ||
|
||
test := WithConfig(t, cfg) | ||
startInstascaleController(test, testEnv, cfg) // setup client required for test to interact with kubernetes machine API | ||
|
||
// initialize a variable with test client | ||
client := test.Client() | ||
|