Skip to content

Commit

Permalink
Add doc and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ribetm committed Nov 9, 2024
1 parent c9ef91a commit 3ac7b78
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/serviceaccount/serviceaccount.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resource "minio_iam_user" "test_user" {
}

resource "minio_iam_service_account" "test_service_account" {
name = "test-svc" # optional
target_user = "test-user"
policy = <<-EOF
{
Expand Down
106 changes: 106 additions & 0 deletions minio/resource_minio_service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -154,6 +155,67 @@ func TestParseUserFromParentUser(t *testing.T) {
assert.Equal(t, "minio-user", parseUserFromParentUser("cn=minio-user, DC=example"))
}

func TestServiceAccount_NameDesc(t *testing.T) {
var serviceAccount madmin.InfoServiceAccountResp

targetUser := "minio"
resourceName := "minio_iam_service_account.test"
name := "svc-account"
description := "A service account"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckMinioServiceAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccMinioServiceAccountConfig(targetUser),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioServiceAccountExists(resourceName, &serviceAccount),
),
},
{
Config: testAccMinioServiceAccountConfigUpdateNameDesc(targetUser, name, description),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioServiceAccountExists(resourceName, &serviceAccount),
testAccCheckMinioServiceAccountNameDesc(resourceName, name, description),
),
},
},
})
}

func TestServiceAccount_Expiration(t *testing.T) {
var serviceAccount madmin.InfoServiceAccountResp

targetUser := "minio"
resourceName := "minio_iam_service_account.test"
expiration := time.Now().Add(time.Hour * 1).UTC().Format(time.RFC3339)
epoch := time.UnixMicro(0).UTC().Format(time.RFC3339)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckMinioServiceAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccMinioServiceAccountConfig(targetUser),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioServiceAccountExists(resourceName, &serviceAccount),
testAccCheckMinioServiceAccountExpiration(resourceName, epoch),
),
},
{
Config: testAccMinioServiceAccountConfigUpdateExpiration(targetUser, expiration),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioServiceAccountExists(resourceName, &serviceAccount),
testAccCheckMinioServiceAccountExpiration(resourceName, expiration),
),
},
},
})
}

func testAccMinioServiceAccountConfig(rName string) string {
return fmt.Sprintf(`
resource "minio_iam_service_account" "test" {
Expand Down Expand Up @@ -237,6 +299,23 @@ resource "minio_iam_service_account" "test_service_account" {
`, rName)
}

func testAccMinioServiceAccountConfigUpdateNameDesc(rName string, name string, description string) string {
return fmt.Sprintf(`
resource "minio_iam_service_account" "test" {
target_user = %q
name = %q
description = %q
}`, rName, name, description)
}

func testAccMinioServiceAccountConfigUpdateExpiration(rName string, expiration string) string {
return fmt.Sprintf(`
resource "minio_iam_service_account" "test" {
target_user = %q
expiration = %q
}`, rName, expiration)
}

func testAccCheckMinioServiceAccountExists(n string, res *madmin.InfoServiceAccountResp) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -380,3 +459,30 @@ func testAccCheckMinioServiceAccountPolicy(n string, expectedPolicy string) reso
return nil
}
}

func testAccCheckMinioServiceAccountNameDesc(n string, name string, description string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs := s.RootModule().Resources[n]

if rs.Primary.Attributes["name"] != name {
return fmt.Errorf("bad name: %s", name)
}
if rs.Primary.Attributes["description"] != description {
return fmt.Errorf("bad description: %s", description)
}

return nil
}
}

func testAccCheckMinioServiceAccountExpiration(n string, expiration string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs := s.RootModule().Resources[n]

if rs.Primary.Attributes["expiration"] != expiration {
return fmt.Errorf("bad expiration: %s", expiration)
}

return nil
}
}

0 comments on commit 3ac7b78

Please sign in to comment.