-
Notifications
You must be signed in to change notification settings - Fork 145
/
show_test.go
141 lines (122 loc) · 4.18 KB
/
show_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
/*
* Open-Falcon
*
* Copyright (c) 2014-2018 Xiaomi, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product may include a number of subcomponents with separate copyright notices
* and license terms. Your use of these subcomponents is subject to the terms and
* conditions of the subcomponent's license, as noted in the LICENSE file.
*/
package main
import (
"fmt"
"io/ioutil"
"strings"
"testing"
"github.com/open-falcon/mymon/common"
"github.com/sebdah/goldie"
"github.com/stretchr/testify/assert"
"github.com/ziutek/mymysql/mysql"
)
// testConfigFile is used to make test
const testConfigFile = "fixtures/test.master.cfg"
const testSlaveConfigFile = "fixtures/test.slave.cfg"
var confTestMaster, confTestSlave *common.Config
var dbTestMaster, dbTestSlave mysql.Conn
func init() {
var err error
confTestMaster, err = common.NewConfig(testConfigFile)
if err != nil {
panic(err)
}
dbTestMaster, err = common.NewMySQLConnection(confTestMaster)
if err != nil {
panic(err)
}
confTestSlave, err = common.NewConfig(testSlaveConfigFile)
if err != nil {
panic(err)
}
dbTestSlave, err = common.NewMySQLConnection(confTestSlave)
if err != nil {
panic(err)
}
}
/**************** TEST SHOW GLOBAL *******************/
func TestShowGlobalStatus(t *testing.T) {
data, err := ShowGlobalStatus(confTestMaster, dbTestMaster)
assert.Nil(t, err)
assert.NotEmpty(t, data)
}
func TestShowGlobalVariables(t *testing.T) {
data, err := ShowGlobalStatus(confTestMaster, dbTestMaster)
assert.Nil(t, err)
assert.NotEmpty(t, data)
}
/**************** TEST INNODB*******************/
func TestParseInnodbStatus(t *testing.T) {
inputbuf, err := ioutil.ReadFile("fixtures/test_innodb_source.golden")
assert.Nil(t, err)
rows := strings.Split(string(inputbuf), "\n")
//Test parseInnodbStatus
res, err := parseInnodbStatus(confTestMaster, rows)
assert.Nil(t, err)
var actualRes string
for i, m := range res {
fmtLine := fmt.Sprintf("%d: MetaData Metric:%s Endpoint:%s Value:%v CounterType:%s Tags:%s Timestamp:%d Step:%d\n",
i, m.Metric, "localhost", m.Value, m.CounterType, "test_tag", 0, m.Step)
actualRes += fmtLine
}
goldie.Assert(t, "test_innodb_result", []byte(actualRes))
}
/**************** TEST BINARY *******************/
func TestShowBinaryLogStatus(t *testing.T) {
data, err := ShowBinaryLogs(confTestMaster, dbTestMaster)
assert.Nil(t, err)
for _, eachData := range data {
assert.NotNilf(t, eachData, "binlog value cannot be %v", eachData.Value)
}
}
func TestShowSlaveStatus(t *testing.T) {
var res []*MetaData
var err error
tempIsSlave := IsSlave
defer func() { IsSlave = tempIsSlave }()
//test master
res, err = ShowSlaveStatus(confTestMaster, dbTestMaster)
assert.Equal(t, 0, IsSlave)
assert.NoError(t, err)
assert.Equal(t, 4, len(res), "Wrong length of Metric with master!")
//test slave
res, err = ShowSlaveStatus(confTestSlave, dbTestSlave)
assert.Equal(t, 1, IsSlave)
assert.NoError(t, err)
assert.Equal(t, len(SlaveStatus)+4, len(res), "Wrong length of Metric with slave!")
}
func TestShowOtherMetric(t *testing.T) {
tempIsReadOnly := IsReadOnly
defer func() { IsReadOnly = tempIsReadOnly }()
testMetaDataStr := []string{
"master_is_read_only", "Master_is_readonly", "slave_is_read_only",
"innodb_stats_on_metadata", "io_thread_dela", "Heartbeats_Behind_Master", "others"}
expectIntValues := []int{0, 0, 1}
// test master readonly
IsReadOnly = 0
for i, meta := range testMetaDataStr[:2] {
getData, _ := ShowOtherMetric(confTestMaster, dbTestMaster, meta)
assert.Equal(t, expectIntValues[i], getData.Value, "Wrong value!")
}
// test slave readonly
IsReadOnly = 1
getData, _ := ShowOtherMetric(confTestSlave, dbTestSlave, testMetaDataStr[2])
assert.Equal(t, expectIntValues[2], getData.Value, "Wrong value!")
for i, meta := range testMetaDataStr[3:6] {
getData, _ = ShowOtherMetric(confTestMaster, dbTestMaster, meta)
assert.NotNil(t, expectIntValues[i], getData.Value, "Wrong value!")
}
getData, _ = ShowOtherMetric(confTestMaster, dbTestMaster, testMetaDataStr[6])
assert.Nil(t, getData.Value, "Wrong default value of metric!")
}