-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Keloran/dependency-updates-branch
refactor: Add MockVaultHelper, MockLogical, and MockVaultClient features
- Loading branch information
Showing
6 changed files
with
139 additions
and
30 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
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
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package vault_helper | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/vault/api" | ||
) | ||
|
||
type MockVaultClient struct { | ||
MockLogical func() LogicalClient | ||
MockSetToken func(string) | ||
} | ||
|
||
func (m *MockVaultClient) Logical() LogicalClient { | ||
if m.MockLogical != nil { | ||
return m.MockLogical() | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MockVaultClient) SetToken(token string) { | ||
if m.MockSetToken != nil { | ||
m.MockSetToken(token) | ||
} | ||
} | ||
|
||
type MockLogical struct { | ||
MockRead func(string) (*api.Secret, error) | ||
} | ||
|
||
func (m *MockLogical) Read(path string) (*api.Secret, error) { | ||
if m.MockRead != nil { | ||
return m.MockRead(path) | ||
} | ||
return nil, nil | ||
} | ||
|
||
type MockVaultHelper struct { | ||
KVSecrets []KVSecret | ||
Lease int | ||
} | ||
|
||
func (m *MockVaultHelper) GetSecrets(path string) error { | ||
if path == "" { | ||
return fmt.Errorf("path not found: %s", path) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *MockVaultHelper) GetSecret(key string) (string, error) { | ||
for _, s := range m.Secrets() { | ||
for s.Key == key { | ||
return s.Value, nil | ||
} | ||
} | ||
|
||
return "", fmt.Errorf("key: '%s' not found", key) | ||
} | ||
|
||
func (m *MockVaultHelper) Secrets() []KVSecret { | ||
return m.KVSecrets | ||
} | ||
func (m *MockVaultHelper) LeaseDuration() int { | ||
return m.Lease | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package vault_helper | ||
|
||
import "testing" | ||
|
||
func TestMock(t *testing.T) { | ||
// Create a new MockVaultHelper | ||
mvh := &MockVaultHelper{ | ||
KVSecrets: []KVSecret{ | ||
{ | ||
Key: "key1", | ||
Value: "value1", | ||
}, | ||
}, | ||
} | ||
|
||
// Test GetSecrets | ||
t.Run("Get Empty Secrets", func(t *testing.T) { | ||
if err := mvh.GetSecrets(""); err == nil { | ||
t.Error("Expected error, got nil") | ||
} | ||
}) | ||
|
||
// Test GetSecret | ||
t.Run("Get Non-Existent Secret", func(t *testing.T) { | ||
if _, err := mvh.GetSecret("key2"); err == nil { | ||
t.Error("Expected error, got nil") | ||
} | ||
}) | ||
|
||
// Test GetSecret | ||
t.Run("Get Existing Secret", func(t *testing.T) { | ||
if _, err := mvh.GetSecret("key1"); err != nil { | ||
t.Error("Expected nil, got error") | ||
} | ||
}) | ||
} |
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
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