From a53b8259c419829bdc5f75a5613544d72d2be956 Mon Sep 17 00:00:00 2001 From: babugeet <97796199+babugeet@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:53:51 +0530 Subject: [PATCH] Update auth_test.go Signed-off-by: Abhinandh B G --- pkg/provisioner/ironic/clients/auth_test.go | 116 ++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/pkg/provisioner/ironic/clients/auth_test.go b/pkg/provisioner/ironic/clients/auth_test.go index a304c13805..2ef21834ed 100644 --- a/pkg/provisioner/ironic/clients/auth_test.go +++ b/pkg/provisioner/ironic/clients/auth_test.go @@ -2,6 +2,9 @@ package clients import ( "testing" + "os" + "path" + "path/filepath" ) func TestConfigFromEndpointURL(t *testing.T) { @@ -97,3 +100,116 @@ func TestConfigFromEndpointURL(t *testing.T) { }) } } + +func TestLoadAuth(t *testing.T) { + // Helper function to set up the environment + setup := func(authRoot string, createFiles bool) (cleanup func(), err error) { + originalAuthRoot := os.Getenv("METAL3_AUTH_ROOT_DIR") + cleanup = func() { + _ = os.Setenv("METAL3_AUTH_ROOT_DIR", originalAuthRoot) + _ = os.RemoveAll(authRoot) + } + + _ = os.Setenv("METAL3_AUTH_ROOT_DIR", authRoot) + + if createFiles { + authPath := path.Join(authRoot, "ironic") + err = os.MkdirAll(authPath, 0755) + if err != nil { + return cleanup, err + } + + err = os.WriteFile(path.Join(authPath, "username"), []byte("testuser"), 0644) + if err != nil { + return cleanup, err + } + + err = os.WriteFile(path.Join(authPath, "password"), []byte("testpassword"), 0644) + if err != nil { + return cleanup, err + } + } + + return cleanup, nil + } + + t.Run("NoAuthDirectory", func(t *testing.T) { + authRoot := filepath.Join(os.TempDir(), "auth_test_no_dir") + cleanup, err := setup(authRoot, false) + if err != nil { + t.Fatalf("Failed to set up test: %v", err) + } + defer cleanup() + + auth, err := LoadAuth() + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + if auth.Type != NoAuth { + t.Fatalf("Expected NoAuth, got %v", auth.Type) + } + }) + + t.Run("ValidAuth", func(t *testing.T) { + authRoot := filepath.Join(os.TempDir(), "auth_test_valid") + cleanup, err := setup(authRoot, true) + if err != nil { + t.Fatalf("Failed to set up test: %v", err) + } + defer cleanup() + + auth, err := LoadAuth() + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + if auth.Type != HTTPBasicAuth { + t.Fatalf("Expected HTTPBasicAuth, got %v", auth.Type) + } + if auth.Username != "testuser" { + t.Fatalf("Expected username 'testuser', got %v", auth.Username) + } + if auth.Password != "testpassword" { + t.Fatalf("Expected password 'testpassword', got %v", auth.Password) + } + }) + + t.Run("EmptyUsername", func(t *testing.T) { + authRoot := filepath.Join(os.TempDir(), "auth_test_empty_username") + cleanup, err := setup(authRoot, true) + if err != nil { + t.Fatalf("Failed to set up test: %v", err) + } + defer cleanup() + + // Overwrite username file with empty content + err = os.WriteFile(path.Join(authRoot, "ironic", "username"), []byte(""), 0644) + if err != nil { + t.Fatalf("Failed to overwrite username file: %v", err) + } + + _, err = LoadAuth() + if err == nil || err.Error() != "empty HTTP Basic Auth username" { + t.Fatalf("Expected 'empty HTTP Basic Auth username' error, got %v", err) + } + }) + + t.Run("EmptyPassword", func(t *testing.T) { + authRoot := filepath.Join(os.TempDir(), "auth_test_empty_password") + cleanup, err := setup(authRoot, true) + if err != nil { + t.Fatalf("Failed to set up test: %v", err) + } + defer cleanup() + + // Overwrite password file with empty content + err = os.WriteFile(path.Join(authRoot, "ironic", "password"), []byte(""), 0644) + if err != nil { + t.Fatalf("Failed to overwrite password file: %v", err) + } + + _, err = LoadAuth() + if err == nil || err.Error() != "empty HTTP Basic Auth password" { + t.Fatalf("Expected 'empty HTTP Basic Auth password' error, got %v", err) + } + }) +}