Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(synth): Update default isBlocking behavior #1538

Merged
merged 2 commits into from
Dec 19, 2023
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
46 changes: 46 additions & 0 deletions internal/synthetics/command_batch_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@
return config, err
}

// Detect YAML file tests whose 'isBlocking' key is absent
indexes, err := getTestsMissingIsBlockingInConfig(content)
if err != nil {
return config, err
}

Check warning on line 109 in internal/synthetics/command_batch_run.go

View check run for this annotation

Codecov / codecov/patch

internal/synthetics/command_batch_run.go#L106-L109

Added lines #L106 - L109 were not covered by tests

// YAML file's tests with 'isBlocking' absent default to 'isBlocking: true' to match Synthetics API's default
if len(indexes) > 0 {
for _, v := range indexes {
config.Tests[v].Config.IsBlocking = true
}

Check warning on line 115 in internal/synthetics/command_batch_run.go

View check run for this annotation

Codecov / codecov/patch

internal/synthetics/command_batch_run.go#L112-L115

Added lines #L112 - L115 were not covered by tests
}

utils.LogIfFatal(err)
return config, nil
}
Expand All @@ -112,6 +125,9 @@
var tests []synthetics.SyntheticsAutomatedTestMonitorInput
for _, id := range guids {
tests = append(tests, synthetics.SyntheticsAutomatedTestMonitorInput{
Config: synthetics.SyntheticsAutomatedTestMonitorConfigInput{
IsBlocking: true, // Matches default value in Synthetics API
},
MonitorGUID: synthetics.EntityGUID(id),
})
}
Expand Down Expand Up @@ -220,3 +236,33 @@

return summaryMessage, tableData
}

// getTestsMissingIsBlockingInConfig parses monitors tests configurations YAML in order to detect
// if any of them is missing the 'isBlocking' config key
func getTestsMissingIsBlockingInConfig(data []byte) ([]int, error) {
type Data struct {
Tests []struct {
monitorGUID string `yaml:"monitorGuid"`
Config struct {
IsBlocking string `yaml:"isBlocking"`
}
} `yaml:"tests"`
}

var d Data

err := yaml.Unmarshal(data, &d)
if err != nil {
return nil, err
}

Check warning on line 257 in internal/synthetics/command_batch_run.go

View check run for this annotation

Codecov / codecov/patch

internal/synthetics/command_batch_run.go#L256-L257

Added lines #L256 - L257 were not covered by tests

c := make([]int, 0)

for i := range d.Tests {
if d.Tests[i].Config.IsBlocking == "" {
c = append(c, i)
}
}

return c, nil
}
143 changes: 143 additions & 0 deletions internal/synthetics/command_batch_run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//go:build unit
// +build unit

package synthetics

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/newrelic/newrelic-client-go/v2/pkg/synthetics"
)

func TestCreateConfigurationUsingGUIDs_ReturnsIsBlockingSetToTrueForAllMonitors(t *testing.T) {
guids := []string{
"abc",
"xyz",
}

config := createConfigurationUsingGUIDs(guids)

for i, guid := range guids {
assert.Equal(t, config.Tests[i].MonitorGUID, synthetics.EntityGUID(guid))
assert.Equal(t, config.Tests[i].Config.IsBlocking, true)
}
}

func TestGetTestsMissingIsBlockingInConfig_ShouldReturnFirstTestOnly(t *testing.T) {
var data = `
config:
batchName: test
branch: dev
commit: abc123
deepLink: https://example.com
platform: CLI
repository: repo
tests:
- monitorGuid: NDE3N
config:
- monitorGuid: ZDFOQ
config:
isBlocking: false
- monitorGuid: USHxN
config:
isBlocking: true
overrides:
location: east-1
domain:
- domain: http://example.org
override: https://example.com
`
indexes, err := getTestsMissingIsBlockingInConfig([]byte(data))

assert.Equal(t, nil, err)
assert.Equal(t, 1, len(indexes))
assert.Equal(t, 0, indexes[0])
}

func TestGetTestsMissingIsBlockingInConfig_ShouldReturnLastTestOnly(t *testing.T) {
var data = `
config:
batchName: test
branch: dev
commit: abc123
deepLink: https://example.com
platform: CLI
repository: repo
tests:
- monitorGuid: NDE3N
config:
isBlocking: true
- monitorGuid: ZDFOQ
config:
isBlocking: false
- monitorGuid: USHxN
config:
overrides:
location: east-1
domain:
- domain: http://example.org
override: https://example.com
`
indexes, err := getTestsMissingIsBlockingInConfig([]byte(data))

assert.Equal(t, nil, err)
assert.Equal(t, 1, len(indexes))
assert.Equal(t, 2, indexes[0])
}

func TestGetTestsMissingIsBlockingInConfig_ShouldReturnThreeTests(t *testing.T) {
var data = `
config:
batchName: test
branch: dev
commit: abc123
tests:
- monitorGuid: NDE3N
config:
- monitorGuid: ZDFOQ
config:
- monitorGuid: USHxN
config:
overrides:
location: east-1
domain:
- domain: http://example.com
override: https://example.com
`
indexes, err := getTestsMissingIsBlockingInConfig([]byte(data))

assert.Equal(t, nil, err)
assert.Equal(t, 3, len(indexes))
}

func TestGetTestsMissingIsBlockingInConfig_ShouldReturnNone(t *testing.T) {
var data = `
config:
batchName: test
branch: dev
commit: abc123
tests:
- monitorGuid: NDE3N
config:
isBlocking: false
overrides:
location: east-1
- monitorGuid: ZDFOQ
config:
isBlocking: false
overrides:
location: east-1
domain:
- domain: http://example.com
override: https://example.com
- monitorGuid: USHxN
config:
isBlocking: true
`
indexes, err := getTestsMissingIsBlockingInConfig([]byte(data))

assert.Equal(t, nil, err)
assert.Equal(t, 0, len(indexes))
}
Loading