-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathkeyring_mock.go
71 lines (64 loc) · 1.67 KB
/
keyring_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package keyring
type mockProvider struct {
mockStore map[string]map[string]string
mockError error
}
// Set stores user and pass in the keyring under the defined service
// name.
func (m *mockProvider) Set(service, user, pass string) error {
if m.mockError != nil {
return m.mockError
}
if m.mockStore == nil {
m.mockStore = make(map[string]map[string]string)
}
if m.mockStore[service] == nil {
m.mockStore[service] = make(map[string]string)
}
m.mockStore[service][user] = pass
return nil
}
// Get gets a secret from the keyring given a service name and a user.
func (m *mockProvider) Get(service, user string) (string, error) {
if m.mockError != nil {
return "", m.mockError
}
if b, ok := m.mockStore[service]; ok {
if v, ok := b[user]; ok {
return v, nil
}
}
return "", ErrNotFound
}
// Delete deletes a secret, identified by service & user, from the keyring.
func (m *mockProvider) Delete(service, user string) error {
if m.mockError != nil {
return m.mockError
}
if m.mockStore != nil {
if _, ok := m.mockStore[service]; ok {
if _, ok := m.mockStore[service][user]; ok {
delete(m.mockStore[service], user)
return nil
}
}
}
return ErrNotFound
}
// DeleteAll deletes all secrets for a given service
func (m *mockProvider) DeleteAll(service string) error {
if m.mockError != nil {
return m.mockError
}
delete(m.mockStore, service)
return nil
}
// MockInit sets the provider to a mocked memory store
func MockInit() {
provider = &mockProvider{}
}
// MockInitWithError sets the provider to a mocked memory store
// that returns the given error on all operations
func MockInitWithError(err error) {
provider = &mockProvider{mockError: err}
}