forked from content-services/content-sources-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_test.go
257 lines (224 loc) · 7.71 KB
/
metrics_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package dao
import (
"testing"
"time"
"github.com/content-services/content-sources-backend/pkg/config"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/seeds"
"github.com/lib/pq"
"github.com/openlyinc/pointy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type MetricsSuite struct {
*DaoSuite
dao MetricsDao
initialRepoCount int
initialRepositoryConfigsCount int
initialPublicRepositoriesIntrospectionCount IntrospectionCount
initialPublicRepositoriesFailedIntrospectionCount int
initialCustomRepositoriesIntrospectionCount IntrospectionCount
}
func (s *MetricsSuite) SetupTest() {
s.DaoSuite.SetupTest()
s.dao = GetMetricsDao(s.tx)
s.initialRepoCount = s.dao.RepositoriesCount()
if s.tx.Error != nil {
s.FailNow(s.tx.Error.Error())
}
s.initialRepositoryConfigsCount = s.dao.RepositoryConfigsCount()
if s.tx.Error != nil {
s.FailNow(s.tx.Error.Error())
}
s.initialPublicRepositoriesIntrospectionCount = s.dao.RepositoriesIntrospectionCount(36, true)
if s.tx.Error != nil {
s.FailNow(s.tx.Error.Error())
}
s.initialCustomRepositoriesIntrospectionCount = s.dao.RepositoriesIntrospectionCount(36, false)
if s.tx.Error != nil {
s.FailNow(s.tx.Error.Error())
}
s.initialPublicRepositoriesFailedIntrospectionCount = s.dao.PublicRepositoriesFailedIntrospectionCount()
if s.tx.Error != nil {
s.FailNow(s.tx.Error.Error())
}
}
func TestMetricsSuite(t *testing.T) {
m := DaoSuite{}
r := MetricsSuite{DaoSuite: &m}
suite.Run(t, &r)
}
func (s *MetricsSuite) TestGetMetricsDao() {
t := s.T()
var dao MetricsDao
dao = GetMetricsDao(nil)
assert.Nil(t, dao)
dao = GetMetricsDao(s.tx)
assert.NotNil(t, dao)
}
func (s *MetricsSuite) TestOrganizationCount() {
t := s.T()
dao := s.dao
var result int64
err := seeds.SeedRepositoryConfigurations(s.tx, 1, seeds.SeedOptions{})
assert.Nil(t, err)
// The initial state should be 0
result = dao.OrganizationTotal()
assert.True(t, result > 0)
}
func (s *MetricsSuite) TestRepositoriesCount() {
t := s.T()
dao := s.dao
var result int
// The initial state should be 0
result = dao.RepositoriesCount()
assert.Equal(t, 0, result-s.initialRepoCount)
// The counter is increased by 1
s.tx.Create(&models.Repository{
URL: "https://",
Public: true,
Status: config.StatusInvalid,
LastIntrospectionTime: nil,
LastIntrospectionSuccessTime: nil,
LastIntrospectionUpdateTime: nil,
LastIntrospectionError: nil,
PackageCount: 0,
})
result = dao.RepositoriesCount()
assert.Equal(t, 1, result-s.initialRepoCount)
}
func (s *MetricsSuite) TestRepositoryConfigsCount() {
t := s.T()
dao := s.dao
var (
result int
err error
)
// The initial state should be 0
result = dao.RepositoryConfigsCount()
assert.Equal(t, 0, result-s.initialRepositoryConfigsCount)
// The counter is increased by 1
repo := &models.Repository{
URL: "https://www.example.test",
}
err = s.tx.Create(repo).Error
require.NoError(t, err)
err = s.tx.Create(&models.RepositoryConfiguration{
Name: "test",
Versions: pq.StringArray{config.El9},
Arch: config.X8664,
GpgKey: "",
MetadataVerification: false,
OrgID: accountIdTest,
RepositoryUUID: repo.UUID,
}).Error
require.NoError(t, err)
result = dao.RepositoryConfigsCount()
assert.Equal(t, 1, result-s.initialRepositoryConfigsCount)
}
func (s *MetricsSuite) TestPublicRepositoriesNotIntrospectedLas24HoursCount() {
t := s.T()
tx := s.tx
var (
result IntrospectionCount
err error
repo models.Repository
)
result = s.dao.RepositoriesIntrospectionCount(36, true)
assert.Equal(t, int64(0), result.Missed-s.initialPublicRepositoriesIntrospectionCount.Missed)
// This repository won't be counted for the metrics
repo = models.Repository{
URL: "https://www.example.test",
Public: true,
Status: config.StatusPending,
LastIntrospectionTime: nil,
LastIntrospectionError: nil,
LastIntrospectionUpdateTime: nil,
LastIntrospectionSuccessTime: nil,
PackageCount: 0,
}
err = tx.Create(&repo).Error
require.NoError(t, err)
result = s.dao.RepositoriesIntrospectionCount(36, true)
assert.Equal(t, int64(0), result.Missed-s.initialPublicRepositoriesIntrospectionCount.Missed)
lastIntrospectionTime := time.Now().Add(-37 * time.Hour)
repo = models.Repository{
URL: "https://www.example2.test",
Public: true,
Status: config.StatusInvalid,
LastIntrospectionTime: &lastIntrospectionTime,
LastIntrospectionError: pointy.String("test"),
LastIntrospectionUpdateTime: nil,
LastIntrospectionSuccessTime: nil,
PackageCount: 0,
}
err = tx.Create(&repo).Error
require.NoError(t, err)
result = s.dao.RepositoriesIntrospectionCount(36, true)
assert.Equal(t, int64(1), result.Missed-s.initialPublicRepositoriesIntrospectionCount.Missed)
repo = models.Repository{
URL: "https://www.example3.test",
Public: true,
Status: config.StatusUnavailable,
LastIntrospectionTime: &lastIntrospectionTime,
LastIntrospectionError: pointy.String("test"),
LastIntrospectionUpdateTime: nil,
LastIntrospectionSuccessTime: nil,
PackageCount: 0,
}
err = tx.Create(&repo).Error
require.NoError(t, err)
result = s.dao.RepositoriesIntrospectionCount(36, true)
assert.Equal(t, int64(2), result.Missed-s.initialPublicRepositoriesIntrospectionCount.Missed)
}
func (s *MetricsSuite) TestPublicRepositoriesFailedIntrospectionCount() {
t := s.T()
var (
result int
err error
repo models.Repository
)
result = s.dao.PublicRepositoriesFailedIntrospectionCount()
assert.Equal(t, 0, result-s.initialPublicRepositoriesFailedIntrospectionCount)
lastIntrospectionTime := time.Now().Add(-37 * time.Hour)
repo = models.Repository{
URL: "https://www.example3.test",
Public: true,
Status: config.StatusInvalid,
LastIntrospectionTime: &lastIntrospectionTime,
LastIntrospectionError: pointy.String("test"),
LastIntrospectionUpdateTime: nil,
LastIntrospectionSuccessTime: nil,
PackageCount: 0,
}
err = s.tx.Create(&repo).Error
require.NoError(t, err)
result = s.dao.PublicRepositoriesFailedIntrospectionCount()
assert.Equal(t, 1, result-s.initialPublicRepositoriesFailedIntrospectionCount)
}
func (s *MetricsSuite) TestNonPublicRepositoriesNonIntrospectedLast24HoursCount() {
t := s.T()
var (
result IntrospectionCount
err error
repo models.Repository
)
result = s.dao.RepositoriesIntrospectionCount(36, false)
assert.Equal(t, int64(0), result.Missed-s.initialCustomRepositoriesIntrospectionCount.Missed)
lastIntrospectionTime := time.Now().Add(-38 * time.Hour)
repo = models.Repository{
URL: "https://www.example4.test",
Public: false,
Status: config.StatusInvalid,
LastIntrospectionTime: &lastIntrospectionTime,
LastIntrospectionError: pointy.String("test"),
LastIntrospectionUpdateTime: nil,
LastIntrospectionSuccessTime: nil,
PackageCount: 0,
}
err = s.tx.Create(&repo).Error
require.NoError(t, err)
result = s.dao.RepositoriesIntrospectionCount(36, false)
assert.Equal(t, int64(1), result.Missed-s.initialCustomRepositoriesIntrospectionCount.Missed)
}