From 0fab7d1c181979a28850c6fd2bb0269115a12a6a Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Fri, 18 Oct 2024 19:38:21 -0700 Subject: [PATCH] fix: issue #579 (space roles not exported) (#582) - The `addSpaceUsers` function needs the space GUID as a function param, but instead the org GUID is supplied, resulting in no space users being found as the supplied space GUID (which is actually an org GUI) does not actually exist. This commit fixes this issue by correctly supplying the space GUID. - this commit reverses [a past change](https://github.com/vmware-tanzu-labs/cf-mgmt/commit/1483b66fe83a7fbea70304e478818e07731ecb56#diff-9982210b028e54f7afea530e407e1544509635e4dae9d236e2045280589b6b65L371) which lacks clear justification (so presumably it's an unintentional change). - backfill an integration test that would have caught this regression. [fixes #579] [https://vmw-jira.broadcom.net/browse/TPCF-27431] --- export/exportconfig.go | 2 +- integration/integration_test.go | 58 ++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/export/exportconfig.go b/export/exportconfig.go index 0eed084d..40830e2b 100644 --- a/export/exportconfig.go +++ b/export/exportconfig.go @@ -401,7 +401,7 @@ func (im *Manager) processSpaces(globalConfig *config.GlobalConfig, orgConfig *c spaceConfig := &config.SpaceConfig{Org: orgConfig.Org, Space: spaceName, EnableUnassignSecurityGroup: true} //Add users - err = im.addSpaceUsers(spaceConfig, orgSpace.Relationships.Organization.Data.GUID) + err = im.addSpaceUsers(spaceConfig, orgSpace.GUID) if err != nil { return err } diff --git a/integration/integration_test.go b/integration/integration_test.go index 06e771f5..1cd95f76 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -3,6 +3,8 @@ package integration_test import ( "bytes" "fmt" + "github.com/vmwarepivotallabs/cf-mgmt/config" + "gopkg.in/yaml.v2" "os" "os/exec" "strings" @@ -14,7 +16,8 @@ import ( ) const ( - configDir = "./fixture" + configDir = "./fixture" + exportedConfigDir = "./exported-config" ) // cf runs the cf CLI with the specified args. @@ -255,5 +258,58 @@ var _ = Describe("cf-mgmt cli", func() { Expect(bytes.Contains(orgInfo, []byte("test2-iso-segment"))).Should(BeTrue()) }) }) + + Describe("export-config", func() { + BeforeEach(func() { + fmt.Println("******** Before called *********") + + err := os.MkdirAll(exportedConfigDir, os.ModePerm) + Expect(err).ShouldNot(HaveOccurred()) + // There seems to be an existing issue where if this ldap.yml does not exist, "cf-mgmt export-config" will fail with: + // "Unable to initialize cf-mgmt. Error : Error reading file exported-config/ldap.yml: open exported-config/ldap.yml: no such file or directory" + _, err = os.Create(exportedConfigDir + "/ldap.yml") + Expect(err).ShouldNot(HaveOccurred()) + + cf("create-org", "test-org") + cf("target", "-o", "test-org") + cf("create-space", "test-space") + cf("target", "-o", "test-org", "-s", "test-space") + }) + + AfterEach(func() { + fmt.Println("******** after called *********") + + err := os.RemoveAll(exportedConfigDir) + Expect(err).ShouldNot(HaveOccurred()) + + cf("delete-space", "test-space", "-o", "test-org", "-f") + cf("delete-org", "-f", "test-org") + }) + + It("should complete successfully", func() { + orgs, err := cf("orgs") + Expect(err).ShouldNot(HaveOccurred()) + Expect(string(orgs)).Should(ContainSubstring("test-org")) + + By("exporting space roles") + exportConfigCommand := exec.Command(outPath, "export-config", + "--config-dir", exportedConfigDir, + "--system-domain", systemDomain, + "--user-id", userID, + "--password", password, + "--client-secret", clientSecret) + session, err := Start(exportConfigCommand, GinkgoWriter, GinkgoWriter) + Expect(err).ShouldNot(HaveOccurred()) + Eventually(session).Should(Exit(0)) + + spaceConfigFile, err := os.ReadFile(exportedConfigDir + "/test-org/test-space/spaceConfig.yml") + Expect(err).ShouldNot(HaveOccurred()) + var spaceConfig config.SpaceConfig + yaml.Unmarshal(spaceConfigFile, &spaceConfig) + + // the user that created the space is auto-assigned as a space developer; the exported config should reflect that + Expect(spaceConfig.Developer.Users).Should(ContainElement("admin")) + }) + }) }) })