Skip to content

Commit

Permalink
Increase code coverage (#201)
Browse files Browse the repository at this point in the history
* added unit tests
  • Loading branch information
msiebeneicher authored Aug 7, 2017
1 parent 76bfa21 commit 2832495
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2015-2017 trivago GmbH
//
// 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 core

import (
"github.com/trivago/tgo/ttesting"
"testing"
)

func TestModulateResultError(t *testing.T) {
expect := ttesting.NewExpect(t)

err := NewModulateResultError("error message %s", "foo")
expect.Equal("error message [foo]", err.Error())
}
36 changes: 36 additions & 0 deletions core/filterresult_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2015-2017 trivago GmbH
//
// 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 core

import (
"github.com/trivago/tgo/ttesting"
"testing"
)

func TestFilterResultMessageReject(t *testing.T) {
expect := ttesting.NewExpect(t)

streamID := GetStreamID("streamFoo")
filterResult := FilterResultMessageReject(streamID)

expect.Equal(streamID, filterResult.GetStreamID())
}

func TestFilterResultMessageAccept(t *testing.T) {
expect := ttesting.NewExpect(t)

filterResult := FilterResultMessageAccept
expect.Equal(InvalidStreamID, filterResult.GetStreamID())
}
40 changes: 40 additions & 0 deletions core/modulator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2015-2017 trivago GmbH
//
// 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 core

import (
"github.com/sirupsen/logrus"
"github.com/trivago/tgo/ttesting"
"testing"
)

func TestModulate(t *testing.T) {
expect := ttesting.NewExpect(t)

TypeRegistry.Register(mockFormatter{})

mockConf := NewPluginConfig("", "core.mockPlugin")
mockConf.Override("Modulators", []interface{}{
"core.mockFormatter",
})

reader := NewPluginConfigReaderWithError(&mockConf)

modulatorArray, err := reader.GetModulatorArray("Modulators", logrus.StandardLogger(), []Modulator{})
expect.NoError(err)

msg := NewMessage(nil, []byte("foo"), nil, InvalidStreamID)
expect.Equal(ModulateResultContinue, modulatorArray.Modulate(msg))
}
82 changes: 82 additions & 0 deletions format/collectd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2015-2017 trivago GmbH
//
// 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 format

import (
"github.com/trivago/gollum/core"
"github.com/trivago/tgo/ttesting"
"testing"
)

const collectdTestPayload = `{"values":[42],"dstypes":["gauge"],"dsnames":["legacy"],"time":1426585562.999,"interval":10.000,"host":"example.com","plugin":"golang","type":"gauge"}`

func TestCollectdToInflux10(t *testing.T) {
expect := ttesting.NewExpect(t)

config := core.NewPluginConfig("", "format.CollectdToInflux10")
plugin, err := core.NewPluginWithConfig(config)
expect.NoError(err)

formatter, casted := plugin.(*CollectdToInflux10)
expect.True(casted)

influxPayload := "golang,plugin_instance=,type=gauge,type_instance=,host=example.com,dstype=gauge,dsname=legacy value=42.000000 1426585562999\\n"

msg := core.NewMessage(nil, []byte(collectdTestPayload), nil, core.InvalidStreamID)

err = formatter.ApplyFormatter(msg)
expect.NoError(err)
expect.Equal(influxPayload, string(msg.GetPayload()))
}

func TestCollectdToInflux09(t *testing.T) {
expect := ttesting.NewExpect(t)

config := core.NewPluginConfig("", "format.CollectdToInflux09")
plugin, err := core.NewPluginWithConfig(config)
expect.NoError(err)

formatter, casted := plugin.(*CollectdToInflux09)
expect.True(casted)

influxPayload := `{"name": "golang", "timestamp": 1426585562, "precision": "ms", "tags": {"plugin_instance": "", "type": "gauge", "type_instance": "", "host": "example.com", "dstype": "gauge", "dsname": "legacy"}, "fields": {"value": 42.000000} },`

msg := core.NewMessage(nil, []byte(collectdTestPayload), nil, core.InvalidStreamID)

err = formatter.ApplyFormatter(msg)
expect.NoError(err)

expect.Equal(influxPayload, string(msg.GetPayload()))
}

func TestCollectdToInflux08(t *testing.T) {
expect := ttesting.NewExpect(t)

config := core.NewPluginConfig("", "format.CollectdToInflux08")
plugin, err := core.NewPluginWithConfig(config)
expect.NoError(err)

formatter, casted := plugin.(*CollectdToInflux08)
expect.True(casted)

influxPayload := `{"name": "example.com.golang.gauge", "columns": ["time", "value"], "points":[[1426585562, 42.000000]]},`

msg := core.NewMessage(nil, []byte(collectdTestPayload), nil, core.InvalidStreamID)

err = formatter.ApplyFormatter(msg)
expect.NoError(err)

expect.Equal(influxPayload, string(msg.GetPayload()))
}

0 comments on commit 2832495

Please sign in to comment.