Skip to content

Commit

Permalink
Fix creation of SyntheticMonitoringCheck resources
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Nov 8, 2024
1 parent cad9d1d commit 1a118b0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
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

0 comments on commit 1a118b0

Please sign in to comment.