| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package funcctl |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/funcapi" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 14 | "github.com/netdata/netdata/go/plugins/plugin/framework/functions" |
| 15 | ) |
| 16 | |
| 17 | func newTestControllerWithCapture(t *testing.T) (*Controller, *map[string]any) { |
| 18 | t.Helper() |
| 19 | |
| 20 | var resp map[string]any |
| 21 | controller := New(Options{ |
| 22 | JSONWriter: func(payload []byte, _ int) { |
| 23 | require.NoError(t, json.Unmarshal(payload, &resp)) |
| 24 | }, |
| 25 | }) |
| 26 | |
| 27 | return controller, &resp |
| 28 | } |
| 29 | |
| 30 | func TestRespondWithParams_ResponseType(t *testing.T) { |
| 31 | controller, resp := newTestControllerWithCapture(t) |
| 32 | |
| 33 | dataResp := &funcapi.FunctionResponse{ |
| 34 | Status: 200, |
| 35 | ResponseType: "topology", |
| 36 | } |
| 37 | |
| 38 | controller.respondWithParams(functions.Function{}, "snmp", dataResp, nil, 1, "", true) |
| 39 | |
| 40 | assert.Equal(t, "topology", (*resp)["type"]) |
| 41 | } |
| 42 | |
| 43 | func TestRespondWithParams_MethodTypeFallback(t *testing.T) { |
| 44 | controller, resp := newTestControllerWithCapture(t) |
| 45 | |
| 46 | dataResp := &funcapi.FunctionResponse{ |
| 47 | Status: 200, |
| 48 | } |
| 49 | |
| 50 | controller.respondWithParams(functions.Function{}, "snmp", dataResp, nil, 1, "topology", true) |
| 51 | |
| 52 | assert.Equal(t, "topology", (*resp)["type"]) |
| 53 | } |
| 54 | |
| 55 | func TestRespondWithParams_AgentWideOmitsJobParam(t *testing.T) { |
| 56 | controller, resp := newTestControllerWithCapture(t) |
| 57 | |
| 58 | dataResp := &funcapi.FunctionResponse{ |
| 59 | Status: 200, |
| 60 | RequiredParams: []funcapi.ParamConfig{{ |
| 61 | ID: "topology_view", |
| 62 | Name: "Topology View", |
| 63 | Selection: funcapi.ParamSelect, |
| 64 | Options: []funcapi.ParamOption{ |
| 65 | {ID: "l2", Name: "L2", Default: true}, |
| 66 | }, |
| 67 | }}, |
| 68 | } |
| 69 | |
| 70 | controller.respondWithParams(functions.Function{}, "snmp", dataResp, nil, 1, "topology", false) |
| 71 | |
| 72 | accepted, ok := (*resp)["accepted_params"].([]any) |
| 73 | assert.True(t, ok) |
| 74 | assert.Equal(t, []any{"topology_view"}, accepted) |
| 75 | |
| 76 | required, ok := (*resp)["required_params"].([]any) |
| 77 | assert.True(t, ok) |
| 78 | assert.Len(t, required, 1) |
| 79 | req0, ok := required[0].(map[string]any) |
| 80 | assert.True(t, ok) |
| 81 | assert.Equal(t, "topology_view", req0["id"]) |
| 82 | } |
| 83 | |
| 84 | func TestHandleMethodFuncInfo_UsesResponseType(t *testing.T) { |
| 85 | controller, resp := newTestControllerWithCapture(t) |
| 86 | |
| 87 | controller.registry.registerModule("snmp", collectorapi.Creator{ |
| 88 | Methods: func() []funcapi.MethodConfig { |
| 89 | return []funcapi.MethodConfig{{ID: "topology:snmp", ResponseType: "topology"}} |
| 90 | }, |
| 91 | }) |
| 92 | |
| 93 | controller.handleMethodFuncInfo("snmp", "topology:snmp", functions.Function{}) |
| 94 | |
| 95 | assert.Equal(t, "topology", (*resp)["type"]) |
| 96 | } |
| 97 | |
| 98 | func TestHandleMethodFuncInfo_AgentWideOmitsJobParam(t *testing.T) { |
| 99 | controller, resp := newTestControllerWithCapture(t) |
| 100 | |
| 101 | controller.registry.registerModule("snmp", collectorapi.Creator{ |
| 102 | Methods: func() []funcapi.MethodConfig { |
| 103 | return []funcapi.MethodConfig{{ |
| 104 | ID: "topology:snmp", |
| 105 | ResponseType: "topology", |
| 106 | AgentWide: true, |
| 107 | RequiredParams: []funcapi.ParamConfig{{ |
| 108 | ID: "topology_view", |
| 109 | Name: "Topology View", |
| 110 | Selection: funcapi.ParamSelect, |
| 111 | Options: []funcapi.ParamOption{ |
| 112 | {ID: "l2", Name: "L2", Default: true}, |
| 113 | }, |
| 114 | }}, |
| 115 | }} |
| 116 | }, |
| 117 | }) |
| 118 | controller.registry.addJob("snmp", "job-a", newTestRuntimeJob("snmp", "job-a", true)) |
| 119 | |
| 120 | controller.handleMethodFuncInfo("snmp", "topology:snmp", functions.Function{}) |
| 121 | |
| 122 | accepted, ok := (*resp)["accepted_params"].([]any) |
| 123 | assert.True(t, ok) |
| 124 | assert.Equal(t, []any{"topology_view"}, accepted) |
| 125 | |
| 126 | required, ok := (*resp)["required_params"].([]any) |
| 127 | assert.True(t, ok) |
| 128 | assert.Len(t, required, 1) |
| 129 | req0, ok := required[0].(map[string]any) |
| 130 | assert.True(t, ok) |
| 131 | assert.Equal(t, "topology_view", req0["id"]) |
| 132 | } |
| 133 | |
| 134 | func TestHandleJobMethodFuncInfo_UsesResponseType(t *testing.T) { |
| 135 | controller, resp := newTestControllerWithCapture(t) |
| 136 | |
| 137 | controller.registry.registerModule("netflow", collectorapi.Creator{}) |
| 138 | controller.registry.registerJobMethods("netflow", "job1", []funcapi.MethodConfig{ |
| 139 | {ID: "flows:netflow", ResponseType: "flows"}, |
| 140 | }) |
| 141 | |
| 142 | controller.handleJobMethodFuncInfo("netflow", "job1", "flows:netflow", functions.Function{}) |
| 143 | |
| 144 | assert.Equal(t, "flows", (*resp)["type"]) |
| 145 | } |
| 146 | |
| 147 | func TestHandleMethodFuncInfo_IncludesPresentation(t *testing.T) { |
| 148 | controller, resp := newTestControllerWithCapture(t) |
| 149 | |
| 150 | controller.registry.registerModule("snmp", collectorapi.Creator{ |
| 151 | Methods: func() []funcapi.MethodConfig { |
| 152 | return []funcapi.MethodConfig{ |
| 153 | funcapi.MethodConfig{ |
| 154 | ID: "topology:snmp", |
| 155 | ResponseType: "topology", |
| 156 | }.WithPresentation(map[string]any{ |
| 157 | "actor_click_behavior": "highlight_connections", |
| 158 | }), |
| 159 | } |
| 160 | }, |
| 161 | }) |
| 162 | |
| 163 | controller.handleMethodFuncInfo("snmp", "topology:snmp", functions.Function{}) |
| 164 | |
| 165 | presentation, ok := (*resp)["presentation"].(map[string]any) |
| 166 | require.True(t, ok) |
| 167 | assert.Equal(t, "highlight_connections", presentation["actor_click_behavior"]) |
| 168 | } |