From 5f5db92faed37b2fbb14103b121b2f21156b804b Mon Sep 17 00:00:00 2001 From: Thomas Casteleyn Date: Tue, 10 Oct 2023 16:51:12 +0200 Subject: [PATCH] test(outputs.bigquery): Reorganise and cleanup tests --- plugins/outputs/bigquery/bigquery_test.go | 169 ++++++++++++---------- 1 file changed, 95 insertions(+), 74 deletions(-) diff --git a/plugins/outputs/bigquery/bigquery_test.go b/plugins/outputs/bigquery/bigquery_test.go index dd029f23f7ff6..57f668c0eaf59 100644 --- a/plugins/outputs/bigquery/bigquery_test.go +++ b/plugins/outputs/bigquery/bigquery_test.go @@ -5,23 +5,19 @@ import ( "encoding/json" "net/http" "net/http/httptest" - "strings" "testing" "time" "cloud.google.com/go/bigquery" - "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" "google.golang.org/api/option" ) const ( - successfulResponse = "{\"kind\": \"bigquery#tableDataInsertAllResponse\"}" + successfulResponse = `{"kind": "bigquery#tableDataInsertAllResponse"}` ) -var testingHost string -var testDuration = config.Duration(5 * time.Second) var receivedBody map[string]json.RawMessage type Row struct { @@ -30,56 +26,121 @@ type Row struct { Value float64 `json:"value"` } +func TestInit(t *testing.T) { + tests := []struct { + name string + errorString string + plugin *BigQuery + }{ + { + name: "dataset is not set", + errorString: `"dataset" is required`, + plugin: &BigQuery{}, + }, + { + name: "valid config", + plugin: &BigQuery{ + Dataset: "test-dataset", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.errorString != "" { + require.EqualError(t, tt.plugin.Init(), tt.errorString) + } else { + require.NoError(t, tt.plugin.Init()) + } + }) + } +} + +func TestMetricToTable(t *testing.T) { + tests := []struct { + name string + replaceHyphenTo string + metricName string + expectedTable string + }{ + { + name: "no rename", + replaceHyphenTo: "_", + metricName: "test", + expectedTable: "test", + }, + { + name: "default config", + replaceHyphenTo: "_", + metricName: "table-with-hyphens", + expectedTable: "table_with_hyphens", + }, + { + name: "custom hypens", + replaceHyphenTo: "*", + metricName: "table-with-hyphens", + expectedTable: "table*with*hyphens", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &BigQuery{ + Dataset: "test-dataset", + ReplaceHyphenTo: tt.replaceHyphenTo, + Log: testutil.Logger{}, + } + require.NoError(t, b.Init()) + + require.Equal(t, tt.expectedTable, b.metricToTable(tt.metricName)) + if tt.metricName != tt.expectedTable { + require.Contains(t, b.warnedOnHyphens, tt.metricName) + require.True(t, b.warnedOnHyphens[tt.metricName]) + } else { + require.NotContains(t, b.warnedOnHyphens, tt.metricName) + } + }) + } +} + func TestConnect(t *testing.T) { srv := localBigQueryServer(t) - testingHost = strings.ReplaceAll(srv.URL, "http://", "") defer srv.Close() b := &BigQuery{ Project: "test-project", Dataset: "test-dataset", - Timeout: testDuration, + Timeout: defaultTimeout, } - cerr := b.setUpTestClient() - require.NoError(t, cerr) - berr := b.Connect() - require.NoError(t, berr) + require.NoError(t, b.Init()) + require.NoError(t, b.setUpTestClient(srv.URL)) + require.NoError(t, b.Connect()) } func TestWrite(t *testing.T) { srv := localBigQueryServer(t) - testingHost = strings.ReplaceAll(srv.URL, "http://", "") defer srv.Close() b := &BigQuery{ Project: "test-project", Dataset: "test-dataset", - Timeout: testDuration, + Timeout: defaultTimeout, } mockMetrics := testutil.MockMetrics() - if err := b.setUpTestClient(); err != nil { - require.NoError(t, err) - } - if err := b.Connect(); err != nil { - require.NoError(t, err) - } + require.NoError(t, b.Init()) + require.NoError(t, b.setUpTestClient(srv.URL)) + require.NoError(t, b.Connect()) - if err := b.Write(mockMetrics); err != nil { - require.NoError(t, err) - } + require.NoError(t, b.Write(mockMetrics)) var rows []map[string]json.RawMessage - if err := json.Unmarshal(receivedBody["rows"], &rows); err != nil { - require.NoError(t, err) - } + require.NoError(t, json.Unmarshal(receivedBody["rows"], &rows)) var row Row - if err := json.Unmarshal(rows[0]["json"], &row); err != nil { - require.NoError(t, err) - } + require.NoError(t, json.Unmarshal(rows[0]["json"], &row)) pt, _ := time.Parse(time.RFC3339, row.Timestamp) require.Equal(t, mockMetrics[0].Tags()["tag1"], row.Tag1) @@ -87,49 +148,13 @@ func TestWrite(t *testing.T) { require.Equal(t, mockMetrics[0].Fields()["value"], row.Value) } -func TestMetricToTableDefault(t *testing.T) { - b := &BigQuery{ - Project: "test-project", - Dataset: "test-dataset", - Timeout: testDuration, - warnedOnHyphens: make(map[string]bool), - ReplaceHyphenTo: "_", - Log: testutil.Logger{}, - } - - otn := "table-with-hyphens" - ntn := b.metricToTable(otn) - - require.Equal(t, "table_with_hyphens", ntn) - require.True(t, b.warnedOnHyphens[otn]) -} - -func TestMetricToTableCustom(t *testing.T) { - log := testutil.Logger{} - - b := &BigQuery{ - Project: "test-project", - Dataset: "test-dataset", - Timeout: testDuration, - warnedOnHyphens: make(map[string]bool), - ReplaceHyphenTo: "*", - Log: log, - } - - otn := "table-with-hyphens" - ntn := b.metricToTable(otn) - - require.Equal(t, "table*with*hyphens", ntn) - require.True(t, b.warnedOnHyphens[otn]) -} - -func (b *BigQuery) setUpTestClient() error { +func (b *BigQuery) setUpTestClient(endpointURL string) error { noAuth := option.WithoutAuthentication() - endpoints := option.WithEndpoint("http://" + testingHost) + endpoint := option.WithEndpoint(endpointURL) ctx := context.Background() - c, err := bigquery.NewClient(ctx, b.Project, noAuth, endpoints) + c, err := bigquery.NewClient(ctx, b.Project, noAuth, endpoint) if err != nil { return err @@ -147,15 +172,11 @@ func localBigQueryServer(t *testing.T) *httptest.Server { switch r.URL.Path { case "/projects/test-project/datasets/test-dataset/tables/test1/insertAll": decoder := json.NewDecoder(r.Body) - - if err := decoder.Decode(&receivedBody); err != nil { - require.NoError(t, err) - } + require.NoError(t, decoder.Decode(&receivedBody)) w.WriteHeader(http.StatusOK) - if _, err := w.Write([]byte(successfulResponse)); err != nil { - require.NoError(t, err) - } + _, err := w.Write([]byte(successfulResponse)) + require.NoError(t, err) default: w.WriteHeader(http.StatusNotFound) }