-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding initial set of unit tests except for client.go * Add unit-test command in Makefile
- Loading branch information
1 parent
d26d2e2
commit 1bd6512
Showing
9 changed files
with
317 additions
and
2 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
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,72 @@ | ||
package metadata | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// mock client for testing purposes | ||
type InstanceMockclient struct { | ||
Resp *InstanceData | ||
Err error | ||
} | ||
|
||
func (m *InstanceMockclient) GetInstance(ctx context.Context) (*InstanceData, error) { | ||
if m.Err != nil { | ||
return nil, m.Err | ||
} | ||
return m.Resp, nil | ||
} | ||
|
||
func TestGetInstance_Success(t *testing.T) { | ||
// Create a mock client with a successful response | ||
mockClient := &InstanceMockclient{ | ||
Resp: &InstanceData{ | ||
ID: 1, | ||
Label: "test-instance", | ||
Region: "us-west", | ||
Type: "standard", | ||
HostUUID: "abc123", | ||
Tags: []string{"tag1", "tag2"}, | ||
Specs: InstanceSpecsData{ | ||
VCPUs: 2, | ||
Memory: 4096, | ||
GPUs: 0, | ||
Transfer: 2000, | ||
Disk: 50, | ||
}, | ||
Backups: InstanceBackupsData{ | ||
Enabled: true, | ||
Status: String("active"), | ||
}, | ||
}, | ||
} | ||
|
||
instance, err := mockClient.GetInstance(context.Background()) | ||
|
||
// Assert the result | ||
assert.NoError(t, err, "Expected no error") | ||
assert.NotNil(t, instance, "Expected non-nil instance") | ||
assert.Equal(t, "test-instance", instance.Label, "Unexpected instance label") | ||
} | ||
|
||
func TestGetInstance_Error(t *testing.T) { | ||
// Create a mock client with an error response | ||
mockClient := &InstanceMockclient{ | ||
Err: errors.New("mock error"), | ||
} | ||
|
||
instance, err := mockClient.GetInstance(context.Background()) | ||
|
||
assert.Error(t, err, "Expected an error") | ||
assert.Nil(t, instance, "Expected nil instance") | ||
assert.EqualError(t, err, "mock error", "Unexpected error message") | ||
} | ||
|
||
// Helper function to create a string pointer | ||
func String(s string) *string { | ||
return &s | ||
} |
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 metadata | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/netip" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// mock client for testing purposes | ||
type NetworkMockclient struct { | ||
Resp *NetworkData | ||
Err error | ||
} | ||
|
||
func (m *NetworkMockclient) GetNetwork(ctx context.Context) (*NetworkData, error) { | ||
if m.Err != nil { | ||
return nil, m.Err | ||
} | ||
return m.Resp, nil | ||
} | ||
|
||
func TestGetNetwork_Success(t *testing.T) { | ||
mockClient := &NetworkMockclient{ | ||
Resp: &NetworkData{ | ||
Interfaces: []InterfaceData{ | ||
{Label: "eth0", Purpose: "public", IPAMAddress: netip.MustParsePrefix("203.0.113.0/24")}, | ||
{Label: "eth1", Purpose: "private", IPAMAddress: netip.MustParsePrefix("192.168.1.0/24")}, | ||
}, | ||
IPv4: IPv4Data{ | ||
Public: []netip.Prefix{netip.MustParsePrefix("203.0.113.0/24")}, | ||
Private: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24")}, | ||
Shared: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")}, | ||
}, | ||
IPv6: IPv6Data{ | ||
SLAAC: netip.MustParsePrefix("2001:db8::/64"), | ||
LinkLocal: netip.MustParsePrefix("fe80::/64"), | ||
Ranges: []netip.Prefix{netip.MustParsePrefix("2001:db8::/64")}, | ||
SharedRanges: []netip.Prefix{netip.MustParsePrefix("fd00::/64")}, | ||
}, | ||
}, | ||
} | ||
|
||
network, err := mockClient.GetNetwork(context.Background()) | ||
|
||
assert.NoError(t, err, "Expected no error") | ||
assert.NotNil(t, network, "Expected non-nil network") | ||
assert.Len(t, network.Interfaces, 2, "Unexpected number of interfaces") | ||
assert.Len(t, network.IPv4.Public, 1, "Unexpected number of public IPv4 prefixes") | ||
assert.Len(t, network.IPv6.Ranges, 1, "Unexpected number of IPv6 ranges") | ||
} | ||
|
||
func TestGetNetwork_Error(t *testing.T) { | ||
mockClient := &NetworkMockclient{ | ||
Err: errors.New("mock error"), | ||
} | ||
|
||
network, err := mockClient.GetNetwork(context.Background()) | ||
|
||
assert.Error(t, err, "Expected an error") | ||
assert.Nil(t, network, "Expected nil network") | ||
assert.EqualError(t, err, "mock error", "Unexpected error message") | ||
} |
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,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: SSHKeysUserData{ | ||
Root: []string{"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") | ||
} |
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,43 @@ | ||
package metadata | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
type TokenMockclient struct { | ||
Token string | ||
GenerateTokenErr error | ||
} | ||
|
||
func (m *TokenMockclient) GenerateToken(ctx context.Context, opts ...TokenOption) (string, error) { | ||
if m.GenerateTokenErr != nil { | ||
return "", m.GenerateTokenErr | ||
} | ||
return m.Token, nil | ||
} | ||
|
||
func TestGenerateToken_Success(t *testing.T) { | ||
mockClient := &TokenMockclient{ | ||
Token: "mock-token-value", | ||
} | ||
|
||
token, err := mockClient.GenerateToken(context.Background()) | ||
|
||
assert.NoError(t, err, "Expected no error") | ||
assert.Equal(t, "mock-token-value", token, "Unexpected token") | ||
} | ||
|
||
func TestGenerateToken_Error(t *testing.T) { | ||
mockClient := &TokenMockclient{ | ||
GenerateTokenErr: errors.New("mock error"), | ||
} | ||
|
||
token, err := mockClient.GenerateToken(context.Background()) | ||
|
||
assert.Error(t, err, "Expected an error") | ||
assert.Equal(t, "", token, "Expected empty token") | ||
assert.EqualError(t, err, "mock error", "Unexpected error message") | ||
} |
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,46 @@ | ||
package metadata | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type UserdataMockClient struct { | ||
UserData string | ||
GetUserDataError error | ||
} | ||
|
||
func (m *UserdataMockClient) GetUserData(ctx context.Context) (string, error) { | ||
if m.GetUserDataError != nil { | ||
return "", m.GetUserDataError | ||
} | ||
return m.UserData, nil | ||
} | ||
|
||
func TestGetUserData_Success(t *testing.T) { | ||
mockClient := &UserdataMockClient{ | ||
UserData: base64.StdEncoding.EncodeToString([]byte("mock-user-data")), | ||
} | ||
|
||
userData, err := mockClient.GetUserData(context.Background()) | ||
|
||
assert.NoError(t, err, "Expected no error") | ||
// Note "bW9jay11c2VyLWRhdGE=" is the encoded value | ||
assert.Equal(t, "bW9jay11c2VyLWRhdGE=", userData, "Unexpected user data") | ||
} | ||
|
||
func TestGetUserData_Error(t *testing.T) { | ||
mockClient := &UserdataMockClient{ | ||
GetUserDataError: errors.New("mock error"), | ||
} | ||
|
||
userData, err := mockClient.GetUserData(context.Background()) | ||
|
||
assert.Error(t, err, "Expected an error") | ||
assert.Equal(t, "", userData, "Expected empty user data") | ||
assert.EqualError(t, err, "mock error", "Unexpected error message") | ||
} |
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,15 @@ | ||
package metadata | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWatcherWithInterval(t *testing.T) { | ||
config := watcherConfig{} | ||
WatcherWithInterval(10 * time.Minute)(&config) | ||
|
||
assert.Equal(t, 10*time.Minute, config.Interval, "Unexpected interval duration") | ||
} |