github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/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": &core.MetricSet{
    31  			MetricValues: map[string]core.MetricValue{
    32  				"m2": core.MetricValue{ValueType: core.ValueInt64, IntValue: 2 ^ 63},
    33  				"m3": core.MetricValue{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": &core.MetricSet{
    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  				core.LabeledMetric{
    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  				core.LabeledMetric{
    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  	metric{
    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  	metric{
    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  	metric{
   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  	metric{
   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 testToken = "e80b74"
   133  
   134  var invalidToken = &tokens.Token{ID: "invalidToken", ExpiresAt: time.Unix(time.Now().Unix()-5000, 0)}
   135  var validToken = &tokens.Token{ID: testToken, ExpiresAt: time.Unix(time.Now().Unix()+50000, 0)}
   136  
   137  var testConfig = Config{}
   138  
   139  const (
   140  	testUsername   = "Joe"
   141  	testPassword   = "bar"
   142  	testUserID     = "0ca8f6"
   143  	testDomainID   = "1789d1"
   144  	testDomainName = "example.com"
   145  )
   146  
   147  var (
   148  	ksVersionResp       string
   149  	ksAuthResp          string
   150  	ksServicesResp      string
   151  	ksEndpointsResp     string
   152  	monUnauthorizedResp string
   153  	monEmptyDimResp     string
   154  )
   155  
   156  func initKeystoneRespStubs() {
   157  	ksVersionResp = `{
   158                        "versions": {
   159                          "values": [{
   160                            "status": "stable",
   161                            "updated": "2015-03-30T00:00:00Z",
   162                            "id": "v3.4",
   163                            "links": [{
   164                              "href": "` + keystoneAPIStub.URL + `",
   165                              "rel": "self"
   166                            }]
   167                          }]
   168                        }
   169                      }`
   170  	ksAuthResp = `{
   171                      "token": {
   172                          "audit_ids": ["VcxU2JYqT8OzfUVvrjEITQ", "qNUTIJntTzO1-XUk5STybw"],
   173                          "expires_at": "2013-02-27T18:30:59.999999Z",
   174                          "issued_at": "2013-02-27T16:30:59.999999Z",
   175                          "methods": [
   176                              "password"
   177                          ],
   178                          "user": {
   179                              "domain": {
   180                                  "id": "1789d1",
   181                                  "name": "example.com"
   182                              },
   183                              "id": "0ca8f6",
   184                              "name": "Joe"
   185                          }
   186                      }
   187                  }`
   188  	ksServicesResp = `{
   189                          "services": [{
   190                            "description": "Monasca Service",
   191                            "id": "ee057c",
   192                            "links": {
   193                              "self": "` + keystoneAPIStub.URL + `/v3/services/ee057c"
   194                            },
   195                            "name": "Monasca",
   196                            "type": "monitoring"
   197                          }],
   198                          "links": {
   199                            "self": "` + keystoneAPIStub.URL + `/v3/services",
   200                            "previous": null,
   201                            "next": null
   202                          }
   203                      }`
   204  	ksEndpointsResp = `{
   205                          "endpoints": [
   206                              {
   207                                  "enabled": true,
   208                                  "id": "6fedc0",
   209                                  "interface": "public",
   210                                  "links": {
   211                                      "self": "` + keystoneAPIStub.URL + `/v3/endpoints/6fedc0"
   212                                  },
   213                                  "region_id": "us-east-1",
   214                                  "service_id": "ee057c",
   215                                  "url": "` + monascaAPIStub.URL + `"
   216                              }
   217                          ],
   218                          "links": {
   219                              "self": "` + keystoneAPIStub.URL + `/v3/endpoints",
   220                              "previous": null,
   221                              "next": null
   222                          }
   223                      }`
   224  	monUnauthorizedResp = "Invaild token provided"
   225  	monEmptyDimResp = "Empty dimension detected"
   226  }