-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add required tests for go/stats/opentsdb
Signed-off-by: Noble Mittal <[email protected]>
- Loading branch information
1 parent
37414c3
commit f8198a7
Showing
7 changed files
with
768 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.