summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/filestatus/store_test.go
blob: fbf7d339bdeaf08923922a7a88ba92a864aae9f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// SPDX-License-Identifier: GPL-3.0-or-later

package filestatus

import (
	"testing"

	"github.com/netdata/netdata/go/go.d.plugin/agent/confgroup"

	"github.com/stretchr/testify/assert"
)

// TODO: tech debt
func TestLoadStore(t *testing.T) {

}

// TODO: tech debt
func TestStore_Contains(t *testing.T) {

}

func TestStore_add(t *testing.T) {
	tests := map[string]struct {
		prepare      func() *Store
		input        confgroup.Config
		wantItemsNum int
	}{
		"add cfg to the empty store": {
			prepare: func() *Store {
				return &Store{}
			},
			input: prepareConfig(
				"module", "modName",
				"name", "jobName",
			),
			wantItemsNum: 1,
		},
		"add cfg that already in the store": {
			prepare: func() *Store {
				return &Store{
					items: map[string]map[string]string{
						"modName": {"jobName:18299273693089411682": "state"},
					},
				}
			},
			input: prepareConfig(
				"module", "modName",
				"name", "jobName",
			),
			wantItemsNum: 1,
		},
		"add cfg with same module, same name, but specific options": {
			prepare: func() *Store {
				return &Store{
					items: map[string]map[string]string{
						"modName": {"jobName:18299273693089411682": "state"},
					},
				}
			},
			input: prepareConfig(
				"module", "modName",
				"name", "jobName",
				"opt", "val",
			),
			wantItemsNum: 2,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			s := test.prepare()
			s.add(test.input, "state")
			assert.Equal(t, test.wantItemsNum, calcStoreItems(s))
		})
	}
}

func TestStore_remove(t *testing.T) {
	tests := map[string]struct {
		prepare      func() *Store
		input        confgroup.Config
		wantItemsNum int
	}{
		"remove cfg from the empty store": {
			prepare: func() *Store {
				return &Store{}
			},
			input: prepareConfig(
				"module", "modName",
				"name", "jobName",
			),
			wantItemsNum: 0,
		},
		"remove cfg from the store": {
			prepare: func() *Store {
				return &Store{
					items: map[string]map[string]string{
						"modName": {
							"jobName:18299273693089411682": "state",
							"jobName:18299273693089411683": "state",
						},
					},
				}
			},
			input: prepareConfig(
				"module", "modName",
				"name", "jobName",
			),
			wantItemsNum: 1,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			s := test.prepare()
			s.remove(test.input)
			assert.Equal(t, test.wantItemsNum, calcStoreItems(s))
		})
	}
}

func calcStoreItems(s *Store) (num int) {
	for _, v := range s.items {
		for range v {
			num++
		}
	}
	return num
}

func prepareConfig(values ...string) confgroup.Config {
	cfg := confgroup.Config{}
	for i := 1; i < len(values); i += 2 {
		cfg[values[i-1]] = values[i]
	}
	return cfg
}