Skip to content

Commit

Permalink
Update auth_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinandh B G <[email protected]>
  • Loading branch information
babugeet committed Jun 7, 2024
1 parent 1a2b66a commit a53b825
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions pkg/provisioner/ironic/clients/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package clients

import (
"testing"
"os"
"path"
"path/filepath"
)

func TestConfigFromEndpointURL(t *testing.T) {
Expand Down Expand Up @@ -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)
}
})
}

0 comments on commit a53b825

Please sign in to comment.