github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/sinks/monasca/data_test.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package monasca 16 17 import ( 18 "time" 19 20 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" 21 "k8s.io/heapster/metrics/core" 22 ) 23 24 var measureTime = time.Now() 25 26 // common labels: 27 var testInput = &core.DataBatch{ 28 Timestamp: measureTime, 29 MetricSets: map[string]*core.MetricSet{ 30 "set1": { 31 MetricValues: map[string]core.MetricValue{ 32 "m2": {ValueType: core.ValueInt64, IntValue: 2 ^ 63}, 33 "m3": {ValueType: core.ValueFloat, FloatValue: -1023.0233}, 34 }, 35 Labels: map[string]string{ 36 core.LabelHostname.Key: "h1", 37 }, 38 LabeledMetrics: []core.LabeledMetric{}, 39 }, 40 "set2": { 41 MetricValues: map[string]core.MetricValue{}, 42 Labels: map[string]string{ 43 core.LabelHostname.Key: "10.140.32.11", 44 }, 45 LabeledMetrics: []core.LabeledMetric{ 46 { 47 Name: "cpu/usage", 48 Labels: map[string]string{ 49 core.LabelContainerName.Key: "POD", 50 core.LabelPodName.Key: "mypod-hc3s", 51 core.LabelLabels.Key: "run:test,pod.name:default/test-u2dc", 52 core.LabelHostID.Key: "", 53 }, 54 MetricValue: core.MetricValue{ 55 ValueType: core.ValueInt64, 56 IntValue: 1, 57 }, 58 }, 59 { 60 Name: "memory/usage", 61 Labels: map[string]string{ 62 core.LabelContainerName.Key: "machine", 63 core.LabelLabels.Key: "pod.name:default/test-u2dc,run:test2,foo:bar", 64 core.LabelHostID.Key: "myhost", 65 }, 66 MetricValue: core.MetricValue{ 67 ValueType: core.ValueFloat, 68 FloatValue: 64.0, 69 }, 70 }, 71 }, 72 }, 73 }, 74 } 75 76 var expectedTransformed = []metric{ 77 { 78 Name: "m2", 79 Dimensions: map[string]string{ 80 "component": emptyValue, 81 "hostname": "h1", 82 "service": "kubernetes", 83 core.LabelContainerName.Key: emptyValue, 84 }, 85 Value: 2 ^ 63, 86 Timestamp: measureTime.UnixNano() / 1000000, 87 ValueMeta: map[string]string{}, 88 }, 89 { 90 Name: "m3", 91 Dimensions: map[string]string{ 92 "component": emptyValue, 93 "hostname": "h1", 94 "service": "kubernetes", 95 core.LabelContainerName.Key: emptyValue, 96 }, 97 Value: float64(float32(-1023.0233)), 98 Timestamp: measureTime.UnixNano() / 1000000, 99 ValueMeta: map[string]string{}, 100 }, 101 { 102 Name: "cpu.usage", 103 Dimensions: map[string]string{ 104 "component": "mypod-hc3s", 105 "hostname": "10.140.32.11", 106 "service": "kubernetes", 107 core.LabelContainerName.Key: "POD", 108 }, 109 Value: 1.0, 110 Timestamp: measureTime.UnixNano() / 1000000, 111 ValueMeta: map[string]string{ 112 core.LabelLabels.Key: "run:test pod.name:default/test-u2dc", 113 }, 114 }, 115 { 116 Name: "memory.usage", 117 Dimensions: map[string]string{ 118 "component": emptyValue, 119 "hostname": "10.140.32.11", 120 "service": "kubernetes", 121 core.LabelContainerName.Key: "machine", 122 }, 123 Value: float64(float32(64.0)), 124 Timestamp: measureTime.UnixNano() / 1000000, 125 ValueMeta: map[string]string{ 126 core.LabelLabels.Key: "pod.name:default/test-u2dc run:test2 foo:bar", 127 core.LabelHostID.Key: "myhost", 128 }, 129 }, 130 } 131 132 const ( 133 testToken = "e80b74" 134 testScopedToken = "ac54e1" 135 ) 136 137 var invalidToken = &tokens.Token{ID: "invalidToken", ExpiresAt: time.Unix(time.Now().Unix()-5000, 0)} 138 var validToken = &tokens.Token{ID: testToken, ExpiresAt: time.Unix(time.Now().Unix()+50000, 0)} 139 140 var testConfig = Config{} 141 142 const ( 143 testUsername = "Joe" 144 testPassword = "bar" 145 testUserID = "0ca8f6" 146 testDomainID = "1789d1" 147 testDomainName = "example.com" 148 testTenantID = "8ca4e3" 149 ) 150 151 var ( 152 ksVersionResp string 153 ksUnscopedAuthResp string 154 ksScopedAuthResp string 155 ksServicesResp string 156 ksEndpointsResp string 157 monUnauthorizedResp string 158 monEmptyDimResp string 159 ) 160 161 func initKeystoneRespStubs() { 162 ksVersionResp = `{ 163 "versions": { 164 "values": [{ 165 "status": "stable", 166 "updated": "2015-03-30T00:00:00Z", 167 "id": "v3.4", 168 "links": [{ 169 "href": "` + keystoneAPIStub.URL + `", 170 "rel": "self" 171 }] 172 }] 173 } 174 }` 175 ksUnscopedAuthResp = `{ 176 "token": { 177 "audit_ids": ["VcxU2JYqT8OzfUVvrjEITQ", "qNUTIJntTzO1-XUk5STybw"], 178 "expires_at": "2013-02-27T18:30:59.999999Z", 179 "issued_at": "2013-02-27T16:30:59.999999Z", 180 "methods": [ 181 "password" 182 ], 183 "user": { 184 "domain": { 185 "id": "1789d1", 186 "name": "example.com" 187 }, 188 "id": "0ca8f6", 189 "name": "Joe" 190 } 191 } 192 }` 193 ksScopedAuthResp = `{ 194 "token":{ 195 "audit_ids":[ 196 "wQ19eUlHQcGi_MZka-CFPA" 197 ], 198 "issued_at":"2013-02-27T16:30:59.999999Z", 199 "expires_at":"2013-02-27T18:30:59.999999Z", 200 "is_domain":false, 201 "methods":[ 202 "password" 203 ], 204 "roles":[ 205 { 206 "id":"241497", 207 "name":"monasca-agent" 208 } 209 ], 210 "is_admin_project":false, 211 "project":{ 212 "domain":{ 213 "id":"` + testDomainID + `", 214 "name":"` + testDomainName + `" 215 }, 216 "id":"` + testTenantID + `", 217 "name":"monasca-project" 218 }, 219 "catalog":[ 220 { 221 "endpoints":[ 222 { 223 "region_id":"RegionOne", 224 "url":"` + monascaAPIStub.URL + `", 225 "region":"RegionOne", 226 "interface":"public", 227 "id":"3afbce" 228 } 229 ], 230 "type":"monitoring", 231 "id":"6d0a8d", 232 "name":"monasca" 233 }, 234 { 235 "endpoints":[ 236 { 237 "region_id":"RegionOne", 238 "url":"` + keystoneAPIStub.URL + `", 239 "region":"RegionOne", 240 "interface":"public", 241 "id":"94927e" 242 } 243 ], 244 "type":"identity", 245 "id":"73c7a2", 246 "name":"keystone" 247 } 248 ], 249 "user":{ 250 "domain":{ 251 "id":"` + testDomainID + `", 252 "name":"` + testDomainName + `" 253 }, 254 "id":"` + testUserID + `", 255 "name":"` + testUsername + `" 256 } 257 } 258 }` 259 ksServicesResp = `{ 260 "services": [{ 261 "description": "Monasca Service", 262 "id": "ee057c", 263 "links": { 264 "self": "` + keystoneAPIStub.URL + `/v3/services/ee057c" 265 }, 266 "name": "Monasca", 267 "type": "monitoring" 268 }], 269 "links": { 270 "self": "` + keystoneAPIStub.URL + `/v3/services", 271 "previous": null, 272 "next": null 273 } 274 }` 275 ksEndpointsResp = `{ 276 "endpoints": [ 277 { 278 "enabled": true, 279 "id": "6fedc0", 280 "interface": "public", 281 "links": { 282 "self": "` + keystoneAPIStub.URL + `/v3/endpoints/6fedc0" 283 }, 284 "region_id": "us-east-1", 285 "service_id": "ee057c", 286 "url": "` + monascaAPIStub.URL + `" 287 } 288 ], 289 "links": { 290 "self": "` + keystoneAPIStub.URL + `/v3/endpoints", 291 "previous": null, 292 "next": null 293 } 294 }` 295 monUnauthorizedResp = "Invaild token provided" 296 monEmptyDimResp = "Empty dimension detected" 297 }