Skip to content

Commit ac465e3

Browse files
author
Joshua Reed
committed
Runs locally. Fingers crossed.
1 parent 2c38d8c commit ac465e3

File tree

6 files changed

+69
-27
lines changed

6 files changed

+69
-27
lines changed

test/helpers/client.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package helpers
2+
3+
import (
4+
"os"
5+
6+
"github.com/apache/cloudstack-go/v2/cloudstack"
7+
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
8+
"github.com/pkg/errors"
9+
"gopkg.in/ini.v1"
10+
)
11+
12+
func NewCSClient() (*cloudstack.CloudStackClient, error) {
13+
projDir := os.Getenv("PROJECT_DIR")
14+
conf := cloud.Config{}
15+
ccPath := projDir + "/cloud-config"
16+
if rawCfg, err := ini.Load(ccPath); err != nil {
17+
return nil, errors.Wrapf(err, "reading config at path %s:", ccPath)
18+
} else if g := rawCfg.Section("Global"); len(g.Keys()) == 0 {
19+
return nil, errors.New("section Global not found")
20+
} else if err = rawCfg.Section("Global").StrictMapTo(&conf); err != nil {
21+
return nil, errors.Wrapf(err, "parsing [Global] section from config at path %s:", ccPath)
22+
}
23+
csClient := cloudstack.NewAsyncClient(conf.APIURL, conf.APIKey, conf.SecretKey, conf.VerifySSL)
24+
return csClient, nil
25+
}

test/helpers/user.go

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

11-
const tempUserName = "TemporaryUser"
11+
const TempUserName = "TemporaryUser"
1212

1313
// GetDomainByPath fetches a domain by its path.
1414
func GetDomainByPath(csClient *cloudstack.CloudStackClient, path string) (string, error, bool) {
@@ -134,7 +134,7 @@ func GetOrCreateAccount(csClient *cloudstack.CloudStackClient, account *cloud.Ac
134134
return fmt.Errorf("expected exactly one role with name 'Domain Admin', found %d", count)
135135
}
136136

137-
p := csClient.Account.NewCreateAccountParams("[email protected]", "first", "last", "temp123", tempUserName)
137+
p := csClient.Account.NewCreateAccountParams("[email protected]", "first", "last", "temp123", TempUserName)
138138
p.SetDomainid(account.Domain.ID)
139139
p.SetRoleid(roleDetails.Id)
140140
resp, err := csClient.Account.CreateAccount(p)

test/helpers/user_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,15 @@ limitations under the License.
1717
package helpers_test
1818

1919
import (
20-
"github.com/apache/cloudstack-go/v2/cloudstack"
2120
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
2221
"github.com/aws/cluster-api-provider-cloudstack/test/helpers"
2322
. "github.com/onsi/ginkgo/v2"
2423
. "github.com/onsi/gomega"
25-
"github.com/pkg/errors"
26-
"gopkg.in/ini.v1"
2724
)
2825

2926
var _ = Describe("Test helper methods", func() {
30-
var csClient *cloudstack.CloudStackClient
31-
ccPath := "../../cloud-config"
32-
conf := cloud.Config{}
33-
if rawCfg, err := ini.Load("../../cloud-config"); err != nil {
34-
Ω(errors.Wrapf(err, "reading config at path %s:", ccPath)).ShouldNot(HaveOccurred())
35-
} else if g := rawCfg.Section("Global"); len(g.Keys()) == 0 {
36-
Ω(errors.New("section Global not found")).ShouldNot(HaveOccurred())
37-
} else if err = rawCfg.Section("Global").StrictMapTo(&conf); err != nil {
38-
Ω(errors.Wrapf(err, "parsing [Global] section from config at path %s:", ccPath)).ShouldNot(HaveOccurred())
39-
}
40-
csClient = cloudstack.NewAsyncClient(conf.APIURL, conf.APIKey, conf.SecretKey, conf.VerifySSL)
27+
csClient, err := helpers.NewCSClient()
28+
Ω(err).ShouldNot(HaveOccurred())
4129

4230
// Get the root domain's ID.
4331
rootDomainID, err, found := helpers.GetDomainByPath(csClient, "ROOT/")

test/unit/cloud/cloud_suite_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,53 @@ import (
2323
"github.com/apache/cloudstack-go/v2/cloudstack"
2424
"github.com/aws/cluster-api-provider-cloudstack-staging/test/unit/dummies"
2525
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
26+
"github.com/aws/cluster-api-provider-cloudstack/test/helpers"
2627
. "github.com/onsi/ginkgo/v2"
2728
. "github.com/onsi/gomega"
29+
"k8s.io/apimachinery/pkg/util/uuid"
2830
)
2931

3032
var (
3133
realCloudClient cloud.Client
3234
client cloud.Client
3335
realCSClient *cloudstack.CloudStackClient
36+
testDomainPath string // Needed in before and in after suite.
3437
)
3538

3639
var _ = BeforeSuite(func() {
40+
// Create a real cloud client.
3741
projDir := os.Getenv("PROJECT_DIR")
3842
var connectionErr error
3943
realCloudClient, connectionErr = cloud.NewClient(projDir + "/cloud-config")
4044
Ω(connectionErr).ShouldNot(HaveOccurred())
4145

42-
Ω().Should(Succeed())
46+
// Create a real CloudStack client.
47+
realCSClient, connectionErr = helpers.NewCSClient()
48+
Ω(connectionErr).ShouldNot(HaveOccurred())
49+
50+
// Create a new account and user to run tests that use a real ACS instance.
51+
uid := string(uuid.NewUUID())
52+
newAccount := cloud.Account{
53+
Name: "TestAccount-" + uid,
54+
Domain: cloud.Domain{Name: "TestDomain-" + uid, Path: "ROOT/TestDomain-" + uid}}
55+
newUser := cloud.User{Account: newAccount}
56+
Ω(helpers.GetOrCreateUserWithKey(realCSClient, &newUser)).Should(Succeed())
57+
testDomainPath = newAccount.Domain.Path
58+
59+
Ω(newUser.APIKey).ShouldNot(BeEmpty())
60+
61+
// Switch to test account user.
62+
cfg := cloud.Config{APIKey: newUser.APIKey, SecretKey: newUser.SecretKey}
63+
realCloudClient, connectionErr = realCloudClient.NewClientFromSpec(cfg)
64+
Ω(connectionErr).ShouldNot(HaveOccurred())
65+
})
66+
67+
var _ = AfterSuite(func() {
68+
// Delete created domain.
69+
id, err, found := helpers.GetDomainByPath(realCSClient, testDomainPath)
70+
Ω(err).ShouldNot(HaveOccurred())
71+
Ω(found).Should(BeTrue())
72+
Ω(helpers.DeleteDomain(realCSClient, id)).Should(Succeed())
4373
})
4474

4575
func TestCloud(t *testing.T) {
@@ -49,10 +79,10 @@ func TestCloud(t *testing.T) {
4979

5080
// FetchIntegTestResources runs through basic CloudStack Client setup methods needed to test others.
5181
func FetchIntegTestResources() {
52-
Ω(client.ResolveZone(dummies.CSZone1)).Should(Succeed())
82+
realCloudClient.ResolveZone(dummies.CSZone1)
5383
Ω(dummies.CSZone1.Spec.ID).ShouldNot(BeEmpty())
5484
dummies.CSMachine1.Status.ZoneID = dummies.CSZone1.Spec.ID
5585
dummies.CSMachine1.Spec.DiskOffering.Name = ""
5686
dummies.CSCluster.Spec.ControlPlaneEndpoint.Host = ""
57-
Ω(client.GetOrCreateIsolatedNetwork(dummies.CSZone1, dummies.CSISONet1, dummies.CSCluster)).Should(Succeed())
87+
Ω(realCloudClient.GetOrCreateIsolatedNetwork(dummies.CSZone1, dummies.CSISONet1, dummies.CSCluster)).Should(Succeed())
5888
}

test/unit/cloud/isolated_network_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,7 @@ var _ = Describe("Network", func() {
213213
dummies.CSZone1.Spec.ID = "" // Make CAPC methods resolve this.
214214
dummies.CSCluster.Status.Zones = capcv1.ZoneStatusMap{}
215215

216-
// Get Zone info needed for network testing.
217-
Ω(client.ResolveZone(dummies.CSZone1)).Should(Succeed())
218-
dummies.CSISONet1.Spec.ID = ""
216+
FetchIntegTestResources()
219217
})
220218

221219
It("fetches an isolated network", func() {
@@ -238,8 +236,10 @@ var _ = Describe("Network", func() {
238236
It("adds an isolated network and doesn't fail when asked to GetOrCreateIsolatedNetwork multiple times", func() {
239237
Ω(client.GetOrCreateIsolatedNetwork(dummies.CSZone1, dummies.CSISONet1, dummies.CSCluster)).Should(Succeed())
240238
Ω(client.GetOrCreateIsolatedNetwork(dummies.CSZone1, dummies.CSISONet1, dummies.CSCluster)).Should(Succeed())
239+
241240
// Network should now exist if it didn't at the start.
242241
Ω(client.ResolveNetwork(&dummies.ISONet1)).Should(Succeed())
242+
243243
// Do once more.
244244
Ω(client.GetOrCreateIsolatedNetwork(dummies.CSZone1, dummies.CSISONet1, dummies.CSCluster)).Should(Succeed())
245245
})

test/unit/cloud/user_credentials_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cloud_test
1919
import (
2020
"github.com/aws/cluster-api-provider-cloudstack-staging/test/unit/dummies"
2121
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
22+
"github.com/aws/cluster-api-provider-cloudstack/test/helpers"
2223
. "github.com/onsi/ginkgo/v2"
2324

2425
. "github.com/onsi/gomega"
@@ -43,11 +44,9 @@ var _ = Describe("User Credentials", func() {
4344
BeforeEach(func() {
4445
client = realCloudClient
4546

46-
// Setup dummies.
47-
// TODO: move these to the test dummies package.
48-
domain = cloud.Domain{Path: "ROOT/blah/blah/subsub"}
49-
account = cloud.Account{Name: "SuperNested", Domain: domain}
50-
user = cloud.User{Name: "SubSub", Account: account}
47+
domain = cloud.Domain{Path: testDomainPath}
48+
account = cloud.Account{Domain: domain}
49+
user = cloud.User{Name: helpers.TempUserName, Account: account}
5150
})
5251

5352
It("can resolve a domain from the path", func() {

0 commit comments

Comments
 (0)