Skip to content

Commit

Permalink
feat: Validate browser capability (#809)
Browse files Browse the repository at this point in the history
* Validate browser capability

k6 functionality is required in the agent by default, and is only NOT
required in case both, scripted and browser checks, have been disabled
for the probe from the API.

* Run test in parallel
  • Loading branch information
ka3de authored Sep 2, 2024
1 parent a17f5e8 commit f94214e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 17 deletions.
7 changes: 5 additions & 2 deletions internal/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,11 @@ func (c *Updater) loop(ctx context.Context) (bool, error) {
}

func (c *Updater) validateProbeCapabilities(capabilities *sm.Probe_Capabilities) error {
// k6 is only not required on this probe if explicitly disabled
requireK6 := capabilities == nil || !capabilities.DisableScriptedChecks
// k6 is required by default unless it has been explicitly disabled from
// the API side by forbidding scripted and browser checks execution.
requireK6 := capabilities == nil ||
!capabilities.DisableScriptedChecks ||
!capabilities.DisableBrowserChecks

if requireK6 && c.k6Runner == nil {
return errCapabilityK6Missing
Expand Down
106 changes: 91 additions & 15 deletions internal/checks/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,72 @@ func TestHandleCheckOp(t *testing.T) {
}

func TestCheckHandlerProbeValidation(t *testing.T) {
t.Parallel()

testcases := map[string]struct {
opts UpdaterOptions
probe sm.Probe
expectedError error
}{
"has K6 when required": {
"has K6 when required for scripted checks": {
expectedError: nil,
opts: UpdaterOptions{
Conn: new(grpc.ClientConn),
PromRegisterer: prometheus.NewPedanticRegistry(),
Publisher: channelPublisher(make(chan pusher.Payload)),
TenantCh: make(chan<- sm.Tenant),
Logger: zerolog.Nop(),
K6Runner: noopRunner{},
},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{
DisableScriptedChecks: false,
DisableBrowserChecks: true,
}},
},
"missing K6 when required for scripted checks": {
expectedError: errCapabilityK6Missing,
opts: UpdaterOptions{
Conn: new(grpc.ClientConn),
PromRegisterer: prometheus.NewPedanticRegistry(),
Publisher: channelPublisher(make(chan pusher.Payload)),
TenantCh: make(chan<- sm.Tenant),
Logger: zerolog.Nop(),
},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{
DisableScriptedChecks: false,
DisableBrowserChecks: true,
}},
},
"has K6 when required for browser checks": {
expectedError: nil,
opts: UpdaterOptions{
Conn: new(grpc.ClientConn),
PromRegisterer: prometheus.NewPedanticRegistry(),
Publisher: channelPublisher(make(chan pusher.Payload)),
TenantCh: make(chan<- sm.Tenant),
Logger: zerolog.Nop(),
K6Runner: noopRunner{},
},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{
DisableScriptedChecks: true,
DisableBrowserChecks: false,
}},
},
"missing K6 when required for browser checks": {
expectedError: errCapabilityK6Missing,
opts: UpdaterOptions{
Conn: new(grpc.ClientConn),
PromRegisterer: prometheus.NewPedanticRegistry(),
Publisher: channelPublisher(make(chan pusher.Payload)),
TenantCh: make(chan<- sm.Tenant),
Logger: zerolog.Nop(),
},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{
DisableScriptedChecks: true,
DisableBrowserChecks: false,
}},
},
"has K6 when required for scripted and browser checks": {
expectedError: nil,
opts: UpdaterOptions{
Conn: new(grpc.ClientConn),
Expand All @@ -284,9 +344,12 @@ func TestCheckHandlerProbeValidation(t *testing.T) {
Logger: zerolog.Nop(),
K6Runner: noopRunner{},
},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{DisableScriptedChecks: false}},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{
DisableScriptedChecks: false,
DisableBrowserChecks: false,
}},
},
"missing K6 when required": {
"missing K6 when required for scripted and browser checks": {
expectedError: errCapabilityK6Missing,
opts: UpdaterOptions{
Conn: new(grpc.ClientConn),
Expand All @@ -295,7 +358,10 @@ func TestCheckHandlerProbeValidation(t *testing.T) {
TenantCh: make(chan<- sm.Tenant),
Logger: zerolog.Nop(),
},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{DisableScriptedChecks: false}},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{
DisableScriptedChecks: false,
DisableBrowserChecks: false,
}},
},
"has K6 but not required": {
expectedError: nil,
Expand All @@ -307,7 +373,10 @@ func TestCheckHandlerProbeValidation(t *testing.T) {
Logger: zerolog.Nop(),
K6Runner: noopRunner{},
},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{DisableScriptedChecks: true}},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{
DisableScriptedChecks: true,
DisableBrowserChecks: true,
}},
},
"missing K6 but not required": {
expectedError: nil,
Expand All @@ -318,7 +387,10 @@ func TestCheckHandlerProbeValidation(t *testing.T) {
TenantCh: make(chan<- sm.Tenant),
Logger: zerolog.Nop(),
},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{DisableScriptedChecks: true}},
probe: sm.Probe{Id: 100, Name: "test-probe", Capabilities: &sm.Probe_Capabilities{
DisableScriptedChecks: true,
DisableBrowserChecks: true,
}},
},
"missing K6 when required by default": {
expectedError: errCapabilityK6Missing,
Expand All @@ -345,17 +417,21 @@ func TestCheckHandlerProbeValidation(t *testing.T) {
},
}

for _, tc := range testcases {
u, err := NewUpdater(tc.opts)
require.NoError(t, err)

err = u.validateProbeCapabilities(tc.probe.Capabilities)
for testName, tc := range testcases {
t.Run(testName, func(t *testing.T) {
t.Parallel()

if tc.expectedError != nil {
require.Error(t, err, tc.expectedError)
} else {
u, err := NewUpdater(tc.opts)
require.NoError(t, err)
}

err = u.validateProbeCapabilities(tc.probe.Capabilities)

if tc.expectedError != nil {
require.Error(t, err, tc.expectedError)
} else {
require.NoError(t, err)
}
})
}
}

Expand Down

0 comments on commit f94214e

Please sign in to comment.