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