github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/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  	"runtime"
    27  	"strings"
    28  	"testing"
    29  
    30  	"github.com/ethereum/go-ethereum/metrics"
    31  	"github.com/ethereum/go-ethereum/metrics/internal"
    32  	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
    33  )
    34  
    35  func TestMain(m *testing.M) {
    36  	metrics.Enabled = true
    37  	os.Exit(m.Run())
    38  }
    39  
    40  func TestExampleV1(t *testing.T) {
    41  	if runtime.GOARCH == "arm64" {
    42  		t.Skip("test skipped on ARM64 due to floating point precision differences")
    43  	}
    44  
    45  	r := internal.ExampleMetrics()
    46  	var have, want string
    47  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    48  		haveB, _ := io.ReadAll(r.Body)
    49  		have = string(haveB)
    50  		r.Body.Close()
    51  	}))
    52  	defer ts.Close()
    53  	u, _ := url.Parse(ts.URL)
    54  	rep := &reporter{
    55  		reg:       r,
    56  		url:       *u,
    57  		namespace: "goth.",
    58  	}
    59  	if err := rep.makeClient(); err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if err := rep.send(978307200); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	if wantB, err := os.ReadFile("./testdata/influxdbv1.want"); err != nil {
    66  		t.Fatal(err)
    67  	} else {
    68  		want = string(wantB)
    69  	}
    70  	if have != want {
    71  		t.Errorf("\nhave:\n%v\nwant:\n%v\n", have, want)
    72  		t.Logf("have vs want:\n%v", findFirstDiffPos(have, want))
    73  	}
    74  }
    75  
    76  func TestExampleV2(t *testing.T) {
    77  	if runtime.GOARCH == "arm64" {
    78  		t.Skip("test skipped on ARM64 due to floating point precision differences")
    79  	}
    80  
    81  	r := internal.ExampleMetrics()
    82  	var have, want string
    83  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    84  		haveB, _ := io.ReadAll(r.Body)
    85  		have = string(haveB)
    86  		r.Body.Close()
    87  	}))
    88  	defer ts.Close()
    89  
    90  	rep := &v2Reporter{
    91  		reg:       r,
    92  		endpoint:  ts.URL,
    93  		namespace: "goth.",
    94  	}
    95  	rep.client = influxdb2.NewClient(rep.endpoint, rep.token)
    96  	defer rep.client.Close()
    97  	rep.write = rep.client.WriteAPI(rep.organization, rep.bucket)
    98  
    99  	rep.send(978307200)
   100  
   101  	if wantB, err := os.ReadFile("./testdata/influxdbv2.want"); err != nil {
   102  		t.Fatal(err)
   103  	} else {
   104  		want = string(wantB)
   105  	}
   106  	if have != want {
   107  		t.Errorf("\nhave:\n%v\nwant:\n%v\n", have, want)
   108  		t.Logf("have vs want:\n%v", findFirstDiffPos(have, want))
   109  	}
   110  }
   111  
   112  func findFirstDiffPos(a, b string) string {
   113  	yy := strings.Split(b, "\n")
   114  	for i, x := range strings.Split(a, "\n") {
   115  		if i >= len(yy) {
   116  			return fmt.Sprintf("have:%d: %s\nwant:%d: <EOF>", i, x, i)
   117  		}
   118  		if y := yy[i]; x != y {
   119  			return fmt.Sprintf("have:%d: %s\nwant:%d: %s", i, x, i, y)
   120  		}
   121  	}
   122  	return ""
   123  }