Skip to content

Commit

Permalink
make tests faster
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Aug 21, 2024
1 parent d5b43fc commit 7a43405
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 86 deletions.
13 changes: 0 additions & 13 deletions internal/provider/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import (
"errors"
"io"
"os"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/bw"
)

func attachmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

itemId := d.Get(attributeAttachmentItemID).(string)

existingAttachments, err := listExistingAttachments(ctx, meta.(bw.Client), itemId)
Expand All @@ -44,9 +40,6 @@ func attachmentCreate(ctx context.Context, d *schema.ResourceData, meta interfac
}

func attachmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

itemId := d.Get(attributeAttachmentItemID).(string)

obj, err := meta.(bw.Client).GetObject(ctx, bw.Object{ID: itemId, Object: bw.ObjectTypeItem})
Expand All @@ -71,9 +64,6 @@ func attachmentRead(ctx context.Context, d *schema.ResourceData, meta interface{
}

func attachmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

itemId := d.Get(attributeAttachmentItemID).(string)

return diag.FromErr(meta.(bw.Client).DeleteAttachment(ctx, itemId, d.Id()))
Expand Down Expand Up @@ -106,9 +96,6 @@ func attachmentDataFromStruct(d *schema.ResourceData, attachment bw.Attachment)

func readDataSourceAttachment() schema.ReadContextFunc {
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

itemId := d.Get(attributeAttachmentItemID).(string)

attachmentId := d.Get(attributeID).(string)
Expand Down
16 changes: 6 additions & 10 deletions internal/provider/data_source_folder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -9,28 +10,23 @@ import (
func TestAccDataSourceFolderAttributes(t *testing.T) {
ensureVaultwardenConfigured(t)

resourceName := "bitwarden_folder.foo"

resource.UnitTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigResourceFolder(),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigDataFolder(),
Check: checkObject(resourceName),
Config: tfConfigProvider() + tfConfigDataFolder(),
Check: checkObject("data.bitwarden_folder.foo_data"),
},
},
})
}

func tfConfigDataFolder() string {
return `
return fmt.Sprintf(`
data "bitwarden_folder" "foo_data" {
provider = bitwarden
search = "folder-bar"
search = "%s"
}
`
`, testFolderName)
}
25 changes: 13 additions & 12 deletions internal/provider/data_source_item_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ func TestAccDataSourceItemLoginAttributes(t *testing.T) {
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemLogin(),
Config: tfConfigProvider() + tfConfigResourceItemLogin(),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemLogin() + tfConfigDataItemLogin(),
Config: tfConfigProvider() + tfConfigResourceItemLogin() + tfConfigDataItemLogin(),
Check: checkItemLogin("data.bitwarden_item_login.foo_data"),
},
},
})
}

func TestAccDataSourceItemLoginFailsOnInexistentItem(t *testing.T) {
t.Skip("Temporarily skipping")
ensureVaultwardenConfigured(t)

resource.UnitTest(t, resource.TestCase{
Expand All @@ -46,10 +47,10 @@ func TestAccDataSourceItemLoginFailsOnWrongResourceType(t *testing.T) {
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemSecureNote(),
Config: tfConfigProvider() + tfConfigResourceItemSecureNote(),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemSecureNote() + tfConfigDataItemLoginCrossReference(),
Config: tfConfigProvider() + tfConfigResourceItemSecureNote() + tfConfigDataItemLoginCrossReference(),
ExpectError: regexp.MustCompile("Error: returned object type does not match requested object type"),
},
},
Expand All @@ -65,30 +66,30 @@ func TestAccDataSourceItemLoginBySearch(t *testing.T) {
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemLogin(),
Config: tfConfigProvider() + tfConfigResourceItemLogin(),
Check: checkItemLogin(resourceName),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemLogin() + tfConfigDataItemLoginWithSearchAndOrg("test-username"),
Config: tfConfigProvider() + tfConfigResourceItemLogin() + tfConfigDataItemLoginWithSearchAndOrg("test-username"),
Check: checkItemLogin("data.bitwarden_item_login.foo_data"),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemLogin() + tfConfigResourceItemLoginDuplicate() + tfConfigDataItemLoginWithSearchAndOrg("test-username"),
Config: tfConfigProvider() + tfConfigResourceItemLogin() + tfConfigResourceItemLoginDuplicate() + tfConfigDataItemLoginWithSearchAndOrg("test-username"),
Check: checkItemLogin("data.bitwarden_item_login.foo_data"),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemLogin() + tfConfigResourceItemLoginDuplicate() + tfConfigDataItemLoginWithSearchOnly("test-username"),
Config: tfConfigProvider() + tfConfigResourceItemLogin() + tfConfigResourceItemLoginDuplicate() + tfConfigDataItemLoginWithSearchOnly("test-username"),
ExpectError: regexp.MustCompile("Error: too many objects found"),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemLogin() + tfConfigDataItemLoginWithSearchAndOrg("missing-item"),
Config: tfConfigProvider() + tfConfigResourceItemLogin() + tfConfigDataItemLoginWithSearchAndOrg("missing-item"),
ExpectError: regexp.MustCompile("Error: no object found matching the filter"),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemSecureNote(),
Config: tfConfigProvider() + tfConfigResourceItemSecureNote(),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemSecureNote() + tfConfigDataItemLoginWithSearchAndOrg("secure-bar"),
Config: tfConfigProvider() + tfConfigResourceItemSecureNote() + tfConfigDataItemLoginWithSearchAndOrg("secure-bar"),
ExpectError: regexp.MustCompile("Error: no object found matching the filter"),
},
},
Expand Down Expand Up @@ -152,7 +153,7 @@ func tfConfigInexistentDataItemLogin() string {
data "bitwarden_item_login" "foo_data" {
provider = bitwarden
id = 123456789
id = "s123456789"
}
`
}
5 changes: 1 addition & 4 deletions internal/provider/data_source_org_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ func TestAccDataSourceOrgCollectionAttributes(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider(),
},
{
Config: tfConfigProvider() + tfConfigDataOrgCollection(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
resourceName, attributeName, regexp.MustCompile("^coll-([0-9]+)-([0-9]+)-([0-9]+)$"),
resourceName, attributeName, regexp.MustCompile("^coll-([0-9]{6})$"),
),
resource.TestMatchResourceAttr(
resourceName, attributeID, regexp.MustCompile(regExpId),
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/data_source_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestAccDataSourceOrganizationAttributes(t *testing.T) {
t.Skip("Temporarily skipping")
ensureVaultwardenConfigured(t)

resourceName := "data.bitwarden_organization.foo_data"
Expand All @@ -24,7 +23,7 @@ func TestAccDataSourceOrganizationAttributes(t *testing.T) {
Config: tfConfigProvider() + tfConfigDataOrganization(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
resourceName, attributeName, regexp.MustCompile("^org-([0-9]+)-([0-9]+)-([0-9]+)$"),
resourceName, attributeName, regexp.MustCompile("^org-([0-9]{6})$"),
),
resource.TestMatchResourceAttr(
resourceName, attributeID, regexp.MustCompile(regExpId),
Expand Down
13 changes: 0 additions & 13 deletions internal/provider/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@ import (
"errors"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/bw"
)

func objectCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

return diag.FromErr(objectOperation(ctx, d, meta.(bw.Client).CreateObject))
}

func objectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

if _, idProvided := d.GetOk(attributeID); !idProvided {
return diag.FromErr(objectSearch(ctx, d, meta))
}
Expand Down Expand Up @@ -137,16 +130,10 @@ func objectReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, meta i
}

func objectUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

return diag.FromErr(objectOperation(ctx, d, meta.(bw.Client).EditObject))
}

func objectDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

return diag.FromErr(objectOperation(ctx, d, func(ctx context.Context, secret bw.Object) (*bw.Object, error) {
return nil, meta.(bw.Client).DeleteObject(ctx, secret)
}))
Expand Down
14 changes: 10 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"os/exec"
"path/filepath"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -84,6 +83,11 @@ func New(version string) func() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("BITWARDENCLI_APPDATA_DIR", ".bitwarden/"),
},
attributeFastMode: {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
attributeExtraCACertsPath: {
Type: schema.TypeString,
Description: descriptionExtraCACertsPath,
Expand Down Expand Up @@ -124,10 +128,12 @@ func providerConfigure(version string, _ *schema.Provider) func(context.Context,
sessionKey, hasSessionKey := d.GetOk(attributeSessionKey)
if hasSessionKey {
bwClient.SetSessionKey(sessionKey.(string))
}

ctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()
// TODO: Finish this
if _, isSet := d.GetOk(attributeFastMode); isSet {
return bwClient, nil
}
}

err = ensureLoggedIn(ctx, d, bwClient)
if err != nil {
Expand Down
26 changes: 14 additions & 12 deletions internal/provider/provider_bitwarden_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@ func TestProviderRetryOnRateLimitExceeded(t *testing.T) {

diag := New(versionDev)().Configure(context.Background(), terraform.NewResourceConfigRaw(raw))

assert.True(t, diag.HasError())
assert.Equal(t, diag[0].Summary, "failing command 'status' for test purposes: Rate limit exceeded. Try again later.")
assert.Equal(t, []string{
"status",
"status",
"status",
}, commandsExecuted())
if assert.True(t, diag.HasError()) {
assert.Equal(t, diag[0].Summary, "failing command 'status' for test purposes: Rate limit exceeded. Try again later.")
assert.Equal(t, []string{
"status",
"status",
"status",
}, commandsExecuted())
}
}

func TestProviderReturnUnhandledError(t *testing.T) {
Expand All @@ -185,9 +186,10 @@ func TestProviderReturnUnhandledError(t *testing.T) {

diag := New(versionDev)().Configure(context.Background(), terraform.NewResourceConfigRaw(raw))

assert.True(t, diag.HasError())
assert.Equal(t, diag[0].Summary, "failing command 'status' for test purposes: Something unknown and bad happened.")
assert.Equal(t, []string{
"status",
}, commandsExecuted())
if assert.True(t, diag.HasError()) {
assert.Equal(t, diag[0].Summary, "failing command 'status' for test purposes: Something unknown and bad happened.")
assert.Equal(t, []string{
"status",
}, commandsExecuted())
}
}
Loading

0 comments on commit 7a43405

Please sign in to comment.