github.com/theQRL/go-zond@v0.1.1/metrics/influxdb/influxdb_test.go (about) 1 // Copyright 2023 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package influxdb 18 19 import ( 20 "fmt" 21 "io" 22 "net/http" 23 "net/http/httptest" 24 "net/url" 25 "os" 26 "strings" 27 "testing" 28 29 "github.com/theQRL/go-zond/metrics" 30 "github.com/theQRL/go-zond/metrics/internal" 31 influxdb2 "github.com/influxdata/influxdb-client-go/v2" 32 ) 33 34 func TestMain(m *testing.M) { 35 metrics.Enabled = true 36 os.Exit(m.Run()) 37 } 38 39 func TestExampleV1(t *testing.T) { 40 r := internal.ExampleMetrics() 41 var have, want string 42 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 43 haveB, _ := io.ReadAll(r.Body) 44 have = string(haveB) 45 r.Body.Close() 46 })) 47 defer ts.Close() 48 u, _ := url.Parse(ts.URL) 49 rep := &reporter{ 50 reg: r, 51 url: *u, 52 namespace: "goth.", 53 } 54 if err := rep.makeClient(); err != nil { 55 t.Fatal(err) 56 } 57 if err := rep.send(978307200); err != nil { 58 t.Fatal(err) 59 } 60 if wantB, err := os.ReadFile("./testdata/influxdbv1.want"); err != nil { 61 t.Fatal(err) 62 } else { 63 want = string(wantB) 64 } 65 if have != want { 66 t.Errorf("\nhave:\n%v\nwant:\n%v\n", have, want) 67 t.Logf("have vs want:\n%v", findFirstDiffPos(have, want)) 68 } 69 } 70 71 func TestExampleV2(t *testing.T) { 72 r := internal.ExampleMetrics() 73 var have, want string 74 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 75 haveB, _ := io.ReadAll(r.Body) 76 have = string(haveB) 77 r.Body.Close() 78 })) 79 defer ts.Close() 80 81 rep := &v2Reporter{ 82 reg: r, 83 endpoint: ts.URL, 84 namespace: "goth.", 85 } 86 rep.client = influxdb2.NewClient(rep.endpoint, rep.token) 87 defer rep.client.Close() 88 rep.write = rep.client.WriteAPI(rep.organization, rep.bucket) 89 90 rep.send(978307200) 91 92 if wantB, err := os.ReadFile("./testdata/influxdbv2.want"); err != nil { 93 t.Fatal(err) 94 } else { 95 want = string(wantB) 96 } 97 if have != want { 98 t.Errorf("\nhave:\n%v\nwant:\n%v\n", have, want) 99 t.Logf("have vs want:\n%v", findFirstDiffPos(have, want)) 100 } 101 } 102 103 func findFirstDiffPos(a, b string) string { 104 yy := strings.Split(b, "\n") 105 for i, x := range strings.Split(a, "\n") { 106 if i >= len(yy) { 107 return fmt.Sprintf("have:%d: %s\nwant:%d: <EOF>", i, x, i) 108 } 109 if y := yy[i]; x != y { 110 return fmt.Sprintf("have:%d: %s\nwant:%d: %s", i, x, i, y) 111 } 112 } 113 return "" 114 }