github.com/netdata/go.d.plugin@v0.58.1/agent/filestatus/store_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package filestatus 4 5 import ( 6 "testing" 7 8 "github.com/netdata/go.d.plugin/agent/confgroup" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 // TODO: tech debt 14 func TestLoadStore(t *testing.T) { 15 16 } 17 18 // TODO: tech debt 19 func TestStore_Contains(t *testing.T) { 20 21 } 22 23 func TestStore_add(t *testing.T) { 24 tests := map[string]struct { 25 prepare func() *Store 26 input confgroup.Config 27 wantItemsNum int 28 }{ 29 "add cfg to the empty store": { 30 prepare: func() *Store { 31 return &Store{} 32 }, 33 input: prepareConfig( 34 "module", "modName", 35 "name", "jobName", 36 ), 37 wantItemsNum: 1, 38 }, 39 "add cfg that already in the store": { 40 prepare: func() *Store { 41 return &Store{ 42 items: map[string]map[string]string{ 43 "modName": {"jobName:18299273693089411682": "state"}, 44 }, 45 } 46 }, 47 input: prepareConfig( 48 "module", "modName", 49 "name", "jobName", 50 ), 51 wantItemsNum: 1, 52 }, 53 "add cfg with same module, same name, but specific options": { 54 prepare: func() *Store { 55 return &Store{ 56 items: map[string]map[string]string{ 57 "modName": {"jobName:18299273693089411682": "state"}, 58 }, 59 } 60 }, 61 input: prepareConfig( 62 "module", "modName", 63 "name", "jobName", 64 "opt", "val", 65 ), 66 wantItemsNum: 2, 67 }, 68 } 69 70 for name, test := range tests { 71 t.Run(name, func(t *testing.T) { 72 s := test.prepare() 73 s.add(test.input, "state") 74 assert.Equal(t, test.wantItemsNum, calcStoreItems(s)) 75 }) 76 } 77 } 78 79 func TestStore_remove(t *testing.T) { 80 tests := map[string]struct { 81 prepare func() *Store 82 input confgroup.Config 83 wantItemsNum int 84 }{ 85 "remove cfg from the empty store": { 86 prepare: func() *Store { 87 return &Store{} 88 }, 89 input: prepareConfig( 90 "module", "modName", 91 "name", "jobName", 92 ), 93 wantItemsNum: 0, 94 }, 95 "remove cfg from the store": { 96 prepare: func() *Store { 97 return &Store{ 98 items: map[string]map[string]string{ 99 "modName": { 100 "jobName:18299273693089411682": "state", 101 "jobName:18299273693089411683": "state", 102 }, 103 }, 104 } 105 }, 106 input: prepareConfig( 107 "module", "modName", 108 "name", "jobName", 109 ), 110 wantItemsNum: 1, 111 }, 112 } 113 114 for name, test := range tests { 115 t.Run(name, func(t *testing.T) { 116 s := test.prepare() 117 s.remove(test.input) 118 assert.Equal(t, test.wantItemsNum, calcStoreItems(s)) 119 }) 120 } 121 } 122 123 func calcStoreItems(s *Store) (num int) { 124 for _, v := range s.items { 125 for range v { 126 num++ 127 } 128 } 129 return num 130 } 131 132 func prepareConfig(values ...string) confgroup.Config { 133 cfg := confgroup.Config{} 134 for i := 1; i < len(values); i += 2 { 135 cfg[values[i-1]] = values[i] 136 } 137 return cfg 138 }