Skip to content

Commit 2f0600d

Browse files
fix: add/update/fix acceptance tests (#16)
1 parent d24087c commit 2f0600d

13 files changed

+324
-83
lines changed

.github/workflows/test.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: test
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- "*"
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: read
12+
13+
defaults:
14+
run:
15+
shell: pwsh
16+
17+
jobs:
18+
test:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
23+
- name: Unshallow
24+
run: git fetch --prune --unshallow
25+
- name: Setup Go
26+
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
27+
with:
28+
go-version-file: 'go.mod'
29+
cache: true
30+
- id: terraform_version
31+
name: Terraform Version
32+
run: ./build/scripts/Get-TerraformVersion.ps1
33+
- name: Setup Terraform
34+
uses: hashicorp/setup-terraform@v3
35+
with:
36+
terraform_version: ${{ steps.terraform_version.outputs.value }}
37+
- name: Run Acceptance Tests
38+
run: |
39+
$env:TF_ACC = 1
40+
go test -v ./...
41+
env:
42+
STACK_OVERFLOW_ACCESS_TOKEN: ${{ secrets.STACK_OVERFLOW_ACCESS_TOKEN }}
43+
STACK_OVERFLOW_API_URL: ${{ secrets.STACK_OVERFLOW_API_URL }}

.terraform-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.12.2

CONTRIBUTING.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Overview
2+
3+
This file will detail instructions on how to develop the Stack Overflow Terraform provider plugin locally.
4+
5+
The original [legacy] documentation used for building custom Terraform providers can be found here:
6+
7+
[https://www.hashicorp.com/en/blog/writing-custom-terraform-providers](https://www.hashicorp.com/en/blog/writing-custom-terraform-providers)
8+
9+
## Getting started
10+
11+
Install Go, ensure that the version you are using meets or exceeds the version listed in the [go.mod](./go.mod) file.
12+
13+
**Build:**
14+
15+
> Test the built executable after building to ensure that it runs correctly.
16+
17+
*Windows:*
18+
19+
```
20+
go build -o terraform-provider-stackoverflow.exe
21+
./terraform-provider-stackoverflow.exe
22+
```
23+
24+
*Linux*
25+
26+
```
27+
go build -o terraform-provider-stackoverflow
28+
./terraform-provider-stackoverflow
29+
```
30+
31+
## Local Development Override
32+
33+
To use the locally built provider, override the `terraform.rc` (Windows) or `.terraformrc` (Linux) file with the following:
34+
35+
`terraform.rc`:
36+
```
37+
provider_installation {
38+
dev_overrides {
39+
"StackExchange/stackoverflow" = "~/source/GitHub/StackExchange/terraform-provider-stackoverflow"
40+
}
41+
42+
direct {}
43+
}
44+
```
45+
46+
> Note, make sure the provider path matches where your actual repository is located.
47+
> The `terraform.rc` file is located in the user's `%APPDATA%` directory on Windows and `.terraformrc` file is located in the user's home directory (`~`) on Linux
48+
49+
Once you have updated the `terraform.rc` file, you can setup a local testing repository and reference the Stack Overflow Terraform provider. You _DO NOT_ need to run `terraform init` when using local development provider overrides! Also, if you encounter any errors, ensure that you remove your `.terraform` folder and `.terraform.lock.hcl` to prevent any version conflicts.
50+
51+
## Acceptance Tests
52+
53+
Acceptence tests (or integration tests) are automatically run from the [test.yml](./github/workflows/test.yml) GitHub Actions Workflow for all pushes and pull requests against a developmentment Stack Overflow Enterprise (SOE) instance. This instance needs to have the following configurations applied to ensure that the tests do not fail quality control checks that prevent duplicated content.
54+
55+
* Disable content length checking, `QualityChecks.Questions.Rejections.QualityThreshold`: `1`
56+
* Disable duplicate content checking, `Questions.Testing.EnableDuplicateCheckForQuestions`: `False`
57+
58+
SOE site settings can be accessed via the `/developer/site-settings` route. Additional details can be found on the documentation help site: [https://stackoverflowteams.help/en/articles/9859814-question-quality-and-duplicate-checks](https://stackoverflowteams.help/en/articles/9859814-question-quality-and-duplicate-checks)
59+
60+
> For details on setting up authentication, check the [docs/index.md#authentication-and-configuration](./docs/index.md#authentication-and-configuration) section.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$terraformVersion = (Get-Content -Path '.terraform-version').Trim()
2+
3+
if ($null -ne $env:CI) {
4+
Write-Output -InputObject "value=$terraformVersion" >> $env:GITHUB_OUTPUT
5+
}

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@ The migration steps should be as follows:
9898
1. Remove all usage of the `filter` field from resources
9999
1. Remove usage of the `default_tags` field from the provider (users may opt to use a local variable with default tags and then subsequently merge that list with each resource's tag values)
100100
1. Remove usage of the `team_name` field from the provider. Users should set the `base_url` field or set the `STACK_OVERFLOW_API_URL` environment variable instead. For example, if the previous configuration only set the `team_name` field to the value `myteam`, the `base_url` field should now be set to `https://api.stackoverflowteams.com/v3/teams/myteam/`
101+
1. Update all `stackoveflow_article` `article_type` fields to one of the following: [`knowledgeArticle`, `announcement`, `policy`, `howToGuide`]

stackoverflow/client/model_answer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ type Answer[T string | Tag] struct {
99
QuestionID int `json:"questionId"`
1010
Title string `json:"title"`
1111
Tags []T `json:"tags"`
12+
IsDeleted bool `json:"isDeleted"`
1213
}

stackoverflow/client/model_article.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ type Article[T string | Tag] struct {
77
BodyMarkdown string `json:"bodyMarkdown"`
88
Title string `json:"title"`
99
Tags []T `json:"tags"`
10+
IsDeleted bool `json:"isDeleted"`
1011
}

stackoverflow/client/model_collection.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ type Collection struct {
66
ContentIds []int `json:"contentIds"`
77
Description string `json:"description"`
88
Title string `json:"title"`
9+
IsDeleted bool `json:"isDeleted"`
910
}

stackoverflow/client/model_question.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ type Question[T string | Tag] struct {
77
Preview bool `json:"preview"`
88
Title string `json:"title"`
99
Tags []T `json:"tags"`
10+
IsDeleted bool `json:"isDeleted"`
1011
}

stackoverflow/resource_answer_test.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,82 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1111
)
1212

13-
func TestStackOverflowAnswer(t *testing.T) {
13+
func TestAccStackOverflowAnswer(t *testing.T) {
14+
resourceType := "stackoverflow_answer"
15+
resourceName := "test"
16+
resourceIdentifier := fmt.Sprintf("%s.%s", resourceType, resourceName)
17+
1418
resource.Test(t, resource.TestCase{
15-
PreCheck: func() { testAccPreCheck(t) },
16-
Providers: testAccProviders,
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testAccStackOverflowAnswerCheckDestroy(resourceIdentifier),
1722
Steps: []resource.TestStep{
1823
{
19-
Config: testStackOverflowAnswerConfig(),
24+
Config: testAccStackOverflowAnswerConfig(resourceType, resourceName, ""),
25+
Check: resource.ComposeTestCheckFunc(
26+
testAccStackOverflowAnswerCheckExists(resourceIdentifier),
27+
),
28+
},
29+
{
30+
Config: testAccStackOverflowAnswerConfig(resourceType, resourceName, " Updated"),
2031
Check: resource.ComposeTestCheckFunc(
21-
testStackOverflowAnswerExists("stackoverflow_answer.test"),
32+
testAccStackOverflowAnswerCheckExists(resourceIdentifier),
2233
),
2334
},
2435
},
2536
})
2637
}
2738

28-
func testStackOverflowAnswerConfig() string {
29-
return `resource "stackoverflow_answer" "test" {
30-
question_id = 58
31-
body_markdown = "Unit test."
32-
}`
39+
func testAccStackOverflowAnswerConfig(resourceType, resourceName, update string) string {
40+
return fmt.Sprintf(`resource "stackoverflow_question" "test" {
41+
title = "How do I run the Terraform Provider Acceptance Tests?"
42+
body_markdown = "I want to run the Terraform Provider for Stack Overflow acceptance tests, how do I do that?"
43+
tags = ["test"]
44+
}
45+
resource "%s" "%s" {
46+
question_id = stackoverflow_question.test.id
47+
body_markdown = "Unit test%s."
48+
}`, resourceType, resourceName, update)
3349
}
3450

35-
func testStackOverflowAnswerDestroy(s *terraform.State) error {
36-
c := testAccProvider.Meta().(*so.Client)
51+
func testAccStackOverflowAnswerCheckDestroy(resourceIdentifier string) resource.TestCheckFunc {
52+
return func(s *terraform.State) error {
53+
resourceState, found := s.RootModule().Resources[resourceIdentifier]
3754

38-
for _, resource := range s.RootModule().Resources {
39-
if resource.Type != "stackoverflow_answer" {
40-
continue
55+
if !found {
56+
return fmt.Errorf("No resource found with id %s", resourceIdentifier)
4157
}
4258

43-
answerID, err := strconv.Atoi(resource.Primary.ID)
59+
if resourceState.Primary.ID == "" {
60+
return fmt.Errorf("Resource with ID is empty for %s", resourceIdentifier)
61+
}
62+
63+
answerID, err := strconv.Atoi(resourceState.Primary.ID)
4464
if err != nil {
4565
return err
4666
}
4767

48-
questionID, err := strconv.Atoi(resource.Primary.Attributes["question_id"])
68+
questionID, err := strconv.Atoi(resourceState.Primary.Attributes["question_id"])
4969
if err != nil {
5070
return err
5171
}
5272

53-
err = c.DeleteAnswer(questionID, answerID)
73+
c := testAccProvider.Meta().(*so.Client)
74+
answer, err := c.GetAnswer(&questionID, &answerID)
75+
5476
if err != nil {
5577
return err
5678
}
57-
}
5879

59-
return nil
80+
if answer != nil && !answer.IsDeleted {
81+
return fmt.Errorf("Resource still exists for %s", resourceIdentifier)
82+
}
83+
84+
return nil
85+
}
6086
}
6187

62-
func testStackOverflowAnswerExists(n string) resource.TestCheckFunc {
88+
func testAccStackOverflowAnswerCheckExists(n string) resource.TestCheckFunc {
6389
return func(s *terraform.State) error {
6490
resource, ok := s.RootModule().Resources[n]
6591

0 commit comments

Comments
 (0)