1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/influxWriteData_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

81 lines
2.3 KiB
Go

//go:build unit
// +build unit
package cmd
import (
"errors"
"testing"
"github.com/SAP/jenkins-library/pkg/influx/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestWriteData(t *testing.T) {
options := &influxWriteDataOptions{
ServerURL: "http://localhost:8086",
AuthToken: "authToken",
Bucket: "piper",
Organization: "org",
}
errString := "some error"
errWriteData := errors.New(errString)
tests := []struct {
name string
dataMap string
dataMapTags string
writeDataErr error
errExpected bool
errIncludeStr string
}{
{
"Test writing metrics with correct json data - success",
`{"series_1": {"field_a": 11, "field_b": 12}, "series_2": {"field_c": 21, "field_d": 22}}`,
`{"series_1": {"tag_a": "a", "tag_b": "b"}, "series_2": {"tag_c": "c", "tag_d": "d"}}`,
nil,
false,
"",
},
{
"Test writing metrics with invalid dataMap",
`"series_1": {"field_a": 11, "field_b": 12}, "series_2": {"field_c": 21, "field_d": 22}`,
`{"series_1": {"tag_a": "a", "tag_b": "b"}, "series_2": {"tag_c": "c", "tag_d": "d"}}`,
nil,
false,
"Failed to unmarshal dataMap:",
},
{
"Test writing metrics with invalid dataMapTags",
`{"series_1": {"field_a": 11, "field_b": 12}, "series_2": {"field_c": 21, "field_d": 22}}`,
`{"series_1": {"tag_a": 2, "tag_b": "b"}, "series_2": {"tag_c": "c", "tag_d": "d"}}`,
nil,
false,
"Failed to unmarshal dataMapTags:",
},
{
"Test writing metrics with correct json data - failed",
`{"series_1": {"field_a": 11, "field_b": 12}, "series_2": {"field_c": 21, "field_d": 22}}`,
`{"series_1": {"tag_a": "a", "tag_b": "b"}, "series_2": {"tag_c": "c", "tag_d": "d"}}`,
errWriteData,
true,
errString,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
influxClientMock := &mocks.Client{}
writeAPIBlockingMock := &mocks.WriteAPIBlocking{}
writeAPIBlockingMock.On("WritePoint", mock.Anything, mock.Anything).Return(tt.writeDataErr)
influxClientMock.On("WriteAPIBlocking", mock.Anything, mock.Anything).Return(writeAPIBlockingMock)
options.DataMap = tt.dataMap
options.DataMapTags = tt.dataMapTags
err := writeData(options, influxClientMock)
if err != nil {
assert.Contains(t, err.Error(), tt.errIncludeStr)
}
})
}
}