|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package helpers_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 21 | + "github.com/aws/cluster-api-provider-cloudstack/pkg/cloud" |
| 22 | + "github.com/aws/cluster-api-provider-cloudstack/test/helpers" |
| 23 | + . "github.com/onsi/ginkgo" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + "github.com/pkg/errors" |
| 26 | + "gopkg.in/ini.v1" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("Test helper methods", func() { |
| 30 | + |
| 31 | + Context("Domain Creation and Deletion Integ Tests", func() { |
| 32 | + var csClient *cloudstack.CloudStackClient |
| 33 | + ccPath := "../../cloud-config" |
| 34 | + var connectionErr error |
| 35 | + conf := cloud.Config{} |
| 36 | + if rawCfg, err := ini.Load("../../cloud-config"); err != nil { |
| 37 | + connectionErr = errors.Wrapf(err, "reading config at path %s:", ccPath) |
| 38 | + } else if g := rawCfg.Section("Global"); len(g.Keys()) == 0 { |
| 39 | + connectionErr = errors.New("section Global not found") |
| 40 | + } else if err = rawCfg.Section("Global").StrictMapTo(&conf); err != nil { |
| 41 | + connectionErr = errors.Wrapf(err, "parsing [Global] section from config at path %s:", ccPath) |
| 42 | + } |
| 43 | + |
| 44 | + csClient = cloudstack.NewAsyncClient(conf.APIURL, conf.APIKey, conf.SecretKey, conf.VerifySSL) |
| 45 | + |
| 46 | + // Get the root domain's ID. |
| 47 | + rootDomainID, err, found := helpers.GetDomainByPath(csClient, "ROOT/") |
| 48 | + Ω(err).ShouldNot(HaveOccurred()) |
| 49 | + Ω(rootDomainID).ShouldNot(BeEmpty()) |
| 50 | + Ω(found).Should(BeTrue()) |
| 51 | + |
| 52 | + BeforeEach(func() { |
| 53 | + if connectionErr != nil { // Only do these tests if an actual ACS instance is available via cloud-config. |
| 54 | + Skip("Could not connect to ACS instance.") |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + AfterEach(func() { |
| 59 | + for _, path := range []string{"ROOT/someNewDomain", "ROOT/blah"} { |
| 60 | + // Delete any created domains. |
| 61 | + id, err, found := helpers.GetDomainByPath(csClient, path) |
| 62 | + Ω(err).ShouldNot(HaveOccurred()) |
| 63 | + if found { |
| 64 | + Ω(helpers.DeleteDomain(csClient, id)).Should(Succeed()) |
| 65 | + } |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + It("Can get the ROOT domain's ID.", func() { |
| 70 | + id, err, found := helpers.GetDomainByPath(csClient, "ROOT/") |
| 71 | + Ω(err).ShouldNot(HaveOccurred()) |
| 72 | + Ω(id).ShouldNot(BeEmpty()) |
| 73 | + Ω(found).Should(BeTrue()) |
| 74 | + }) |
| 75 | + |
| 76 | + It("Doesn't error when unable to get a domain's ID.", func() { |
| 77 | + id, err, found := helpers.GetDomainByPath(csClient, "ROOT/blahnotpresent") |
| 78 | + Ω(err).ShouldNot(HaveOccurred()) |
| 79 | + Ω(found).Should(BeFalse()) |
| 80 | + Ω(id).Should(BeEmpty()) |
| 81 | + }) |
| 82 | + |
| 83 | + It("Can create a domain under a parent domain.", func() { |
| 84 | + id, err := helpers.CreateDomainUnderParent(csClient, rootDomainID, "someNewDomain") |
| 85 | + Ω(id).ShouldNot(BeEmpty()) |
| 86 | + Ω(err).ShouldNot(HaveOccurred()) |
| 87 | + }) |
| 88 | + |
| 89 | + It("Returns an appropriate error when the domain already exists.", func() { |
| 90 | + someDomain := &cloud.Domain{Name: "blah", Path: "blah"} |
| 91 | + Ω(helpers.GetOrCreateDomain(someDomain, csClient)).Should(Succeed()) |
| 92 | + Ω(someDomain.Name).Should(Equal("blah")) |
| 93 | + Ω(someDomain.Path).Should(Equal("ROOT/blah")) |
| 94 | + Ω(someDomain.ID).ShouldNot(BeEmpty()) |
| 95 | + _, err = helpers.CreateDomainUnderParent(csClient, rootDomainID, "blah") |
| 96 | + Ω(err).Should(HaveOccurred()) |
| 97 | + Ω(err.Error()).Should(ContainSubstring("already exists")) |
| 98 | + }) |
| 99 | + |
| 100 | + It("Doesn't error if the domain already exists.", func() { |
| 101 | + someDomain := &cloud.Domain{Name: "blah", Path: "blah"} |
| 102 | + Ω(helpers.GetOrCreateDomain(someDomain, csClient)).Should(Succeed()) |
| 103 | + Ω(someDomain.Name).Should(Equal("blah")) |
| 104 | + Ω(someDomain.Path).Should(Equal("ROOT/blah")) |
| 105 | + Ω(someDomain.ID).ShouldNot(BeEmpty()) |
| 106 | + |
| 107 | + Ω(helpers.GetOrCreateDomain(someDomain, csClient)).Should(Succeed()) |
| 108 | + Ω(someDomain.Name).Should(Equal("blah")) |
| 109 | + Ω(someDomain.Path).Should(Equal("ROOT/blah")) |
| 110 | + Ω(someDomain.ID).ShouldNot(BeEmpty()) |
| 111 | + }) |
| 112 | + |
| 113 | + It("Can create a wholly new multi-level sub-domain path.", func() { |
| 114 | + someDomain := &cloud.Domain{Name: "tooBlah", Path: "ROOT/someNewDomain/tooBlah"} |
| 115 | + Ω(helpers.GetOrCreateDomain(someDomain, csClient)).Should(Succeed()) |
| 116 | + Ω(someDomain.Name).Should(Equal("tooBlah")) |
| 117 | + Ω(someDomain.Path).Should(Equal("ROOT/someNewDomain/tooBlah")) |
| 118 | + Ω(someDomain.ID).ShouldNot(BeEmpty()) |
| 119 | + }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments