github.com/leeclow-ops/gophercloud@v1.2.1/openstack/identity/v3/limits/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/leeclow-ops/gophercloud/openstack/identity/v3/limits" 9 th "github.com/leeclow-ops/gophercloud/testhelper" 10 "github.com/leeclow-ops/gophercloud/testhelper/client" 11 ) 12 13 const GetEnforcementModelOutput = ` 14 { 15 "model": { 16 "description": "Limit enforcement and validation does not take project hierarchy into consideration.", 17 "name": "flat" 18 } 19 } 20 ` 21 22 // ListOutput provides a single page of List results. 23 const ListOutput = ` 24 { 25 "links": { 26 "self": "http://10.3.150.25/identity/v3/limits", 27 "previous": null, 28 "next": null 29 }, 30 "limits": [ 31 { 32 "resource_name": "volume", 33 "region_id": null, 34 "links": { 35 "self": "http://10.3.150.25/identity/v3/limits/25a04c7a065c430590881c646cdcdd58" 36 }, 37 "service_id": "9408080f1970482aa0e38bc2d4ea34b7", 38 "project_id": "3a705b9f56bb439381b43c4fe59dccce", 39 "domain_id": null, 40 "id": "25a04c7a065c430590881c646cdcdd58", 41 "resource_limit": 11, 42 "description": "Number of volumes for project 3a705b9f56bb439381b43c4fe59dccce" 43 }, 44 { 45 "resource_name": "snapshot", 46 "region_id": "RegionOne", 47 "links": { 48 "self": "http://10.3.150.25/identity/v3/limits/3229b3849f584faea483d6851f7aab05" 49 }, 50 "service_id": "9408080f1970482aa0e38bc2d4ea34b7", 51 "project_id": "3a705b9f56bb439381b43c4fe59dccce", 52 "domain_id": null, 53 "id": "3229b3849f584faea483d6851f7aab05", 54 "resource_limit": 5, 55 "description": null 56 } 57 ] 58 } 59 ` 60 61 const CreateRequest = ` 62 { 63 "limits":[ 64 { 65 "service_id": "9408080f1970482aa0e38bc2d4ea34b7", 66 "project_id": "3a705b9f56bb439381b43c4fe59dccce", 67 "region_id": "RegionOne", 68 "resource_name": "snapshot", 69 "resource_limit": 5 70 }, 71 { 72 "service_id": "9408080f1970482aa0e38bc2d4ea34b7", 73 "domain_id": "edbafc92be354ffa977c58aa79c7bdb2", 74 "resource_name": "volume", 75 "resource_limit": 11, 76 "description": "Number of volumes for project 3a705b9f56bb439381b43c4fe59dccce" 77 } 78 ] 79 } 80 ` 81 82 // Model is the enforcement model in the GetEnforcementModel request. 83 var Model = limits.EnforcementModel{ 84 Name: "flat", 85 Description: "Limit enforcement and validation does not take project hierarchy into consideration.", 86 } 87 88 const CreateOutput = ListOutput 89 90 // FirstLimit is the first limit in the List request. 91 var FirstLimit = limits.Limit{ 92 ResourceName: "volume", 93 Links: map[string]interface{}{ 94 "self": "http://10.3.150.25/identity/v3/limits/25a04c7a065c430590881c646cdcdd58", 95 }, 96 ServiceID: "9408080f1970482aa0e38bc2d4ea34b7", 97 ProjectID: "3a705b9f56bb439381b43c4fe59dccce", 98 ID: "25a04c7a065c430590881c646cdcdd58", 99 ResourceLimit: 11, 100 Description: "Number of volumes for project 3a705b9f56bb439381b43c4fe59dccce", 101 } 102 103 // SecondLimit is the second limit in the List request. 104 var SecondLimit = limits.Limit{ 105 ResourceName: "snapshot", 106 RegionID: "RegionOne", 107 Links: map[string]interface{}{ 108 "self": "http://10.3.150.25/identity/v3/limits/3229b3849f584faea483d6851f7aab05", 109 }, 110 ServiceID: "9408080f1970482aa0e38bc2d4ea34b7", 111 ProjectID: "3a705b9f56bb439381b43c4fe59dccce", 112 ID: "3229b3849f584faea483d6851f7aab05", 113 ResourceLimit: 5, 114 } 115 116 // ExpectedLimitsSlice is the slice of limits expected to be returned from ListOutput. 117 var ExpectedLimitsSlice = []limits.Limit{FirstLimit, SecondLimit} 118 119 // HandleGetEnforcementModelSuccessfully creates an HTTP handler at `/limits/model` on the 120 // test handler mux that responds with a enforcement model. 121 func HandleGetEnforcementModelSuccessfully(t *testing.T) { 122 th.Mux.HandleFunc("/limits/model", func(w http.ResponseWriter, r *http.Request) { 123 th.TestMethod(t, r, "GET") 124 th.TestHeader(t, r, "Accept", "application/json") 125 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 126 127 w.Header().Set("Content-Type", "application/json") 128 w.WriteHeader(http.StatusOK) 129 fmt.Fprintf(w, GetEnforcementModelOutput) 130 }) 131 } 132 133 // HandleListLimitsSuccessfully creates an HTTP handler at `/limits` on the 134 // test handler mux that responds with a list of two limits. 135 func HandleListLimitsSuccessfully(t *testing.T) { 136 th.Mux.HandleFunc("/limits", func(w http.ResponseWriter, r *http.Request) { 137 th.TestMethod(t, r, "GET") 138 th.TestHeader(t, r, "Accept", "application/json") 139 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 140 141 w.Header().Set("Content-Type", "application/json") 142 w.WriteHeader(http.StatusOK) 143 fmt.Fprintf(w, ListOutput) 144 }) 145 } 146 147 // HandleCreateLimitSuccessfully creates an HTTP handler at `/limits` on the 148 // test handler mux that tests limit creation. 149 func HandleCreateLimitSuccessfully(t *testing.T) { 150 th.Mux.HandleFunc("/limits", func(w http.ResponseWriter, r *http.Request) { 151 th.TestMethod(t, r, "POST") 152 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 153 th.TestJSONRequest(t, r, CreateRequest) 154 155 w.WriteHeader(http.StatusCreated) 156 fmt.Fprintf(w, CreateOutput) 157 }) 158 }