github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/security_groups/defaults/running/running_test.go (about) 1 package running_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "time" 7 8 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 9 "github.com/cloudfoundry/cli/cf/configuration/core_config" 10 "github.com/cloudfoundry/cli/cf/models" 11 "github.com/cloudfoundry/cli/cf/net" 12 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 13 testnet "github.com/cloudfoundry/cli/testhelpers/net" 14 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 15 16 . "github.com/cloudfoundry/cli/cf/api/security_groups/defaults/running" 17 . "github.com/cloudfoundry/cli/testhelpers/matchers" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 ) 21 22 var _ = Describe("RunningSecurityGroupsRepo", func() { 23 var ( 24 testServer *httptest.Server 25 testHandler *testnet.TestHandler 26 configRepo core_config.ReadWriter 27 repo RunningSecurityGroupsRepo 28 ) 29 30 BeforeEach(func() { 31 configRepo = testconfig.NewRepositoryWithDefaults() 32 gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{}) 33 repo = NewRunningSecurityGroupsRepo(configRepo, gateway) 34 }) 35 36 AfterEach(func() { 37 testServer.Close() 38 }) 39 40 setupTestServer := func(reqs ...testnet.TestRequest) { 41 testServer, testHandler = testnet.NewServer(reqs) 42 configRepo.SetApiEndpoint(testServer.URL) 43 } 44 45 Describe(".BindToRunningSet", func() { 46 It("makes a correct request", func() { 47 setupTestServer( 48 testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 49 Method: "PUT", 50 Path: "/v2/config/running_security_groups/a-real-guid", 51 Response: testnet.TestResponse{ 52 Status: http.StatusCreated, 53 Body: bindRunningResponse, 54 }, 55 }), 56 ) 57 58 err := repo.BindToRunningSet("a-real-guid") 59 60 Expect(err).ToNot(HaveOccurred()) 61 Expect(testHandler).To(HaveAllRequestsCalled()) 62 }) 63 }) 64 65 Describe(".UnbindFromRunningSet", func() { 66 It("makes a correct request", func() { 67 testServer, testHandler = testnet.NewServer([]testnet.TestRequest{ 68 testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 69 Method: "DELETE", 70 Path: "/v2/config/running_security_groups/my-guid", 71 Response: testnet.TestResponse{ 72 Status: http.StatusNoContent, 73 }, 74 }), 75 }) 76 77 configRepo.SetApiEndpoint(testServer.URL) 78 err := repo.UnbindFromRunningSet("my-guid") 79 80 Expect(err).ToNot(HaveOccurred()) 81 Expect(testHandler).To(HaveAllRequestsCalled()) 82 }) 83 }) 84 85 Describe(".List", func() { 86 It("returns a list of security groups that are the defaults for running", func() { 87 setupTestServer( 88 testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 89 Method: "GET", 90 Path: "/v2/config/running_security_groups", 91 Response: testnet.TestResponse{ 92 Status: http.StatusOK, 93 Body: firstRunningListItem, 94 }, 95 }), 96 testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 97 Method: "GET", 98 Path: "/v2/config/running_security_groups", 99 Response: testnet.TestResponse{ 100 Status: http.StatusOK, 101 Body: secondRunningListItem, 102 }, 103 }), 104 ) 105 106 defaults, err := repo.List() 107 108 Expect(err).ToNot(HaveOccurred()) 109 Expect(testHandler).To(HaveAllRequestsCalled()) 110 Expect(defaults).To(ConsistOf([]models.SecurityGroupFields{ 111 { 112 Name: "name-71", 113 Guid: "cd186158-b356-474d-9861-724f34f48502", 114 SpaceUrl: "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces", 115 Rules: []map[string]interface{}{{ 116 "protocol": "udp", 117 }}, 118 }, 119 { 120 Name: "name-72", 121 Guid: "d3374b62-7eac-4823-afbd-460d2bf44c67", 122 SpaceUrl: "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces", 123 Rules: []map[string]interface{}{{ 124 "destination": "198.41.191.47/1", 125 }}, 126 }, 127 })) 128 }) 129 }) 130 }) 131 132 var bindRunningResponse string = `{ 133 "metadata": { 134 "guid": "897341eb-ef31-406f-b57b-414f51583a3a", 135 "url": "/v2/config/running_security_groups/897341eb-ef31-406f-b57b-414f51583a3a", 136 "created_at": "2014-06-23T21:43:30+00:00", 137 "updated_at": "2014-06-23T21:43:30+00:00" 138 }, 139 "entity": { 140 "name": "name-904", 141 "rules": [ 142 { 143 "protocol": "udp", 144 "ports": "8080", 145 "destination": "198.41.191.47/1" 146 } 147 ] 148 } 149 }` 150 151 var firstRunningListItem string = `{ 152 "next_url": "/v2/config/running_security_groups?page=2", 153 "resources": [ 154 { 155 "metadata": { 156 "guid": "cd186158-b356-474d-9861-724f34f48502", 157 "url": "/v2/security_groups/cd186158-b356-474d-9861-724f34f48502", 158 "created_at": "2014-06-23T22:55:30+00:00", 159 "updated_at": null 160 }, 161 "entity": { 162 "name": "name-71", 163 "rules": [ 164 { 165 "protocol": "udp" 166 } 167 ], 168 "spaces_url": "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces" 169 } 170 } 171 ] 172 }` 173 174 var secondRunningListItem string = `{ 175 "next_url": null, 176 "resources": [ 177 { 178 "metadata": { 179 "guid": "d3374b62-7eac-4823-afbd-460d2bf44c67", 180 "url": "/v2/config/running_security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67", 181 "created_at": "2014-06-23T22:55:30+00:00", 182 "updated_at": null 183 }, 184 "entity": { 185 "name": "name-72", 186 "rules": [ 187 { 188 "destination": "198.41.191.47/1" 189 } 190 ], 191 "spaces_url": "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces" 192 } 193 } 194 ] 195 }`