endpoint_location_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package testing
  2. import (
  3. "strings"
  4. "testing"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack"
  7. tokens2 "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/identity/v2/tokens"
  8. tokens3 "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/identity/v3/tokens"
  9. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  10. )
  11. // Service catalog fixtures take too much vertical space!
  12. var catalog2 = tokens2.ServiceCatalog{
  13. Entries: []tokens2.CatalogEntry{
  14. tokens2.CatalogEntry{
  15. Type: "same",
  16. Name: "same",
  17. Endpoints: []tokens2.Endpoint{
  18. tokens2.Endpoint{
  19. Region: "same",
  20. PublicURL: "https://public.correct.com/",
  21. InternalURL: "https://internal.correct.com/",
  22. AdminURL: "https://admin.correct.com/",
  23. },
  24. tokens2.Endpoint{
  25. Region: "different",
  26. PublicURL: "https://badregion.com/",
  27. },
  28. },
  29. },
  30. tokens2.CatalogEntry{
  31. Type: "same",
  32. Name: "different",
  33. Endpoints: []tokens2.Endpoint{
  34. tokens2.Endpoint{
  35. Region: "same",
  36. PublicURL: "https://badname.com/",
  37. },
  38. tokens2.Endpoint{
  39. Region: "different",
  40. PublicURL: "https://badname.com/+badregion",
  41. },
  42. },
  43. },
  44. tokens2.CatalogEntry{
  45. Type: "different",
  46. Name: "different",
  47. Endpoints: []tokens2.Endpoint{
  48. tokens2.Endpoint{
  49. Region: "same",
  50. PublicURL: "https://badtype.com/+badname",
  51. },
  52. tokens2.Endpoint{
  53. Region: "different",
  54. PublicURL: "https://badtype.com/+badregion+badname",
  55. },
  56. },
  57. },
  58. },
  59. }
  60. func TestV2EndpointExact(t *testing.T) {
  61. expectedURLs := map[gophercloud.Availability]string{
  62. gophercloud.AvailabilityPublic: "https://public.correct.com/",
  63. gophercloud.AvailabilityAdmin: "https://admin.correct.com/",
  64. gophercloud.AvailabilityInternal: "https://internal.correct.com/",
  65. }
  66. for availability, expected := range expectedURLs {
  67. actual, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
  68. Type: "same",
  69. Name: "same",
  70. Region: "same",
  71. Availability: availability,
  72. })
  73. th.AssertNoErr(t, err)
  74. th.CheckEquals(t, expected, actual)
  75. }
  76. }
  77. func TestV2EndpointNone(t *testing.T) {
  78. _, actual := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
  79. Type: "nope",
  80. Availability: gophercloud.AvailabilityPublic,
  81. })
  82. expected := &gophercloud.ErrEndpointNotFound{}
  83. th.CheckEquals(t, expected.Error(), actual.Error())
  84. }
  85. func TestV2EndpointMultiple(t *testing.T) {
  86. _, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
  87. Type: "same",
  88. Region: "same",
  89. Availability: gophercloud.AvailabilityPublic,
  90. })
  91. if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") {
  92. t.Errorf("Received unexpected error: %v", err)
  93. }
  94. }
  95. func TestV2EndpointBadAvailability(t *testing.T) {
  96. _, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
  97. Type: "same",
  98. Name: "same",
  99. Region: "same",
  100. Availability: "wat",
  101. })
  102. th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
  103. }
  104. var catalog3 = tokens3.ServiceCatalog{
  105. Entries: []tokens3.CatalogEntry{
  106. tokens3.CatalogEntry{
  107. Type: "same",
  108. Name: "same",
  109. Endpoints: []tokens3.Endpoint{
  110. tokens3.Endpoint{
  111. ID: "1",
  112. Region: "same",
  113. Interface: "public",
  114. URL: "https://public.correct.com/",
  115. },
  116. tokens3.Endpoint{
  117. ID: "2",
  118. Region: "same",
  119. Interface: "admin",
  120. URL: "https://admin.correct.com/",
  121. },
  122. tokens3.Endpoint{
  123. ID: "3",
  124. Region: "same",
  125. Interface: "internal",
  126. URL: "https://internal.correct.com/",
  127. },
  128. tokens3.Endpoint{
  129. ID: "4",
  130. Region: "different",
  131. Interface: "public",
  132. URL: "https://badregion.com/",
  133. },
  134. },
  135. },
  136. tokens3.CatalogEntry{
  137. Type: "same",
  138. Name: "different",
  139. Endpoints: []tokens3.Endpoint{
  140. tokens3.Endpoint{
  141. ID: "5",
  142. Region: "same",
  143. Interface: "public",
  144. URL: "https://badname.com/",
  145. },
  146. tokens3.Endpoint{
  147. ID: "6",
  148. Region: "different",
  149. Interface: "public",
  150. URL: "https://badname.com/+badregion",
  151. },
  152. },
  153. },
  154. tokens3.CatalogEntry{
  155. Type: "different",
  156. Name: "different",
  157. Endpoints: []tokens3.Endpoint{
  158. tokens3.Endpoint{
  159. ID: "7",
  160. Region: "same",
  161. Interface: "public",
  162. URL: "https://badtype.com/+badname",
  163. },
  164. tokens3.Endpoint{
  165. ID: "8",
  166. Region: "different",
  167. Interface: "public",
  168. URL: "https://badtype.com/+badregion+badname",
  169. },
  170. },
  171. },
  172. tokens3.CatalogEntry{
  173. Type: "someother",
  174. Name: "someother",
  175. Endpoints: []tokens3.Endpoint{
  176. tokens3.Endpoint{
  177. ID: "1",
  178. Region: "someother",
  179. Interface: "public",
  180. URL: "https://public.correct.com/",
  181. },
  182. tokens3.Endpoint{
  183. ID: "2",
  184. RegionID: "someother",
  185. Interface: "admin",
  186. URL: "https://admin.correct.com/",
  187. },
  188. tokens3.Endpoint{
  189. ID: "3",
  190. RegionID: "someother",
  191. Interface: "internal",
  192. URL: "https://internal.correct.com/",
  193. },
  194. },
  195. },
  196. },
  197. }
  198. func TestV3EndpointExact(t *testing.T) {
  199. expectedURLs := map[gophercloud.Availability]string{
  200. gophercloud.AvailabilityPublic: "https://public.correct.com/",
  201. gophercloud.AvailabilityAdmin: "https://admin.correct.com/",
  202. gophercloud.AvailabilityInternal: "https://internal.correct.com/",
  203. }
  204. for availability, expected := range expectedURLs {
  205. actual, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
  206. Type: "same",
  207. Name: "same",
  208. Region: "same",
  209. Availability: availability,
  210. })
  211. th.AssertNoErr(t, err)
  212. th.CheckEquals(t, expected, actual)
  213. }
  214. }
  215. func TestV3EndpointNone(t *testing.T) {
  216. _, actual := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
  217. Type: "nope",
  218. Availability: gophercloud.AvailabilityPublic,
  219. })
  220. expected := &gophercloud.ErrEndpointNotFound{}
  221. th.CheckEquals(t, expected.Error(), actual.Error())
  222. }
  223. func TestV3EndpointMultiple(t *testing.T) {
  224. _, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
  225. Type: "same",
  226. Region: "same",
  227. Availability: gophercloud.AvailabilityPublic,
  228. })
  229. if !strings.HasPrefix(err.Error(), "Discovered 2 matching endpoints:") {
  230. t.Errorf("Received unexpected error: %v", err)
  231. }
  232. }
  233. func TestV3EndpointBadAvailability(t *testing.T) {
  234. _, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
  235. Type: "same",
  236. Name: "same",
  237. Region: "same",
  238. Availability: "wat",
  239. })
  240. th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
  241. }
  242. func TestV3EndpointWithRegionID(t *testing.T) {
  243. expectedURLs := map[gophercloud.Availability]string{
  244. gophercloud.AvailabilityPublic: "https://public.correct.com/",
  245. gophercloud.AvailabilityAdmin: "https://admin.correct.com/",
  246. gophercloud.AvailabilityInternal: "https://internal.correct.com/",
  247. }
  248. for availability, expected := range expectedURLs {
  249. actual, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
  250. Type: "someother",
  251. Name: "someother",
  252. Region: "someother",
  253. Availability: availability,
  254. })
  255. th.AssertNoErr(t, err)
  256. th.CheckEquals(t, expected, actual)
  257. }
  258. }