-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add all unit tests and gha workflow (#985)
* Add unit tests for accountlogin, accountsettings * Add unit tests for linode/backup * Add unit tests for linode/databasebackups * Add unit tests for all database related * More unit tests * more unit tests.. * more unit tests.. * all the unit tests * Add unit tests for accountlogin, accountsettings * Add unit tests for linode/backup * Add unit tests for linode/databasebackups * Add unit tests for all database related * More unit tests * more unit tests.. * more unit tests.. * all the unit tests * Fix and stabilize unit tests * Add workflow file for GHA * remove LINODE_TOKEN in unit-tests.yml and add missing tags for integration tests * formatting and fix mistake in unit-tests.yml * Fixing failures * tag integration tests in sshkeys and add unit test
- Loading branch information
1 parent
26910fc
commit acbaf0c
Showing
42 changed files
with
2,618 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Unit Tests | ||
on: | ||
workflow_dispatch: null | ||
push: | ||
pull_request: | ||
jobs: | ||
unit_tests: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.20' | ||
|
||
- run: go version | ||
|
||
- name: Run unit tests | ||
run: make unittest |
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,44 @@ | ||
//go:build unit | ||
|
||
package accountlogins | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/linode/linodego" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestParseLogins(t *testing.T) { | ||
model := &AccountLoginFilterModel{} | ||
|
||
// Sample input login data | ||
login1 := linodego.Login{ | ||
ID: 1, | ||
Datetime: &time.Time{}, | ||
IP: "127.0.0.1", | ||
Restricted: false, | ||
Username: "user1", | ||
Status: "success", | ||
} | ||
|
||
login2 := linodego.Login{ | ||
ID: 2, | ||
Datetime: &time.Time{}, | ||
IP: "192.168.1.1", | ||
Restricted: true, | ||
Username: "user2", | ||
Status: "failure", | ||
} | ||
|
||
model.parseLogins([]linodego.Login{login1, login2}) | ||
|
||
if len(model.Logins) != 2 { | ||
t.Errorf("Expected %d logins, but got %d", 2, len(model.Logins)) | ||
} | ||
|
||
// Check if the fields of the first login in the model have been populated correctly | ||
if model.Logins[0].ID != types.Int64Value(1) { | ||
t.Errorf("Expected ID to be %d, but got %d", 1, model.Logins[0].ID) | ||
} | ||
} |
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,59 @@ | ||
//go:build unit | ||
|
||
package accountsettings | ||
|
||
import ( | ||
"github.com/linode/linodego" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func TestParseAccountSettings(t *testing.T) { | ||
// Create mock AccountSettings data | ||
mockEmail := "[email protected]" | ||
longviewSubscriptionValue := "longview-3" | ||
objectStorageValue := "active" | ||
backupsEnabledValue := true | ||
managedValue := true | ||
networkHelperValue := false | ||
|
||
mockSettings := &linodego.AccountSettings{ | ||
BackupsEnabled: backupsEnabledValue, | ||
Managed: managedValue, | ||
NetworkHelper: networkHelperValue, | ||
LongviewSubscription: &longviewSubscriptionValue, | ||
ObjectStorage: &objectStorageValue, | ||
} | ||
|
||
// Create a mock AccountSettingsModel instance | ||
model := &AccountSettingsModel{} | ||
|
||
// Call the parseAccountSettings function | ||
model.parseAccountSettings(mockEmail, mockSettings) | ||
|
||
// Check if the fields in the model have been populated correctly | ||
if model.ID != types.StringValue(mockEmail) { | ||
t.Errorf("Expected ID to be %s, but got %s", mockEmail, model.ID) | ||
} | ||
|
||
if model.LongviewSubscription != types.StringValue("longview-3") { | ||
t.Errorf("Expected LongviewSubscription to be %s, but got %s", "longview-3", model.LongviewSubscription) | ||
} | ||
|
||
if model.ObjectStorage != types.StringValue("active") { | ||
t.Errorf("Expected ObjectStorage to be %s, but got %s", "active", model.ObjectStorage) | ||
} | ||
|
||
if model.BackupsEnabed != types.BoolValue(true) { | ||
t.Errorf("Expected BackupsEnabed to be %v, but got %v", true, model.BackupsEnabed) | ||
} | ||
|
||
if model.Managed != types.BoolValue(true) { | ||
t.Errorf("Expected Managed to be %v, but got %v", true, model.Managed) | ||
} | ||
|
||
if model.NetworkHelper != types.BoolValue(false) { | ||
t.Errorf("Expected NetworkHelper to be %v, but got %v", false, model.NetworkHelper) | ||
} | ||
} |
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,50 @@ | ||
//go:build unit | ||
|
||
package backup | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"testing" | ||
|
||
"github.com/linode/linodego" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseBackups(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// Create mock data for InstanceSnapshot | ||
mockSnapshot := &linodego.InstanceSnapshot{ | ||
ID: 1, | ||
Label: "Linode Snapshot Label", | ||
Status: "successful", | ||
Type: "snapshot", | ||
Created: nil, | ||
Updated: nil, | ||
Finished: nil, | ||
Configs: []string{"config1", "config2"}, | ||
Disks: []*linodego.InstanceSnapshotDisk{}, // You can populate this with mock disk data | ||
Available: true, | ||
} | ||
|
||
mockBackupSnapshotResponse := &linodego.InstanceBackupSnapshotResponse{ | ||
Current: mockSnapshot, | ||
InProgress: mockSnapshot, | ||
} | ||
|
||
mockBackups := &linodego.InstanceBackupsResponse{ | ||
Automatic: []*linodego.InstanceSnapshot{mockSnapshot}, | ||
Snapshot: mockBackupSnapshotResponse, | ||
} | ||
|
||
linodeId := int64(123) | ||
|
||
data := &DataSourceModel{} | ||
|
||
diags := data.parseBackups(ctx, mockBackups, types.Int64Value(linodeId)) | ||
|
||
assert.False(t, diags.HasError(), "Expected no errors in diagnostics") | ||
|
||
assert.Equal(t, types.Int64Value(linodeId), data.ID) | ||
} |
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,119 @@ | ||
//go:build unit | ||
|
||
package databasebackups | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/linode/linodego" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestParseMySQLBackup(t *testing.T) { | ||
mockBackup := linodego.MySQLDatabaseBackup{ | ||
ID: 123, | ||
Label: "Scheduled - 02/04/22 11:11 UTC-XcCRmI", | ||
Type: "manual", | ||
Created: &time.Time{}, | ||
} | ||
|
||
model := DatabaseBackupModel{} | ||
|
||
model.ParseMySQLBackup(mockBackup) | ||
|
||
assert.Equal(t, types.Int64Value(123), model.ID) | ||
assert.Equal(t, types.StringValue("Scheduled - 02/04/22 11:11 UTC-XcCRmI"), model.Label) | ||
assert.Equal(t, types.StringValue("manual"), model.Type) | ||
assert.NotNil(t, model.Created) // Created field should not be nil | ||
|
||
expectedFormattedTime := mockBackup.Created.Format(time.RFC3339) | ||
assert.Equal(t, types.StringValue(expectedFormattedTime), model.Created) | ||
} | ||
|
||
func TestParseMySQLBackups(t *testing.T) { | ||
mockBackups := []linodego.MySQLDatabaseBackup{ | ||
{ | ||
ID: 1, | ||
Label: "Scheduled - 02/07/22 11:18 UTC-XcCRmI", | ||
Type: "manual", | ||
Created: &time.Time{}, | ||
}, | ||
{ | ||
ID: 2, | ||
Label: "Scheduled - 02/07/22 11:18 UTC-XcCRmI", | ||
Type: "auto", | ||
Created: &time.Time{}, | ||
}, | ||
} | ||
|
||
model := DatabaseBackupFilterModel{} | ||
|
||
model.parseMySQLBackups(mockBackups) | ||
|
||
assert.Len(t, model.Backups, len(mockBackups)) | ||
|
||
assert.Equal(t, types.Int64Value(1), model.Backups[0].ID) | ||
assert.Equal(t, types.StringValue("Scheduled - 02/07/22 11:18 UTC-XcCRmI"), model.Backups[0].Label) | ||
assert.Equal(t, types.StringValue("manual"), model.Backups[0].Type) | ||
assert.NotNil(t, model.Backups[0].Created) | ||
|
||
assert.Equal(t, types.Int64Value(2), model.Backups[1].ID) | ||
assert.Equal(t, types.StringValue("Scheduled - 02/07/22 11:18 UTC-XcCRmI"), model.Backups[1].Label) | ||
assert.Equal(t, types.StringValue("auto"), model.Backups[1].Type) | ||
assert.NotNil(t, model.Backups[1].Created) | ||
} | ||
|
||
func TestParsePostgresSQLBackup(t *testing.T) { | ||
mockBackup := linodego.PostgresDatabaseBackup{ | ||
ID: 123, | ||
Label: "Postgres Backup", | ||
Type: "auto", | ||
Created: &time.Time{}, | ||
} | ||
|
||
model := DatabaseBackupModel{} | ||
|
||
model.ParsePostgresSQLBackup(mockBackup) | ||
|
||
assert.Equal(t, types.Int64Value(123), model.ID) | ||
assert.Equal(t, types.StringValue("Postgres Backup"), model.Label) | ||
assert.Equal(t, types.StringValue("auto"), model.Type) | ||
assert.NotNil(t, model.Created) // Created field should not be nil | ||
|
||
expectedFormattedTime := mockBackup.Created.Format(time.RFC3339) | ||
assert.Equal(t, types.StringValue(expectedFormattedTime), model.Created) | ||
} | ||
|
||
func TestParsePostgresSQLBackups(t *testing.T) { | ||
mockBackups := []linodego.PostgresDatabaseBackup{ | ||
{ | ||
ID: 1, | ||
Label: "Postgres Backup 1", | ||
Type: "manual", | ||
Created: &time.Time{}, | ||
}, | ||
{ | ||
ID: 2, | ||
Label: "Postgres Backup 2", | ||
Type: "auto", | ||
Created: &time.Time{}, | ||
}, | ||
} | ||
|
||
model := DatabaseBackupFilterModel{} | ||
|
||
model.parsePostgresSQLBackups(mockBackups) | ||
|
||
assert.Len(t, model.Backups, len(mockBackups)) | ||
|
||
assert.Equal(t, types.Int64Value(1), model.Backups[0].ID) | ||
assert.Equal(t, types.StringValue("Postgres Backup 1"), model.Backups[0].Label) | ||
assert.Equal(t, types.StringValue("manual"), model.Backups[0].Type) | ||
assert.NotNil(t, model.Backups[0].Created) | ||
|
||
assert.Equal(t, types.Int64Value(2), model.Backups[1].ID) | ||
assert.Equal(t, types.StringValue("Postgres Backup 2"), model.Backups[1].Label) | ||
assert.Equal(t, types.StringValue("auto"), model.Backups[1].Type) | ||
assert.NotNil(t, model.Backups[1].Created) | ||
} |
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,40 @@ | ||
//go:build unit | ||
|
||
package databaseengines | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"testing" | ||
|
||
"github.com/linode/linodego" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseEngines(t *testing.T) { | ||
mockEngines := []linodego.DatabaseEngine{ | ||
{ | ||
ID: "mysql/8.0.26", | ||
Engine: "mysql", | ||
Version: "8.0.26", | ||
}, | ||
{ | ||
ID: "postgresql/13.0.26", | ||
Engine: "postgresql", | ||
Version: "13.0.26", | ||
}, | ||
} | ||
|
||
model := DatabaseEngineFilterModel{} | ||
|
||
model.parseEngines(mockEngines) | ||
|
||
assert.Len(t, model.Engines, len(mockEngines)) | ||
|
||
assert.Equal(t, types.StringValue("mysql/8.0.26"), model.Engines[0].ID) | ||
assert.Equal(t, types.StringValue("mysql"), model.Engines[0].Engine) | ||
assert.Equal(t, types.StringValue("8.0.26"), model.Engines[0].Version) | ||
|
||
assert.Equal(t, types.StringValue("postgresql/13.0.26"), model.Engines[1].ID) | ||
assert.Equal(t, types.StringValue("postgresql"), model.Engines[1].Engine) | ||
assert.Equal(t, types.StringValue("13.0.26"), model.Engines[1].Version) | ||
} |
Oops, something went wrong.