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

Fix creation of SyntheticMonitoringCheck resources #520

Merged
merged 1 commit into from
Nov 8, 2024
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
7 changes: 5 additions & 2 deletions pkg/syntheticmonitoring/synthetic-monitoring-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ func (h *SyntheticMonitoringHandler) Unprepare(resource grizzly.Resource) *grizz

// Prepare gets a resource ready for dispatch to the remote endpoint
func (h *SyntheticMonitoringHandler) Prepare(existing *grizzly.Resource, resource grizzly.Resource) *grizzly.Resource {
resource.SetSpecValue("tenantId", existing.GetSpecValue("tenantId"))
resource.SetSpecValue("id", existing.GetSpecValue("id"))
if existing != nil {
resource.SetSpecValue("tenantId", existing.GetSpecValue("tenantId"))
resource.SetSpecValue("id", existing.GetSpecValue("id"))
}

_, exists := resource.GetSpecString("job")
if !exists {
resource.SetSpecString("job", resource.GetMetadata("name"))
Expand Down
66 changes: 65 additions & 1 deletion pkg/syntheticmonitoring/synthetic-monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestSyntheticMonitoring(t *testing.T) {
t.Run("Check getUID is functioning correctly", func(t *testing.T) {
resource := grizzly.Resource{
Body: map[string]any{
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "test",
"type": "http",
},
Expand All @@ -28,6 +28,70 @@ func TestSyntheticMonitoring(t *testing.T) {
})
}

func TestSyntheticMonitoringPrepare(t *testing.T) {
handler := NewSyntheticMonitoringHandler(nil)

t.Run("job is copied from name if not set", func(t *testing.T) {
resource := grizzly.Resource{
Body: map[string]any{
"metadata": map[string]any{
"name": "test",
"type": "http",
},
"spec": map[string]any{},
},
}
handler.Prepare(nil, resource)

require.Equal(t, "test", resource.GetSpecValue("job"))
})

t.Run("job is left untouched if set", func(t *testing.T) {
resource := grizzly.Resource{
Body: map[string]any{
"metadata": map[string]any{
"name": "test",
"type": "http",
},
"spec": map[string]any{
"job": "foo",
},
},
}
handler.Prepare(nil, resource)

require.Equal(t, "foo", resource.GetSpecValue("job"))
})

t.Run("tenantId and id are set from existing resource if available", func(t *testing.T) {
existing := grizzly.Resource{
Body: map[string]any{
"metadata": map[string]any{
"name": "test",
"type": "http",
},
"spec": map[string]any{
"id": "id",
"tenantId": "tenantId",
},
},
}
resource := grizzly.Resource{
Body: map[string]any{
"metadata": map[string]any{
"name": "test",
"type": "http",
},
"spec": map[string]any{},
},
}
handler.Prepare(&existing, resource)

require.Equal(t, "id", resource.GetSpecValue("id"))
require.Equal(t, "tenantId", resource.GetSpecValue("tenantId"))
})
}

func TestSyntheticMonitoringCheckUID(t *testing.T) {
testCases := []struct {
name string
Expand Down
Loading