blob: 9c0119cb28c23549843c6d7d01308e37f11e2876 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsonc4360202014-08-29 14:14:24 -04002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
jrperritt3d966162016-06-06 14:08:54 -05009 "github.com/gophercloud/gophercloud/openstack/utils"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsonc4360202014-08-29 14:14:24 -040011)
12
Ash Wilsona0c4c842014-09-09 11:30:58 -040013func setupVersionHandler() {
14 testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
15 fmt.Fprintf(w, `
16 {
17 "versions": {
18 "values": [
19 {
20 "status": "stable",
21 "id": "v3.0",
22 "links": [
23 { "href": "%s/v3.0", "rel": "self" }
24 ]
25 },
26 {
27 "status": "stable",
28 "id": "v2.0",
29 "links": [
30 { "href": "%s/v2.0", "rel": "self" }
31 ]
32 }
33 ]
34 }
35 }
36 `, testhelper.Server.URL, testhelper.Server.URL)
37 })
38}
39
Ash Wilsonc4360202014-08-29 14:14:24 -040040func TestChooseVersion(t *testing.T) {
41 testhelper.SetupHTTP()
42 defer testhelper.TeardownHTTP()
Ash Wilsona0c4c842014-09-09 11:30:58 -040043 setupVersionHandler()
Ash Wilsonc4360202014-08-29 14:14:24 -040044
jrperritt3d966162016-06-06 14:08:54 -050045 v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "blarg"}
46 v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "hargl"}
Ash Wilsonc4360202014-08-29 14:14:24 -040047
Ash Wilson2491b4c2015-02-12 16:13:39 -050048 c := &gophercloud.ProviderClient{
49 IdentityBase: testhelper.Endpoint(),
50 IdentityEndpoint: "",
51 }
jrperritt3d966162016-06-06 14:08:54 -050052 v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3})
Ash Wilsonc4360202014-08-29 14:14:24 -040053
54 if err != nil {
55 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
56 }
57
58 if v != v3 {
59 t.Errorf("Expected %#v to win, but %#v did instead", v3, v)
60 }
61
Ash Wilsone7da01c2014-09-09 12:31:06 -040062 expected := testhelper.Endpoint() + "v3.0/"
Ash Wilsona0c4c842014-09-09 11:30:58 -040063 if endpoint != expected {
64 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
65 }
66}
67
68func TestChooseVersionOpinionatedLink(t *testing.T) {
69 testhelper.SetupHTTP()
70 defer testhelper.TeardownHTTP()
71 setupVersionHandler()
72
jrperritt3d966162016-06-06 14:08:54 -050073 v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "nope"}
74 v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "northis"}
Ash Wilsona0c4c842014-09-09 11:30:58 -040075
Ash Wilson2491b4c2015-02-12 16:13:39 -050076 c := &gophercloud.ProviderClient{
77 IdentityBase: testhelper.Endpoint(),
78 IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
79 }
jrperritt3d966162016-06-06 14:08:54 -050080 v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3})
Ash Wilson09694b92014-09-09 14:08:27 -040081 if err != nil {
82 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
83 }
84
85 if v != v2 {
86 t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
87 }
88
89 expected := testhelper.Endpoint() + "v2.0/"
90 if endpoint != expected {
91 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
92 }
93}
94
95func TestChooseVersionFromSuffix(t *testing.T) {
96 testhelper.SetupHTTP()
97 defer testhelper.TeardownHTTP()
98
jrperritt3d966162016-06-06 14:08:54 -050099 v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "/v2.0/"}
100 v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "/v3.0/"}
Ash Wilson09694b92014-09-09 14:08:27 -0400101
Ash Wilson2491b4c2015-02-12 16:13:39 -0500102 c := &gophercloud.ProviderClient{
103 IdentityBase: testhelper.Endpoint(),
104 IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
105 }
jrperritt3d966162016-06-06 14:08:54 -0500106 v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3})
Ash Wilsona0c4c842014-09-09 11:30:58 -0400107 if err != nil {
108 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
109 }
110
111 if v != v2 {
112 t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
113 }
114
Ash Wilsone7da01c2014-09-09 12:31:06 -0400115 expected := testhelper.Endpoint() + "v2.0/"
Ash Wilsonc4360202014-08-29 14:14:24 -0400116 if endpoint != expected {
117 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
118 }
119}