Skip to content

Commit 24ca7e8

Browse files
committed
makefile refactor for help. Cleanup. Seeing a panic
1 parent 6468ff6 commit 24ca7e8

File tree

6 files changed

+83
-42
lines changed

6 files changed

+83
-42
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ terraform-provider-aap
66
*.tfstate
77
*.tfstate.*
88
*.tfrc
9+
*.txt
910

1011
# Crash log files
1112
crash.log
@@ -38,3 +39,5 @@ config.tfrc
3839

3940
.env
4041
.DS_Store
42+
.tools
43+
.vscode

Makefile

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1+
include makefiles/golangci.mk
2+
13
.PHONY: build test lint
24

5+
36
default: build
47

5-
build:
8+
build: ## Compile the Go package. This is the default.
69
@echo "==> Building package..."
710
go build
811

9-
lint:
10-
@echo "==> Checking source code against linters..."
11-
golangci-lint run -v ./...
12-
13-
test:
12+
test: ## Execute all unit tests with verbose output."
1413
@echo "==> Running unit tests..."
1514
go test -v ./...
1615

17-
testacc:
18-
@echo "==> Running acceptance tests..."
19-
TF_ACC=1 AAP_HOST="https://localhost:8043" AAP_INSECURE_SKIP_VERIFY=true go test -count=1 -v ./...
20-
21-
testacc-aapdev:
16+
testacc: ## Run acceptance tests against local aap-dev instance (https://localhost:9080)."
2217
@echo "==> Running acceptance tests..."
2318
TF_ACC=1 AAP_HOST="http://localhost:9080" go test -count=1 -v ./...
2419

25-
gofmt:
26-
@echo "==> Format code using gofmt..."
27-
gofmt -s -w internal/provider
28-
29-
generatedocs:
20+
generatedocs: ## Format example Terraform configurations and generate plugin documentation."
3021
@echo "==> Formatting examples and generating docs..."
3122
terraform fmt -recursive ./examples/
3223
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs generate
24+
25+
.PHONY: help
26+
help: ## Show this help message
27+
@grep -hE '^[a-zA-Z0-9._-]+:.*?##' $(MAKEFILE_LIST) | \
28+
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-24s\033[0m %s\n", $$1, $$2}' | \
29+
sort

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Run `make test`
4242

4343
Acceptance tests apply test terraform configurations to a running AAP instance and make changes to resources in that instance, use with caution!
4444

45-
To run acceptance tests locally, start a local AAP instance following the [docker-compose instructions for local AWX development](https://github.com/ansible/awx/blob/devel/tools/docker-compose/README.md). Create an admin user for the AAP instance and save the credentials to these environment variables:
45+
To run acceptance tests locally, start a local AAP instance following the [The AAP development environment](https://github.com/ansible/aap-dev/blob/main/README.md). Create an admin user for the AAP instance and save the credentials to these environment variables:
4646

4747
```bash
4848
export AAP_USERNAME=<your admin username>

internal/provider/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ func (c *AAPClient) Create(path string, data io.Reader) ([]byte, diag.Diagnostic
150150

151151
// Get sends a GET request to the provided path, checks for errors, and returns the response body with any errors as diagnostics.
152152
func (c *AAPClient) GetWithStatus(path string) ([]byte, diag.Diagnostics, int) {
153+
if c == nil {
154+
return nil, diag.Diagnostics{diag.NewErrorDiagnostic("AAPClient is nil", "Client was not initialized properly")}, 0
155+
}
153156
getResponse, body, err := c.doRequest("GET", path, nil)
154157
diags := ValidateResponse(getResponse, body, err, []int{http.StatusOK})
155158
return body, diags, getResponse.StatusCode

internal/provider/group_resource_test.go

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import (
1919
"github.com/hashicorp/terraform-plugin-testing/terraform"
2020
)
2121

22+
var (
23+
reGroupURLPattern = regexp.MustCompile(`^/api(/controller)?/v2/groups/\d+/$`)
24+
reInvalidVars = regexp.MustCompile("Input type `str` is not a dictionary")
25+
)
26+
2227
func TestGroupResourceSchema(t *testing.T) {
2328
t.Parallel()
2429

@@ -35,14 +40,13 @@ func TestGroupResourceSchema(t *testing.T) {
3540

3641
// Validate the schema
3742
diagnostics := schemaResponse.Schema.ValidateImplementation(ctx)
38-
3943
if diagnostics.HasError() {
4044
t.Fatalf("Schema validation diagnostics: %+v", diagnostics)
4145
}
4246
}
4347

4448
func TestGroupResourceCreateRequestBody(t *testing.T) {
45-
var testTable = []struct {
49+
testCases := []struct {
4650
name string
4751
input GroupResourceModel
4852
expected []byte
@@ -97,14 +101,14 @@ func TestGroupResourceCreateRequestBody(t *testing.T) {
97101
},
98102
}
99103

100-
for _, test := range testTable {
101-
t.Run(test.name, func(t *testing.T) {
102-
actual, diags := test.input.CreateRequestBody()
104+
for _, testCase := range testCases {
105+
t.Run(testCase.name, func(t *testing.T) {
106+
actual, diags := testCase.input.CreateRequestBody()
103107
if diags.HasError() {
104108
t.Fatal(diags.Errors())
105109
}
106-
if !bytes.Equal(test.expected, actual) {
107-
t.Errorf("Expected (%s) not equal to actual (%s)", test.expected, actual)
110+
if !bytes.Equal(testCase.expected, actual) {
111+
t.Errorf("Expected (%s) not equal to actual (%s)", testCase.expected, actual)
108112
}
109113
})
110114
}
@@ -114,7 +118,15 @@ func TestGroupResourceParseHttpResponse(t *testing.T) {
114118
jsonError := diag.Diagnostics{}
115119
jsonError.AddError("Error parsing JSON response from AAP", "invalid character 'N' looking for beginning of value")
116120

117-
var testTable = []struct {
121+
const groupJSON = `{
122+
"inventory": 1,
123+
"description": "A basic test group",
124+
"name": "group1",
125+
"url": "/api/v2/groups/1/",
126+
"variables": "{\"foo\":\"bar\",\"nested\":{\"foobar\":\"baz\"}}"
127+
}`
128+
129+
testCases := []struct {
118130
name string
119131
input []byte
120132
expected GroupResourceModel
@@ -139,9 +151,8 @@ func TestGroupResourceParseHttpResponse(t *testing.T) {
139151
errors: diag.Diagnostics{},
140152
},
141153
{
142-
name: "test with all values",
143-
input: []byte(`{"inventory":1,"description":"A basic test group","name":"group1","url":"/api/v2/groups/1/",` +
144-
`"variables":"{\"foo\":\"bar\",\"nested\":{\"foobar\":\"baz\"}}"}`),
154+
name: "test with all values",
155+
input: []byte(groupJSON),
145156
expected: GroupResourceModel{
146157
InventoryId: types.Int64Value(1),
147158
Id: types.Int64Value(0),
@@ -154,15 +165,15 @@ func TestGroupResourceParseHttpResponse(t *testing.T) {
154165
},
155166
}
156167

157-
for _, test := range testTable {
158-
t.Run(test.name, func(t *testing.T) {
168+
for _, testCase := range testCases {
169+
t.Run(testCase.name, func(t *testing.T) {
159170
resource := GroupResourceModel{}
160-
diags := resource.ParseHttpResponse(test.input)
161-
if !test.errors.Equal(diags) {
162-
t.Errorf("Expected error diagnostics (%s), actual was (%s)", test.errors, diags)
171+
diags := resource.ParseHttpResponse(testCase.input)
172+
if !testCase.errors.Equal(diags) {
173+
t.Errorf("Expected error diagnostics (%s), actual was (%s)", testCase.errors, diags)
163174
}
164-
if !reflect.DeepEqual(test.expected, resource) {
165-
t.Errorf("Expected (%s) not equal to actual (%s)", test.expected, resource)
175+
if !reflect.DeepEqual(testCase.expected, resource) {
176+
t.Errorf("Expected (%s) not equal to actual (%s)", testCase.expected, resource)
166177
}
167178
})
168179
}
@@ -172,30 +183,29 @@ func TestGroupResourceParseHttpResponse(t *testing.T) {
172183

173184
func TestAccGroupResource(t *testing.T) {
174185
var groupApiModel GroupAPIModel
175-
var description = "A test group"
176-
var variables = "{\"foo\": \"bar\"}"
177186
inventoryName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
178187
groupName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
179188
updatedName := "updated" + groupName
189+
description := "A test group"
190+
variables := "{\"foo\": \"bar\"}"
180191

181192
resource.Test(t, resource.TestCase{
182193
PreCheck: func() { testAccPreCheck(t) },
183194
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
184195
Steps: []resource.TestStep{
185-
// Invalid variables testing
196+
// Invalid variables testing.
186197
{
187198
Config: testAccGroupResourceBadVariables(inventoryName, updatedName),
188-
ExpectError: regexp.MustCompile("Input type `str` is not a dictionary"),
199+
ExpectError: reInvalidVars,
189200
},
190-
// Create and Read testing
191201
{
192202
Config: testAccGroupResourceMinimal(inventoryName, groupName),
193203
Check: resource.ComposeAggregateTestCheckFunc(
194204
testAccCheckGroupResourceExists("aap_group.test", &groupApiModel),
195205
testAccCheckGroupResourceValues(&groupApiModel, groupName, "", ""),
196206
resource.TestCheckResourceAttr("aap_group.test", "name", groupName),
197207
resource.TestCheckResourceAttrPair("aap_group.test", "inventory_id", "aap_inventory.test", "id"),
198-
resource.TestMatchResourceAttr("aap_group.test", "url", regexp.MustCompile("^/api(/controller)?/v2/groups/[0-9]*/$")),
208+
resource.TestMatchResourceAttr("aap_group.test", "url", reGroupURLPattern),
199209
),
200210
},
201211
{
@@ -207,7 +217,7 @@ func TestAccGroupResource(t *testing.T) {
207217
resource.TestCheckResourceAttrPair("aap_group.test", "inventory_id", "aap_inventory.test", "id"),
208218
resource.TestCheckResourceAttr("aap_group.test", "description", description),
209219
resource.TestCheckResourceAttr("aap_group.test", "variables", variables),
210-
resource.TestMatchResourceAttr("aap_group.test", "url", regexp.MustCompile("^/api(/controller)?/v2/groups/[0-9]*/$")),
220+
resource.TestMatchResourceAttr("aap_group.test", "url", reGroupURLPattern),
211221
),
212222
},
213223
},

makefiles/golangci.mk

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
ifndef GOLANGCI_LINT_MK_INCLUDED
3+
4+
TOOLS_DIR := $(CURDIR)/.tools
5+
GOLANGCI_LINT := $(TOOLS_DIR)/golangci-lint
6+
GOLANGCI_LINT_VERSION ?= v1.60.1
7+
8+
export GOLANGCI_LINT
9+
10+
$(GOLANGCI_LINT):
11+
@echo "==> Installing golangci-lint into $(TOOLS_DIR)..."
12+
@mkdir -p $(TOOLS_DIR)
13+
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TOOLS_DIR) $(GOLANGCI_LINT_VERSION)
14+
15+
.PHONY: lint-tools
16+
lint-tools: $(GOLANGCI_LINT)
17+
18+
.PHONY: lint
19+
lint: lint-tools ## Run static analysis via golangci-lint
20+
@echo "==> Checking source code against linters..."
21+
$(GOLANGCI_LINT) run -v ./...
22+
23+
gofmt: ## Format Go source code in 'internal/provider' using gofmt."
24+
@echo "==> Format code using gofmt..."
25+
gofmt -s -w internal/provider
26+
27+
GOLANGCI_LINT_MK_INCLUDED := 1
28+
endif

0 commit comments

Comments
 (0)