-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
135 lines (105 loc) · 3.56 KB
/
main_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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func PrepareStats(tmpDir string) {
log.Debug("Creating stats dir")
err := os.MkdirAll(path.Join(tmpDir, "stats", "some"), os.ModePerm)
if err != nil {
log.Error(fmt.Errorf("can not create test path: %s", err))
}
err = os.MkdirAll(path.Join(tmpDir, "stats", "timers"), os.ModePerm)
if err != nil {
log.Error(fmt.Errorf("can not create test path: %s", err))
}
stat1Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "stat_1"), stat1Data, 0644)
check(err)
stat2Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "stat_2"), stat2Data, 0644)
check(err)
stat3Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "stat_3"), stat3Data, 0644)
check(err)
some1Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "some", "some_1"), some1Data, 0644)
check(err)
some2Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "some", "some_2"), some2Data, 0644)
check(err)
some3Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "some", "some_3"), some3Data, 0644)
check(err)
timer1Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "timers", "timer_1"), timer1Data, 0644)
check(err)
timer2Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "timers", "timer_2"), timer2Data, 0644)
check(err)
timer3Data := []byte("100")
err = ioutil.WriteFile(path.Join(tmpDir, "stats", "timers", "timer_3"), timer3Data, 0644)
check(err)
log.Debug("Created stats dir:", filepath.Dir(path.Join(tmpDir, "stats")))
}
func TearDownStats(tmpDir string) {
log.Debug("Clearing up stats dir")
err := os.RemoveAll(path.Join(tmpDir, "stats"))
if err != nil {
log.Error(fmt.Errorf("can not create test path: %s", err))
}
}
func TestGetStats(t *testing.T) {
config = viper.New()
log.SetLevel(logrus.DebugLevel)
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
log.Error(fmt.Errorf("can not remove test path: %s", err))
}
PrepareStats(tmpDir)
err = os.Setenv("PLM_STATS_PATH", path.Join(tmpDir, "stats"))
if err != nil { // Handle errors reading the config file
log.Error(fmt.Errorf("can not set env var gpe_stats_path: %s", err))
}
err = os.Setenv("PLM_STATS_PREFIX", "myapp")
if err != nil { // Handle errors reading the config file
log.Error(fmt.Errorf("can not set env var gpe_stats_path: %s", err))
}
config.SetEnvPrefix("PLM")
err = config.BindEnv("stats_path")
if err != nil { // Handle errors reading the config file
log.Error(fmt.Errorf("Fatal error config file: %s", err))
}
err = config.BindEnv("stats_prefix")
if err != nil { // Handle errors reading the config file
log.Error(fmt.Errorf("Fatal error config file: %s", err))
}
result, err := GetStats()
if err != nil {
TearDownStats(tmpDir)
log.Debug(fmt.Errorf("something goes wrong: %s", err))
t.Error("something goes wrong", err)
}
var sb strings.Builder
sb.WriteString("myapp_some_some_1 100\n")
sb.WriteString("myapp_some_some_2 100\n")
sb.WriteString("myapp_some_some_3 100\n")
sb.WriteString("myapp_stat_1 100\n")
sb.WriteString("myapp_stat_2 100\n")
sb.WriteString("myapp_stat_3 100\n")
sb.WriteString("myapp_timers_timer_1 100\n")
sb.WriteString("myapp_timers_timer_2 100\n")
sb.WriteString("myapp_timers_timer_3 100\n")
log.Debug(result)
if result != sb.String() {
t.Error("String should pass template:\n", sb.String(), "*** not equal ***\n", result)
}
TearDownStats(tmpDir)
}