Skip to content

Commit 642051c

Browse files
committed
chore: add tests
1 parent eb6cc02 commit 642051c

File tree

9 files changed

+128
-121
lines changed

9 files changed

+128
-121
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/ebuildy/mattermost-plugin-minotor
33
go 1.23.3
44

55
require (
6-
github.com/gorilla/mux v1.8.1
76
github.com/jarcoal/httpmock v1.3.1
87
github.com/mattermost/mattermost/server/public v0.1.5
98
github.com/pkg/errors v0.9.1
@@ -48,6 +47,7 @@ require (
4847
github.com/prometheus/common v0.55.0 // indirect
4948
github.com/prometheus/procfs v0.15.1 // indirect
5049
github.com/sirupsen/logrus v1.9.3 // indirect
50+
github.com/stretchr/objx v0.5.2 // indirect
5151
github.com/tinylib/msgp v1.2.0 // indirect
5252
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
5353
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
6868
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
6969
github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
7070
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
71-
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
72-
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
7371
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
7472
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
7573
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=

server/internal/adapters/collector/mattermost/collector.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/ebuildy/mattermost-plugin-minotor/server/internal/core/ports"
88
)
99

10+
// Collector is responsible for collecting various metrics and aggregating data from different sources.
1011
type Collector struct {
1112
logger ports.Logger
1213
gateway *mattermost_gateway.Client

server/internal/adapters/collector/mattermost/health_collector.go

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"github.com/mattermost/mattermost/server/public/model"
88
)
99

10+
// collectHealth gathers system health metrics including overall status, database, and filestore health.
11+
// It interacts with the Mattermost API to fetch ping information with full status options.
12+
// Returns a MetricsDataHealth object containing boolean values for each health status.
1013
func (c *Collector) collectHealth(ctx context.Context) *domain.MetricsDataHealth {
1114
pingResp, _, err := c.gateway.API.GetPingWithOptions(ctx, model.SystemPingOptions{FullStatus: true})
1215
if err != nil {

server/internal/adapters/collector/mattermost/info_collector.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@ import (
44
"github.com/ebuildy/mattermost-plugin-minotor/server/internal/core/domain"
55
)
66

7+
// collectInfo gathers and returns core system information such as server version, edition, installation time, and SQL driver name.
78
func (c *Collector) collectInfo() *domain.MetricsDataInfo {
89
apiSystem := c.gateway.PluginAPI.System
910
apiConfiguration := c.gateway.PluginAPI.Configuration
1011

1112
installationTime, _ := apiSystem.GetSystemInstallDate()
1213

14+
SQLDriverName := ""
15+
16+
if apiConfiguration.GetConfig() != nil {
17+
SQLDriverName = *apiConfiguration.GetConfig().SqlSettings.DriverName
18+
}
19+
1320
return &domain.MetricsDataInfo{
1421
MattermostVersion: apiSystem.GetServerVersion(),
1522
MattermostEdition: c.getInfoEdition(),
1623
MattermostInstallationTime: installationTime,
17-
SQLDriverName: *apiConfiguration.GetConfig().SqlSettings.DriverName,
24+
SQLDriverName: SQLDriverName,
1825
}
1926
}
2027

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package mattermost
2+
3+
import (
4+
"github.com/ebuildy/mattermost-plugin-minotor/server/internal/adapters/logger/fake"
5+
"github.com/ebuildy/mattermost-plugin-minotor/server/internal/adapters/services/mattermost_gateway"
6+
"github.com/ebuildy/mattermost-plugin-minotor/server/internal/core/domain"
7+
"github.com/ebuildy/mattermost-plugin-minotor/server/internal/utils"
8+
"github.com/jarcoal/httpmock"
9+
"github.com/mattermost/mattermost/server/public/model"
10+
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
11+
"github.com/mattermost/mattermost/server/public/pluginapi"
12+
"github.com/stretchr/testify/assert"
13+
"testing"
14+
"time"
15+
)
16+
17+
func TestDriver_collectInfo(t *testing.T) {
18+
httpmock.Activate()
19+
20+
tests := []struct {
21+
name string
22+
want domain.MetricsDataInfo
23+
setupMock func(api *plugintest.API)
24+
}{
25+
{
26+
name: "all is good",
27+
want: domain.MetricsDataInfo{
28+
MattermostVersion: "10.0",
29+
MattermostEdition: "free",
30+
MattermostInstallationTime: time.Unix(1638224000, 0),
31+
SQLDriverName: "mysql",
32+
},
33+
setupMock: func(api *plugintest.API) {
34+
api.On("GetConfig").Return(&model.Config{
35+
SqlSettings: model.SqlSettings{
36+
DriverName: utils.SafeRef("mysql"),
37+
},
38+
}, nil)
39+
api.On("GetSystemInstallDate").Return(int64(1638224000*1000), nil)
40+
api.On("GetServerVersion").Return("10.0", nil)
41+
api.On("GetLicense").Return(nil, nil)
42+
},
43+
},
44+
{
45+
name: "entreprise",
46+
want: domain.MetricsDataInfo{
47+
MattermostVersion: "10.0",
48+
MattermostEdition: "entreprise",
49+
MattermostInstallationTime: time.Unix(1638224000, 0),
50+
SQLDriverName: "postgres",
51+
},
52+
setupMock: func(api *plugintest.API) {
53+
api.On("GetConfig").Return(&model.Config{
54+
SqlSettings: model.SqlSettings{
55+
DriverName: utils.SafeRef("postgres"),
56+
},
57+
}, nil)
58+
api.On("GetSystemInstallDate").Return(int64(1638224000*1000), nil)
59+
api.On("GetServerVersion").Return("10.0", nil)
60+
api.On("GetLicense").Return(&model.License{}, nil)
61+
},
62+
},
63+
{
64+
name: "API send a 500 error",
65+
want: domain.MetricsDataInfo{
66+
MattermostVersion: "",
67+
MattermostEdition: "free",
68+
MattermostInstallationTime: time.Unix(0, 0),
69+
SQLDriverName: "",
70+
},
71+
setupMock: func(api *plugintest.API) {
72+
api.On("GetServerVersion").Return("", nil)
73+
api.On("GetSystemInstallDate").Return(int64(0), nil)
74+
api.On("GetConfig").Return(nil, nil)
75+
api.On("GetLicense").Return(nil, nil)
76+
},
77+
},
78+
}
79+
80+
for _, tt := range tests {
81+
api := plugintest.NewAPI(t)
82+
83+
mattermostGatewayClient := &mattermost_gateway.Client{
84+
API: model.NewAPIv4Client(mattermostEndpointURL),
85+
PluginAPI: pluginapi.NewClient(api, &plugintest.Driver{}),
86+
}
87+
88+
c := NewCollector(fake.NewFakeLogger(), mattermostGatewayClient)
89+
90+
tt.setupMock(api)
91+
92+
t.Run(tt.name, func(t *testing.T) {
93+
metrics := c.collectInfo()
94+
95+
assert.Equal(t, tt.want.MattermostInstallationTime, metrics.MattermostInstallationTime)
96+
assert.Equal(t, tt.want.MattermostVersion, metrics.MattermostVersion)
97+
assert.Equal(t, tt.want.MattermostEdition, metrics.MattermostEdition)
98+
assert.Equal(t, tt.want.SQLDriverName, metrics.SQLDriverName)
99+
})
100+
}
101+
}

server/internal/adapters/collector/mattermost/job_collector.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66
)
77

8+
// collectJob retrieves job data from the database, including job counts grouped by type and status, and returns metric data.
89
func (c *Collector) collectJob() *domain.MetricsDataJobs {
910
var countByTypesStatus []domain.JobCountByStatusType
1011

server/internal/adapters/collector/mattermost_api.go

-117
This file was deleted.

server/internal/utils/utils.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package utils
2+
3+
func SafeDeref[T any](p *T) T {
4+
if p == nil {
5+
var v T
6+
return v
7+
}
8+
return *p
9+
}
10+
11+
func SafeRef[T any](p T) *T {
12+
return &p
13+
}

0 commit comments

Comments
 (0)