This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb_test.go
129 lines (111 loc) · 4.24 KB
/
db_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
// qan-api2
// Copyright (C) 2019 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"log"
"math"
"os"
"os/exec"
"strings"
"testing"
"time"
_ "github.com/ClickHouse/clickhouse-go" // register database/sql driver
_ "github.com/golang-migrate/migrate/database/clickhouse"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setup() *sqlx.DB {
cmdStr := `docker exec pmm-clickhouse-test clickhouse client -n --query='DROP DATABASE IF EXISTS pmm_test_parts; CREATE DATABASE pmm_test_parts;'`
if out, err := exec.Command("/bin/sh", "-c", cmdStr).Output(); err != nil {
log.Printf("Docker create db: %v, %v", out, err)
}
dsn, ok := os.LookupEnv("QANAPI_DSN_TEST")
dsn = strings.Replace(dsn, "?database=pmm_test", "?database=pmm_test_parts", 1)
if !ok {
dsn = "clickhouse://127.0.0.1:19000?database=pmm_test_parts"
}
db, err := sqlx.Connect("clickhouse", dsn)
if err != nil {
log.Fatal("Connection: ", err)
}
err = runMigrations(dsn)
if err != nil {
log.Fatal("Migration: ", err)
}
cmdStr = `cat fixture/metrics.part_*.json | docker exec -i pmm-clickhouse-test clickhouse client -d pmm_test_parts --query="INSERT INTO metrics FORMAT JSONEachRow"`
if out, err := exec.Command("/bin/sh", "-c", cmdStr).Output(); err != nil {
log.Fatalf("Docker load fixture: %v, %v", out, err)
}
return db
}
func cleanup() {
cleanupDatabases := []string{"pmm_test_parts", "pmm_created_db"}
for _, database := range cleanupDatabases {
cmdStr := fmt.Sprintf(`docker exec pmm-clickhouse-test clickhouse client --query='DROP DATABASE IF EXISTS %s;'`, database)
if out, err := exec.Command("/bin/sh", "-c", cmdStr).Output(); err != nil {
log.Fatalf("Docker drop db: %v, %v", out, err)
}
}
}
func TestDropOldPartition(t *testing.T) {
db := setup()
const query = `SELECT DISTINCT partition FROM system.parts WHERE database = 'pmm_test_parts' ORDER BY partition`
start := time.Now()
// fixtures have two partition 20190101 and 20190102
// here calculates how many days old partitions are.
end := time.Date(2019, 1, 2, 0, 0, 0, 0, time.UTC)
difference := end.Sub(start)
daysNewestPartition := uint(math.Abs(difference.Hours()) / 24)
t.Run("no so old partition", func(t *testing.T) {
partitions := []string{}
days := daysNewestPartition + 1
DropOldPartition(db, days)
err := db.Select(
&partitions,
query,
)
require.NoError(t, err, "Unexpected error in selecting metrics partition")
require.Equal(t, 2, len(partitions), "No one patrition were truncated. Partition %+v, days %d", partitions, days)
assert.Equal(t, "20190101", partitions[0], "Newest partition was not truncated")
assert.Equal(t, "20190102", partitions[1], "Oldest partition was not truncated")
})
t.Run("delete one day old partition", func(t *testing.T) {
partitions := []string{}
days := daysNewestPartition
DropOldPartition(db, days)
err := db.Select(
&partitions,
query,
)
require.NoError(t, err, "Unexpected error in selecting metrics partition")
require.Equal(t, 1, len(partitions), "Only one partition left. Partition %+v, days %d", partitions, days)
assert.Equal(t, "20190102", partitions[0], "Newest partition was not truncated")
})
cleanup()
}
func TestCreateDbIfNotExists(t *testing.T) {
t.Run("connect to db that doesnt exist", func(t *testing.T) {
dsn, ok := os.LookupEnv("QANAPI_DSN_TEST")
dsn = strings.Replace(dsn, "?database=pmm_test", "?database=pmm_created_db", 1)
if !ok {
dsn = "clickhouse://127.0.0.1:19000?database=pmm_created_db"
}
db := createDB(dsn)
require.Equal(t, db, nil, "Check connection after we create database")
})
}