-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a04b1cb
commit 29c0936
Showing
13 changed files
with
240 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
terraform-provider-dockerhub | ||
|
||
.terraform.lock.hcl | ||
.terraform | ||
terraform.tfstate* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters