PageRenderTime 155ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/hawkular/hawkular-client-go/metrics/client_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 275 lines | 198 code | 56 blank | 21 comment | 8 complexity | ad8d7887a11077f6c7b2b4936c2817a3 MD5 | raw file
  1. package metrics
  2. import (
  3. "crypto/rand"
  4. "crypto/tls"
  5. "fmt"
  6. assert "github.com/stretchr/testify/require"
  7. "net/http"
  8. "net/http/httptest"
  9. "reflect"
  10. "testing"
  11. "time"
  12. )
  13. func integrationClient() (*Client, error) {
  14. t, err := randomString()
  15. if err != nil {
  16. return nil, err
  17. }
  18. p := Parameters{Tenant: t, Url: "http://localhost:8080"}
  19. // p := Parameters{Tenant: t, Host: "localhost:8180"}
  20. // p := Parameters{Tenant: t, Url: "http://192.168.1.105:8080"}
  21. // p := Parameters{Tenant: t, Host: "209.132.178.218:18080"}
  22. return NewHawkularClient(p)
  23. }
  24. func randomString() (string, error) {
  25. b := make([]byte, 16)
  26. if _, err := rand.Read(b); err != nil {
  27. return "", err
  28. }
  29. return fmt.Sprintf("%X", b[:]), nil
  30. }
  31. func createError(err error) {
  32. }
  33. func TestTenantModifier(t *testing.T) {
  34. c, err := integrationClient()
  35. assert.Nil(t, err)
  36. ot, _ := randomString()
  37. // Create for another tenant
  38. id := "test.metric.create.numeric.tenant.1"
  39. md := MetricDefinition{Id: id, Type: Gauge}
  40. ok, err := c.Create(md, Tenant(ot))
  41. assert.Nil(t, err)
  42. assert.True(t, ok, "MetricDefinition should have been created")
  43. // Try to fetch from default tenant - should fail
  44. mds, err := c.Definitions(Filters(TypeFilter(Gauge)))
  45. assert.Nil(t, err)
  46. assert.Nil(t, mds)
  47. // Try to fetch from the given tenant - should succeed
  48. mds, err = c.Definitions(Filters(TypeFilter(Gauge)), Tenant(ot))
  49. assert.Nil(t, err)
  50. assert.Equal(t, 1, len(mds))
  51. }
  52. func TestCreate(t *testing.T) {
  53. c, err := integrationClient()
  54. assert.Nil(t, err)
  55. id := "test.metric.create.numeric.1"
  56. md := MetricDefinition{Id: id, Type: Gauge}
  57. ok, err := c.Create(md)
  58. assert.Nil(t, err)
  59. assert.True(t, ok, "MetricDefinition should have been created")
  60. // Following would be nice:
  61. // mdd, err := c.Definitions(Filters(Type(Gauge), Id(id)))
  62. // mdd, err := c.Definition(Gauge, id)
  63. // assert.Nil(t, err)
  64. // assert.Equal(t, md.Id, mdd.Id)
  65. // Try to recreate the same..
  66. ok, err = c.Create(md)
  67. assert.False(t, ok, "Should have received false when recreating them same metric")
  68. assert.Nil(t, err)
  69. // Use tags and dataRetention
  70. tags := make(map[string]string)
  71. tags["units"] = "bytes"
  72. tags["env"] = "unittest"
  73. md_tags := MetricDefinition{Id: "test.metric.create.numeric.2", Tags: tags, Type: Gauge}
  74. ok, err = c.Create(md_tags)
  75. assert.True(t, ok, "MetricDefinition should have been created")
  76. assert.Nil(t, err)
  77. md_reten := MetricDefinition{Id: "test/metric/create/availability/1", RetentionTime: 12, Type: Availability}
  78. ok, err = c.Create(md_reten)
  79. assert.Nil(t, err)
  80. assert.True(t, ok, "MetricDefinition should have been created")
  81. // Fetch all the previously created metrics and test equalities..
  82. mdq, err := c.Definitions(Filters(TypeFilter(Gauge)))
  83. assert.Nil(t, err)
  84. assert.Equal(t, 2, len(mdq), "Size of the returned gauge metrics does not match 2")
  85. mdm := make(map[string]MetricDefinition)
  86. for _, v := range mdq {
  87. mdm[v.Id] = *v
  88. }
  89. assert.Equal(t, md.Id, mdm[id].Id)
  90. assert.True(t, reflect.DeepEqual(tags, mdm["test.metric.create.numeric.2"].Tags))
  91. mda, err := c.Definitions(Filters(TypeFilter(Availability)))
  92. assert.Nil(t, err)
  93. assert.Equal(t, 1, len(mda))
  94. assert.Equal(t, "test/metric/create/availability/1", mda[0].Id)
  95. assert.Equal(t, 12, mda[0].RetentionTime)
  96. if mda[0].Type != Availability {
  97. assert.FailNow(t, "Type did not match Availability", int(mda[0].Type))
  98. }
  99. }
  100. func TestTagsModification(t *testing.T) {
  101. c, err := integrationClient()
  102. assert.Nil(t, err)
  103. id := "test/tags/modify/1"
  104. // Create metric without tags
  105. md := MetricDefinition{Id: id, Type: Gauge}
  106. ok, err := c.Create(md)
  107. assert.Nil(t, err)
  108. assert.True(t, ok, "MetricDefinition should have been created")
  109. // Add tags
  110. tags := make(map[string]string)
  111. tags["ab"] = "ac"
  112. tags["host"] = "test"
  113. err = c.UpdateTags(Gauge, id, tags)
  114. assert.Nil(t, err)
  115. // Fetch metric tags - check for equality
  116. md_tags, err := c.Tags(Gauge, id)
  117. assert.Nil(t, err)
  118. assert.True(t, reflect.DeepEqual(tags, md_tags), "Tags did not match the updated ones")
  119. // Delete some metric tags
  120. err = c.DeleteTags(Gauge, id, tags)
  121. assert.Nil(t, err)
  122. // Fetch metric - check that tags were deleted
  123. md_tags, err = c.Tags(Gauge, id)
  124. assert.Nil(t, err)
  125. assert.False(t, len(md_tags) > 0, "Received deleted tags")
  126. }
  127. func TestAddMixedMulti(t *testing.T) {
  128. // Modify to send both Availability as well as Gauge metrics at the same time
  129. c, err := integrationClient()
  130. assert.NoError(t, err)
  131. mone := Datapoint{Value: 1.45, Timestamp: UnixMilli(time.Now())}
  132. hone := MetricHeader{
  133. Id: "test.multi.numeric.1",
  134. Data: []Datapoint{mone},
  135. Type: Gauge,
  136. }
  137. mtwo_1 := Datapoint{Value: 2, Timestamp: UnixMilli(time.Now())}
  138. mtwo_2_t := UnixMilli(time.Now()) - 1e3
  139. mtwo_2 := Datapoint{Value: float64(4.56), Timestamp: mtwo_2_t}
  140. htwo := MetricHeader{
  141. Id: "test.multi.numeric.2",
  142. Data: []Datapoint{mtwo_1, mtwo_2},
  143. Type: Counter,
  144. }
  145. h := []MetricHeader{hone, htwo}
  146. err = c.Write(h)
  147. assert.NoError(t, err)
  148. time.Sleep(1000 * time.Millisecond)
  149. var checkDatapoints = func(id string, typ MetricType, expected int) []*Datapoint {
  150. metric, err := c.ReadMetric(typ, id)
  151. assert.NoError(t, err)
  152. assert.Equal(t, expected, len(metric), "Amount of datapoints does not match expected value")
  153. return metric
  154. }
  155. checkDatapoints(hone.Id, hone.Type, 1)
  156. checkDatapoints(htwo.Id, htwo.Type, 2)
  157. }
  158. func TestCheckErrors(t *testing.T) {
  159. c, err := integrationClient()
  160. assert.Nil(t, err)
  161. mH := MetricHeader{
  162. Id: "test.number.as.string",
  163. Data: []Datapoint{Datapoint{Value: "notFloat"}},
  164. Type: Gauge,
  165. }
  166. err = c.Write([]MetricHeader{mH})
  167. assert.NotNil(t, err, "Invalid non-float value should not be accepted")
  168. _, err = c.ReadMetric(mH.Type, mH.Id)
  169. assert.Nil(t, err, "Querying empty metric should not generate an error")
  170. }
  171. func TestTokenAuthenticationWithSSL(t *testing.T) {
  172. s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  173. w.Header().Set("X-Authorization", r.Header.Get("Authorization"))
  174. }))
  175. defer s.Close()
  176. tenant, err := randomString()
  177. assert.NoError(t, err)
  178. tC := &tls.Config{InsecureSkipVerify: true}
  179. p := Parameters{
  180. Tenant: tenant,
  181. Url: s.URL,
  182. Token: "62590bf9827213afadea8b5077a5bdc0",
  183. TLSConfig: tC,
  184. }
  185. c, err := NewHawkularClient(p)
  186. assert.NoError(t, err)
  187. r, err := c.Send(c.Url("GET"))
  188. assert.NoError(t, err)
  189. assert.Equal(t, fmt.Sprintf("Bearer %s", p.Token), r.Header.Get("X-Authorization"))
  190. }
  191. func TestBuckets(t *testing.T) {
  192. c, err := integrationClient()
  193. assert.NoError(t, err)
  194. tags := make(map[string]string)
  195. tags["units"] = "bytes"
  196. tags["env"] = "unittest"
  197. md_tags := MetricDefinition{Id: "test.buckets.1", Tags: tags, Type: Gauge}
  198. ok, err := c.Create(md_tags)
  199. assert.NoError(t, err)
  200. assert.True(t, ok)
  201. mone := Datapoint{Value: 1.45, Timestamp: UnixMilli(time.Now())}
  202. hone := MetricHeader{
  203. Id: "test.buckets.1",
  204. Data: []Datapoint{mone},
  205. Type: Gauge,
  206. }
  207. err = c.Write([]MetricHeader{hone})
  208. assert.NoError(t, err)
  209. // TODO Muuta PercentilesFilter -> Percentiles (modifier)
  210. bp, err := c.ReadBuckets(Gauge, Filters(TagsFilter(tags), BucketsFilter(1), PercentilesFilter([]float64{90.0, 99.0})))
  211. assert.NoError(t, err)
  212. assert.NotNil(t, bp)
  213. assert.Equal(t, 1, len(bp))
  214. assert.Equal(t, int64(1), bp[0].Samples)
  215. assert.Equal(t, 2, len(bp[0].Percentiles))
  216. assert.Equal(t, 1.45, bp[0].Percentiles[0].Value)
  217. assert.Equal(t, 0.9, bp[0].Percentiles[0].Quantile)
  218. assert.True(t, bp[0].Percentiles[1].Quantile >= 0.99) // Double arithmetic could cause this to be 0.9900000001 etc
  219. }