-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
68 lines (58 loc) · 1.85 KB
/
service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//go:build servicetest
// +build servicetest
package service
import (
"log/slog"
"net/http"
"testing"
"github.com/go-resty/resty/v2"
"github.com/num30/config"
"github.com/stretchr/testify/assert"
)
type TestConfig struct {
LogLevel string `default:"info" envvar:"LOG_LEVEL"`
ServiceHost string `default:"http://localhost:8080" envvar:"SERVICE_HOST"`
MetricsHost string `default:"http://localhost:10250" envvar:"METRICS_HOST"`
}
var tConfig = &TestConfig{}
var restClient = resty.New()
func init() {
err := config.NewConfReader("service_test").Read(tConfig)
if err != nil {
slog.With("error", err).Error("Error reading config")
}
}
func Test_Ping(t *testing.T) {
t.Run("Ping", func(t *testing.T) {
r, err := restClient.R().Get(tConfig.ServiceHost + "/ping")
assert.NoError(t, err)
assert.Equal(t, r.StatusCode(), http.StatusOK)
})
}
// Call endpoint and check response
func Test_Things(t *testing.T) {
// call the PUT endpoint
t.Run("Add", func(tt *testing.T) {
r, err := restClient.R().SetBody(`{ "value": "Thing value" }`).Put(tConfig.ServiceHost + "/things/some-name")
if assert.NoError(tt, err) {
assert.Equal(tt, http.StatusCreated, r.StatusCode(), "Invalid status code. Response body: %s", string(r.Body()))
}
})
// call the GET endpoint
t.Run("Get", func(tt *testing.T) {
r, err := restClient.R().Get(tConfig.ServiceHost + "/things/some-name")
if assert.NoError(tt, err) {
assert.Equal(tt, http.StatusOK, r.StatusCode(), "Invalid status code. Response body: %s", string(r.Body()))
}
})
}
// Check that prometheus metrics are available
func Test_GetMetrics(t *testing.T) {
t.Run("Metrics", func(t *testing.T) {
r, err := restClient.R().Get(tConfig.MetricsHost + "/metrics")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, r.StatusCode())
assert.Contains(t, string(r.Body()), "things_requests_total")
}
})
}