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 alias 21 22import ( 23 "testing" 24 25 "zabbix.com/internal/agent" 26 "zabbix.com/pkg/conf" 27 "zabbix.com/pkg/log" 28) 29 30func TestGetAlias(t *testing.T) { 31 type Result struct { 32 input, key string 33 fail bool 34 } 35 36 aliases := []string{ 37 `x:y`, 38 `a[*]:k[*]`, 39 `alias[*]:key[*]`, 40 `alias2:key`, 41 `alias3:key[*]`, 42 `alias4[*]:key`, 43 `xalias4[*]:xkey`, 44 `alias5[*]:key[a]`, 45 `alias5[ *]:key[a]`, 46 `agent.hostname:agent.ping`, 47 } 48 49 results := []Result{ 50 Result{input: `x`, key: `y`}, 51 Result{input: `a[]`, key: `k[]`}, 52 Result{input: `a[a,b]`, key: `k[a,b]`}, 53 Result{input: `alias[]`, key: `key[]`}, 54 Result{input: `alias[a]`, key: `key[a]`}, 55 Result{input: `alias[,a,]`, key: `key[,a,]`}, 56 Result{input: `alias[ a]`, key: `key[ a]`}, 57 Result{input: `alias[a,b]`, key: `key[a,b]`}, 58 Result{input: `alias2`, key: `key`}, 59 Result{input: `alias3`, key: `key[*]`}, 60 Result{input: `alias4[a]`, key: `key`}, 61 Result{input: `xalias4[a]`, key: `xkey`}, 62 Result{input: `alias5[b]`, key: `key[a]`}, 63 Result{input: `alias5[b]`, key: `key[a]`}, 64 Result{input: `alias5[123abc]`, key: `key[a]`}, 65 Result{input: `alias5[ *]`, key: `key[a]`}, 66 Result{input: `agent.hostname`, key: `agent.ping`}, 67 Result{input: `no.alias`, key: `key`, fail: true}, 68 Result{input: `no.alias`, key: `no.alias`, fail: true}, 69 Result{input: `no.alias[*]`, key: `no.alias[*]`, fail: true}, 70 Result{input: `no.alias[*]`, key: `no.alias[a,b]`, fail: true}, 71 } 72 73 _ = log.Open(log.Console, log.Debug, "", 0) 74 var options agent.AgentOptions 75 _ = conf.Unmarshal([]byte{}, &options) 76 options.Alias = aliases 77 78 if manager, err := NewManager(&options); err == nil { 79 for _, result := range results { 80 t.Run(result.input, func(t *testing.T) { 81 t.Logf("result.input: %s", result.input) 82 key := manager.Get(result.input) 83 if !result.fail { 84 if key != result.key { 85 t.Errorf("Expected key '%s' while got '%s'", result.key, key) 86 } 87 } else if key != result.input { 88 t.Errorf("Expected original key '%s' while got '%s'", result.input, key) 89 } 90 }) 91 } 92 } else { 93 t.Errorf("Cannot create new manager: %s", err) 94 } 95} 96