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.Empty(t, w.String()) 19 assert.NoError(t, err, "should not error when no metrics 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.Empty(t, w.String()) 49 assert.NoError(t, err, "should not error when no metrics 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