|
| 1 | +// Copyright 2019 The OpenSDS Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +// License for the specific language governing permissions and limitations |
| 13 | +// under the License. |
| 14 | + |
| 15 | +package lvm |
| 16 | + |
| 17 | +import ( |
| 18 | + "reflect" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/opensds/opensds/pkg/utils/exec" |
| 22 | +) |
| 23 | + |
| 24 | +func TestNewMetricCli(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + want *MetricCli |
| 28 | + wantErr bool |
| 29 | + }{ |
| 30 | + {name: "test1", want: &MetricCli{ |
| 31 | + BaseExecuter: exec.NewBaseExecuter(), |
| 32 | + RootExecuter: exec.NewRootExecuter(), |
| 33 | + }, wantErr: false}, |
| 34 | + } |
| 35 | + for _, tt := range tests { |
| 36 | + t.Run(tt.name, func(t *testing.T) { |
| 37 | + got, err := NewMetricCli() |
| 38 | + if (err != nil) != tt.wantErr { |
| 39 | + t.Errorf("unexpected error output for NewMetricCli() error = %v, wantErr %v", err, tt.wantErr) |
| 40 | + return |
| 41 | + } |
| 42 | + if !reflect.DeepEqual(got, tt.want) { |
| 43 | + t.Errorf("unexpected cli output for NewMetricCli() = %v, want %v", got, tt.want) |
| 44 | + } |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestMetricCli_execute(t *testing.T) { |
| 50 | + type fields struct { |
| 51 | + BaseExecuter exec.Executer |
| 52 | + RootExecuter exec.Executer |
| 53 | + } |
| 54 | + type args struct { |
| 55 | + cmd []string |
| 56 | + } |
| 57 | + tests := []struct { |
| 58 | + name string |
| 59 | + fields fields |
| 60 | + args args |
| 61 | + want string |
| 62 | + wantErr bool |
| 63 | + }{ |
| 64 | + {name: "test1", fields: fields{}, args: args{}, want: "env: ‘error_cmd’: No such file or directory\n", wantErr: true}, |
| 65 | + } |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + c, _ := NewMetricCli() |
| 69 | + got, err := c.execute("env", "LC_ALL=C", "error_cmd") |
| 70 | + if (err != nil) != tt.wantErr { |
| 71 | + t.Errorf("difference in expected result of MetricCli.execute() error = %v, wantErr %v", err, tt.wantErr) |
| 72 | + return |
| 73 | + } |
| 74 | + if got != tt.want { |
| 75 | + t.Errorf("difference in expected result of of MetricCli.execute() = %v, want %v", got, tt.want) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func Test_isSarEnabled(t *testing.T) { |
| 82 | + type args struct { |
| 83 | + out string |
| 84 | + } |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + args args |
| 88 | + want bool |
| 89 | + }{ |
| 90 | + {name: "test1", args: args{out: "Please check if data collecting is enabled"}, want: false}, |
| 91 | + {name: "test1", args: args{out: "Command 'sar' not found"}, want: false}, |
| 92 | + } |
| 93 | + |
| 94 | + for _, tt := range tests { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + if got := isSarEnabled(tt.args.out); got != tt.want { |
| 97 | + t.Errorf("difference in expected result of expected isSarEnabled() = %v, want %v", got, tt.want) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestMetricCli_parseCommandOutput(t *testing.T) { |
| 104 | + |
| 105 | + type fields struct { |
| 106 | + BaseExecuter exec.Executer |
| 107 | + RootExecuter exec.Executer |
| 108 | + } |
| 109 | + type args struct { |
| 110 | + metricList []string |
| 111 | + |
| 112 | + out *MetricFakeResp |
| 113 | + } |
| 114 | + tests := []struct { |
| 115 | + name string |
| 116 | + fields fields |
| 117 | + args args |
| 118 | + }{ |
| 119 | + {name: "test1", fields: fields{}, args: args{metricList: expctdMetricList, out: respMap["sar"]}}, |
| 120 | + } |
| 121 | + for _, tt := range tests { |
| 122 | + t.Run(tt.name, func(t *testing.T) { |
| 123 | + returnMap := make(map[string]map[string]string) |
| 124 | + labelMap := make(map[string]map[string]string) |
| 125 | + metricMap := make(map[string]int) |
| 126 | + c := &MetricCli{ |
| 127 | + BaseExecuter: tt.fields.BaseExecuter, |
| 128 | + RootExecuter: tt.fields.RootExecuter, |
| 129 | + } |
| 130 | + c.parseCommandOutput(tt.args.metricList, returnMap, labelMap, metricMap, tt.args.out.out) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestMetricCli_CollectMetrics(t *testing.T) { |
| 136 | + type fields struct { |
| 137 | + BaseExecuter exec.Executer |
| 138 | + RootExecuter exec.Executer |
| 139 | + } |
| 140 | + type args struct { |
| 141 | + metricList []string |
| 142 | + } |
| 143 | + var expctedLabelMap map[string]map[string]string = map[string]map[string]string{"loop2": map[string]string{"device": "loop2"}, "loop3": map[string]string{"device": "loop3"}, "loop4": map[string]string{"device": "loop4"}, "loop5": map[string]string{"device": "loop5"}, "loop6": map[string]string{"device": "loop6"}, "loop7": map[string]string{"device": "loop7"}, "loop9": map[string]string{"device": "loop9"}, "loop10": map[string]string{"device": "loop10"}, "opensds--volumes--default-volume-d96cc42b-b285-474e-aa98-c61e66df7461": map[string]string{"device": "opensds--volumes--default-volume-d96cc42b-b285-474e-aa98-c61e66df7461"}, "opensds--volumes--default-volume--b902e771--8e02--4099--b601--a6b3881f8": map[string]string{"device": "opensds--volumes--default-volume--b902e771--8e02--4099--b601--a6b3881f8"}} |
| 144 | + |
| 145 | + tests := []struct { |
| 146 | + name string |
| 147 | + fields fields |
| 148 | + args args |
| 149 | + want map[string]map[string]string |
| 150 | + wantErr bool |
| 151 | + }{ |
| 152 | + {name: "test1", fields: fields{}, args: args{metricList: expctdMetricList}, want: expctedLabelMap, wantErr: false}, |
| 153 | + } |
| 154 | + for _, tt := range tests { |
| 155 | + t.Run(tt.name, func(t *testing.T) { |
| 156 | + cli := &MetricCli{ |
| 157 | + BaseExecuter: NewMetricFakeExecuter(respMap), |
| 158 | + RootExecuter: NewMetricFakeExecuter(respMap), |
| 159 | + } |
| 160 | + _, got1, err := cli.CollectMetrics(tt.args.metricList) |
| 161 | + if (err != nil) != tt.wantErr { |
| 162 | + t.Errorf("difference in expected result of MetricCli.CollectMetrics() error = %v, wantErr %v", err, tt.wantErr) |
| 163 | + return |
| 164 | + } |
| 165 | + if !reflect.DeepEqual(got1, tt.want) { |
| 166 | + t.Errorf("difference in expected result of MetricCli.CollectMetrics() got = %v, want %v", got1, tt.want) |
| 167 | + } |
| 168 | + |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func TestMetricCli_DiscoverVolumes(t *testing.T) { |
| 174 | + type fields struct { |
| 175 | + BaseExecuter exec.Executer |
| 176 | + RootExecuter exec.Executer |
| 177 | + } |
| 178 | + tests := []struct { |
| 179 | + name string |
| 180 | + fields fields |
| 181 | + want []string |
| 182 | + want1 []string |
| 183 | + wantErr bool |
| 184 | + }{ |
| 185 | + {name: "test1", fields: fields{}, want: expctedVolList, want1: expectdVgs}, |
| 186 | + } |
| 187 | + for _, tt := range tests { |
| 188 | + t.Run(tt.name, func(t *testing.T) { |
| 189 | + c := &MetricCli{ |
| 190 | + BaseExecuter: NewMetricFakeExecuter(respMap), |
| 191 | + RootExecuter: NewMetricFakeExecuter(respMap), |
| 192 | + } |
| 193 | + got, got1, err := c.DiscoverVolumes() |
| 194 | + if (err != nil) != tt.wantErr { |
| 195 | + t.Errorf("difference in expected result of MetricCli.DiscoverVolumes() error = %v, wantErr %v", err, tt.wantErr) |
| 196 | + return |
| 197 | + } |
| 198 | + if !reflect.DeepEqual(got, tt.want) { |
| 199 | + t.Errorf("difference in expected result of MetricCli.DiscoverVolumes() got = %v, want %v", got, tt.want) |
| 200 | + } |
| 201 | + if !reflect.DeepEqual(got1, tt.want1) { |
| 202 | + t.Errorf("difference in expected result of MetricCli.DiscoverVolumes() got1 = %v, want %v", got1, tt.want1) |
| 203 | + } |
| 204 | + }) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +func TestMetricCli_DiscoverDisks(t *testing.T) { |
| 209 | + type fields struct { |
| 210 | + BaseExecuter exec.Executer |
| 211 | + RootExecuter exec.Executer |
| 212 | + } |
| 213 | + tests := []struct { |
| 214 | + name string |
| 215 | + fields fields |
| 216 | + want []string |
| 217 | + wantErr bool |
| 218 | + }{ |
| 219 | + {name: "test1", fields: fields{}, want: expctedDiskList, wantErr: false}, |
| 220 | + } |
| 221 | + for _, tt := range tests { |
| 222 | + t.Run(tt.name, func(t *testing.T) { |
| 223 | + c := &MetricCli{ |
| 224 | + BaseExecuter: NewMetricFakeExecuter(respMap), |
| 225 | + RootExecuter: NewMetricFakeExecuter(respMap), |
| 226 | + } |
| 227 | + got, err := c.DiscoverDisks() |
| 228 | + if (err != nil) != tt.wantErr { |
| 229 | + t.Errorf("difference in expected result of MetricCli.DiscoverDisks() error = %v, wantErr %v", err, tt.wantErr) |
| 230 | + return |
| 231 | + } |
| 232 | + if !reflect.DeepEqual(got, tt.want) { |
| 233 | + t.Errorf("difference in expected result of MetricCli.DiscoverDisks() = %v, want %v", got, tt.want) |
| 234 | + } |
| 235 | + }) |
| 236 | + } |
| 237 | +} |
0 commit comments