main_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2018 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package main
  5. import (
  6. "os"
  7. "testing"
  8. "time"
  9. "notabug.org/themusicgod1/goarista/test"
  10. "github.com/influxdata/influxdb/client/v2"
  11. )
  12. func newPoint(t *testing.T, measurement string, tags map[string]string,
  13. fields map[string]interface{}, timeString string) *client.Point {
  14. t.Helper()
  15. timestamp, err := time.Parse(time.RFC3339Nano, timeString)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. p, err := client.NewPoint(measurement, tags, fields, timestamp)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. return p
  24. }
  25. func TestParseTestOutput(t *testing.T) {
  26. // Verify tags and fields set by flags are set in records
  27. flagTags.Set("tag=foo")
  28. flagFields.Set("field=true")
  29. defer func() {
  30. flagTags = nil
  31. flagFields = nil
  32. }()
  33. f, err := os.Open("testdata/output.txt")
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. makeTags := func(pkg, resultType string) map[string]string {
  38. return map[string]string{"package": pkg, "type": resultType, "tag": "foo"}
  39. }
  40. makeFields := func(pass, elapsed float64, test string) map[string]interface{} {
  41. m := map[string]interface{}{"pass": pass, "elapsed": elapsed, "field": true}
  42. if test != "" {
  43. m["test"] = test
  44. }
  45. return m
  46. }
  47. expected := []*client.Point{
  48. newPoint(t,
  49. "result",
  50. makeTags("pkg/passed", "test"),
  51. makeFields(1, 0, "TestPass"),
  52. "2018-03-08T10:33:12.344165231-08:00",
  53. ),
  54. newPoint(t,
  55. "result",
  56. makeTags("pkg/passed", "package"),
  57. makeFields(1, 0.013, ""),
  58. "2018-03-08T10:33:12.34533033-08:00",
  59. ),
  60. newPoint(t,
  61. "result",
  62. makeTags("pkg/panic", "test"),
  63. makeFields(0, 600.029, "TestPanic"),
  64. "2018-03-08T10:33:20.272440286-08:00",
  65. ),
  66. newPoint(t,
  67. "result",
  68. makeTags("pkg/failed", "test"),
  69. makeFields(0, 0.18, "TestFail"),
  70. "2018-03-08T10:33:27.158860934-08:00",
  71. ),
  72. newPoint(t,
  73. "result",
  74. makeTags("pkg/failed", "package"),
  75. makeFields(0, 0.204, ""),
  76. "2018-03-08T10:33:27.161302093-08:00",
  77. ),
  78. newPoint(t,
  79. "result",
  80. makeTags("pkg/panic", "package"),
  81. makeFields(0, 0, ""),
  82. "2018-03-08T10:33:20.273440286-08:00",
  83. ),
  84. }
  85. batch, err := client.NewBatchPoints(client.BatchPointsConfig{})
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if err := parseTestOutput(f, batch); err != nil {
  90. t.Fatal(err)
  91. }
  92. if diff := test.Diff(expected, batch.Points()); diff != "" {
  93. t.Errorf("unexpected diff: %s", diff)
  94. }
  95. }
  96. func TestTagsFlag(t *testing.T) {
  97. for tc, expected := range map[string]tags{
  98. "abc=def": tags{tag{key: "abc", value: "def"}},
  99. "abc=def,ghi=klm": tags{tag{key: "abc", value: "def"}, tag{key: "ghi", value: "klm"}},
  100. } {
  101. t.Run(tc, func(t *testing.T) {
  102. var ts tags
  103. ts.Set(tc)
  104. if diff := test.Diff(expected, ts); diff != "" {
  105. t.Errorf("unexpected diff from Set: %s", diff)
  106. }
  107. if s := ts.String(); s != tc {
  108. t.Errorf("unexpected diff from String: %q vs. %q", tc, s)
  109. }
  110. })
  111. }
  112. }
  113. func TestFieldsFlag(t *testing.T) {
  114. for tc, expected := range map[string]fields{
  115. "str=abc": fields{field{key: "str", value: "abc"}},
  116. "bool=true": fields{field{key: "bool", value: true}},
  117. "bool=false": fields{field{key: "bool", value: false}},
  118. "float64=42": fields{field{key: "float64", value: float64(42)}},
  119. "float64=42.123": fields{field{key: "float64", value: float64(42.123)}},
  120. "int64=42i": fields{field{key: "int64", value: int64(42)}},
  121. "str=abc,bool=true,float64=42,int64=42i": fields{field{key: "str", value: "abc"},
  122. field{key: "bool", value: true},
  123. field{key: "float64", value: float64(42)},
  124. field{key: "int64", value: int64(42)}},
  125. } {
  126. t.Run(tc, func(t *testing.T) {
  127. var fs fields
  128. fs.Set(tc)
  129. if diff := test.Diff(expected, fs); diff != "" {
  130. t.Errorf("unexpected diff from Set: %s", diff)
  131. }
  132. if s := fs.String(); s != tc {
  133. t.Errorf("unexpected diff from String: %q vs. %q", tc, s)
  134. }
  135. })
  136. }
  137. }