Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BarnabyShearer committed Nov 23, 2021
1 parent a04b1cb commit 29c0936
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
terraform-provider-dockerhub

.terraform.lock.hcl
.terraform
terraform.tfstate*
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
terraform-provider-dockerhub: *.go */*.go go.mod
test:
terraform fmt -recursive
go fmt ./...
go vet .
go test ./...

testacc: test
TF_ACC=1 go test ./...

terraform-provider-dockerhub: *.go */*.go go.mod docs/index.md
go build .

install: terraform-provider-dockerhub
mkdir -p ~/.terraform.d/plugins/registry.terraform.io/BarnabyShearer/dockerhub/0.1.0/linux_amd64
cp $+ ~/.terraform.d/plugins/registry.terraform.io/BarnabyShearer/dockerhub/0.1.0/linux_amd64
-rm .terraform.lock.hcl
terraform init

docs/index.md: $(shell find -name "*.go" -or -name "*.tmpl" -or -name "*.tf")
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
27 changes: 27 additions & 0 deletions dockerhub/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dockerhub

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"dockerhub": testAccProvider,
}
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("DOCKER_USERNAME"); v == "" {
t.Fatal("DOCKER_USERNAME must be set for acceptance tests")
}
if v := os.Getenv("DOCKER_PASSWORD"); v == "" {
t.Fatal("DOCKER_PASSWORD must be set for acceptance tests")
}
}
2 changes: 1 addition & 1 deletion dockerhub/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func resourceRepository() *schema.Resource {
return &schema.Resource{
Description: "A hub.docker.io repository.",
Description: "A hub.docker.com repository.",
CreateContext: resourceRepositoryCreate,
UpdateContext: resourceRepositoryUpdate,
ReadContext: resourceRepositoryRead,
Expand Down
67 changes: 67 additions & 0 deletions dockerhub/resource_repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dockerhub

import (
"context"
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

dh "github.com/BarnabyShearer/dockerhub/v2"
)

func TestAccDockerhubRepository_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRepositoryResourceDestroy,
Steps: []resource.TestStep{
{
Config: `
resource "dockerhub_repository" "foo" {
namespace = "barnabyshearer"
name = "foo"
description = "bar"
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("dockerhub_repository.foo", "description", "bar"),
),
},
{
Config: `
resource "dockerhub_repository" "foo" {
namespace = "barnabyshearer"
name = "foo"
description = "baz"
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("dockerhub_repository.foo", "description", "baz"),
),
},
},
})
}

func testAccCheckRepositoryResourceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*dh.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "dockerhub_repository" {
continue
}

_, err := client.GetRepository(context.Background(), rs.Primary.ID)
if err == nil {
return fmt.Errorf("Repository (%s) still exists.", rs.Primary.ID)
}
if !strings.Contains(err.Error(), "Object not found") {
return err
}
}

return nil
}
21 changes: 13 additions & 8 deletions dockerhub/resource_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func resourceToken() *schema.Resource {
return &schema.Resource{
Description: "A hub.docker.io personal access token (for uploading images).",
Description: "A hub.docker.com personal access token (for uploading images).",
CreateContext: resourceTokenCreate,
ReadContext: noop,
DeleteContext: resourceTokenDelete,
Expand All @@ -38,22 +38,27 @@ func resourceToken() *schema.Resource {
Required: true,
ForceNew: true,
Description: "Permissions e.g. 'repo:admin'",
Elem: schema.TypeString,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func readSetString(set *schema.Set) []string {
ret := make([]string, len(set.List()))
for i, raw := range set.List() {
ret[i] = raw.(string)
}
return ret
}

func resourceTokenCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*dh.Client)
scopesRaw := d.Get("scopes").(*schema.Set).List()
scopes := make([]string, len(scopesRaw))
for i, raw := range scopesRaw {
scopes[i] = raw.(string)
}
token, err := client.CreatePersonalAccessToken(ctx, dh.CreatePersonalAccessToken{
TokenLabel: d.Get("label").(string),
Scopes: scopes,
Scopes: readSetString(d.Get("scopes").(*schema.Set)),
})
if err != nil {
return diag.FromErr(err)
Expand Down
103 changes: 103 additions & 0 deletions dockerhub/resource_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package dockerhub

import (
"context"
"fmt"
"reflect"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

dh "github.com/BarnabyShearer/dockerhub/v2"
)

func toGenericArray(old []string) []interface{} {
new := make([]interface{}, len(old))
for i, v := range old {
new[i] = v
}
return new
}

func TestReadSetString(t *testing.T) {
cases := []struct {
input *schema.Set
expected []string
}{
{
input: schema.NewSet(
schema.HashString,
toGenericArray([]string{"a", "b", "c"}),
),
expected: []string{"c", "b", "a"}, // hashed order
},
{
input: schema.NewSet(
schema.HashString,
toGenericArray([]string{}),
),
expected: []string{},
},
}
for _, c := range cases {
out := readSetString(c.input)
if !reflect.DeepEqual(out, c.expected) {
t.Fatalf("Error matching output and expected: %#v vs %#v", out, c.expected)
}
}
}

func TestAccDockerhubToken_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTokenResourceDestroy,
Steps: []resource.TestStep{
{
Config: `
resource "dockerhub_token" "foo" {
label = "foo"
scopes = ["repo:admin"]
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("dockerhub_token.foo", "label", "foo"),
),
},
{
Config: `
resource "dockerhub_token" "foo" {
label = "bar"
scopes = ["repo:admin"]
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("dockerhub_token.foo", "label", "bar"),
),
},
},
})
}

func testAccCheckTokenResourceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*dh.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "dockerhub_token" {
continue
}

_, err := client.GetPersonalAccessToken(context.Background(), rs.Primary.ID)
if err == nil {
return fmt.Errorf("Token (%s) still exists.", rs.Primary.ID)
}
if !strings.Contains(err.Error(), "does not exist") {
return err
}
}

return nil
}
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
layout: ""
page_title: "Provider: dockerhub"
description: |-
Register hub.docker.io repositories.
Register hub.docker.com repositories.
---

# dockerhub Provider

Register hub.docker.io repositories.
Register hub.docker.com repositories.

## Example Usage

Expand Down
4 changes: 2 additions & 2 deletions docs/resources/repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "dockerhub_repository Resource - terraform-provider-dockerhub"
subcategory: ""
description: |-
A hub.docker.io repository.
A hub.docker.com repository.
---

# dockerhub_repository (Resource)

A hub.docker.io repository.
A hub.docker.com repository.

## Example Usage

Expand Down
6 changes: 3 additions & 3 deletions docs/resources/token.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "dockerhub_token Resource - terraform-provider-dockerhub"
subcategory: ""
description: |-
A hub.docker.io personal access token (for uploading images).
A hub.docker.com personal access token (for uploading images).
---

# dockerhub_token (Resource)

A hub.docker.io personal access token (for uploading images).
A hub.docker.com personal access token (for uploading images).

## Example Usage

Expand All @@ -24,7 +24,7 @@ resource "dockerhub_token" "example" {
### Required

- **label** (String) Token label.
- **scopes** (List of String) Permissions e.g. 'repo:admin'
- **scopes** (Set of String) Permissions e.g. 'repo:admin'

### Read-Only

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/BarnabyShearer/terraform-provider-dockerhub
go 1.16

require (
github.com/BarnabyShearer/dockerhub/v2 v2.0.1
github.com/BarnabyShearer/dockerhub/v2 v2.0.2
github.com/hashicorp/terraform-plugin-docs v0.5.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.9.0
)
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0 h1:STgFzyU5/8miMl0//zKh2aQeTyeaUH3WN9bSUiJ09bA=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BarnabyShearer/dockerhub/v2 v2.0.1 h1:+gNwbk1IATOhfqj58ykBS53+RmHHHksV6SvCk8Op50w=
github.com/BarnabyShearer/dockerhub/v2 v2.0.1/go.mod h1:z5UKZBcwG+G4G+ZepWmzbdS7lPY/jTC8q/mcVKJIyWc=
github.com/BarnabyShearer/dockerhub/v2 v2.0.2 h1:igvlrNosWSBS9eOHCdEs31jqS+xskMTvIPaOK8fam08=
github.com/BarnabyShearer/dockerhub/v2 v2.0.2/go.mod h1:z5UKZBcwG+G4G+ZepWmzbdS7lPY/jTC8q/mcVKJIyWc=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
Expand Down Expand Up @@ -193,6 +193,7 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl/v2 v2.3.0 h1:iRly8YaMwTBAKhn1Ybk7VSdzbnopghktCD031P8ggUE=
github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.15.0 h1:cqjh4d8HYNQrDoEmlSGelHmg2DYDh5yayckvJ5bV18E=
github.com/hashicorp/terraform-exec v0.15.0/go.mod h1:H4IG8ZxanU+NW0ZpDRNsvh9f0ul7C0nHP+rUR/CHs7I=
Expand Down
4 changes: 2 additions & 2 deletions templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
layout: ""
page_title: "Provider: dockerhub"
description: |-
Register hub.docker.io repositories.
Register hub.docker.com repositories.
---

# dockerhub Provider

Register hub.docker.io repositories.
Register hub.docker.com repositories.

## Example Usage

Expand Down

0 comments on commit 29c0936

Please sign in to comment.