| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package collectorapi |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/pkg/funcapi" |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | ) |
| 12 | |
| 13 | func TestRegister(t *testing.T) { |
| 14 | modName := "modName" |
| 15 | registry := make(Registry) |
| 16 | |
| 17 | // OK case |
| 18 | assert.NotPanics( |
| 19 | t, |
| 20 | func() { |
| 21 | registry.Register(modName, Creator{}) |
| 22 | }) |
| 23 | |
| 24 | _, exist := registry[modName] |
| 25 | |
| 26 | require.True(t, exist) |
| 27 | |
| 28 | // Panic case: duplicate registration |
| 29 | assert.Panics( |
| 30 | t, |
| 31 | func() { |
| 32 | registry.Register(modName, Creator{}) |
| 33 | }) |
| 34 | |
| 35 | } |
| 36 | |
| 37 | func TestRegister_FunctionOnlyWithoutMethods(t *testing.T) { |
| 38 | registry := make(Registry) |
| 39 | |
| 40 | // Panic case: FunctionOnly without Methods |
| 41 | assert.Panics( |
| 42 | t, |
| 43 | func() { |
| 44 | registry.Register("funcOnly", Creator{FunctionOnly: true}) |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | func TestRegisterPanicOnMethodsAndJobMethodsConflict(t *testing.T) { |
| 49 | tests := map[string]struct { |
| 50 | name string |
| 51 | creator Creator |
| 52 | }{ |
| 53 | "panic when both methods and job methods are set": { |
| 54 | name: "conflict", |
| 55 | creator: Creator{ |
| 56 | Methods: func() []funcapi.MethodConfig { |
| 57 | return nil |
| 58 | }, |
| 59 | JobMethods: func(RuntimeJob) []funcapi.MethodConfig { |
| 60 | return nil |
| 61 | }, |
| 62 | }, |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | for name, tc := range tests { |
| 67 | t.Run(name, func(t *testing.T) { |
| 68 | registry := make(Registry) |
| 69 | |
| 70 | assert.PanicsWithValue( |
| 71 | t, |
| 72 | "conflict has both Methods and JobMethods defined (mutually exclusive)", |
| 73 | func() { registry.Register(tc.name, tc.creator) }, |
| 74 | ) |
| 75 | }) |
| 76 | } |
| 77 | } |