Skip to content

Commit 22fda1f

Browse files
author
Han Zhou
committed
add acc test for importer
1 parent 677aea2 commit 22fda1f

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

internal/provider/resource_gitlab_group_ldap_link_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
func TestAccGitlabGroupLdapLink_basic(t *testing.T) {
1515
rInt := acctest.RandInt()
16+
resourceName := "gitlab_group_ldap_link.foo"
1617

1718
// PreCheck runs after Config so load test data here
1819
var ldapLink gitlab.LDAPGroupLink
@@ -31,18 +32,27 @@ func TestAccGitlabGroupLdapLink_basic(t *testing.T) {
3132
SkipFunc: isRunningInCE,
3233
Config: testAccGitlabGroupLdapLinkCreateConfig(rInt, &testLdapLink),
3334
Check: resource.ComposeTestCheckFunc(
34-
testAccCheckGitlabGroupLdapLinkExists("gitlab_group_ldap_link.foo", &ldapLink),
35+
testAccCheckGitlabGroupLdapLinkExists(resourceName, &ldapLink),
3536
testAccCheckGitlabGroupLdapLinkAttributes(&ldapLink, &testAccGitlabGroupLdapLinkExpectedAttributes{
3637
accessLevel: fmt.Sprintf("developer"), // nolint // TODO: Resolve this golangci-lint issue: S1039: unnecessary use of fmt.Sprintf (gosimple)
3738
})),
3839
},
3940

41+
// Import the group LDAP link (re-uses testAccGitlabGroupLdapLinkCreateConfig for Config)
42+
{
43+
SkipFunc: isRunningInCE,
44+
ResourceName: resourceName,
45+
ImportStateIdFunc: getGitlabGroupLdapLinkImportID(resourceName),
46+
ImportState: true,
47+
ImportStateVerify: true,
48+
},
49+
4050
// Update the group LDAP link to change the access level (uses testAccGitlabGroupLdapLinkUpdateConfig for Config)
4151
{
4252
SkipFunc: isRunningInCE,
4353
Config: testAccGitlabGroupLdapLinkUpdateConfig(rInt, &testLdapLink),
4454
Check: resource.ComposeTestCheckFunc(
45-
testAccCheckGitlabGroupLdapLinkExists("gitlab_group_ldap_link.foo", &ldapLink),
55+
testAccCheckGitlabGroupLdapLinkExists(resourceName, &ldapLink),
4656
testAccCheckGitlabGroupLdapLinkAttributes(&ldapLink, &testAccGitlabGroupLdapLinkExpectedAttributes{
4757
accessLevel: fmt.Sprintf("maintainer"), // nolint // TODO: Resolve this golangci-lint issue: S1039: unnecessary use of fmt.Sprintf (gosimple)
4858
})),
@@ -51,6 +61,30 @@ func TestAccGitlabGroupLdapLink_basic(t *testing.T) {
5161
})
5262
}
5363

64+
func getGitlabGroupLdapLinkImportID(resourceName string) resource.ImportStateIdFunc {
65+
return func(s *terraform.State) (string, error) {
66+
rs, ok := s.RootModule().Resources[resourceName]
67+
if !ok {
68+
return "", fmt.Errorf("Not Found: %s", resourceName)
69+
}
70+
71+
groupID := rs.Primary.Attributes["group_id"]
72+
if groupID == "" {
73+
return "", fmt.Errorf("No group ID is set")
74+
}
75+
ldapProvider := rs.Primary.Attributes["ldap_provider"]
76+
if ldapProvider == "" {
77+
return "", fmt.Errorf("No LDAP provider is set")
78+
}
79+
ldapCN := rs.Primary.Attributes["cn"]
80+
if ldapCN == "" {
81+
return "", fmt.Errorf("No LDAP CN is set")
82+
}
83+
84+
return fmt.Sprintf("%s:%s:%s", groupID, ldapProvider, ldapCN), nil
85+
}
86+
}
87+
5488
func testAccCheckGitlabGroupLdapLinkExists(resourceName string, ldapLink *gitlab.LDAPGroupLink) resource.TestCheckFunc {
5589
return func(s *terraform.State) error {
5690
// Clear the "found" LDAP link before checking for existence

0 commit comments

Comments
 (0)