github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/delete_orphaned_routes_command_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/integration/helpers" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 . "github.com/onsi/gomega/gexec" 11 ) 12 13 var _ = Describe("delete-orphaned-routes command", func() { 14 Context("when the environment is not setup correctly", func() { 15 Context("when no API endpoint is set", func() { 16 BeforeEach(func() { 17 helpers.UnsetAPI() 18 }) 19 20 It("fails with no API endpoint set message", func() { 21 session := helpers.CF("delete-orphaned-routes", "-f") 22 Eventually(session.Out).Should(Say("FAILED")) 23 Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint.")) 24 Eventually(session).Should(Exit(1)) 25 }) 26 }) 27 28 Context("when not logged in", func() { 29 BeforeEach(func() { 30 helpers.LogoutCF() 31 }) 32 33 It("fails with not logged in message", func() { 34 session := helpers.CF("delete-orphaned-routes", "-f") 35 Eventually(session.Out).Should(Say("FAILED")) 36 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in.")) 37 Eventually(session).Should(Exit(1)) 38 }) 39 }) 40 41 Context("when there no org set", func() { 42 BeforeEach(func() { 43 helpers.LogoutCF() 44 helpers.LoginCF() 45 }) 46 47 It("fails with no targeted org error message", func() { 48 session := helpers.CF("delete-orphaned-routes", "-f") 49 Eventually(session.Out).Should(Say("FAILED")) 50 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org.")) 51 Eventually(session).Should(Exit(1)) 52 }) 53 }) 54 55 Context("when there no space set", func() { 56 BeforeEach(func() { 57 helpers.LogoutCF() 58 helpers.LoginCF() 59 helpers.TargetOrg(ReadOnlyOrg) 60 }) 61 62 It("fails with no space targeted error message", func() { 63 session := helpers.CF("delete-orphaned-routes", "-f") 64 Eventually(session.Out).Should(Say("FAILED")) 65 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space")) 66 Eventually(session).Should(Exit(1)) 67 }) 68 }) 69 }) 70 71 Context("when the environment is setup correctly", func() { 72 var ( 73 orgName string 74 spaceName string 75 domainName string 76 appName string 77 domain helpers.Domain 78 ) 79 80 BeforeEach(func() { 81 orgName = helpers.NewOrgName() 82 spaceName = helpers.NewSpaceName() 83 domainName = helpers.DomainName() 84 appName = helpers.PrefixedRandomName("APP") 85 86 setupCF(orgName, spaceName) 87 domain = helpers.NewDomain(orgName, domainName) 88 domain.Create() 89 }) 90 91 AfterEach(func() { 92 helpers.QuickDeleteOrg(orgName) 93 }) 94 95 Context("when there are orphaned routes", func() { 96 var ( 97 orphanedRoute1 helpers.Route 98 orphanedRoute2 helpers.Route 99 ) 100 101 BeforeEach(func() { 102 orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1") 103 orphanedRoute2 = helpers.NewRoute(spaceName, domainName, "orphan-2", "path-2") 104 orphanedRoute1.Create() 105 orphanedRoute2.Create() 106 }) 107 108 It("deletes all the orphaned routes", func() { 109 Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll( 110 Exit(0), 111 Say("Getting routes as"), 112 Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)), 113 Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", domainName)), 114 Say("OK"), 115 )) 116 }) 117 }) 118 119 Context("when there are orphaned routes and bound routes", func() { 120 var ( 121 orphanedRoute1 helpers.Route 122 orphanedRoute2 helpers.Route 123 boundRoute helpers.Route 124 ) 125 126 BeforeEach(func() { 127 orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1") 128 orphanedRoute2 = helpers.NewRoute(spaceName, domainName, "orphan-2", "path-2") 129 orphanedRoute1.Create() 130 orphanedRoute2.Create() 131 132 helpers.WithHelloWorldApp(func(appDir string) { 133 Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0)) 134 }) 135 Eventually(helpers.CF("apps")).Should(And(Exit(0), Say(fmt.Sprintf("%s\\s+stopped\\s+0/1", appName)))) 136 137 boundRoute = helpers.NewRoute(spaceName, domainName, "bound-1", "path-3") 138 boundRoute.Create() 139 helpers.BindRouteToApplication(appName, boundRoute.Domain, boundRoute.Host, boundRoute.Path) 140 }) 141 142 It("deletes only the orphaned routes", func() { 143 Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll( 144 Exit(0), 145 Say("Getting routes as"), 146 Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)), 147 Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", domainName)), 148 Not(Say(fmt.Sprintf("Deleting route bound-1.%s/path-3...", domainName))), 149 Say("OK"), 150 )) 151 }) 152 }) 153 154 Context("when there are more than one page of routes", func() { 155 BeforeEach(func() { 156 var orphanedRoute helpers.Route 157 for i := 0; i < 51; i++ { 158 orphanedRoute = helpers.NewRoute(spaceName, domainName, fmt.Sprintf("orphan-multi-page-%d", i), "") 159 orphanedRoute.Create() 160 } 161 }) 162 It("deletes all the orphaned routes", func() { 163 session := helpers.CF("delete-orphaned-routes", "-f") 164 165 for i := 0; i < 51; i++ { 166 Eventually(session.Out).Should(Say(fmt.Sprintf("Deleting route orphan-multi-page-%d.%s...", i, domainName))) 167 } 168 Eventually(session).Should(Exit(0)) 169 }) 170 }) 171 172 Context("when the force flag is not given", func() { 173 var buffer *Buffer 174 BeforeEach(func() { 175 orphanedRoute := helpers.NewRoute(spaceName, domainName, "orphan", "path") 176 orphanedRoute.Create() 177 }) 178 179 Context("when the user inputs y", func() { 180 BeforeEach(func() { 181 buffer = NewBuffer() 182 buffer.Write([]byte("y\n")) 183 }) 184 185 It("deletes the orphaned routes", func() { 186 session := helpers.CFWithStdin(buffer, "delete-orphaned-routes") 187 Eventually(session).Should(Say("Really delete orphaned routes?")) 188 Eventually(session).Should(SatisfyAll( 189 Exit(0), 190 Say("Getting routes as"), 191 Say(fmt.Sprintf("Deleting route orphan.%s/path...", domainName)), 192 Say("OK"), 193 )) 194 }) 195 }) 196 197 Context("when the user inputs n", func() { 198 BeforeEach(func() { 199 buffer = NewBuffer() 200 buffer.Write([]byte("n\n")) 201 }) 202 203 It("exits without deleting the orphaned routes", func() { 204 session := helpers.CFWithStdin(buffer, "delete-orphaned-routes") 205 Eventually(session).Should(Say("Really delete orphaned routes?")) 206 Eventually(session).Should(SatisfyAll( 207 Exit(0), 208 Not(Say("Getting routes as")), 209 Not(Say(fmt.Sprintf("Deleting route orphan.%s/path...", domainName))), 210 Not(Say("OK")), 211 )) 212 }) 213 }) 214 }) 215 216 Context("when there are no orphaned routes", func() { 217 var ( 218 boundRoute helpers.Route 219 ) 220 221 BeforeEach(func() { 222 helpers.WithHelloWorldApp(func(appDir string) { 223 Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0)) 224 }) 225 Eventually(helpers.CF("apps")).Should(And(Exit(0), Say(fmt.Sprintf("%s\\s+stopped\\s+0/1", appName)))) 226 227 boundRoute = helpers.NewRoute(spaceName, domainName, "bound-route", "bound-path") 228 boundRoute.Create() 229 helpers.BindRouteToApplication(appName, boundRoute.Domain, boundRoute.Host, boundRoute.Path) 230 }) 231 232 It("displays OK without deleting any routes", func() { 233 Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll( 234 Exit(0), 235 Say("Getting routes as"), 236 Not(Say(fmt.Sprintf("Deleting route bound-route.%s/bound-path...", domainName))), 237 Say("OK"), 238 )) 239 }) 240 }) 241 242 Context("when the orphaned routes are attached to both shared and private domains", func() { 243 var ( 244 orphanedRoute1 helpers.Route 245 orphanedRoute2 helpers.Route 246 sharedDomainName string 247 ) 248 249 BeforeEach(func() { 250 sharedDomainName = helpers.DomainName() 251 sharedDomain := helpers.NewDomain(orgName, sharedDomainName) 252 sharedDomain.Create() 253 sharedDomain.Share() 254 255 orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1") 256 orphanedRoute2 = helpers.NewRoute(spaceName, sharedDomainName, "orphan-2", "path-2") 257 orphanedRoute1.Create() 258 orphanedRoute2.Create() 259 }) 260 261 It("deletes both the routes", func() { 262 Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll( 263 Exit(0), 264 Say("Getting routes as"), 265 Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)), 266 Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", sharedDomainName)), 267 Say("OK"), 268 )) 269 }) 270 }) 271 }) 272 })