Skip to content

Commit

Permalink
Linters, build and test pipeline (#95)
Browse files Browse the repository at this point in the history
This commit implements a lint, build and test pipeline as well as the initial piece for a release pipeline.

Now that bflad/tfproviderlint#255 has been closed, we can fully implement all linters as well
  • Loading branch information
karencfv authored Apr 27, 2023
1 parent f2de1a1 commit 046faad
Show file tree
Hide file tree
Showing 22 changed files with 229 additions and 99 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: build & test
on:
push:
branches: main
paths:
- "**.go"
- .github/workflows/build-test.yml
- "go.mod"
- "go.sum"
- "docs/**"
- "examples/**"
pull_request:
branches: main
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.x"
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: build
run: make build
- name: test
run: make test
- name: lint
run: sudo make lint
33 changes: 21 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ VERSION ?= $(shell cat $(CURDIR)/VERSION)
BINARY ?= terraform-provider-oxide_$(VERSION)
BINARY_LOCATION ?= bin/$(BINARY)
OS_ARCH ?= $(shell go env GOOS)_$(shell go env GOARCH)
export GOBIN = $(shell pwd)/bin

# Terraform currently does not have a binary for Illumos.
# The one for Solaris works fine with Illumos, so we'll need
Expand All @@ -15,11 +16,15 @@ PROVIDER_PATH ?= registry.terraform.io/oxidecomputer/oxide/$(VERSION)/$(OS_ARCH)
PLUGIN_LOCATION ?= ~/.terraform.d/plugins/$(PROVIDER_PATH)

# Acceptance test variables
TEST_COUNT ?= 1
TEST_ACC_COUNT ?= 1
TEST_ACC ?= github.com/oxidecomputer/terraform-provider-oxide/oxide
TEST_NAME ?= TestAcc
TEST_ACC_NAME ?= TestAcc
TEST_ACC_PARALLEL = 6

# Unit test variables
TEST_ARGS ?= -timeout 10m -race -cover
TEST_PACKAGE ?= ./oxide

include Makefile.tools

### Build targets
Expand All @@ -37,16 +42,20 @@ install: build
@ mkdir -p $(PLUGIN_LOCATION)
@ cp $(BINARY_LOCATION) $(PLUGIN_LOCATION)

## Run unit tests. Use TEST_ARGS to set `go test` CLI arguments, and TEST_UNIT_DIR to set packages to be tested
.PHONY: test
test:
@ echo "-> Running unit tests for $(BINARY)..."
@ go test $(TEST_PACKAGE) $(TEST_ARGS) $(TESTUNITARGS)

## Lints all of the source files
.PHONY: lint
lint: golint golangci-lint tfproviderdocs terrafmt configfmt # tfproviderlint
lint: golint golangci-lint tfproviderdocs terrafmt tfproviderlint # configfmt

# tf providerlint currently has a bug with the latest stable go version (1.18), will uncomment
# when this issue is solved https://github.com/bflad/tfproviderlint/issues/255
# .PHONY: tfproviderlint
# tfproviderlint: tools
# @ echo "-> Checking source code against terraform provider linters..."
# @ $(GOBIN)/tfproviderlint ./...
.PHONY: tfproviderlint
tfproviderlint: tools
@ echo "-> Checking source code against terraform provider linters..."
@ $(GOBIN)/tfproviderlint ./...

.PHONY: tfproviderdocs
tfproviderdocs: tools
Expand All @@ -66,17 +75,17 @@ golint: tools
.PHONY: terrafmt
terrafmt: tools
@ echo "-> Checking that the terraform docs codeblocks are formatted..."
@ find ./docs -type f -name "*.md" -exec $(GOBIN)/terrafmt diff -c -q {} \;
@ find ./docs -type f -name "*.md" -exec $(GOBIN)/terrafmt diff -f {} \;

configfmt:
@ echo "-> Checking that the terraform .tf files are formatted..."
@ terraform fmt -write=false -recursive -check

.PHONY: testacc
## Runs the Terraform acceptance tests. Use TEST_NAME, TESTARGS, TEST_COUNT and TEST_ACC_PARALLEL to control execution.
## Runs the Terraform acceptance tests. Use TEST_ACC_NAME, TEST_ACC_ARGS, TEST_ACC_COUNT and TEST_ACC_PARALLEL for acceptance testing settings.
testacc:
@ echo "-> Running terraform acceptance tests..."
@ TF_ACC=1 go test $(TEST_ACC) -v -count $(TEST_COUNT) -parallel $(TEST_ACC_PARALLEL) $(TESTARGS) -timeout 20m -run $(TEST_NAME)
@ TF_ACC=1 go test $(TEST_ACC) -v -count $(TEST_ACC_COUNT) -parallel $(TEST_ACC_PARALLEL) $(TEST_ACC_ARGS) -timeout 20m -run $(TEST_ACC_NAME)

.PHONY: local-api
## Use local API language client
Expand Down
22 changes: 10 additions & 12 deletions Makefile.tools
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ OS:=$(shell uname -s | tr '[:upper:]' '[:lower:]')
VERSION_DIR:=$(GOBIN)/versions

VERSION_GOLINT:=v0.0.0-20210508222113-6edffad5e616
VERSION_GOLANGCILINT:=v1.46.2
VERSION_GOLANGCILINT:=v1.52.2
VERSION_TFPROVIDERDOCS:=v0.9.1
VERSION_TERRAFMT:=v0.4.0
VERSION_TFPROVIDERLINT:=v0.28.1
VERSION_TERRAFMT:=v0.5.2
VERSION_TFPROVIDERLINT:=v0.29.0

tools: $(GOBIN)/golint $(GOBIN)/golangci-lint $(GOBIN)/tfproviderdocs $(GOBIN)/terrafmt
# tfproviderlint currently has a bug https://github.com/bflad/tfproviderlint/issues/255 will uncomment when resolved.
# $(GOBIN)/tfproviderlint
tools: $(GOBIN)/golint $(GOBIN)/golangci-lint $(GOBIN)/tfproviderdocs $(GOBIN)/terrafmt $(GOBIN)/tfproviderlint

$(GOBIN):
@ mkdir -p $(GOBIN)
Expand Down Expand Up @@ -49,10 +47,10 @@ $(GOBIN)/terrafmt: $(VERSION_DIR)/.version-terrafmt-$(VERSION_TERRAFMT) | $(GOBI
@ echo "-> Installing terrafmt..."
@ go install github.com/katbyte/terrafmt@$(VERSION_TERRAFMT)

# $(VERSION_DIR)/.version-tfproviderlint-$(VERSION_TFPROVIDERLINT): | $(VERSION_DIR)
# @ rm -f $(VERSION_DIR)/.version-tfproviderlint-*
# @ echo $(VERSION_TFPROVIDERLINT) > $(VERSION_DIR)/.version-tfproviderlint-$(VERSION_TFPROVIDERLINT)
$(VERSION_DIR)/.version-tfproviderlint-$(VERSION_TFPROVIDERLINT): | $(VERSION_DIR)
@ rm -f $(VERSION_DIR)/.version-tfproviderlint-*
@ echo $(VERSION_TFPROVIDERLINT) > $(VERSION_DIR)/.version-tfproviderlint-$(VERSION_TFPROVIDERLINT)

# $(GOBIN)/tfproviderlint: $(VERSION_DIR)/.version-tfproviderlint-$(VERSION_TFPROVIDERLINT) | $(GOBIN)
# @ echo "-> Installing tfproviderlint..."
# @ go install github.com/bflad/tfproviderlint/cmd/tfproviderlint@$(VERSION_TFPROVIDERLINT)
$(GOBIN)/tfproviderlint: $(VERSION_DIR)/.version-tfproviderlint-$(VERSION_TFPROVIDERLINT) | $(GOBIN)
@ echo "-> Installing tfproviderlint..."
@ go install github.com/bflad/tfproviderlint/cmd/tfproviderlint@$(VERSION_TFPROVIDERLINT)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Requirements

- [Terraform](https://www.terraform.io/downloads) 0.1.x and above, we recommend using the latest stable release whenever possible. When installing on an Illumos machine use the Solaris binary.
- [Go](https://go.dev/dl/) 1.19.x and above (to build the provider plugin)
- [Go](https://go.dev/dl/) 1.20.x and above (to build the provider plugin)

## Building the provider

Expand Down
20 changes: 10 additions & 10 deletions docs/resources/oxide_disk.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ This resource manages disks.

```hcl
resource "oxide_disk" "example" {
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test disk"
name = "mydisk"
size = 1073741824
disk_source = { blank = 512 }
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test disk"
name = "mydisk"
size = 1073741824
disk_source = { blank = 512 }
}
resource "oxide_disk" "example2" {
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test disk"
name = "mydisk2"
size = 1073741824
disk_source = { global_image = "611bb17d-6883-45be-b3aa-8a186fdeafe8" }
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test disk"
name = "mydisk2"
size = 1073741824
disk_source = { global_image = "611bb17d-6883-45be-b3aa-8a186fdeafe8" }
}
```

Expand Down
52 changes: 26 additions & 26 deletions docs/resources/oxide_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,53 @@ This resource manages instances.

```hcl
resource "oxide_instance" "example" {
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test instance"
name = "myinstance"
host_name = "<host value>"
memory = 1073741824
ncpus = 1
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test instance"
name = "myinstance"
host_name = "<host value>"
memory = 1073741824
ncpus = 1
}
```

### Assign an IP pool for the instance

```hcl
resource "oxide_instance" "example" {
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test instance"
name = "myinstance"
host_name = "<host value>"
memory = 1073741824
ncpus = 1
external_ips = ["myippool"]
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test instance"
name = "myinstance"
host_name = "<host value>"
memory = 1073741824
ncpus = 1
external_ips = ["myippool"]
}
```

### Attach two disks to the instance

```hcl
resource "oxide_instance" "example" {
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test instance"
name = "myinstance"
host_name = "<host value>"
memory = 1073741824
ncpus = 1
attach_to_disks = ["disk1", "disk2"]
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test instance"
name = "myinstance"
host_name = "<host value>"
memory = 1073741824
ncpus = 1
attach_to_disks = ["disk1", "disk2"]
}
```

### Attach a network interface to the instance

```hcl
resource "oxide_instance" "example" {
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test instance"
name = "myinstance"
host_name = "<host value>"
memory = 1073741824
ncpus = 1
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test instance"
name = "myinstance"
host_name = "<host value>"
memory = 1073741824
ncpus = 1
network_interface = [
{
description = "a network interface"
Expand Down
10 changes: 5 additions & 5 deletions docs/resources/oxide_ip_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ This resource manages IP pools.

```hcl
resource "oxide_ip_pool" "example" {
description = "a test ippool"
name = "myippool"
description = "a test ippool"
name = "myippool"
ranges = [
{
first_address = "172.20.18.227"
last_address = "172.20.18.239"
}
first_address = "172.20.18.227"
last_address = "172.20.18.239"
}
]
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/oxide_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ This resource manages projects.

```hcl
resource "oxide_project" "example" {
description = "a test project"
name = "myproject"
description = "a test project"
name = "myproject"
}
```

Expand Down
10 changes: 5 additions & 5 deletions docs/resources/oxide_vpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ This resource manages VPCs.

```hcl
resource "oxide_vpc" "example" {
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test vpc"
name = "myvpc"
dns_name = "my-vpc-dns"
ipv6_prefix = "fd1e:4947:d4a1::/48"
project_id = "c1dee930-a8e4-11ed-afa1-0242ac120002"
description = "a test vpc"
name = "myvpc"
dns_name = "my-vpc-dns"
ipv6_prefix = "fd1e:4947:d4a1::/48"
}
```

Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ require (
github.com/hashicorp/terraform-plugin-go v0.15.0
github.com/hashicorp/terraform-plugin-testing v1.2.0
github.com/oxidecomputer/oxide.go v0.0.22-0.20230411045251-335c1187c3bd
github.com/stretchr/testify v1.8.2
)

require (
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
Expand Down Expand Up @@ -44,6 +46,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand All @@ -57,4 +60,5 @@ require (
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,18 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
2 changes: 1 addition & 1 deletion oxide/data_source_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// NB: The project must be populated with at least one image for this test to pass
func TestAccDataSourceImages(t *testing.T) {
func TestAccDataSourceImages_full(t *testing.T) {
datasourceName := "data.oxide_images.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down
2 changes: 1 addition & 1 deletion oxide/data_source_projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDataSourceProjects(t *testing.T) {
func TestAccDataSourceProjects_full(t *testing.T) {
datasourceName := "data.oxide_projects.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down
Loading

0 comments on commit 046faad

Please sign in to comment.