Skip to content

Commit 1a118b0

Browse files
committed
Fix creation of SyntheticMonitoringCheck resources
1 parent cad9d1d commit 1a118b0

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

pkg/syntheticmonitoring/synthetic-monitoring-handler.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ func (h *SyntheticMonitoringHandler) Unprepare(resource grizzly.Resource) *grizz
6262

6363
// Prepare gets a resource ready for dispatch to the remote endpoint
6464
func (h *SyntheticMonitoringHandler) Prepare(existing *grizzly.Resource, resource grizzly.Resource) *grizzly.Resource {
65-
resource.SetSpecValue("tenantId", existing.GetSpecValue("tenantId"))
66-
resource.SetSpecValue("id", existing.GetSpecValue("id"))
65+
if existing != nil {
66+
resource.SetSpecValue("tenantId", existing.GetSpecValue("tenantId"))
67+
resource.SetSpecValue("id", existing.GetSpecValue("id"))
68+
}
69+
6770
_, exists := resource.GetSpecString("job")
6871
if !exists {
6972
resource.SetSpecString("job", resource.GetMetadata("name"))

pkg/syntheticmonitoring/synthetic-monitoring_test.go

+65-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestSyntheticMonitoring(t *testing.T) {
1414
t.Run("Check getUID is functioning correctly", func(t *testing.T) {
1515
resource := grizzly.Resource{
1616
Body: map[string]any{
17-
"metadata": map[string]interface{}{
17+
"metadata": map[string]any{
1818
"name": "test",
1919
"type": "http",
2020
},
@@ -28,6 +28,70 @@ func TestSyntheticMonitoring(t *testing.T) {
2828
})
2929
}
3030

31+
func TestSyntheticMonitoringPrepare(t *testing.T) {
32+
handler := NewSyntheticMonitoringHandler(nil)
33+
34+
t.Run("job is copied from name if not set", func(t *testing.T) {
35+
resource := grizzly.Resource{
36+
Body: map[string]any{
37+
"metadata": map[string]any{
38+
"name": "test",
39+
"type": "http",
40+
},
41+
"spec": map[string]any{},
42+
},
43+
}
44+
handler.Prepare(nil, resource)
45+
46+
require.Equal(t, "test", resource.GetSpecValue("job"))
47+
})
48+
49+
t.Run("job is left untouched if set", func(t *testing.T) {
50+
resource := grizzly.Resource{
51+
Body: map[string]any{
52+
"metadata": map[string]any{
53+
"name": "test",
54+
"type": "http",
55+
},
56+
"spec": map[string]any{
57+
"job": "foo",
58+
},
59+
},
60+
}
61+
handler.Prepare(nil, resource)
62+
63+
require.Equal(t, "foo", resource.GetSpecValue("job"))
64+
})
65+
66+
t.Run("tenantId and id are set from existing resource if available", func(t *testing.T) {
67+
existing := grizzly.Resource{
68+
Body: map[string]any{
69+
"metadata": map[string]any{
70+
"name": "test",
71+
"type": "http",
72+
},
73+
"spec": map[string]any{
74+
"id": "id",
75+
"tenantId": "tenantId",
76+
},
77+
},
78+
}
79+
resource := grizzly.Resource{
80+
Body: map[string]any{
81+
"metadata": map[string]any{
82+
"name": "test",
83+
"type": "http",
84+
},
85+
"spec": map[string]any{},
86+
},
87+
}
88+
handler.Prepare(&existing, resource)
89+
90+
require.Equal(t, "id", resource.GetSpecValue("id"))
91+
require.Equal(t, "tenantId", resource.GetSpecValue("tenantId"))
92+
})
93+
}
94+
3195
func TestSyntheticMonitoringCheckUID(t *testing.T) {
3296
testCases := []struct {
3397
name string

0 commit comments

Comments
 (0)