| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package jobmgr |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/jobruntime" |
| 10 | "github.com/netdata/netdata/go/plugins/plugin/framework/vnodes" |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/funcapi" |
| 15 | "github.com/netdata/netdata/go/plugins/pkg/metrix" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 17 | ) |
| 18 | |
| 19 | type testV1Module struct { |
| 20 | collectorapi.Base |
| 21 | } |
| 22 | |
| 23 | func (m *testV1Module) Configuration() any { return nil } |
| 24 | func (m *testV1Module) Init(context.Context) error { return nil } |
| 25 | func (m *testV1Module) Check(context.Context) error { return nil } |
| 26 | func (m *testV1Module) Collect(context.Context) map[string]int64 { return map[string]int64{"value": 1} } |
| 27 | func (m *testV1Module) Charts() *collectorapi.Charts { return &collectorapi.Charts{} } |
| 28 | func (m *testV1Module) Cleanup(context.Context) {} |
| 29 | func (m *testV1Module) VirtualNode() *vnodes.VirtualNode { return nil } |
| 30 | |
| 31 | type testV2Module struct { |
| 32 | collectorapi.Base |
| 33 | store metrix.CollectorStore |
| 34 | } |
| 35 | |
| 36 | func (m *testV2Module) Configuration() any { return nil } |
| 37 | func (m *testV2Module) Init(context.Context) error { return nil } |
| 38 | func (m *testV2Module) Check(context.Context) error { return nil } |
| 39 | func (m *testV2Module) Collect(context.Context) error { return nil } |
| 40 | func (m *testV2Module) Cleanup(context.Context) {} |
| 41 | func (m *testV2Module) VirtualNode() *vnodes.VirtualNode { return nil } |
| 42 | func (m *testV2Module) MetricStore() metrix.CollectorStore { return m.store } |
| 43 | func (m *testV2Module) ChartTemplateYAML() string { |
| 44 | return ` |
| 45 | version: "1" |
| 46 | groups: |
| 47 | - family: "test" |
| 48 | metrics: ["test.value"] |
| 49 | charts: |
| 50 | - context: "value" |
| 51 | dimensions: |
| 52 | - selector: 'test.value' |
| 53 | name: "value" |
| 54 | ` |
| 55 | } |
| 56 | |
| 57 | func TestManagerCreateCollectorJobV2Branching(t *testing.T) { |
| 58 | tests := map[string]struct { |
| 59 | creator collectorapi.Creator |
| 60 | functionOnly bool |
| 61 | wantV2 bool |
| 62 | wantErr string |
| 63 | }{ |
| 64 | "prefer v2 when hooks do not require legacy runtime": { |
| 65 | creator: collectorapi.Creator{ |
| 66 | Create: func() collectorapi.CollectorV1 { return &testV1Module{} }, |
| 67 | CreateV2: func() collectorapi.CollectorV2 { |
| 68 | return &testV2Module{store: metrix.NewCollectorStore()} |
| 69 | }, |
| 70 | }, |
| 71 | wantV2: true, |
| 72 | }, |
| 73 | "prefer v2 when job methods are configured": { |
| 74 | creator: collectorapi.Creator{ |
| 75 | Create: func() collectorapi.CollectorV1 { return &testV1Module{} }, |
| 76 | CreateV2: func() collectorapi.CollectorV2 { |
| 77 | return &testV2Module{store: metrix.NewCollectorStore()} |
| 78 | }, |
| 79 | JobMethods: func(_ collectorapi.RuntimeJob) []funcapi.MethodConfig { return nil }, |
| 80 | }, |
| 81 | wantV2: true, |
| 82 | }, |
| 83 | "allow v2 only creator when job methods are configured": { |
| 84 | creator: collectorapi.Creator{ |
| 85 | CreateV2: func() collectorapi.CollectorV2 { |
| 86 | return &testV2Module{store: metrix.NewCollectorStore()} |
| 87 | }, |
| 88 | JobMethods: func(_ collectorapi.RuntimeJob) []funcapi.MethodConfig { return nil }, |
| 89 | }, |
| 90 | wantV2: true, |
| 91 | }, |
| 92 | "allow function_only config for v2 when methods exist": { |
| 93 | creator: collectorapi.Creator{ |
| 94 | CreateV2: func() collectorapi.CollectorV2 { |
| 95 | return &testV2Module{store: nil} |
| 96 | }, |
| 97 | JobMethods: func(_ collectorapi.RuntimeJob) []funcapi.MethodConfig { return nil }, |
| 98 | }, |
| 99 | functionOnly: true, |
| 100 | wantV2: true, |
| 101 | }, |
| 102 | } |
| 103 | |
| 104 | for name, tc := range tests { |
| 105 | t.Run(name, func(t *testing.T) { |
| 106 | mgr := New(Config{PluginName: testPluginName}) |
| 107 | mgr.modules = collectorapi.Registry{ |
| 108 | "testmod": tc.creator, |
| 109 | } |
| 110 | cfg := prepareUserCfg("testmod", "job1") |
| 111 | if tc.functionOnly { |
| 112 | cfg.Set("function_only", true) |
| 113 | } |
| 114 | |
| 115 | job, err := mgr.createCollectorJob(cfg) |
| 116 | if tc.wantErr != "" { |
| 117 | require.Error(t, err) |
| 118 | assert.Contains(t, err.Error(), tc.wantErr) |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | require.NoError(t, err) |
| 123 | _, isV2 := job.(*jobruntime.JobV2) |
| 124 | assert.Equal(t, tc.wantV2, isV2) |
| 125 | }) |
| 126 | } |
| 127 | } |