Skip to content

Commit

Permalink
test(outputs.bigquery): Reorganise and cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hipska committed Oct 10, 2023
1 parent ca5191b commit 5f5db92
Showing 1 changed file with 95 additions and 74 deletions.
169 changes: 95 additions & 74 deletions plugins/outputs/bigquery/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,106 +26,135 @@ 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)
require.Equal(t, mockMetrics[0].Time(), pt)
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
Expand All @@ -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)
}
Expand Down

0 comments on commit 5f5db92

Please sign in to comment.