|
| 1 | +// +build fixtures |
| 2 | + |
| 3 | +package meters |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + th "github.com/rackspace/gophercloud/testhelper" |
| 12 | + "github.com/rackspace/gophercloud/testhelper/client" |
| 13 | +) |
| 14 | + |
| 15 | +// MeterListBody contains the canned body of a meters.List response. |
| 16 | +const MeterListBody = ` |
| 17 | +[ |
| 18 | + { |
| 19 | + "meter_id": "YmQ5NDMxYzEtOGQ2OS00YWQzLTgwM2EtOGQ0YTZiODlmZDM2K2luc3RhbmNl", |
| 20 | + "name": "instance", |
| 21 | + "project_id": "35b17138-b364-4e6a-a131-8f3099c5be68", |
| 22 | + "resource_id": "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36", |
| 23 | + "source": "openstack", |
| 24 | + "type": "gauge", |
| 25 | + "unit": "instance", |
| 26 | + "user_id": "efd87807-12d2-4b38-9c70-5f5c2ac427ff" |
| 27 | + }, |
| 28 | + { |
| 29 | + "user_id": "7ya0f7a33717400b951037d55b929c53", |
| 30 | + "name": "cpu_util", |
| 31 | + "resource_id": "5b88239b-8ba1-44ff-a154-978cbda23479", |
| 32 | + "source": "region2", |
| 33 | + "meter_id": "NWI4ODIzOWAtOGJhMS00NGZhLWExNTQtOTc4Y2JkYTIzNDc5K2NwdV91dGls", |
| 34 | + "project_id": "69e6e7c4ed8b434e92feacbf3d4891fd", |
| 35 | + "type": "gauge", |
| 36 | + "unit": "%%" |
| 37 | + } |
| 38 | +] |
| 39 | +` |
| 40 | + |
| 41 | +// MeterShowBody is the canned body of a Get request on an existing meter. |
| 42 | +const MeterShowBody = ` |
| 43 | +[ |
| 44 | + { |
| 45 | + "counter_name": "instance", |
| 46 | + "counter_type": "gauge", |
| 47 | + "counter_unit": "instance", |
| 48 | + "counter_volume": 1.0, |
| 49 | + "message_id": "5460acce-4fd6-480d-ab18-9735ec7b1996", |
| 50 | + "project_id": "35b17138-b364-4e6a-a131-8f3099c5be68", |
| 51 | + "resource_id": "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36", |
| 52 | + "resource_metadata": { |
| 53 | + "name1": "value1", |
| 54 | + "name2": "value2" |
| 55 | + }, |
| 56 | + "source": "openstack", |
| 57 | + "timestamp": "2013-11-21T12:33:08.323533", |
| 58 | + "user_id": "efd87807-12d2-4b38-9c70-5f5c2ac427ff" |
| 59 | + } |
| 60 | +] |
| 61 | +` |
| 62 | + |
| 63 | +// MeterStatisticsBody is the canned body of a Get statistics request on an existing meter. |
| 64 | +const MeterStatisticsBody = ` |
| 65 | +[ |
| 66 | + { |
| 67 | + "avg": 4.5, |
| 68 | + "count": 10, |
| 69 | + "duration": 300.0, |
| 70 | + "duration_end": "2013-01-04T16:47:00", |
| 71 | + "duration_start": "2013-01-04T16:42:00", |
| 72 | + "max": 9.0, |
| 73 | + "min": 1.0, |
| 74 | + "period": 7200, |
| 75 | + "period_end": "2013-01-04T18:00:00", |
| 76 | + "period_start": "2013-01-04T16:00:00", |
| 77 | + "sum": 45.0, |
| 78 | + "unit": "GiB" |
| 79 | + }, |
| 80 | + { |
| 81 | + "count": 28162, |
| 82 | + "duration_start": "2015-06-27T20:52:08", |
| 83 | + "min": 0.06999999999999999, |
| 84 | + "max": 10.06799336650083, |
| 85 | + "duration_end": "2015-07-27T20:47:02", |
| 86 | + "period": 0, |
| 87 | + "sum": 44655.782463977856, |
| 88 | + "period_end": "2015-07-17T16:43:31", |
| 89 | + "duration": 2591694.0, |
| 90 | + "period_start": "2015-07-17T16:43:31", |
| 91 | + "avg": 1.5856751105737468, |
| 92 | + "groupby": null, |
| 93 | + "unit": "%%" |
| 94 | + } |
| 95 | +] |
| 96 | +` |
| 97 | + |
| 98 | +var ( |
| 99 | + // MeterHerp is a Meter struct that should correspond to the first result in *[]Meter. |
| 100 | + MeterHerp = Meter{ |
| 101 | + MeterId: "YmQ5NDMxYzEtOGQ2OS00YWQzLTgwM2EtOGQ0YTZiODlmZDM2K2luc3RhbmNl", |
| 102 | + Name: "instance", |
| 103 | + ProjectId: "35b17138-b364-4e6a-a131-8f3099c5be68", |
| 104 | + ResourceId: "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36", |
| 105 | + Source: "openstack", |
| 106 | + Type: "gauge", |
| 107 | + Unit: "instance", |
| 108 | + UserId: "efd87807-12d2-4b38-9c70-5f5c2ac427ff", |
| 109 | + } |
| 110 | + |
| 111 | + // MeterDerp is a Meter struct that should correspond to the second result in *[]Meter. |
| 112 | + MeterDerp = Meter{ |
| 113 | + MeterId: "NWI4ODIzOWAtOGJhMS00NGZhLWExNTQtOTc4Y2JkYTIzNDc5K2NwdV91dGls", |
| 114 | + Name: "cpu_util", |
| 115 | + ProjectId: "69e6e7c4ed8b434e92feacbf3d4891fd", |
| 116 | + ResourceId: "5b88239b-8ba1-44ff-a154-978cbda23479", |
| 117 | + Source: "region2", |
| 118 | + Type: "gauge", |
| 119 | + Unit: "%", |
| 120 | + UserId: "7ya0f7a33717400b951037d55b929c53", |
| 121 | + } |
| 122 | + |
| 123 | + ShowHerp = OldSample{ |
| 124 | + Name: "instance", |
| 125 | + Type: "gauge", |
| 126 | + Unit: "instance", |
| 127 | + Volume: 1.0, |
| 128 | + MessageId: "5460acce-4fd6-480d-ab18-9735ec7b1996", |
| 129 | + ProjectId: "35b17138-b364-4e6a-a131-8f3099c5be68", |
| 130 | + ResourceId: "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36", |
| 131 | + ResourceMetadata: map[string]string{ |
| 132 | + "name1": "value1", |
| 133 | + "name2": "value2", |
| 134 | + }, |
| 135 | + Source: "openstack", |
| 136 | + Timestamp: time.Date(2013, time.November, 21, 12, 33, 8, 323533000, time.UTC), |
| 137 | + UserId: "efd87807-12d2-4b38-9c70-5f5c2ac427ff", |
| 138 | + } |
| 139 | + |
| 140 | + // StatisticsHerp is a Statistics struct that should correspond to the first result in *[]Statistics. |
| 141 | + StatisticsHerp = Statistics{ |
| 142 | + Avg: 4.5, |
| 143 | + Count: 10, |
| 144 | + Duration: 300.0, |
| 145 | + DurationEnd: "2013-01-04T16:47:00", |
| 146 | + DurationStart: "2013-01-04T16:42:00", |
| 147 | + Max: 9.0, |
| 148 | + Min: 1.0, |
| 149 | + Period: 7200, |
| 150 | + PeriodEnd: "2013-01-04T18:00:00", |
| 151 | + PeriodStart: "2013-01-04T16:00:00", |
| 152 | + Sum: 45.0, |
| 153 | + Unit: "GiB", |
| 154 | + } |
| 155 | + |
| 156 | + // StatisticsDerp is a Statistics struct that should correspond to the second result in *[]Statistics. |
| 157 | + StatisticsDerp = Statistics{ |
| 158 | + Avg: 1.5856751105737468, |
| 159 | + Count: 28162, |
| 160 | + Duration: 2591694.0, |
| 161 | + DurationEnd: "2015-07-27T20:47:02", |
| 162 | + DurationStart: "2015-06-27T20:52:08", |
| 163 | + Max: 10.06799336650083, |
| 164 | + Min: 0.06999999999999999, |
| 165 | + Period: 0, |
| 166 | + PeriodEnd: "2015-07-17T16:43:31", |
| 167 | + PeriodStart: "2015-07-17T16:43:31", |
| 168 | + Sum: 44655.782463977856, |
| 169 | + Unit: "%", |
| 170 | + } |
| 171 | +) |
| 172 | + |
| 173 | +// HandleMeterListSuccessfully sets up the test server to respond to a meters List request. |
| 174 | +func HandleMeterListSuccessfully(t *testing.T) { |
| 175 | + th.Mux.HandleFunc("/v2/meters", func(w http.ResponseWriter, r *http.Request) { |
| 176 | + th.TestMethod(t, r, "GET") |
| 177 | + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 178 | + |
| 179 | + w.Header().Add("Content-Type", "application/json; charset=UTF-8") |
| 180 | + w.WriteHeader(http.StatusOK) |
| 181 | + fmt.Fprintf(w, MeterListBody) |
| 182 | + }) |
| 183 | +} |
| 184 | + |
| 185 | +// HandleMeterShowSuccessfully sets up the test server to respond to a show meter request. |
| 186 | +func HandleMeterShowSuccessfully(t *testing.T) { |
| 187 | + th.Mux.HandleFunc("/v2/meters/instance", func(w http.ResponseWriter, r *http.Request) { |
| 188 | + th.TestMethod(t, r, "GET") |
| 189 | + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 190 | + |
| 191 | + w.Header().Add("Content-Type", "application/json") |
| 192 | + w.WriteHeader(http.StatusOK) |
| 193 | + fmt.Fprintf(w, MeterShowBody) |
| 194 | + }) |
| 195 | +} |
| 196 | + |
| 197 | +// HandleMeterStatisticsSuccessfully sets up the test server to respond to a show meter request. |
| 198 | +func HandleMeterStatisticsSuccessfully(t *testing.T) { |
| 199 | + th.Mux.HandleFunc("/v2/meters/memory/statistics", func(w http.ResponseWriter, r *http.Request) { |
| 200 | + th.TestMethod(t, r, "GET") |
| 201 | + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 202 | + |
| 203 | + w.Header().Add("Content-Type", "application/json") |
| 204 | + w.WriteHeader(http.StatusOK) |
| 205 | + fmt.Fprintf(w, MeterStatisticsBody) |
| 206 | + }) |
| 207 | +} |
0 commit comments