Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions x-pack/heartbeat/monitors/browser/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {

var ErrNameRequired = fmt.Errorf("config 'name' must be specified for this monitor")
var ErrIdRequired = fmt.Errorf("config 'id' must be specified for this monitor")
var ErrSourceRequired = fmt.Errorf("config 'source' must be specified for this monitor, if upgrading from a previous experimental version please see our new config docs")

func (c *Config) Validate() error {
if c.Name == "" {
Expand All @@ -33,5 +34,9 @@ func (c *Config) Validate() error {
return ErrIdRequired
}

if c.Source == nil {
return ErrSourceRequired
}

return nil
}
55 changes: 55 additions & 0 deletions x-pack/heartbeat/monitors/browser/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package browser

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/source"
)

func TestConfig_Validate(t *testing.T) {
testSource := source.Source{Inline: &source.InlineSource{Script: "//something"}}

tests := []struct {
name string
cfg *Config
wantErr error
}{
{
"no error",
&Config{Id: "myid", Name: "myname", Source: &testSource},
nil,
},
{
"no id",
&Config{Name: "myname", Source: &testSource},
ErrIdRequired,
},
{
"no name",
&Config{Id: "myid", Source: &testSource},
ErrNameRequired,
},
{
"no source",
&Config{Id: "myid", Name: "myname"},
ErrSourceRequired,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.cfg.Validate()

if tt.wantErr != nil {
require.Equal(t, tt.wantErr, err)
} else {
require.NoError(t, err)
}
})
}
}
4 changes: 3 additions & 1 deletion x-pack/heartbeat/monitors/browser/source/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ type InlineSource struct {
BaseSource
}

var ErrNoInlineScript = fmt.Errorf("no 'script' value specified for inline source")

func (s *InlineSource) Validate() error {
if !regexp.MustCompile("\\S").MatchString(s.Script) {
return fmt.Errorf("no 'script' value specified for inline source")
return ErrNoInlineScript
}

return nil
Expand Down
42 changes: 42 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/inline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package source

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestInlineSourceValidation(t *testing.T) {
type testCase struct {
name string
source *InlineSource
wantErr error
}
testCases := []testCase{
{
"no error",
&InlineSource{Script: "a script"},
nil,
},
{
"no script",
&InlineSource{},
ErrNoInlineScript,
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
err := tt.source.Validate()
if tt.wantErr != nil {
require.Equal(t, tt.wantErr, err)
} else {
require.NoError(t, err)
}
})
}
}
2 changes: 1 addition & 1 deletion x-pack/heartbeat/monitors/browser/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *Source) Active() ISource {
return s.ActiveMemo
}

var ErrInvalidSource = fmt.Errorf("no or unknown source type specified for synthetic monitor")
var ErrInvalidSource = fmt.Errorf("no or unknown source type specified for synthetic monitor.")

func (s *Source) Validate() error {
if s.Active() == nil {
Expand Down
42 changes: 42 additions & 0 deletions x-pack/heartbeat/monitors/browser/source/source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package source

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSourceValidation(t *testing.T) {
type testCase struct {
name string
source Source
wantErr error
}
testCases := []testCase{
{
"no error",
Source{Inline: &InlineSource{}},
nil,
},
{
"no concrete source",
Source{},
ErrInvalidSource,
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
err := tt.source.Validate()
if tt.wantErr != nil {
require.Equal(t, tt.wantErr, err)
} else {
require.NoError(t, err)
}
})
}
}