Skip to content

Commit c78b225

Browse files
init tests
1 parent d7cfda5 commit c78b225

File tree

3 files changed

+196
-4
lines changed

3 files changed

+196
-4
lines changed

codefresh/cfclient/service_user.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import (
55
"golang.org/x/exp/slices"
66
)
77

8+
type ServiceUserTeam struct {
9+
ID string `json:"_id,omitempty"`
10+
}
11+
812
type ServiceUser struct {
9-
ID string `json:"_id,omitempty"`
10-
Name string `json:"userName,omitempty"`
11-
Teams []Team `json:"teams,omitempty"`
12-
Roles []string `json:"roles,omitempty"`
13+
ID string `json:"_id,omitempty"`
14+
Name string `json:"userName,omitempty"`
15+
Teams []ServiceUserTeam `json:"teams,omitempty"`
16+
Roles []string `json:"roles,omitempty"`
1317
}
1418

1519
type ServiceUserCreateUpdate struct {
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12+
)
13+
14+
var serviceUserNamePrefix = "TerraformAccTest_"
15+
16+
func TestAccCodefreshServiceUser_basic(t *testing.T) {
17+
name := serviceUserNamePrefix + acctest.RandString(10)
18+
19+
resourceName := "codefresh_service_account.test"
20+
teamResourceName := "codefresh_team.test"
21+
22+
resource.Test(t, resource.TestCase{
23+
PreCheck: func() { testAccPreCheck(t) },
24+
Providers: testAccProviders,
25+
CheckDestroy: testAccCheckCodefreshServiceUserDestroy,
26+
Steps: []resource.TestStep{
27+
{
28+
Config: testAccCodefreshServiceUserTeamToken(name, name),
29+
Check: resource.ComposeTestCheckFunc(
30+
testAccCheckCodefreshServiceUserExists(resourceName),
31+
testAccCheckCodefreshServiceUserAssignedToTeam(resourceName, teamResourceName),
32+
resource.TestCheckResourceAttr(resourceName, "name", name),
33+
),
34+
},
35+
{
36+
ResourceName: resourceName,
37+
ImportState: true,
38+
ImportStateVerify: true,
39+
},
40+
},
41+
})
42+
}
43+
44+
func testAccCheckCodefreshServiceUserExists(resource string) resource.TestCheckFunc {
45+
return func(state *terraform.State) error {
46+
rs, ok := state.RootModule().Resources[resource]
47+
if !ok {
48+
return fmt.Errorf("Not found: %s", resource)
49+
}
50+
if rs.Primary.ID == "" {
51+
return fmt.Errorf("No Record ID is set")
52+
}
53+
54+
serviceUserID := rs.Primary.ID
55+
56+
apiClient := testAccProvider.Meta().(*cfclient.Client)
57+
_, err := apiClient.GetServiceUserByID(serviceUserID)
58+
59+
if err != nil {
60+
return fmt.Errorf("error fetching serviceUser with resource %s. %s", resource, err)
61+
}
62+
return nil
63+
}
64+
}
65+
66+
func testAccCheckCodefreshServiceUserAssignedToTeam(serviceUserResource string, teamResource string) resource.TestCheckFunc {
67+
return func(state *terraform.State) error {
68+
serviceUserState, ok := state.RootModule().Resources[serviceUserResource]
69+
70+
if !ok {
71+
return fmt.Errorf("Not found: %s", serviceUserResource)
72+
}
73+
74+
if serviceUserState.Primary.ID == "" {
75+
return fmt.Errorf("No Record ID is set")
76+
}
77+
78+
teamState, ok := state.RootModule().Resources[teamResource]
79+
80+
if !ok {
81+
return fmt.Errorf("Not found: %s", teamResource)
82+
}
83+
84+
if teamState.Primary.ID == "" {
85+
return fmt.Errorf("No Record ID is set for team")
86+
}
87+
88+
serviceUserID := serviceUserState.Primary.ID
89+
teamID := teamState.Primary.ID
90+
91+
apiClient := testAccProvider.Meta().(*cfclient.Client)
92+
serviceUser, err := apiClient.GetServiceUserByID(serviceUserID)
93+
94+
if err != nil {
95+
return fmt.Errorf("error fetching serviceUser with resource %s. %s", serviceUserID, err)
96+
}
97+
98+
isTeamAssigned := false
99+
100+
for _, team := range serviceUser.Teams {
101+
if team.ID == teamID {
102+
isTeamAssigned = true
103+
break
104+
}
105+
}
106+
107+
if !isTeamAssigned {
108+
return fmt.Errorf("service user %s is not assigned to team %s", serviceUserID, teamID)
109+
}
110+
111+
return nil
112+
}
113+
}
114+
115+
func testAccCheckCodefreshServiceUserDestroy(s *terraform.State) error {
116+
apiClient := testAccProvider.Meta().(*cfclient.Client)
117+
118+
for _, rs := range s.RootModule().Resources {
119+
120+
if rs.Type != "codefresh_service_account" {
121+
continue
122+
}
123+
124+
_, err := apiClient.GetServiceUserByID(rs.Primary.ID)
125+
126+
if err == nil {
127+
return fmt.Errorf("Alert still exists")
128+
}
129+
notFoundErr := "does not exist"
130+
expectedErr := regexp.MustCompile(notFoundErr)
131+
if !expectedErr.Match([]byte(err.Error())) {
132+
return fmt.Errorf("expected %s, got %s", notFoundErr, err)
133+
}
134+
135+
}
136+
137+
return nil
138+
}
139+
140+
func testAccCodefreshServiceUserTeamToken(serviceUserName string, teamName string) string {
141+
return fmt.Sprintf(`
142+
resource "codefresh_team" "test" {
143+
name = "%s"
144+
}
145+
146+
resource "codefresh_service_account" "test" {
147+
name = "%s"
148+
assigned_teams = [codefresh_team.test.id]
149+
}
150+
`, serviceUserName, teamName)
151+
}
152+
153+
// CONFIGS
154+
func testAccCodefreshServiceUserBasicConfig(rName string) string {
155+
return fmt.Sprintf(`
156+
resource "codefresh_service_account" "test" {
157+
name = "%s"
158+
}
159+
`, rName)
160+
}
161+
162+
func testAccCodefreshServiceUserBasicConfigTags(rName, tag1, tag2 string) string {
163+
return fmt.Sprintf(`
164+
resource "codefresh_service_user" "test" {
165+
name = "%s"
166+
tags = [
167+
%q,
168+
%q,
169+
]
170+
}
171+
`, rName, tag1, tag2)
172+
}
173+
174+
func testAccCodefreshServiceUserBasicConfigVariables(rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name, encrytedVar1Value string) string {
175+
return fmt.Sprintf(`
176+
resource "codefresh_serviceUser" "test" {
177+
name = "%s"
178+
variables = {
179+
%q = %q
180+
%q = %q
181+
}
182+
183+
encrypted_variables = {
184+
%q = %q
185+
}
186+
}
187+
`, rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name, encrytedVar1Value)
188+
}

0 commit comments

Comments
 (0)