Skip to content

Commit 2c38d8c

Browse files
author
Joshua Reed
committed
Helpers now generate a user w/API keys for the duration of the test.
1 parent 0d013bc commit 2c38d8c

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

test/helpers/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package helpers_test
33
import (
44
"testing"
55

6-
. "github.com/onsi/ginkgo"
6+
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
88
)
99

test/helpers/user.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
99
)
1010

11+
const tempUserName = "TemporaryUser"
12+
1113
// GetDomainByPath fetches a domain by its path.
1214
func GetDomainByPath(csClient *cloudstack.CloudStackClient, path string) (string, error, bool) {
1315
// Split path and get name.
@@ -113,14 +115,26 @@ func GetOrCreateAccount(csClient *cloudstack.CloudStackClient, account *cloud.Ac
113115
return err
114116
}
115117

118+
// Attempt to fetch account.
119+
if resp, count, err := csClient.Account.GetAccountByName(
120+
account.Name, cloudstack.WithDomain(account.Domain.ID)); err != nil && !strings.Contains(err.Error(), "No match found") {
121+
return err
122+
} else if count > 1 {
123+
return fmt.Errorf("expected exactly 1 account, but got %d", count)
124+
} else if count == 1 {
125+
account.ID = resp.Id
126+
return nil
127+
} // Account not found, do account creation.
128+
129+
// Get role for account creation.
116130
roleDetails, count, err := csClient.Role.GetRoleByName("Domain Admin")
117131
if err != nil {
118132
return err
119133
} else if count != 1 {
120134
return fmt.Errorf("expected exactly one role with name 'Domain Admin', found %d", count)
121135
}
122136

123-
p := csClient.Account.NewCreateAccountParams("[email protected]", "first", "last", "temp123", "TempUser")
137+
p := csClient.Account.NewCreateAccountParams("[email protected]", "first", "last", "temp123", tempUserName)
124138
p.SetDomainid(account.Domain.ID)
125139
p.SetRoleid(roleDetails.Id)
126140
resp, err := csClient.Account.CreateAccount(p)
@@ -133,11 +147,44 @@ func GetOrCreateAccount(csClient *cloudstack.CloudStackClient, account *cloud.Ac
133147
return nil
134148
}
135149

136-
// GetOrCreateUser creates a domain as specified in the passed account object.
137-
func GetOrCreateUser(csClient *cloudstack.CloudStackClient, user *cloud.User) error {
150+
// GetOrCreateUserWithKey creates a domain as specified in the passed account object.
151+
// Right now only works with a default TemporaryUser name. This function was only built to get a testing user built.
152+
func GetOrCreateUserWithKey(csClient *cloudstack.CloudStackClient, user *cloud.User) error {
138153
if err := GetOrCreateAccount(csClient, &user.Account); err != nil {
139154
return err
140155
}
141156

157+
p := csClient.User.NewListUsersParams()
158+
p.SetAccount(user.Account.Name)
159+
p.SetDomainid(user.Account.Domain.ID)
160+
if resp, err := csClient.User.ListUsers(p); err != nil {
161+
return err
162+
} else if resp.Count > 1 {
163+
return fmt.Errorf("expected exactly one User with name %s, found %d", user.Name, resp.Count)
164+
} else if resp.Count == 1 {
165+
user.ID = resp.Users[0].Id
166+
} else { // User not found, create user.
167+
// TODO: If ever needed, actually implement user creation here.
168+
// For now we only care about the default account since this is a testing infrastructure method.
169+
return fmt.Errorf("User not found for %s", user.Name)
170+
}
171+
172+
pGKey := csClient.User.NewGetUserKeysParams(user.ID)
173+
if resp, err := csClient.User.GetUserKeys(pGKey); err != nil {
174+
return err
175+
} else if user.APIKey != "" {
176+
user.APIKey = resp.Apikey
177+
user.SecretKey = resp.Secretkey
178+
return nil
179+
}
180+
181+
pKey := csClient.User.NewRegisterUserKeysParams(user.ID)
182+
if resp, err := csClient.User.RegisterUserKeys(pKey); err != nil {
183+
return err
184+
} else {
185+
user.APIKey = resp.Apikey
186+
user.SecretKey = resp.Secretkey
187+
}
188+
142189
return nil
143190
}

test/helpers/user_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/apache/cloudstack-go/v2/cloudstack"
2121
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
2222
"github.com/aws/cluster-api-provider-cloudstack/test/helpers"
23-
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/ginkgo/v2"
2424
. "github.com/onsi/gomega"
2525
"github.com/pkg/errors"
2626
"gopkg.in/ini.v1"
@@ -116,6 +116,7 @@ var _ = Describe("Test helper methods", func() {
116116
account := cloud.Account{Name: "TempTestAccount", Domain: domain}
117117
Ω(helpers.GetOrCreateAccount(csClient, &account)).Should(Succeed())
118118
})
119+
// already exists
119120
It("Doesn't fail if the account already exists.", func() {
120121
domain := cloud.Domain{Path: "ROOT/someNewDomain/tooBlah"}
121122
account := cloud.Account{Name: "TempTestAccount", Domain: domain}
@@ -124,11 +125,14 @@ var _ = Describe("Test helper methods", func() {
124125
})
125126
})
126127

127-
// Context("User Creation w/Keys.", func() {
128-
// It("Can create a new account in a new domain.", func() {
129-
// domain := cloud.Domain{Path: "ROOT/someNewDomain/tooBlah"}
130-
// account := cloud.Account{Name: "TempTestAccount", Domain: domain}
131-
// Ω(helpers.GetOrCreateAccount(csClient, &account)).Should(Succeed())
132-
// })
133-
// })
128+
Context("User Creation w/Keys.", func() {
129+
It("Can create a new user with keys.", func() {
130+
domain := cloud.Domain{Path: "ROOT/someNewDomain/tooBlah"}
131+
account := cloud.Account{Name: "TempTestAccount", Domain: domain}
132+
user := cloud.User{Account: account}
133+
Ω(helpers.GetOrCreateUserWithKey(csClient, &user)).Should(Succeed())
134+
Ω(user.ID).ShouldNot(BeEmpty())
135+
Ω(user.APIKey).ShouldNot(BeEmpty())
136+
})
137+
})
134138
})

0 commit comments

Comments
 (0)