github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/isolated/delete_security_group_command_test.go (about) 1 package isolated 2 3 import ( 4 "code.cloudfoundry.org/cli/integration/helpers" 5 "code.cloudfoundry.org/cli/resources" 6 . "github.com/onsi/ginkgo" 7 . "github.com/onsi/gomega" 8 . "github.com/onsi/gomega/gbytes" 9 . "github.com/onsi/gomega/gexec" 10 ) 11 12 var _ = Describe("delete-security-group command", func() { 13 var helpText func(*Session) 14 15 BeforeEach(func() { 16 helpText = func(session *Session) { 17 Eventually(session).Should(Say("NAME:")) 18 Eventually(session).Should(Say(`\s+delete-security-group - Deletes a security group`)) 19 Eventually(session).Should(Say("USAGE:")) 20 Eventually(session).Should(Say(`\s+cf delete-security-group SECURITY_GROUP \[-f\]`)) 21 Eventually(session).Should(Say(`TIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 22 Eventually(session).Should(Say("OPTIONS:")) 23 Eventually(session).Should(Say(`\s+--force, -f\s+Force deletion without confirmation`)) 24 Eventually(session).Should(Say("SEE ALSO:")) 25 Eventually(session).Should(Say(`\s+security-groups`)) 26 } 27 }) 28 29 Describe("help", func() { 30 When("--help flag is set", func() { 31 It("Displays command usage to output", func() { 32 session := helpers.CF("delete-security-group", "--help") 33 helpText(session) 34 Eventually(session).Should(Exit(0)) 35 }) 36 }) 37 }) 38 39 When("the environment is not setup correctly", func() { 40 It("fails with the appropriate errors", func() { 41 helpers.CheckEnvironmentTargetedCorrectly(false, false, ReadOnlyOrg, "bind-staging-security-group", "bogus") 42 }) 43 }) 44 45 When("the security group name is not provided", func() { 46 BeforeEach(func() { 47 helpers.LoginCF() 48 }) 49 50 It("displays an error and help", func() { 51 session := helpers.CF("delete-security-group") 52 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SECURITY_GROUP` was not provided")) 53 helpText(session) 54 Eventually(session).Should(Exit(1)) 55 }) 56 }) 57 58 When("the security-group does not exist", func() { 59 BeforeEach(func() { 60 helpers.LoginCF() 61 }) 62 63 It("displays a warning and exits 0", func() { 64 username, _ := helpers.GetCredentials() 65 session := helpers.CF("delete-security-group", "-f", "please-do-not-exist-in-real-life") 66 Eventually(session).Should(Say("Deleting security group please-do-not-exist-in-real-life as %s", username)) 67 Eventually(session).Should(Say("OK")) 68 Eventually(session.Err).Should(Say("Security group 'please-do-not-exist-in-real-life' does not exist.")) 69 Eventually(session).Should(Exit(0)) 70 }) 71 }) 72 73 When("the security group exists", func() { 74 var ( 75 securityGroupName string 76 securityGroup resources.SecurityGroup 77 ports string 78 description string 79 ) 80 81 BeforeEach(func() { 82 helpers.LoginCF() 83 }) 84 85 When("the -f flag not is provided", func() { 86 var buffer *Buffer 87 88 BeforeEach(func() { 89 buffer = NewBuffer() 90 securityGroupName = helpers.NewSecurityGroupName() 91 ports = "8080" 92 description = "my favorite description" 93 securityGroup = helpers.NewSecurityGroup(securityGroupName, "tcp", "0.0.0.0", &ports, &description) 94 helpers.CreateSecurityGroup(securityGroup) 95 }) 96 97 AfterEach(func() { 98 helpers.DeleteSecurityGroup(securityGroup) 99 }) 100 101 When("the user enters 'y'", func() { 102 BeforeEach(func() { 103 _, err := buffer.Write([]byte("y\n")) 104 Expect(err).ToNot(HaveOccurred()) 105 }) 106 107 It("deletes the security group", func() { 108 username, _ := helpers.GetCredentials() 109 session := helpers.CFWithStdin(buffer, "delete-security-group", securityGroupName) 110 Eventually(session).Should(Say(`Really delete the security group %s?`, securityGroupName)) 111 Eventually(session).Should(Say("Deleting security group %s as %s", securityGroupName, username)) 112 Eventually(session).Should(Say("OK")) 113 Eventually(session).Should(Say(`TIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 114 Eventually(session).Should(Exit(0)) 115 Eventually(helpers.CF("security-group", securityGroupName)).Should(Exit(1)) 116 }) 117 }) 118 119 When("the user enters 'n'", func() { 120 BeforeEach(func() { 121 _, err := buffer.Write([]byte("n\n")) 122 Expect(err).ToNot(HaveOccurred()) 123 }) 124 125 It("does not delete the security group", func() { 126 session := helpers.CFWithStdin(buffer, "delete-security-group", securityGroupName) 127 Eventually(session).Should(Say(`Really delete the security group %s?`, securityGroupName)) 128 Eventually(session).Should(Say(`Security group '%s' has not been deleted.`, securityGroupName)) 129 Eventually(session).Should(Exit(0)) 130 Eventually(helpers.CF("security-group", securityGroupName)).Should(Exit(0)) 131 }) 132 }) 133 134 When("the user enters the default input (hits return)", func() { 135 BeforeEach(func() { 136 _, err := buffer.Write([]byte("\n")) 137 Expect(err).ToNot(HaveOccurred()) 138 }) 139 140 It("does not delete the security group", func() { 141 session := helpers.CFWithStdin(buffer, "delete-security-group", securityGroupName) 142 Eventually(session).Should(Say(`Really delete the security group %s?`, securityGroupName)) 143 Eventually(session).Should(Say(`Security group '%s' has not been deleted.`, securityGroupName)) 144 Eventually(session).Should(Exit(0)) 145 Eventually(helpers.CF("security-group", securityGroupName)).Should(Exit(0)) 146 }) 147 }) 148 149 When("the user enters an invalid answer", func() { 150 BeforeEach(func() { 151 // The second '\n' is intentional. Otherwise the buffer will be 152 // closed while the interaction is still waiting for input; it gets 153 // an EOF and causes an error. 154 _, err := buffer.Write([]byte("wat\n\n")) 155 Expect(err).ToNot(HaveOccurred()) 156 }) 157 158 It("asks again", func() { 159 session := helpers.CFWithStdin(buffer, "delete-security-group", securityGroupName) 160 Eventually(session).Should(Say(`Really delete the security group %s?`, securityGroupName)) 161 Eventually(session).Should(Say(`Security group '%s' has not been deleted.`, securityGroupName)) 162 Eventually(session).Should(Exit(0)) 163 Eventually(helpers.CF("security-group", securityGroupName)).Should(Exit(0)) 164 }) 165 }) 166 }) 167 168 When("the -f flag is provided", func() { 169 BeforeEach(func() { 170 securityGroupName = helpers.NewSecurityGroupName() 171 ports = "8080" 172 description = "my favorite description" 173 securityGroup := helpers.NewSecurityGroup(securityGroupName, "tcp", "0.0.0.0", &ports, &description) 174 helpers.CreateSecurityGroup(securityGroup) 175 }) 176 177 It("deletes the security group", func() { 178 username, _ := helpers.GetCredentials() 179 session := helpers.CF("delete-security-group", securityGroupName, "-f") 180 Eventually(session).Should(Say("Deleting security group %s as %s", securityGroupName, username)) 181 Eventually(session).Should(Say("OK")) 182 Eventually(session).Should(Say(`TIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 183 Eventually(session).Should(Exit(0)) 184 Eventually(helpers.CF("security-group", securityGroupName)).Should(Exit(1)) 185 }) 186 }) 187 }) 188 })