Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit-test for Run method and other improvements #45

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions promremote/client_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package promremote

import (
"bytes"
"encoding/base64"
"log"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
Expand Down Expand Up @@ -161,3 +165,25 @@ func TestCollect(t *testing.T) {
assert.Nil(err)
assert.NotEmpty(ts)
}

func TestRun(t *testing.T) {
reg := prometheus.NewRegistry()
reg.MustRegister(collectors.NewBuildInfoCollector())

c, _ := NewWriteClient("testendpoint", "test", "test", reg)

var buf bytes.Buffer
log.SetOutput(&buf)
t.Cleanup(func() {
log.SetOutput(os.Stderr)
})

quit := make(chan bool)
c.Run(time.Second, quit)

<-time.After(time.Second * 2)
quit <- true

output := buf.String()
assert.Contains(t, output, "ERROR Failed to send metrics to remote endpoint err=", "Should output error to log and not fail")
}
2 changes: 1 addition & 1 deletion promremote/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ErrRemoteWriteFailed struct {
Body string
}

func NewErrRemoteWriteFailed(status int, resBody io.ReadCloser) *ErrRemoteWriteFailed {
func NewErrRemoteWriteFailed(status int, resBody io.ReadCloser) error {
var body string
b, err := io.ReadAll(resBody)
if err != nil {
Expand Down
27 changes: 21 additions & 6 deletions promremote/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@ package promremote

import (
"io"
"os"
"strings"
"testing"

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

func TestNewErrRemoteWriteFailed(t *testing.T) {
result := &ErrRemoteWriteFailed{StatusCode: 400, Body: "testresult"}
r := io.NopCloser(strings.NewReader(result.Body))
defer r.Close()
err := NewErrRemoteWriteFailed(400, r)
assert.Equal(t, result, err)
func TestErrRemoteWriteFailed(t *testing.T) {
t.Run("New", func(t *testing.T) {
assert := assert.New(t)

expectedResult := &ErrRemoteWriteFailed{StatusCode: 400, Body: "testresult"}

r := io.NopCloser(strings.NewReader(expectedResult.Body))
defer r.Close()
result := NewErrRemoteWriteFailed(400, r).(*ErrRemoteWriteFailed)
assert.Equal(expectedResult, result, "Should read the response body")

f, err := os.Open("/dev/null")
if !assert.NoError(err, "Should not fail to open /dev/null") {
t.FailNow()
}
f.Close()
result = NewErrRemoteWriteFailed(400, f).(*ErrRemoteWriteFailed)
assert.Contains(result.Body, "file already closed", "Should return error of reader as body")
})

}
Loading