Skip to content

Commit

Permalink
Add required tests for go/stats/opentsdb (#15394)
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 authored Mar 6, 2024
1 parent 35ac6da commit 9d895fb
Show file tree
Hide file tree
Showing 7 changed files with 821 additions and 19 deletions.
72 changes: 72 additions & 0 deletions go/stats/opentsdb/backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package opentsdb

import (
"testing"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/stats"
)

type mockWriter struct {
data []*DataPoint
}

func (mw *mockWriter) Write(data []*DataPoint) error {
mw.data = data
return nil
}

func TestPushAll(t *testing.T) {
mw := &mockWriter{}
b := &backend{
prefix: "testPrefix",
commonTags: map[string]string{"tag1": "value1"},
writer: mw,
}

err := b.PushAll()
assert.NoError(t, err)
before := len(mw.data)

stats.NewGaugeFloat64("test_push_all1", "help")
stats.NewGaugeFloat64("test_push_all2", "help")

err = b.PushAll()
assert.NoError(t, err)
after := len(mw.data)

assert.Equalf(t, after-before, 2, "length of writer.data should have been increased by 2")
}

func TestPushOne(t *testing.T) {
mw := &mockWriter{}
b := &backend{
prefix: "testPrefix",
commonTags: map[string]string{"tag1": "value1"},
writer: mw,
}

s := stats.NewGaugeFloat64("test_push_one", "help")
err := b.PushOne("test_push_one", s)
assert.NoError(t, err)

assert.Len(t, mw.data, 1)
assert.Equal(t, "testprefix.test_push_one", mw.data[0].Metric)
}
57 changes: 57 additions & 0 deletions go/stats/opentsdb/datapoint_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package opentsdb

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRead(t *testing.T) {
invalidInputs := []string{
"testMetric 0.100000 1.100000 key1=val1 key2=val2",
"InvalidMarshalText\n",
}

for _, in := range invalidInputs {
mockReader := bytes.NewBufferString(in)
dpr := NewDataPointReader(mockReader)
dp, err := dpr.Read()

assert.Error(t, err)
assert.Nil(t, dp)
}

mockReader := bytes.NewBufferString("testMetric 0.100000 1.100000 key1=val1 key2=val2\n")
dpr := NewDataPointReader(mockReader)
dp, err := dpr.Read()

assert.NoError(t, err)

expectedDataPoint := DataPoint{
Metric: "testMetric",
Timestamp: 0.1,
Value: 1.1,
Tags: map[string]string{
"key1": "val1",
"key2": "val2",
},
}
assert.Equal(t, expectedDataPoint, *dp)
}
68 changes: 68 additions & 0 deletions go/stats/opentsdb/datapoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package opentsdb

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMarshalText(t *testing.T) {
dp := DataPoint{
Metric: "testMetric",
Timestamp: 0.1,
Value: 1.1,
Tags: map[string]string{
"key1": "val1",
},
}

str, err := dp.MarshalText()
assert.NoError(t, err)
assert.Equal(t, "testMetric 0.100000 1.100000 key1=val1\n", str)
}

func TestUnmarshalTextToData(t *testing.T) {
dp := DataPoint{}

invalidMarshalTestCases := []string{
"InvalidMarshalText",
"testMetric invalidFloat invalidFloat",
"testMetric 0.100000 invalidFloat",
"testMetric 0.100000 1.100000 invalidKey:ValuePair",
}

for _, text := range invalidMarshalTestCases {
err := unmarshalTextToData(&dp, []byte(text))
assert.Error(t, err)
}

err := unmarshalTextToData(&dp, []byte("testMetric 0.100000 1.100000 key1=val1 key2=val2"))
assert.NoError(t, err)

expectedDataPoint := DataPoint{
Metric: "testMetric",
Timestamp: 0.1,
Value: 1.1,
Tags: map[string]string{
"key1": "val1",
"key2": "val2",
},
}
assert.Equal(t, expectedDataPoint, dp)
}
54 changes: 54 additions & 0 deletions go/stats/opentsdb/file_writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package opentsdb

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFileWriter(t *testing.T) {
tempFile, err := os.CreateTemp("", "tempfile")
assert.NoError(t, err)
defer os.Remove(tempFile.Name())

w, err := newFileWriter(tempFile.Name())
assert.NoError(t, err)

dp := []*DataPoint{
{
Metric: "testMetric",
Timestamp: 1.0,
Value: 2.0,
Tags: map[string]string{
"key1": "value1",
},
},
}

err = w.Write(dp)
assert.NoError(t, err)

err = tempFile.Close()
assert.NoError(t, err)

content, err := os.ReadFile(tempFile.Name())
assert.NoError(t, err)
assert.Equal(t, "testMetric 1.000000 2.000000 key1=value1\n", string(content))
}
39 changes: 39 additions & 0 deletions go/stats/opentsdb/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package opentsdb

import (
"testing"

"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)

func TestRegisterFlags(t *testing.T) {
oldOpenTSDBURI := openTSDBURI
defer func() {
openTSDBURI = oldOpenTSDBURI
}()

fs := pflag.NewFlagSet("test", pflag.ExitOnError)

registerFlags(fs)

err := fs.Set("opentsdb_uri", "testURI")
assert.NoError(t, err)
assert.Equal(t, "testURI", openTSDBURI)
}
60 changes: 60 additions & 0 deletions go/stats/opentsdb/http_writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package opentsdb

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestWrite(t *testing.T) {
sampleData := []*DataPoint{
{
Metric: "testMetric",
Timestamp: 1.0,
Value: 2.0,
Tags: map[string]string{
"tag1": "value1",
},
},
}

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))

var receivedData []*DataPoint
err := json.NewDecoder(r.Body).Decode(&receivedData)

assert.NoError(t, err)
assert.Len(t, receivedData, 1)
assert.Equal(t, sampleData[0], receivedData[0])

w.WriteHeader(http.StatusOK)
}))

defer server.Close()

client := &http.Client{}
hw := newHTTPWriter(client, server.URL)

err := hw.Write(sampleData)
assert.NoError(t, err)
}
Loading

0 comments on commit 9d895fb

Please sign in to comment.