| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package filecheck |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest" |
| 12 | |
| 13 | "github.com/stretchr/testify/assert" |
| 14 | "github.com/stretchr/testify/require" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | dataConfigJSON, _ = os.ReadFile("testdata/config.json") |
| 19 | dataConfigYAML, _ = os.ReadFile("testdata/config.yaml") |
| 20 | ) |
| 21 | |
| 22 | func Test_testDataIsValid(t *testing.T) { |
| 23 | for name, data := range map[string][]byte{ |
| 24 | "dataConfigJSON": dataConfigJSON, |
| 25 | "dataConfigYAML": dataConfigYAML, |
| 26 | } { |
| 27 | require.NotNil(t, data, name) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 32 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 33 | } |
| 34 | |
| 35 | func TestCollector_Cleanup(t *testing.T) { |
| 36 | assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) |
| 37 | } |
| 38 | |
| 39 | func TestCollector_Init(t *testing.T) { |
| 40 | tests := map[string]struct { |
| 41 | config Config |
| 42 | wantFail bool |
| 43 | }{ |
| 44 | "default": { |
| 45 | wantFail: true, |
| 46 | config: New().Config, |
| 47 | }, |
| 48 | "empty files->include and dirs->include": { |
| 49 | wantFail: true, |
| 50 | config: Config{ |
| 51 | Files: filesConfig{}, |
| 52 | Dirs: dirsConfig{}, |
| 53 | }, |
| 54 | }, |
| 55 | "files->include and dirs->include": { |
| 56 | wantFail: false, |
| 57 | config: Config{ |
| 58 | Files: filesConfig{ |
| 59 | Include: []string{ |
| 60 | "/path/to/file1", |
| 61 | "/path/to/file2", |
| 62 | }, |
| 63 | }, |
| 64 | Dirs: dirsConfig{ |
| 65 | Include: []string{ |
| 66 | "/path/to/dir1", |
| 67 | "/path/to/dir2", |
| 68 | }, |
| 69 | CollectDirSize: true, |
| 70 | }, |
| 71 | }, |
| 72 | }, |
| 73 | "only files->include": { |
| 74 | wantFail: false, |
| 75 | config: Config{ |
| 76 | Files: filesConfig{ |
| 77 | Include: []string{ |
| 78 | "/path/to/file1", |
| 79 | "/path/to/file2", |
| 80 | }, |
| 81 | }, |
| 82 | }, |
| 83 | }, |
| 84 | "only dirs->include": { |
| 85 | wantFail: false, |
| 86 | config: Config{ |
| 87 | Dirs: dirsConfig{ |
| 88 | Include: []string{ |
| 89 | "/path/to/dir1", |
| 90 | "/path/to/dir2", |
| 91 | }, |
| 92 | CollectDirSize: true, |
| 93 | }, |
| 94 | }, |
| 95 | }, |
| 96 | } |
| 97 | |
| 98 | for name, test := range tests { |
| 99 | t.Run(name, func(t *testing.T) { |
| 100 | collr := New() |
| 101 | collr.Config = test.config |
| 102 | |
| 103 | if test.wantFail { |
| 104 | assert.Error(t, collr.Init(context.Background())) |
| 105 | } else { |
| 106 | require.NoError(t, collr.Init(context.Background())) |
| 107 | } |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestCollector_Check(t *testing.T) { |
| 113 | tests := map[string]struct { |
| 114 | prepare func() *Collector |
| 115 | }{ |
| 116 | "collect files": {prepare: prepareFilecheckFiles}, |
| 117 | "collect files filepath pattern": {prepare: prepareFilecheckGlobFiles}, |
| 118 | "collect only non existent files": {prepare: prepareFilecheckNonExistentFiles}, |
| 119 | "collect dirs": {prepare: prepareFilecheckDirs}, |
| 120 | "collect dirs filepath pattern": {prepare: prepareFilecheckGlobDirs}, |
| 121 | "collect only non existent dirs": {prepare: prepareFilecheckNonExistentDirs}, |
| 122 | "collect files and dirs": {prepare: prepareFilecheckFilesDirs}, |
| 123 | } |
| 124 | |
| 125 | for name, test := range tests { |
| 126 | t.Run(name, func(t *testing.T) { |
| 127 | collr := test.prepare() |
| 128 | require.NoError(t, collr.Init(context.Background())) |
| 129 | |
| 130 | assert.NoError(t, collr.Check(context.Background())) |
| 131 | }) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestCollector_Collect(t *testing.T) { |
| 136 | // TODO: should use TEMP dir and create files/dirs dynamically during a test case |
| 137 | tests := map[string]struct { |
| 138 | prepare func() *Collector |
| 139 | wantCollected map[string]int64 |
| 140 | }{ |
| 141 | "collect files": { |
| 142 | prepare: prepareFilecheckFiles, |
| 143 | wantCollected: map[string]int64{ |
| 144 | "file_testdata/empty_file.log_existence_status_exist": 1, |
| 145 | "file_testdata/empty_file.log_existence_status_not_exist": 0, |
| 146 | "file_testdata/empty_file.log_mtime_ago": 517996, |
| 147 | "file_testdata/empty_file.log_size_bytes": 0, |
| 148 | "file_testdata/file.log_existence_status_exist": 1, |
| 149 | "file_testdata/file.log_existence_status_not_exist": 0, |
| 150 | "file_testdata/file.log_mtime_ago": 517996, |
| 151 | "file_testdata/file.log_size_bytes": 5707, |
| 152 | "file_testdata/non_existent_file.log_existence_status_exist": 0, |
| 153 | "file_testdata/non_existent_file.log_existence_status_not_exist": 1, |
| 154 | }, |
| 155 | }, |
| 156 | "collect files filepath pattern": { |
| 157 | prepare: prepareFilecheckGlobFiles, |
| 158 | wantCollected: map[string]int64{ |
| 159 | "file_testdata/empty_file.log_existence_status_exist": 1, |
| 160 | "file_testdata/empty_file.log_existence_status_not_exist": 0, |
| 161 | "file_testdata/empty_file.log_mtime_ago": 517985, |
| 162 | "file_testdata/empty_file.log_size_bytes": 0, |
| 163 | "file_testdata/file.log_existence_status_exist": 1, |
| 164 | "file_testdata/file.log_existence_status_not_exist": 0, |
| 165 | "file_testdata/file.log_mtime_ago": 517985, |
| 166 | "file_testdata/file.log_size_bytes": 5707, |
| 167 | }, |
| 168 | }, |
| 169 | "collect only non existent files": { |
| 170 | prepare: prepareFilecheckNonExistentFiles, |
| 171 | wantCollected: map[string]int64{ |
| 172 | "file_testdata/non_existent_file.log_existence_status_exist": 0, |
| 173 | "file_testdata/non_existent_file.log_existence_status_not_exist": 1, |
| 174 | }, |
| 175 | }, |
| 176 | "collect dirs": { |
| 177 | prepare: prepareFilecheckDirs, |
| 178 | wantCollected: map[string]int64{ |
| 179 | "dir_testdata/dir_existence_status_exist": 1, |
| 180 | "dir_testdata/dir_existence_status_not_exist": 0, |
| 181 | "dir_testdata/dir_files_count": 3, |
| 182 | "dir_testdata/dir_mtime_ago": 517914, |
| 183 | "dir_testdata/non_existent_dir_existence_status_exist": 0, |
| 184 | "dir_testdata/non_existent_dir_existence_status_not_exist": 1, |
| 185 | }, |
| 186 | }, |
| 187 | "collect dirs filepath pattern": { |
| 188 | prepare: prepareFilecheckGlobDirs, |
| 189 | wantCollected: map[string]int64{ |
| 190 | "dir_testdata/dir_existence_status_exist": 1, |
| 191 | "dir_testdata/dir_existence_status_not_exist": 0, |
| 192 | "dir_testdata/dir_files_count": 3, |
| 193 | "dir_testdata/dir_mtime_ago": 517902, |
| 194 | "dir_testdata/non_existent_dir_existence_status_exist": 0, |
| 195 | "dir_testdata/non_existent_dir_existence_status_not_exist": 1, |
| 196 | }, |
| 197 | }, |
| 198 | "collect dirs w/o size": { |
| 199 | prepare: prepareFilecheckDirsWithoutSize, |
| 200 | wantCollected: map[string]int64{ |
| 201 | "dir_testdata/dir_existence_status_exist": 1, |
| 202 | "dir_testdata/dir_existence_status_not_exist": 0, |
| 203 | "dir_testdata/dir_files_count": 3, |
| 204 | "dir_testdata/dir_mtime_ago": 517892, |
| 205 | "dir_testdata/non_existent_dir_existence_status_exist": 0, |
| 206 | "dir_testdata/non_existent_dir_existence_status_not_exist": 1, |
| 207 | }, |
| 208 | }, |
| 209 | "collect only non existent dirs": { |
| 210 | prepare: prepareFilecheckNonExistentDirs, |
| 211 | wantCollected: map[string]int64{ |
| 212 | "dir_testdata/non_existent_dir_existence_status_exist": 0, |
| 213 | "dir_testdata/non_existent_dir_existence_status_not_exist": 1, |
| 214 | }, |
| 215 | }, |
| 216 | "collect files and dirs": { |
| 217 | prepare: prepareFilecheckFilesDirs, |
| 218 | wantCollected: map[string]int64{ |
| 219 | "dir_testdata/dir_existence_status_exist": 1, |
| 220 | "dir_testdata/dir_existence_status_not_exist": 0, |
| 221 | "dir_testdata/dir_files_count": 3, |
| 222 | "dir_testdata/dir_mtime_ago": 517858, |
| 223 | "dir_testdata/dir_size_bytes": 8160, |
| 224 | "dir_testdata/non_existent_dir_existence_status_exist": 0, |
| 225 | "dir_testdata/non_existent_dir_existence_status_not_exist": 1, |
| 226 | "file_testdata/empty_file.log_existence_status_exist": 1, |
| 227 | "file_testdata/empty_file.log_existence_status_not_exist": 0, |
| 228 | "file_testdata/empty_file.log_mtime_ago": 517858, |
| 229 | "file_testdata/empty_file.log_size_bytes": 0, |
| 230 | "file_testdata/file.log_existence_status_exist": 1, |
| 231 | "file_testdata/file.log_existence_status_not_exist": 0, |
| 232 | "file_testdata/file.log_mtime_ago": 517858, |
| 233 | "file_testdata/file.log_size_bytes": 5707, |
| 234 | "file_testdata/non_existent_file.log_existence_status_exist": 0, |
| 235 | "file_testdata/non_existent_file.log_existence_status_not_exist": 1, |
| 236 | }, |
| 237 | }, |
| 238 | } |
| 239 | |
| 240 | for name, test := range tests { |
| 241 | t.Run(name, func(t *testing.T) { |
| 242 | collr := test.prepare() |
| 243 | require.NoError(t, collr.Init(context.Background())) |
| 244 | |
| 245 | mx := collr.Collect(context.Background()) |
| 246 | |
| 247 | copyModTime(test.wantCollected, mx) |
| 248 | |
| 249 | assert.Equal(t, test.wantCollected, mx) |
| 250 | |
| 251 | collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) |
| 252 | }) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | func prepareFilecheckFiles() *Collector { |
| 257 | collr := New() |
| 258 | collr.Config.Files.Include = []string{ |
| 259 | "testdata/empty_file.log", |
| 260 | "testdata/file.log", |
| 261 | "testdata/non_existent_file.log", |
| 262 | } |
| 263 | return collr |
| 264 | } |
| 265 | |
| 266 | func prepareFilecheckGlobFiles() *Collector { |
| 267 | collr := New() |
| 268 | collr.Config.Files.Include = []string{ |
| 269 | "testdata/*.log", |
| 270 | } |
| 271 | return collr |
| 272 | } |
| 273 | |
| 274 | func prepareFilecheckNonExistentFiles() *Collector { |
| 275 | collr := New() |
| 276 | collr.Config.Files.Include = []string{ |
| 277 | "testdata/non_existent_file.log", |
| 278 | } |
| 279 | return collr |
| 280 | } |
| 281 | |
| 282 | func prepareFilecheckDirs() *Collector { |
| 283 | collr := New() |
| 284 | collr.Config.Dirs.Include = []string{ |
| 285 | "testdata/dir", |
| 286 | "testdata/non_existent_dir", |
| 287 | } |
| 288 | return collr |
| 289 | } |
| 290 | |
| 291 | func prepareFilecheckGlobDirs() *Collector { |
| 292 | collr := New() |
| 293 | collr.Config.Dirs.Include = []string{ |
| 294 | "testdata/*ir", |
| 295 | "testdata/non_existent_dir", |
| 296 | } |
| 297 | return collr |
| 298 | } |
| 299 | |
| 300 | func prepareFilecheckDirsWithoutSize() *Collector { |
| 301 | collr := New() |
| 302 | collr.Config.Dirs.Include = []string{ |
| 303 | "testdata/dir", |
| 304 | "testdata/non_existent_dir", |
| 305 | } |
| 306 | return collr |
| 307 | } |
| 308 | |
| 309 | func prepareFilecheckNonExistentDirs() *Collector { |
| 310 | collr := New() |
| 311 | collr.Config.Dirs.Include = []string{ |
| 312 | "testdata/non_existent_dir", |
| 313 | } |
| 314 | return collr |
| 315 | } |
| 316 | |
| 317 | func prepareFilecheckFilesDirs() *Collector { |
| 318 | collr := New() |
| 319 | collr.Config.Dirs.CollectDirSize = true |
| 320 | collr.Config.Files.Include = []string{ |
| 321 | "testdata/empty_file.log", |
| 322 | "testdata/file.log", |
| 323 | "testdata/non_existent_file.log", |
| 324 | } |
| 325 | collr.Config.Dirs.Include = []string{ |
| 326 | "testdata/dir", |
| 327 | "testdata/non_existent_dir", |
| 328 | } |
| 329 | return collr |
| 330 | } |
| 331 | |
| 332 | func copyModTime(dst, src map[string]int64) { |
| 333 | if src == nil || dst == nil { |
| 334 | return |
| 335 | } |
| 336 | for key := range src { |
| 337 | if strings.Contains(key, "mtime") { |
| 338 | dst[key] = src[key] |
| 339 | } |
| 340 | } |
| 341 | } |