-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! add tests compatible with test suite
- Loading branch information
Jeremy Udit
committed
Aug 9, 2021
1 parent
9dc391c
commit b5b793a
Showing
2 changed files
with
95 additions
and
220 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
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,249 +1,124 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-github/v35/github" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccGithubActionsRunnerGroup_all(t *testing.T) { | ||
// ??? | ||
// resource_github_actions_runner_group_test.go:19: Skipping because GITHUB_OWNER is a user, not an organization. | ||
// if err := testAccCheckOrganization(); err != nil { | ||
// t.Skipf("Skipping because %s.", err.Error()) | ||
// } | ||
|
||
var runnerGroup github.RunnerGroup | ||
func TestAccGithubActionsRunnerGroup(t *testing.T) { | ||
|
||
var testAccGithubActionsRunnerGroupConfigAll = ` | ||
resource "github_actions_runner_group" "test_all" { | ||
name = "test-runner-group-all" | ||
visibility = "all" | ||
} | ||
` | ||
rn := "github_actions_runner_group.test_all" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccGithubActionsRunnerGroupDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccGithubActionsRunnerGroupConfigAll, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckGithubActionsRunnerGroupExists(rn, &runnerGroup), | ||
testAccCheckGithubActionsRunnerGroupAttributes(&runnerGroup, &testAccGithubActionsRunnerGroupExpectedAttributes{ | ||
Name: "test-runner-group-all", | ||
Visibility: "all", | ||
Default: false, | ||
AllowsPublicRepositories: false, | ||
RunnersURL: fmt.Sprintf(`https://api.github.com/orgs/octo-org/actions/runner_groups/%d/runners`, runnerGroup.ID), | ||
SelectedRepositoriesURL: fmt.Sprintf(`https://api.github.com/orgs/octo-org/actions/runner_groups/%d/repositories`, runnerGroup.ID), | ||
}), | ||
), | ||
}, | ||
{ | ||
ResourceName: rn, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
|
||
func TestAccGithubActionsRunnerGroup_private(t *testing.T) { | ||
// ??? | ||
// resource_github_actions_runner_group_test.go:19: Skipping because GITHUB_OWNER is a user, not an organization. | ||
// if err := testAccCheckOrganization(); err != nil { | ||
// t.Skipf("Skipping because %s.", err.Error()) | ||
// } | ||
t.Run("creates runner groups without error", func(t *testing.T) { | ||
|
||
var runnerGroup github.RunnerGroup | ||
t.Skip("requires an enterprise cloud account") | ||
|
||
rn := "github_actions_runner_group.test_private" | ||
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
var testAccGithubActionsRunnerGroupConfigPrivate = fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
visibility = "private" | ||
} | ||
resource "github_actions_runner_group" "test_private" { | ||
name = "test-runner-group-private" | ||
visibility = "private" | ||
} | ||
`, randomID) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccGithubActionsRunnerGroupDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccGithubActionsRunnerGroupConfigPrivate, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckGithubActionsRunnerGroupExists(rn, &runnerGroup), | ||
testAccCheckGithubActionsRunnerGroupAttributes(&runnerGroup, &testAccGithubActionsRunnerGroupExpectedAttributes{ | ||
Name: "test-runner-group-private", | ||
Visibility: "private", | ||
Default: false, | ||
AllowsPublicRepositories: false, | ||
RunnersURL: fmt.Sprintf(`https://api.github.com/orgs/octo-org/actions/runner_groups/%d/runners`, runnerGroup.ID), | ||
SelectedRepositoriesURL: fmt.Sprintf(`https://api.github.com/orgs/octo-org/actions/runner_groups/%d/repositories`, runnerGroup.ID), | ||
}), | ||
), | ||
}, | ||
{ | ||
ResourceName: rn, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
config := fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
vulnerability_alerts = false | ||
} | ||
func TestAccGithubActionsRunnerGroup_selected(t *testing.T) { | ||
// ??? | ||
// resource_github_actions_runner_group_test.go:19: Skipping because GITHUB_OWNER is a user, not an organization. | ||
// if err := testAccCheckOrganization(); err != nil { | ||
// t.Skipf("Skipping because %s.", err.Error()) | ||
// } | ||
resource "github_actions_runner_group" "test" { | ||
name = github_repository.test.name | ||
visibility = "all" | ||
} | ||
`, randomID) | ||
|
||
check := resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet( | ||
"github_actions_runner_group.test", "name", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"github_actions_runner_group.test", "visibility", | ||
"all", | ||
), | ||
) | ||
|
||
testCase := func(t *testing.T, mode string) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { skipUnlessMode(t, mode) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: check, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
var runnerGroup github.RunnerGroup | ||
t.Run("with an anonymous account", func(t *testing.T) { | ||
t.Skip("anonymous account not supported for this operation") | ||
}) | ||
|
||
rn := "github_actions_runner_group.test_selected" | ||
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
var testAccGithubActionsRunnerGroupConfigSelected = fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
auto_init = true | ||
} | ||
t.Run("with an individual account", func(t *testing.T) { | ||
t.Skip("individual account not supported for this operation") | ||
}) | ||
|
||
resource "github_actions_runner_group" "test_selected" { | ||
name = "test-runner-group-selected" | ||
visibility = "selected" | ||
selected_repository_ids = [github_repository.test.repo_id] | ||
} | ||
`, randomID) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccGithubActionsRunnerGroupDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccGithubActionsRunnerGroupConfigSelected, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckGithubActionsRunnerGroupExists(rn, &runnerGroup), | ||
testAccCheckGithubActionsRunnerGroupAttributes(&runnerGroup, &testAccGithubActionsRunnerGroupExpectedAttributes{ | ||
Name: "test-runner-group-selected", | ||
Visibility: "selected", | ||
Default: false, | ||
AllowsPublicRepositories: false, | ||
RunnersURL: fmt.Sprintf(`https://api.github.com/orgs/octo-org/actions/runner_groups/%d/runners`, runnerGroup.ID), | ||
SelectedRepositoriesURL: fmt.Sprintf(`https://api.github.com/orgs/octo-org/actions/runner_groups/%d/repositories`, runnerGroup.ID), | ||
}), | ||
), | ||
}, | ||
{ | ||
ResourceName: rn, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
t.Run("with an organization account", func(t *testing.T) { | ||
testCase(t, organization) | ||
}) | ||
}) | ||
} | ||
|
||
func testAccGithubActionsRunnerGroupDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*Owner).v3client | ||
t.Run("manages runner visibility", func(t *testing.T) { | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "github_actions_runner_group" { | ||
continue | ||
} | ||
t.Skip("requires an enterprise cloud account") | ||
|
||
runnerGroupID, err := strconv.ParseInt(rs.Primary.ID, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
orgName := testAccProvider.Meta().(*Owner).name | ||
runnerGroup, res, err := conn.Actions.GetOrganizationRunnerGroup(context.TODO(), orgName, runnerGroupID) | ||
if err == nil { | ||
if runnerGroup != nil && | ||
runnerGroup.GetID() == runnerGroupID { | ||
return fmt.Errorf("Organization runner group still exists") | ||
config := fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
} | ||
} | ||
if res.StatusCode != 404 { | ||
return err | ||
} | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
func testAccCheckGithubActionsRunnerGroupExists(n string, runnerGroup *github.RunnerGroup) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not Found: %s", n) | ||
} | ||
|
||
runnerGroupID, err := strconv.ParseInt(rs.Primary.ID, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
conn := testAccProvider.Meta().(*Owner).v3client | ||
orgName := testAccProvider.Meta().(*Owner).name | ||
gotRunnerGroup, _, err := conn.Actions.GetOrganizationRunnerGroup(context.TODO(), orgName, runnerGroupID) | ||
if err != nil { | ||
return err | ||
resource "github_actions_runner_group" "test" { | ||
name = github_repository.test.name | ||
visibility = "selected" | ||
selected_repository_ids = [github_repository.test.repo_id] | ||
} | ||
`, randomID) | ||
|
||
check := resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet( | ||
"github_actions_runner_group.test", "name", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"github_actions_runner_group.test", "visibility", | ||
"selected", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"github_actions_runner_group.test", "selected_repository_ids.#", | ||
"1", | ||
), | ||
) | ||
|
||
testCase := func(t *testing.T, mode string) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { skipUnlessMode(t, mode) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: check, | ||
}, | ||
}, | ||
}) | ||
} | ||
*runnerGroup = *gotRunnerGroup | ||
return nil | ||
} | ||
} | ||
|
||
type testAccGithubActionsRunnerGroupExpectedAttributes struct { | ||
AllowsPublicRepositories bool | ||
Default bool | ||
ID int64 | ||
Inherited bool | ||
Name string | ||
Runners []int64 | ||
RunnersURL string | ||
SelectedRepositoriesURL string | ||
SelectedRepositoryIDs []int64 | ||
Visibility string | ||
} | ||
t.Run("with an anonymous account", func(t *testing.T) { | ||
t.Skip("anonymous account not supported for this operation") | ||
}) | ||
|
||
func testAccCheckGithubActionsRunnerGroupAttributes(runnerGroup *github.RunnerGroup, want *testAccGithubActionsRunnerGroupExpectedAttributes) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
t.Run("with an individual account", func(t *testing.T) { | ||
t.Skip("individual account not supported for this operation") | ||
}) | ||
|
||
if name := runnerGroup.GetName(); name != want.Name { | ||
return fmt.Errorf("got runnerGroup name %q; want %q", name, want.Name) | ||
} | ||
if visibility := runnerGroup.GetVisibility(); visibility != want.Visibility { | ||
return fmt.Errorf("got runnerGroup visibility %q; want %q", visibility, want.Visibility) | ||
} | ||
if inherited := runnerGroup.GetInherited(); inherited != want.Inherited { | ||
return fmt.Errorf("got runnerGroup inherited %t; want %t", inherited, want.Inherited) | ||
} | ||
if URL := runnerGroup.GetRunnersURL(); !strings.HasPrefix(URL, "https://") { | ||
return fmt.Errorf("got runners URL %q; want to start with 'https://'", URL) | ||
} | ||
if isDefault := runnerGroup.GetDefault(); isDefault != want.Default { | ||
return fmt.Errorf("got runnerGroup default %t; want %t", isDefault, want.Default) | ||
} | ||
t.Run("with an organization account", func(t *testing.T) { | ||
testCase(t, organization) | ||
}) | ||
}) | ||
|
||
return nil | ||
} | ||
} |