Skip to content

Commit eead400

Browse files
authored
Merge pull request #1 from circa10a/provider-test
Add tests
2 parents 7cc4f1d + f43cd95 commit eead400

File tree

8 files changed

+177
-18
lines changed

8 files changed

+177
-18
lines changed

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
on: [
2+
push,
3+
pull_request
4+
]
5+
name: Test
6+
jobs:
7+
test:
8+
strategy:
9+
matrix:
10+
go-version: [
11+
1.15.x
12+
]
13+
os: [
14+
ubuntu-latest,
15+
]
16+
runs-on: ${{ matrix.os }}
17+
steps:
18+
- name: Install Go
19+
uses: actions/setup-go@v2
20+
with:
21+
go-version: ${{ matrix.go-version }}
22+
- name: Checkout code
23+
uses: actions/checkout@v2
24+
- name: Test
25+
env:
26+
TF_ACC: 1
27+
run: go test ./...

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.terraform/
22
*tfstate*
3-
terraform-provider-mcbroken
3+
terraform-provider-mcbroken
4+
*.log

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ build-linux:
1515
test -d $(PLUGIN_DIR) || mkdir -p $(PLUGIN_DIR)
1616
go build -o $(PLUGIN_DIR)/$(PROVIDER_FULL_NAME)
1717

18+
test: export TF_ACC=true
19+
test:
20+
go test -v ./...
21+
1822
lint:
1923
@if ! command -v golangci-lint 1>/dev/null; then\
2024
echo "Need to install golangci-lint";\

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Base the count of your infrastucture resources on the current number of broken m
1111
* [Usage](#usage)
1212
* [Development](#development)
1313
+ [Linting](#linting)
14+
+ [Testing](#testing)
1415
+ [Mac](#mac)
1516
+ [Linux](#linux)
1617
+ [Windows](#windows)
@@ -138,6 +139,12 @@ output "user_specified_city_not_found" {
138139
make lint
139140
```
140141

142+
### Testing
143+
144+
```bash
145+
make test
146+
```
147+
141148
### Mac
142149

143150
```bash
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mcbroken
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccMcbrokenCities(t *testing.T) {
11+
brokenNumberRegex, _ := regexp.Compile(`\d{1,3}.\d{1,2}`)
12+
cityRegex, _ := regexp.Compile(`\w`)
13+
resource.ParallelTest(t, resource.TestCase{
14+
PreCheck: func() { /* no precheck needed testAccPreCheck(t) */ },
15+
ProviderFactories: testAccProviderFactories,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCheckCities(),
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttrSet(
21+
"data.mcbroken_cities.all", "broken"),
22+
resource.TestMatchResourceAttr(
23+
"data.mcbroken_cities.all", "broken", brokenNumberRegex),
24+
resource.TestCheckResourceAttrSet(
25+
"data.mcbroken_cities.all", "cities.0.city"),
26+
resource.TestCheckResourceAttrSet(
27+
"data.mcbroken_cities.all", "cities.0.broken"),
28+
resource.TestMatchResourceAttr(
29+
"data.mcbroken_cities.all", "cities.0.city", cityRegex),
30+
resource.TestMatchResourceAttr(
31+
"data.mcbroken_cities.all", "cities.0.broken", brokenNumberRegex),
32+
resource.TestCheckResourceAttrSet(
33+
"data.mcbroken_cities.all", "cities.1.city"),
34+
resource.TestCheckResourceAttrSet(
35+
"data.mcbroken_cities.all", "cities.1.broken"),
36+
resource.TestMatchResourceAttr(
37+
"data.mcbroken_cities.all", "cities.1.city", cityRegex),
38+
resource.TestMatchResourceAttr(
39+
"data.mcbroken_cities.all", "cities.1.broken", brokenNumberRegex),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func testAccCheckCities() string {
47+
return `data "mcbroken_cities" "all" {}`
48+
}

mcbroken/data_source_city_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package mcbroken
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
func TestAccMcbrokenCity(t *testing.T) {
13+
brokenNumberRegex, _ := regexp.Compile(`\d{1,3}.\d{1,2}`)
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() { /* no precheck needed testAccPreCheck(t) */ },
16+
ProviderFactories: testAccProviderFactories,
17+
Steps: []resource.TestStep{
18+
// valid city
19+
{
20+
Config: testAccCheckCity("Dallas"),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttr(
23+
"data.mcbroken_city.dallas", "city", "dallas"),
24+
resource.TestCheckResourceAttrSet(
25+
"data.mcbroken_city.dallas", "broken"),
26+
resource.TestMatchResourceAttr(
27+
"data.mcbroken_city.dallas", "broken", brokenNumberRegex),
28+
),
29+
},
30+
// invalid city
31+
{
32+
Config: testAccCheckCity("not_found"),
33+
Check: resource.ComposeTestCheckFunc(
34+
resource.TestCheckResourceAttr(
35+
"data.mcbroken_city.not_found", "city", "not_found"),
36+
resource.TestCheckResourceAttr(
37+
"data.mcbroken_city.not_found", "broken", "-1"),
38+
),
39+
},
40+
},
41+
})
42+
}
43+
44+
func testAccCheckCity(city string) string {
45+
return fmt.Sprintf(`
46+
data "mcbroken_city" "%[1]v" {
47+
city = "%[1]v"
48+
}
49+
`, strings.ToLower(city))
50+
}

mcbroken/provider.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package mcbroken
22

33
import (
44
"context"
5-
"net/url"
65

76
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
99
)
1010

1111
// Provider -
1212
func Provider() *schema.Provider {
1313
return &schema.Provider{
1414
Schema: map[string]*schema.Schema{
1515
"url": {
16-
Type: schema.TypeString,
17-
Optional: true,
18-
Default: "https://mcbroken.com/stats.json",
16+
Type: schema.TypeString,
17+
Optional: true,
18+
Default: "https://mcbroken.com/stats.json",
19+
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
1920
},
2021
},
2122
DataSourcesMap: map[string]*schema.Resource{
@@ -29,18 +30,6 @@ func Provider() *schema.Provider {
2930
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
3031
var diags diag.Diagnostics
3132
providerConfig := make(map[string]interface{})
32-
mcbrokenEndpoint := d.Get("url").(string)
33-
34-
_, err := url.Parse(mcbrokenEndpoint)
35-
if err != nil {
36-
diags = append(diags, diag.Diagnostic{
37-
Severity: diag.Error,
38-
Summary: "Unable to parse url",
39-
Detail: "Mcbroken endpoint provided is not a valid url",
40-
})
41-
return nil, diags
42-
}
43-
44-
providerConfig["url"] = mcbrokenEndpoint
33+
providerConfig["url"] = d.Get("url").(string)
4534
return providerConfig, diags
4635
}

mcbroken/provider_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mcbroken
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
var testAccProviders map[string]*schema.Provider
10+
var testAccProvider *schema.Provider
11+
var testAccProviderFactories map[string]func() (*schema.Provider, error)
12+
13+
func init() {
14+
testAccProvider = Provider()
15+
testAccProviders = map[string]*schema.Provider{
16+
"mcbroken": testAccProvider,
17+
}
18+
testAccProviderFactories = map[string]func() (*schema.Provider, error){
19+
"mcbroken": func() (*schema.Provider, error) {
20+
return testAccProvider, nil
21+
},
22+
}
23+
}
24+
25+
func TestProvider(t *testing.T) {
26+
if err := testAccProvider.InternalValidate(); err != nil {
27+
t.Fatalf("err: %s", err)
28+
}
29+
}
30+
31+
func TestProvider_Impl(t *testing.T) {
32+
var _ *schema.Provider = Provider()
33+
}

0 commit comments

Comments
 (0)