github.com/kuoss/venti@v0.2.20/pkg/service/dashboard/dashboard_test.go (about) 1 package dashboard 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/kuoss/venti/pkg/model" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 var ( 13 service1 *DashboardService 14 sampleDashboard *model.Dashboard 15 ) 16 17 func init() { 18 _ = os.Chdir("../../..") 19 sampleDashboard = &model.Dashboard{Title: "Sample", Rows: []model.Row{ 20 {Panels: []model.Panel{ 21 {Title: "time", Type: "stat", Headers: []string(nil), Targets: []model.Target{{Expr: "time()", Legend: "", Legends: []string(nil), Unit: "dateTimeAsLocal", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 22 {Title: "apiserver%", Type: "stat", Headers: []string(nil), Targets: []model.Target{{Expr: "100 * up{job=\"kubernetes-apiservers\"}", Legend: "", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold{{Values: []int{80, 100}, Invert: true}}, Aggregation: ""}}, ChartOptions: nil}, 23 {Title: "k8s version", Type: "stat", Headers: []string(nil), Targets: []model.Target{{Expr: "kubernetes_build_info{job=\"kubernetes-apiservers\"}", Legend: "{{git_version}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 24 {Title: "no data I", Type: "stat", Headers: []string(nil), Targets: []model.Target{{Expr: "kubernetes_build_info{job=\"no-data\"}", Legend: "{{git_version}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 25 {Title: "no data II", Type: "stat", Headers: []string(nil), Targets: []model.Target{{Expr: "no_data", Legend: "hello {{world}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}}}, 26 {Panels: []model.Panel{ 27 {Title: "node", Type: "piechart", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(kube_node_status_condition{status='true'}) by (condition) > 0", Legend: "{{condition}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 28 {Title: "namespace", Type: "piechart", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(kube_namespace_status_phase) by (phase) > 0", Legend: "{{phase}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 29 {Title: "pod", Type: "piechart", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(kube_pod_status_phase{namespace=~\"$namespace\",node=~\"$node\"}) by (phase) > 0", Legend: "{{phase}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 30 {Title: "job", Type: "piechart", Headers: []string(nil), Targets: []model.Target{ 31 {Expr: "sum(kube_job_status_active{namespace=~\"$namespace\"}) > 0", Legend: "Active", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}, 32 {Expr: "sum(kube_job_status_failed{namespace=~\"$namespace\"}) > 0", Legend: "Failed", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}, 33 {Expr: "sum(kube_job_status_succeeded{namespace=~\"$namespace\"}) > 0", Legend: "Succeeded", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 34 {Title: "pvc", Type: "piechart", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(kube_persistentvolumeclaim_status_phase{namespace=~\"$namespace\"}) by (phase) > 0", Legend: "{{phase}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 35 {Title: "pv", Type: "piechart", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(kube_persistentvolume_status_phase) by (phase) > 0", Legend: "{{phase}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}}}, 36 {Panels: []model.Panel{ 37 {Title: "node load", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "node_load1", Legend: "{{node}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 38 {Title: "node cpu%", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "100 * sum(rate(node_cpu_seconds_total{mode!=\"idle\",mode!=\"iowait\"}[3m])) by (node) / sum(kube_node_status_allocatable{resource=\"cpu\"}) by (node)", Legend: "{{node}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil)}}, ChartOptions: &model.ChartOptions{YMax: 100}}, 39 {Title: "node mem%", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "100 * (1 - ( node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes ))", Legend: "{{node}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil)}}, ChartOptions: &model.ChartOptions{YMax: 100}}, 40 {Title: "node pods", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(kubelet_running_pods) by (instance)", Legend: "{{instance}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil)}}, ChartOptions: &model.ChartOptions{YMax: 120}}}}, 41 {Panels: []model.Panel{ 42 {Title: "node receive (Ki/m)", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(rate(node_network_receive_bytes_total[3m])) by (node) / 1024", Legend: "{{node}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 43 {Title: "node transmit (Ki/m)", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(rate(node_network_transmit_bytes_total[3m])) by (node) / 1024", Legend: "{{node}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 44 {Title: "node disk read (Ki/m)", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(rate(node_disk_read_bytes_total[3m])) by (node) / 1024", Legend: "{{node}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 45 {Title: "node disk write (Ki/m)", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(rate(node_disk_written_bytes_total[3m])) by (node) / 1024", Legend: "{{node}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 46 {Title: "node root fs%", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "100 * sum( 1-(node_filesystem_avail_bytes{mountpoint=\"/\"} / node_filesystem_size_bytes{mountpoint=\"/\"}) ) by (node)", Legend: "{{node}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil)}}, ChartOptions: &model.ChartOptions{YMax: 100}}}}, 47 {Panels: []model.Panel{ 48 {Title: "pvc%", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "100 * max( 1 - kubelet_volume_stats_available_bytes / kubelet_volume_stats_capacity_bytes) by (namespace, persistentvolumeclaim)", Legend: "{{namespace}}/{{persistentvolumeclaim}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil)}}, ChartOptions: &model.ChartOptions{YMax: 100}}, 49 {Title: "pvc inodes%", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "kubelet_volume_stats_inodes_used / kubelet_volume_stats_inodes * 100", Legend: "{{namespace}}/{{persistentvolumeclaim}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil)}}, ChartOptions: &model.ChartOptions{YMax: 100}}, 50 {Title: "pod cpu(mcores)", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(rate(container_cpu_usage_seconds_total{namespace=~\"$namespace\", instance=~\"$node\", container!=\"\"}[5m])) by (namespace, pod) * 1000", Legend: "{{namespace}}/{{pod}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 51 {Title: "pod mem(Mi)", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(container_memory_working_set_bytes{namespace=~\"$namespace\", instance=~\"$node\", container!=\"\"}) by (namespace, pod) / 1024 / 1024", Legend: "{{namespace}}/{{pod}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 52 {Title: "no data I", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "sum(container_memory_working_set_bytes{namespace=~\"$namespace\", instance=\"no-data\", container!=\"\"}) by (namespace, pod) / 1024 / 1024", Legend: "{{namespace}}/{{pod}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 53 {Title: "no data", Type: "time_series", Headers: []string(nil), Targets: []model.Target{{Expr: "no_data", Legend: "hello {{world}}", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}}}, 54 {Panels: []model.Panel{ 55 {Title: "event", Type: "logs", Headers: []string(nil), Targets: []model.Target{{Expr: "pod{namespace=~\"kube-system|logmon\",container=\"eventrouter\"}", Legend: "", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 56 {Title: "no data I", Type: "logs", Headers: []string(nil), Targets: []model.Target{{Expr: "pod{namespace=~\"kube-system|logmon\",container=\"no_data\"}", Legend: "", Legends: []string(nil), Unit: "", Columns: []string(nil), Headers: []string(nil), Key: "", Thresholds: []model.Threshold(nil), Aggregation: ""}}, ChartOptions: nil}, 57 }}}} 58 59 var err error 60 service1, err = New("etc/dashboards") 61 if err != nil { 62 panic(err) 63 } 64 } 65 66 func TestNew(t *testing.T) { 67 testCases := []struct { 68 dirpath string 69 want *DashboardService 70 wantError string 71 }{ 72 { 73 "", 74 &DashboardService{dashboards: []model.Dashboard{*sampleDashboard}}, 75 "", 76 }, 77 { 78 "etc/hello", 79 nil, 80 "getDashboardFilesFromPath err: no dashboard file: dirpath: etc/hello", 81 }, 82 { 83 "etc/dashboards", 84 &DashboardService{dashboards: []model.Dashboard{*sampleDashboard}}, 85 "", 86 }, 87 } 88 for i, tc := range testCases { 89 t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) { 90 service, err := New(tc.dirpath) 91 if tc.wantError == "" { 92 assert.NoError(t, err) 93 } else { 94 assert.EqualError(t, err, tc.wantError) 95 } 96 assert.Equal(t, tc.want, service) 97 }) 98 } 99 } 100 101 func TestLoadDashboardFromFile(t *testing.T) { 102 testCases := []struct { 103 pattern string 104 want *model.Dashboard 105 wantError string 106 }{ 107 { 108 "", 109 nil, 110 "error on ReadFile: open : no such file or directory", 111 }, 112 { 113 "etc/dashboards/sample.yml", 114 sampleDashboard, 115 "", 116 }, 117 } 118 for _, tc := range testCases { 119 got, err := loadDashboardFromFile(tc.pattern) 120 if tc.wantError == "" { 121 assert.NoError(t, err) 122 } else { 123 assert.EqualError(t, err, tc.wantError) 124 } 125 assert.Equal(t, tc.want, got) 126 } 127 } 128 129 func TestDashboards(t *testing.T) { 130 want := []model.Dashboard{*sampleDashboard} 131 got := service1.Dashboards() 132 assert.Equal(t, want, got) 133 }