Skip to content

Commit

Permalink
Add locking for setting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Sep 15, 2021
1 parent 2df4eb2 commit fc76d54
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/provider/resource_setting_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/paultyng/go-unifi/unifi"
)

// TODO: probably need to update this to be more like setting_usg,
// using locking, and upsert, more computed, etc.

func resourceSettingMgmt() *schema.Resource {
return &schema.Resource{
Description: "`unifi_setting_mgmt` manages settings for a unifi site.",
Expand Down
24 changes: 24 additions & 0 deletions internal/provider/resource_setting_mgmt_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package provider

import (
"sync"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

var settingMgmtLock = sync.Mutex{}

func TestAccSettingMgmt_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
preCheck(t)
settingMgmtLock.Lock()
t.Cleanup(func() {
settingMgmtLock.Unlock()
})
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -21,6 +31,13 @@ func TestAccSettingMgmt_basic(t *testing.T) {

func TestAccSettingMgmt_site(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
preCheck(t)
settingMgmtLock.Lock()
t.Cleanup(func() {
settingMgmtLock.Unlock()
})
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -39,6 +56,13 @@ func TestAccSettingMgmt_site(t *testing.T) {

func TestAccSettingMgmt_sshKeys(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
preCheck(t)
settingMgmtLock.Lock()
t.Cleanup(func() {
settingMgmtLock.Unlock()
})
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down
17 changes: 14 additions & 3 deletions internal/provider/resource_setting_usg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ package provider
import (
"context"
"fmt"
"sync"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/paultyng/go-unifi/unifi"
)

var resourceSettingUsgLock = sync.Mutex{}

func resourceSettingUsgLocker(f func(context.Context, *schema.ResourceData, interface{}) diag.Diagnostics) func(context.Context, *schema.ResourceData, interface{}) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
resourceSettingUsgLock.Lock()
defer resourceSettingUsgLock.Unlock()
return f(ctx, d, meta)
}
}

func resourceSettingUsg() *schema.Resource {
return &schema.Resource{
Description: "`unifi_setting_usg` manages settings for a Unifi Security Gateway.",

CreateContext: resourceSettingUsgUpsert,
ReadContext: resourceSettingUsgRead,
UpdateContext: resourceSettingUsgUpsert,
CreateContext: resourceSettingUsgLocker(resourceSettingUsgUpsert),
ReadContext: resourceSettingUsgLocker(resourceSettingUsgRead),
UpdateContext: resourceSettingUsgLocker(resourceSettingUsgUpsert),
DeleteContext: schema.NoopContext,
Importer: &schema.ResourceImporter{
StateContext: importSiteAndID,
Expand Down
25 changes: 25 additions & 0 deletions internal/provider/resource_setting_usg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ package provider

import (
"fmt"
"sync"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

// using an additional lock to the one around the resource to avoid deadlocking accidentally
var settingUsgLock = sync.Mutex{}

func TestAccSettingUsg_mdns(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
preCheck(t)
settingUsgLock.Lock()
t.Cleanup(func() {
settingUsgLock.Unlock()
})
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -32,6 +43,13 @@ func TestAccSettingUsg_mdns(t *testing.T) {

func TestAccSettingUsg_dhcpRelay(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
preCheck(t)
settingUsgLock.Lock()
t.Cleanup(func() {
settingUsgLock.Unlock()
})
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -45,6 +63,13 @@ func TestAccSettingUsg_dhcpRelay(t *testing.T) {

func TestAccSettingUsg_site(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
preCheck(t)
settingUsgLock.Lock()
t.Cleanup(func() {
settingUsgLock.Unlock()
})
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down

0 comments on commit fc76d54

Please sign in to comment.