/
mock_storage.go
247 lines (196 loc) · 8.35 KB
/
mock_storage.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
// Copyright 2020 Red Hat, Inc
//
// 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 helpers
import (
"database/sql"
"fmt"
"os"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/RedHatInsights/insights-operator-utils/tests/helpers"
"github.com/google/uuid"
"github.com/RedHatInsights/insights-results-aggregator/conf"
"github.com/RedHatInsights/insights-results-aggregator/migration"
"github.com/RedHatInsights/insights-results-aggregator/storage"
"github.com/RedHatInsights/insights-results-aggregator/types"
)
const (
postgres = "postgres"
unfulfilledExpectations = "there were unfulfilled expectations: %s"
)
// MustGetMockStorageWithExpects returns mock db storage
// with a driver "github.com/DATA-DOG/go-sqlmock" which requires you to write expect
// before each query, so first try to use MustGetPostgresStorage
// don't forget to call MustCloseMockStorageWithExpects
func MustGetMockStorageWithExpects(t *testing.T) (storage.OCPRecommendationsStorage, sqlmock.Sqlmock) {
return MustGetMockStorageWithExpectsForDriver(t, types.DBDriverGeneral)
}
// MustGetMockStorageWithExpectsForDriver returns mock db storage
// with specified driver type and
// with a driver "github.com/DATA-DOG/go-sqlmock" which requires you to write expect
// before each query, so first try to use MustGetPostgresStorage
// don't forget to call MustCloseMockStorageWithExpects
func MustGetMockStorageWithExpectsForDriver(
t *testing.T, driverType types.DBDriver,
) (storage.OCPRecommendationsStorage, sqlmock.Sqlmock) {
db, expects := MustGetMockDBWithExpects(t)
return storage.NewOCPRecommendationsFromConnection(db, driverType), expects
}
// MustGetMockStorageWithExpectsForDriverDVO same as MustGetMockStorageWithExpectsForDriver
// but for DVO
func MustGetMockStorageWithExpectsForDriverDVO(
t *testing.T, driverType types.DBDriver,
) (storage.DVORecommendationsStorage, sqlmock.Sqlmock) {
db, expects := MustGetMockDBWithExpects(t)
return storage.NewDVORecommendationsFromConnection(db, driverType), expects
}
// MustGetMockDBWithExpects returns mock db
// with a driver "github.com/DATA-DOG/go-sqlmock" which requires you to write expect
// before each query, so first try to use MustGetPostgresStorage
// don't forget to call MustCloseMockDBWithExpects
func MustGetMockDBWithExpects(t *testing.T) (*sql.DB, sqlmock.Sqlmock) {
db, expects, err := sqlmock.New()
helpers.FailOnError(t, err)
return db, expects
}
// MustCloseMockStorageWithExpects closes mock storage with expects and panics if it wasn't successful
func MustCloseMockStorageWithExpects(
t *testing.T, mockStorage storage.OCPRecommendationsStorage, expects sqlmock.Sqlmock,
) {
if err := expects.ExpectationsWereMet(); err != nil {
t.Errorf(unfulfilledExpectations, err)
}
expects.ExpectClose()
helpers.FailOnError(t, mockStorage.Close())
}
// MustCloseMockStorageWithExpectsDVO same asMustCloseMockStorageWithExpects
func MustCloseMockStorageWithExpectsDVO(
t *testing.T, mockStorage storage.DVORecommendationsStorage, expects sqlmock.Sqlmock,
) {
if err := expects.ExpectationsWereMet(); err != nil {
t.Errorf(unfulfilledExpectations, err)
}
expects.ExpectClose()
helpers.FailOnError(t, mockStorage.Close())
}
// MustCloseMockDBWithExpects closes mock db with expects and panics if it wasn't successful
func MustCloseMockDBWithExpects(
t *testing.T, db *sql.DB, expects sqlmock.Sqlmock,
) {
if err := expects.ExpectationsWereMet(); err != nil {
t.Errorf(unfulfilledExpectations, err)
}
expects.ExpectClose()
helpers.FailOnError(t, db.Close())
}
// MustGetPostgresStorage creates test postgres storage with credentials from config-devel
func MustGetPostgresStorage(tb testing.TB, init bool) (storage.OCPRecommendationsStorage, func()) {
dbAdminPassword := os.Getenv("INSIGHTS_RESULTS_AGGREGATOR__TESTS_DB_ADMIN_PASS")
err := conf.LoadConfiguration("../config-devel")
helpers.FailOnError(tb, err)
// force postgres and replace db name with test one
storageConf := &conf.Config.OCPRecommendationsStorage
storageConf.Driver = postgres
storageConf.PGDBName += "_test_db_" + strings.ReplaceAll(uuid.New().String(), "-", "_")
storageConf.PGPassword = dbAdminPassword
connString := fmt.Sprintf(
"host=%s port=%d user=%s password=%s sslmode=disable",
storageConf.PGHost, storageConf.PGPort, storageConf.PGUsername, storageConf.PGPassword,
)
adminConn, err := sql.Open(storageConf.Driver, connString)
helpers.FailOnError(tb, err)
query := "CREATE DATABASE " + storageConf.PGDBName + ";"
_, err = adminConn.Exec(query)
helpers.FailOnError(tb, err)
postgresStorage, err := storage.NewOCPRecommendationsStorage(*storageConf)
helpers.FailOnError(tb, err)
helpers.FailOnError(tb, postgresStorage.GetConnection().Ping())
if init {
helpers.FailOnError(tb, postgresStorage.MigrateToLatest())
helpers.FailOnError(tb, postgresStorage.Init())
}
return postgresStorage, func() {
MustCloseStorage(tb, postgresStorage)
_, err := adminConn.Exec("DROP DATABASE " + conf.Config.OCPRecommendationsStorage.PGDBName)
helpers.FailOnError(tb, err)
helpers.FailOnError(tb, adminConn.Close())
}
}
// MustGetPostgresStorageDVO creates test postgres storage with credentials from config-devel for DVO storage
func MustGetPostgresStorageDVO(tb testing.TB, init bool) (storage.DVORecommendationsStorage, func()) {
dbAdminPassword := os.Getenv("INSIGHTS_RESULTS_AGGREGATOR__TESTS_DB_ADMIN_PASS")
err := conf.LoadConfiguration("../config-devel")
helpers.FailOnError(tb, err)
// force postgres and replace db name with test one
storageConf := &conf.Config.DVORecommendationsStorage
storageConf.Driver = postgres
storageConf.PGDBName += "_test_db_" + strings.ReplaceAll(uuid.New().String(), "-", "_")
storageConf.PGPassword = dbAdminPassword
connString := fmt.Sprintf(
"host=%s port=%d user=%s password=%s sslmode=disable",
storageConf.PGHost, storageConf.PGPort, storageConf.PGUsername, storageConf.PGPassword,
)
adminConn, err := sql.Open(storageConf.Driver, connString)
helpers.FailOnError(tb, err)
query := "CREATE DATABASE " + storageConf.PGDBName + ";"
_, err = adminConn.Exec(query)
helpers.FailOnError(tb, err)
postgresStorage, err := storage.NewDVORecommendationsStorage(*storageConf)
helpers.FailOnError(tb, err)
helpers.FailOnError(tb, postgresStorage.GetConnection().Ping())
helpers.FailOnError(tb, migration.InitDBSchema(postgresStorage.GetConnection(), postgresStorage.GetDBSchema()))
if init {
helpers.FailOnError(tb, postgresStorage.MigrateToLatest())
helpers.FailOnError(tb, postgresStorage.Init())
}
return postgresStorage, func() {
MustCloseStorage(tb, postgresStorage)
_, err := adminConn.Exec("DROP DATABASE " + conf.Config.DVORecommendationsStorage.PGDBName)
helpers.FailOnError(tb, err)
helpers.FailOnError(tb, adminConn.Close())
}
}
// MustCloseStorage closes the storage and calls t.Fatal on error
func MustCloseStorage(tb testing.TB, s storage.Storage) {
helpers.FailOnError(tb, s.Close())
}
// PrepareDB prepares mock OCPRecommendationsDBStorage
func PrepareDB(t *testing.T) (*storage.OCPRecommendationsDBStorage, func()) {
mockStorage, closer := MustGetPostgresStorage(t, false)
dbStorage := mockStorage.(*storage.OCPRecommendationsDBStorage)
return dbStorage, closer
}
// PrepareDBDVO prepares mock DVORecommendationsDBStorage
func PrepareDBDVO(t *testing.T) (*storage.DVORecommendationsDBStorage, func()) {
mockStorage, closer := MustGetPostgresStorageDVO(t, true)
dbStorage := mockStorage.(*storage.DVORecommendationsDBStorage)
return dbStorage, closer
}
// PrepareDBAndInfo prepares mock OCPRecommendationsDBStorage and info table
func PrepareDBAndInfo(t *testing.T) (
*sql.DB,
types.DBDriver,
migration.Schema,
func(),
) {
storage, closer := PrepareDB(t)
dbConn := storage.GetConnection()
dbSchema := storage.GetDBSchema()
if err := migration.InitInfoTable(dbConn, dbSchema); err != nil {
closer()
t.Fatal(err)
}
return dbConn, storage.GetDBDriverType(), dbSchema, closer
}