Skip to content

Commit

Permalink
Merge pull request #152 from maxlaverse/speed_up_some_tests
Browse files Browse the repository at this point in the history
speed up some tests
  • Loading branch information
maxlaverse authored Aug 25, 2024
2 parents c4ff7e2 + c37e92e commit 6dc6e27
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -coverprofile=profile.cov -v ./...
run: go test -failfast -coverprofile=profile.cov -v ./...
env:
VAULTWARDEN_HOST: "127.0.0.1"
VAULTWARDEN_PORT: "8080"
Expand Down
14 changes: 2 additions & 12 deletions internal/provider/data_source_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceAttachmentAttributes(t *testing.T) {
func TestAccDataSourceAttachment(t *testing.T) {
ensureVaultwardenConfigured(t)

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -22,16 +22,6 @@ func TestAccDataSourceAttachmentAttributes(t *testing.T) {
"data.bitwarden_attachment.foo_data", attributeAttachmentContent, regexp.MustCompile(`^Hello, I'm a text attachment$`),
),
},
},
})
}

func TestAccDataSourceAttachmentFailsOnInvalidConfiguration(t *testing.T) {
ensureVaultwardenConfigured(t)

resource.UnitTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigResourceAttachment("fixtures/attachment1.txt") + tfConfigDataAttachmentInexistent(),
ExpectError: regexp.MustCompile("Error: attachment not found"),
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/data_source_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestAccDataSourceFolderAttributes(t *testing.T) {
ensureVaultwardenConfigured(t)

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down
18 changes: 6 additions & 12 deletions internal/provider/data_source_item_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestAccDataSourceItemLoginAttributes(t *testing.T) {
ensureVaultwardenConfigured(t)

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -21,16 +21,6 @@ func TestAccDataSourceItemLoginAttributes(t *testing.T) {
Config: tfConfigProvider() + tfConfigResourceItemLogin() + tfConfigDataItemLogin(),
Check: checkItemLogin("data.bitwarden_item_login.foo_data"),
},
},
})
}

func TestAccDataSourceItemLoginFailsOnInexistentItem(t *testing.T) {
ensureVaultwardenConfigured(t)

resource.UnitTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigInexistentDataItemLogin(),
ExpectError: regexp.MustCompile("Error: object not found"),
Expand Down Expand Up @@ -59,7 +49,7 @@ func TestAccDataSourceItemLoginFailsOnWrongResourceType(t *testing.T) {
func TestAccDataSourceItemLoginBySearch(t *testing.T) {
ensureVaultwardenConfigured(t)

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -81,6 +71,10 @@ func TestAccDataSourceItemLoginBySearch(t *testing.T) {
Config: tfConfigProvider() + tfConfigResourceItemLogin() + tfConfigDataItemLoginWithSearchAndOrg("missing-item"),
ExpectError: regexp.MustCompile("Error: no object found matching the filter"),
},
// Test: search for a secure note item with a login data source should fail
{
Config: tfConfigProvider(),
},
{
Config: tfConfigProvider() + tfConfigResourceItemSecureNote(),
},
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/data_source_item_secure_note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ func TestAccDataSourceItemSecureNote(t *testing.T) {

resourceName := "data.bitwarden_item_secure_note.foo_data"

resource.UnitTest(t, resource.TestCase{
ProviderFactories: providerFactories,
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
PreventPostDestroyRefresh: false,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigResourceItemSecureNote(),
Expand Down
7 changes: 2 additions & 5 deletions internal/provider/data_source_org_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceOrgCollectionAttributes(t *testing.T) {
func TestAccDataSourceOrgCollection(t *testing.T) {
ensureVaultwardenConfigured(t)

resourceName := "data.bitwarden_org_collection.foo_data"

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider(),
},
{
Config: tfConfigProvider() + tfConfigDataOrgCollection(),
Check: resource.ComposeTestCheckFunc(
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/data_source_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ func TestAccDataSourceOrganizationAttributes(t *testing.T) {

resourceName := "data.bitwarden_organization.foo_data"

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider(),
},
{
Config: tfConfigProvider() + tfConfigDataOrganization(),
Check: resource.ComposeTestCheckFunc(
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccProviderAuthUsernamePassword(t *testing.T) {
invalidPassword := usernamePasswordTestProvider(testEmail, "incorrect-password")
invalidAccount := usernamePasswordTestProvider("[email protected]", testPassword)

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestAccProviderAuthSessionKey(t *testing.T) {
validProvider := sessionKeyTestProvider(testEmail, bwTestClient(t).GetSessionKey())
invalidProvider := sessionKeyTestProvider(testEmail, "invalid-session-key")

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down
14 changes: 9 additions & 5 deletions internal/provider/provider_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ var isUserCreated bool
var userMu sync.Mutex

func init() {
testUniqueIdentifier = fmt.Sprintf("%02d%02d%02d", time.Now().Hour(), time.Now().Minute(), time.Now().Second())

host := os.Getenv("VAULTWARDEN_HOST")
port := os.Getenv("VAULTWARDEN_PORT")

Expand All @@ -59,7 +57,9 @@ func init() {
if len(port) == 0 {
port = "8080"
}

testServerURL = fmt.Sprintf("http://%s:%s/", host, port)
testUniqueIdentifier = fmt.Sprintf("%02d%02d%02d", time.Now().Hour(), time.Now().Minute(), time.Now().Second())
}

func ensureVaultwardenConfigured(t *testing.T) {
Expand Down Expand Up @@ -147,7 +147,7 @@ func createTestUserResources(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Log("Synced Bitwarden client")
t.Log("Synced test client")
}

func bwTestClient(t *testing.T) bw.Client {
Expand Down Expand Up @@ -176,23 +176,27 @@ func bwTestClient(t *testing.T) bw.Client {
if status.Status == bw.StatusUnauthenticated {

retries := 0
for retries < 3 {
for {
err = client.LoginWithPassword(context.Background(), testEmail, testPassword)
if err != nil {
// Retry if the user creation hasn't been fully taken into account yet
if retries < 3 {
retries++
time.Sleep(1 * time.Second)
t.Log("Account creation not taken into account yet, retrying...")
time.Sleep(time.Duration(retries) * time.Second)
continue
}
t.Fatal(err)
}
break
}
} else if status.Status == bw.StatusLocked {
err = client.Unlock(context.Background(), testPassword)
if err != nil {
t.Fatal(err)
}
} else {
t.Logf("Test client already logged-in: %s", status.Status)
}
return client
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestAccResourceFolder(t *testing.T) {
resourceName := "bitwarden_folder.foo"
var objectID string

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_item_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccResourceItemLoginAttributes(t *testing.T) {
resourceName := "bitwarden_item_login.foo"
var objectID string

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -42,7 +42,7 @@ func TestAccMissingResourceItemLoginIsRecreated(t *testing.T) {

var objectID string

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_item_secure_note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccResourceItemSecureNote(t *testing.T) {
resourceName := "bitwarden_item_secure_note.foo"
var objectID string

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_org_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestAccResourceOrgCollection(t *testing.T) {
resourceName := "bitwarden_org_collection.foo_org_col"
var objectID string

resource.UnitTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down

0 comments on commit 6dc6e27

Please sign in to comment.