| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nagios |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | ) |
| 12 | |
| 13 | func TestJobConfigSetDefaults(t *testing.T) { |
| 14 | cfg := JobConfig{Name: "sample", Plugin: "/usr/lib/nagios/plugins/check_ping"} |
| 15 | cfg.setDefaults() |
| 16 | |
| 17 | assert.Equal(t, confopt.Duration(5*time.Second), cfg.Timeout) |
| 18 | assert.NotZero(t, cfg.CheckInterval) |
| 19 | assert.NotZero(t, cfg.RetryInterval) |
| 20 | assert.NotZero(t, cfg.MaxCheckAttempts) |
| 21 | assert.NotEmpty(t, cfg.CheckPeriod) |
| 22 | } |
| 23 | |
| 24 | func TestJobConfigValidate(t *testing.T) { |
| 25 | tests := map[string]struct { |
| 26 | cfg JobConfig |
| 27 | wantErr bool |
| 28 | }{ |
| 29 | "valid": { |
| 30 | cfg: JobConfig{Name: "sample", Plugin: "/bin/true"}, |
| 31 | }, |
| 32 | "arg_values over limit": { |
| 33 | cfg: func() JobConfig { |
| 34 | cfg := JobConfig{Name: "sample", Plugin: "/bin/true"} |
| 35 | for range maxArgMacros + 1 { |
| 36 | cfg.ArgValues = append(cfg.ArgValues, "value") |
| 37 | } |
| 38 | return cfg |
| 39 | }(), |
| 40 | wantErr: true, |
| 41 | }, |
| 42 | "relative plugin path": { |
| 43 | cfg: JobConfig{Name: "sample", Plugin: "check_ping"}, |
| 44 | wantErr: true, |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | for name, tc := range tests { |
| 49 | t.Run(name, func(t *testing.T) { |
| 50 | tc.cfg.setDefaults() |
| 51 | |
| 52 | if tc.wantErr { |
| 53 | assert.Error(t, tc.cfg.validate()) |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | assert.NoError(t, tc.cfg.validate()) |
| 58 | }) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestJobConfigNormalizedCheckName(t *testing.T) { |
| 63 | tests := map[string]struct { |
| 64 | cfg JobConfig |
| 65 | wantCheckName string |
| 66 | }{ |
| 67 | "derives from plugin basename when omitted": { |
| 68 | cfg: JobConfig{Name: "sample", Plugin: "/usr/lib/nagios/plugins/check_ping.pl"}, |
| 69 | wantCheckName: "check_ping", |
| 70 | }, |
| 71 | "sanitizes explicit check name": { |
| 72 | cfg: JobConfig{Name: "sample", Plugin: "/bin/bash", CheckName: "Database Health Check"}, |
| 73 | wantCheckName: "database_health_check", |
| 74 | }, |
| 75 | "explicit check name does not strip suffixes as file extensions": { |
| 76 | cfg: JobConfig{Name: "sample", Plugin: "/bin/bash", CheckName: "check_service.ps1"}, |
| 77 | wantCheckName: "check_service_ps1", |
| 78 | }, |
| 79 | } |
| 80 | |
| 81 | for name, tc := range tests { |
| 82 | t.Run(name, func(t *testing.T) { |
| 83 | got, err := tc.cfg.normalized() |
| 84 | assert.NoError(t, err) |
| 85 | assert.Equal(t, tc.wantCheckName, got.CheckName) |
| 86 | }) |
| 87 | } |
| 88 | } |