1/* 2** Zabbix 3** Copyright (C) 2001-2021 Zabbix SIA 4** 5** This program is free software; you can redistribute it and/or modify 6** it under the terms of the GNU General Public License as published by 7** the Free Software Foundation; either version 2 of the License, or 8** (at your option) any later version. 9** 10** This program is distributed in the hope that it will be useful, 11** but WITHOUT ANY WARRANTY; without even the implied warranty of 12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13** GNU General Public License for more details. 14** 15** You should have received a copy of the GNU General Public License 16** along with this program; if not, write to the Free Software 17** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18**/ 19 20package memcached 21 22import ( 23 "errors" 24 "reflect" 25 "testing" 26 27 "zabbix.com/pkg/zbxerr" 28 29 "github.com/memcachier/mc/v3" 30) 31 32func TestPlugin_statsHandler(t *testing.T) { 33 fakeConn := stubConn{ 34 StatsFunc: func(key string) (mc.McStats, error) { 35 switch key { 36 case statsTypeGeneral: 37 return mc.McStats{ 38 "pid": "1234", 39 "version": "1.4.15", 40 }, nil 41 42 case statsTypeSizes: 43 return mc.McStats{ 44 "96": "1", 45 }, nil 46 47 case statsTypeSettings: // generates error for tests 48 return nil, errors.New("some error") 49 50 default: 51 return nil, errors.New("unknown type") 52 } 53 }, 54 NoOpFunc: nil, 55 } 56 57 type args struct { 58 conn MCClient 59 params map[string]string 60 } 61 62 tests := []struct { 63 name string 64 args args 65 want interface{} 66 wantErr error 67 }{ 68 { 69 name: "Should return error if cannot fetch data", 70 args: args{ 71 conn: &fakeConn, 72 params: map[string]string{"Type": statsTypeSettings}, 73 }, 74 want: nil, 75 wantErr: zbxerr.ErrorCannotFetchData, 76 }, 77 { 78 name: "Type should be passed to stats command if specified", 79 args: args{ 80 conn: &fakeConn, 81 params: map[string]string{"Type": statsTypeSizes}, 82 }, 83 want: `{"96":"1"}`, 84 wantErr: nil, 85 }, 86 } 87 88 for _, tt := range tests { 89 t.Run(tt.name, func(t *testing.T) { 90 got, err := statsHandler(tt.args.conn, tt.args.params) 91 if !errors.Is(err, tt.wantErr) { 92 t.Errorf("Plugin.statsHandler() error = %v, wantErr %v", err, tt.wantErr) 93 return 94 } 95 96 if !reflect.DeepEqual(got, tt.want) { 97 t.Errorf("Plugin.statsHandler() = %v, want %v", got, tt.want) 98 } 99 }) 100 } 101} 102