-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsshkeys_test.go
53 lines (43 loc) · 1.28 KB
/
sshkeys_test.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
package metadata
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
type SshkeysMockclient struct {
Resp *SSHKeysData
Err error
}
func (m *SshkeysMockclient) GetSSHKeys(ctx context.Context) (*SSHKeysData, error) {
if m.Err != nil {
return nil, m.Err
}
return m.Resp, nil
}
func TestGetSSHKeys_Success(t *testing.T) {
// Create a mock client with a successful response
mockClient := &SshkeysMockclient{
Resp: &SSHKeysData{
Users: map[string][]string{
"Root": {"ssh-randomkeyforunittestas;ldkjfqweeru", "ssh-randomkeyforunittestas;ldkjfqweerutwo"},
},
},
}
sshKeys, err := mockClient.GetSSHKeys(context.Background())
assert.NoError(t, err, "Expected no error")
assert.NotNil(t, sshKeys, "Expected non-nil SSHKeysData")
assert.Len(t, sshKeys.Users["Root"], 2, "Unexpected number of root SSH keys")
}
func TestGetSSHKeys_Error(t *testing.T) {
// Create a mock client with an error response
mockClient := &SshkeysMockclient{
Err: errors.New("mock error"),
}
// Call the GetSSHKeys method
sshKeys, err := mockClient.GetSSHKeys(context.Background())
// Assert the result
assert.Error(t, err, "Expected an error")
assert.Nil(t, sshKeys, "Expected nil SSHKeysData")
assert.EqualError(t, err, "mock error", "Unexpected error message")
}