Skip to content

Commit

Permalink
Fix: improve acc tests (Twingate#596)
Browse files Browse the repository at this point in the history
Co-authored-by: Bob Lee <[email protected]>
  • Loading branch information
vmanilo and twingate-blee authored Nov 4, 2024
1 parent 041be8b commit 6940d82
Show file tree
Hide file tree
Showing 25 changed files with 1,791 additions and 1,741 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,13 @@ jobs:
needs: build
runs-on: ubuntu-latest
if: "!github.event.pull_request.head.repo.fork"
timeout-minutes: 15
strategy:
max-parallel: 2
max-parallel: 3
fail-fast: false
matrix:
terraform:
- '1.8.*'
- '1.9.*'
# - '1.9.*'
- 'latest'
steps:

Expand Down Expand Up @@ -161,6 +160,7 @@ jobs:
TWINGATE_API_TOKEN: ${{ secrets.TWINGATE_API_TOKEN }}
TEST_UNIQUE_VALUE: ${{ github.run_id }}-${{ github.run_number }}-${{ matrix.terraform }}
run: |
terraform version
make testacc
- name: Send coverage
Expand All @@ -175,13 +175,13 @@ jobs:
needs: build
runs-on: ubuntu-latest
if: "!github.event.pull_request.head.repo.fork"
timeout-minutes: 15
strategy:
max-parallel: 2
max-parallel: 3
fail-fast: false
matrix:
tofu:
- '1.6.*'
- '1.7.*'
- 'latest'

steps:
Expand Down Expand Up @@ -224,7 +224,7 @@ jobs:
cleanup:
name: Cleanup
if: "!github.event.pull_request.head.repo.fork"
if: '!github.event.pull_request.head.repo.fork && always()'
needs: [tests-acceptance, tests-acceptance-opentofu]
runs-on: ubuntu-latest
timeout-minutes: 15
Expand Down
13 changes: 6 additions & 7 deletions .github/workflows/smoketests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ jobs:
name: Matrix Acceptance Tests
needs: build
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
max-parallel: 2
max-parallel: 3
fail-fast: false
matrix:
terraform:
- '1.8.*'
- '1.9.*'
# - '1.9.*'
- 'latest'
steps:
- name: Check out code into the Go module directory
Expand Down Expand Up @@ -94,6 +93,7 @@ jobs:
TWINGATE_API_TOKEN: ${{ secrets.TWINGATE_API_TOKEN }}
TEST_UNIQUE_VALUE: ${{ github.run_id }}-${{ github.run_number }}-${{ matrix.terraform }}
run: |
terraform version
make testacc
- name: Report Status
Expand All @@ -112,14 +112,13 @@ jobs:
name: OpenTofu Matrix Acceptance Tests
needs: build
runs-on: ubuntu-latest
if: "!github.event.pull_request.head.repo.fork"
timeout-minutes: 15
strategy:
max-parallel: 2
max-parallel: 3
fail-fast: false
matrix:
tofu:
- '1.6.*'
- '1.7.*'
- 'latest'

steps:
Expand Down Expand Up @@ -162,7 +161,7 @@ jobs:
cleanup:
name: Cleanup
if: always()
if: '!github.event.pull_request.head.repo.fork && always()'
needs: [tests-acceptance, tests-acceptance-opentofu]
runs-on: ubuntu-latest
timeout-minutes: 15
Expand Down
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mkdir -p "${TEST_RESULTS}"

echo PACKAGE_NAME: "$PACKAGE_NAME"
echo "Running tests:"
go run gotest.tools/gotestsum --rerun-fails --packages "${PACKAGE_NAME}" --format standard-quiet --junitfile "${TEST_RESULTS}"/test-results.xml -- -coverpkg="${PACKAGE_NAME}" -coverprofile="${TEST_RESULTS}"/coverage.out.tmp "${PACKAGE_NAME}"
go run gotest.tools/gotestsum --rerun-fails=5 --packages "${PACKAGE_NAME}" --format standard-quiet --junitfile "${TEST_RESULTS}"/test-results.xml -- -coverpkg="${PACKAGE_NAME}" -coverprofile="${TEST_RESULTS}"/coverage.out.tmp "${PACKAGE_NAME}"
echo

echo "Generating coverage report (removing generated **/api/gen/** and *.pb.go files)"
Expand Down
86 changes: 43 additions & 43 deletions twingate/internal/test/acctests/datasource/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import (
)

func TestAccDatasourceTwingateConnector_basic(t *testing.T) {
t.Run("Test Twingate Datasource : Acc Connector Basic", func(t *testing.T) {
networkName := test.RandomName()
connectorName := test.RandomConnectorName()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() { acctests.PreCheck(t) },
CheckDestroy: acctests.CheckTwingateConnectorDestroy,
Steps: []resource.TestStep{
{
Config: testDatasourceTwingateConnector(networkName, connectorName),
Check: acctests.ComposeTestCheckFunc(
resource.TestCheckOutput("my_connector", connectorName),
resource.TestCheckOutput("my_connector_notification_status", "true"),
),
},
t.Parallel()

networkName := test.RandomName()
connectorName := test.RandomConnectorName()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() { acctests.PreCheck(t) },
CheckDestroy: acctests.CheckTwingateConnectorDestroy,
Steps: []resource.TestStep{
{
Config: testDatasourceTwingateConnector(networkName, connectorName),
Check: acctests.ComposeTestCheckFunc(
resource.TestCheckOutput("my_connector", connectorName),
resource.TestCheckOutput("my_connector_notification_status", "true"),
),
},
})
},
})
}

Expand Down Expand Up @@ -59,21 +59,21 @@ func testDatasourceTwingateConnector(remoteNetworkName, connectorName string) st
}

func TestAccDatasourceTwingateConnector_negative(t *testing.T) {
t.Run("Test Twingate Datasource : Acc Connector - does not exists", func(t *testing.T) {
connectorID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("Connector:%d", acctest.RandInt())))
t.Parallel()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() {
acctests.PreCheck(t)
},
Steps: []resource.TestStep{
{
Config: testTwingateConnectorDoesNotExists(connectorID),
ExpectError: regexp.MustCompile("failed to read connector with id"),
},
connectorID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("Connector:%d", acctest.RandInt())))

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() {
acctests.PreCheck(t)
},
Steps: []resource.TestStep{
{
Config: testTwingateConnectorDoesNotExists(connectorID),
ExpectError: regexp.MustCompile("failed to read connector with id"),
},
})
},
})
}

Expand All @@ -90,20 +90,20 @@ func testTwingateConnectorDoesNotExists(id string) string {
}

func TestAccDatasourceTwingateConnector_invalidID(t *testing.T) {
t.Run("Test Twingate Datasource : Acc Connector - failed parse ID", func(t *testing.T) {
connectorID := acctest.RandString(10)
t.Parallel()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() {
acctests.PreCheck(t)
},
Steps: []resource.TestStep{
{
Config: testTwingateConnectorDoesNotExists(connectorID),
ExpectError: regexp.MustCompile("failed to read connector with id"),
},
connectorID := acctest.RandString(10)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() {
acctests.PreCheck(t)
},
Steps: []resource.TestStep{
{
Config: testTwingateConnectorDoesNotExists(connectorID),
ExpectError: regexp.MustCompile("failed to read connector with id"),
},
})
},
})
}
45 changes: 2 additions & 43 deletions twingate/internal/test/acctests/datasource/connectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
)

func TestAccDatasourceTwingateConnectors_basic(t *testing.T) {
acctests.SetPageLimit(1)
t.Parallel()

networkName1 := test.RandomName()
networkName2 := test.RandomName()
Expand Down Expand Up @@ -69,6 +69,7 @@ func testDatasourceTwingateConnectors(networkName1, connectorName1, networkName2
}

func TestAccDatasourceTwingateConnectors_emptyResult(t *testing.T) {
t.Parallel()
prefix := acctest.RandString(10)

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -153,48 +154,6 @@ func testCheckOutputAttr(name string, index int, attr string, expected interface
}
}

func testCheckOutputNestedLen(name string, index int, attr string, length int) resource.TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()

res, ok := ms.Outputs[name]
if !ok || res == nil || res.Value == nil {
return fmt.Errorf("output '%s' not found", name)
}

list, ok := res.Value.([]interface{})
if !ok {
return fmt.Errorf("output '%s' is not a list", name)
}

if index >= len(list) {
return fmt.Errorf("index out of bounds, actual length %d", len(list))
}

item := list[index]
obj, ok := item.(map[string]interface{})
if !ok {
return fmt.Errorf("expected map, actual is %T", item)
}

actual, ok := obj[attr]
if !ok {
return fmt.Errorf("attribute '%s' not found", attr)
}

attrList, ok := actual.([]interface{})
if !ok {
return fmt.Errorf("output '%s' is not a list", attr)
}

if len(attrList) != length {
return fmt.Errorf("expected length %d, got %d", length, len(attrList))
}

return nil
}
}

func TestAccDatasourceTwingateConnectorsFilterByName(t *testing.T) {
t.Parallel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
)

func TestAccDatasourceTwingateDNSFilteringProfile_basic(t *testing.T) {
t.Parallel()

testName := "t" + acctest.RandString(6)
profileName := test.RandomName()

Expand Down
Loading

0 comments on commit 6940d82

Please sign in to comment.