1 package cmdopts
2
3 import (
4 "os"
5 "strings"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 )
10
11 func TestMetricPrintInit_Execute(t *testing.T) {
12
13 var err error
14
15 w := &strings.Builder{}
16 os.Args = []string{0: "config_test", "metric", "print-init", "test1"}
17 _, err = New(w)
18 assert.Error(t, err, "should error when metric not found")
19 assert.Contains(t, err.Error(), "not found")
20
21 w.Reset()
22 os.Args = []string{0: "config_test", "metric", "print-init", "cpu_load"}
23 _, err = New(w)
24 assert.Contains(t, w.String(), "-- cpu_load")
25 assert.NoError(t, err)
26
27 w.Reset()
28 os.Args = []string{0: "config_test", "metric", "print-init", "standard"}
29 _, err = New(w)
30 assert.Contains(t, w.String(), "-- cpu_load")
31 assert.NoError(t, err)
32
33 os.Args = []string{0: "config_test", "--metrics=foo", "metric", "print-init", "standard"}
34 _, err = New(w)
35 assert.Error(t, err, "should error when no metric definitions found")
36
37 os.Args = []string{0: "config_test", "--metrics=postgresql://foo@bar/fail", "metric", "print-init", "standard"}
38 _, err = New(w)
39 assert.Error(t, err, "should error when no config database found")
40 }
41
42 func TestMetricPrintSQL_Execute(t *testing.T) {
43 var err error
44
45 w := &strings.Builder{}
46 os.Args = []string{0: "config_test", "metric", "print-sql", "test1"}
47 _, err = New(w)
48 assert.Error(t, err, "should error when metric not found")
49 assert.Contains(t, err.Error(), "not found")
50
51 w.Reset()
52 os.Args = []string{0: "config_test", "metric", "print-sql", "cpu_load"}
53 _, err = New(w)
54 assert.Contains(t, w.String(), "get_load_average()")
55 assert.NoError(t, err)
56
57 w.Reset()
58 os.Args = []string{0: "config_test", "metric", "print-sql", "cpu_load", "--version=10"}
59 _, err = New(w)
60 assert.Empty(t, w.String(), "should not print anything for deprecated version")
61 assert.NoError(t, err)
62
63 os.Args = []string{0: "config_test", "--metrics=foo", "metric", "print-sql", "foo"}
64 _, err = New(w)
65 assert.Error(t, err, "should error when no metric definitions found")
66
67 os.Args = []string{0: "config_test", "--metrics=postgresql://foo@bar/fail", "metric", "print-sql", "foo"}
68 _, err = New(w)
69 assert.Error(t, err, "should error when no config database found")
70 }
71
72 func TestMetricList_Execute(t *testing.T) {
73 var err error
74
75
76 w := &strings.Builder{}
77 os.Args = []string{0: "config_test", "metric", "list"}
78 _, err = New(w)
79 assert.NoError(t, err, "should not error when listing all metrics")
80 assert.Contains(t, w.String(), "metrics:")
81 assert.Contains(t, w.String(), "presets:")
82 assert.Contains(t, w.String(), "cpu_load")
83 assert.Contains(t, w.String(), "standard")
84
85
86 w.Reset()
87 os.Args = []string{0: "config_test", "metric", "list", "cpu_load"}
88 _, err = New(w)
89 assert.NoError(t, err, "should not error when listing specific metric")
90 assert.Contains(t, w.String(), "cpu_load")
91 assert.Contains(t, w.String(), "metrics:")
92
93 assert.NotContains(t, w.String(), "presets:")
94
95
96 w.Reset()
97 os.Args = []string{0: "config_test", "metric", "list", "standard"}
98 _, err = New(w)
99 assert.NoError(t, err, "should not error when listing preset")
100 assert.Contains(t, w.String(), "presets:")
101 assert.Contains(t, w.String(), "standard")
102 assert.Contains(t, w.String(), "metrics:")
103
104 assert.Contains(t, w.String(), "cpu_load")
105
106
107 w.Reset()
108 os.Args = []string{0: "config_test", "metric", "list", "nonexistent"}
109 _, err = New(w)
110 assert.Error(t, err, "should error when metric/preset not found")
111 assert.Contains(t, err.Error(), "not found")
112
113
114 os.Args = []string{0: "config_test", "--metrics=foo", "metric", "list"}
115 _, err = New(w)
116 assert.Error(t, err, "should error when no metric definitions found")
117
118 os.Args = []string{0: "config_test", "--metrics=postgresql://foo@bar/fail", "metric", "list"}
119 _, err = New(w)
120 assert.Error(t, err, "should error when no config database found")
121 }
122