Skip to content

Commit

Permalink
x-pack/filebeat/input/cel: relax integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
efd6 committed Jul 18, 2024
1 parent 20897c0 commit be65f66
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions x-pack/filebeat/input/cel/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -284,7 +284,7 @@ func TestCheckinV2(t *testing.T) {
return false, allStreams
}

if !reflect.DeepEqual(map[string]interface{}{
if !similarMaps(map[string]interface{}{
"streams": map[string]interface{}{
"cel-cel.cel-1e8b33de-d54a-45cd-90da-23ed71c482e2": map[string]interface{}{
"status": "HEALTHY",
Expand All @@ -310,11 +310,11 @@ func TestCheckinV2(t *testing.T) {
return false, allStreams
}

if !reflect.DeepEqual(map[string]interface{}{
if !similarMaps(map[string]interface{}{
"streams": map[string]interface{}{
"cel-cel.cel-1e8b33de-d54a-45cd-90da-23ed71c482e2": map[string]interface{}{
"status": "DEGRADED",
"error": "failed evaluation: failed eval: ERROR: <input>:1:30: failed to unmarshal JSON message: invalid character 'i' looking for beginning of value\n | bytes(get(state.url).Body).as(body,{\"events\":[body.decode_json()]})\n | .............................^",
"error": "failed evaluation", // Error prefix.
},
"cel-cel.cel-1e8b33de-d54a-45cd-90da-ffffffc482e2": map[string]interface{}{
"status": "HEALTHY",
Expand All @@ -335,15 +335,15 @@ func TestCheckinV2(t *testing.T) {
return false, allStreams
}

if !reflect.DeepEqual(map[string]interface{}{
if !similarMaps(map[string]interface{}{
"streams": map[string]interface{}{
"cel-cel.cel-1e8b33de-d54a-45cd-90da-23ed71c482e2": map[string]interface{}{
"status": "DEGRADED",
"error": "failed evaluation: failed eval: ERROR: <input>:1:30: failed to unmarshal JSON message: invalid character 'i' looking for beginning of value\n | bytes(get(state.url).Body).as(body,{\"events\":[body.decode_json()]})\n | .............................^",
"error": "failed evaluation", // Error prefix.
},
"cel-cel.cel-1e8b33de-d54a-45cd-90da-ffffffc482e2": map[string]interface{}{
"status": "DEGRADED",
"error": "failed evaluation: failed eval: ERROR: <input>:1:30: failed to unmarshal JSON message: invalid character 'i' looking for beginning of value\n | bytes(get(state.url).Body).as(body,{\"events\":[body.decode_json()]})\n | .............................^",
"error": "failed evaluation", // Error prefix.
},
},
}, payload) {
Expand All @@ -361,7 +361,7 @@ func TestCheckinV2(t *testing.T) {
return false, allStreams
}

if !reflect.DeepEqual(map[string]interface{}{
if !similarMaps(map[string]interface{}{
"streams": map[string]interface{}{
"cel-cel.cel-1e8b33de-d54a-45cd-90da-23ed71c482e2": map[string]interface{}{
"status": "HEALTHY",
Expand All @@ -386,15 +386,15 @@ func TestCheckinV2(t *testing.T) {
return false, allStreams
}

if !reflect.DeepEqual(map[string]interface{}{
if !similarMaps(map[string]interface{}{
"streams": map[string]interface{}{
"cel-cel.cel-1e8b33de-d54a-45cd-90da-23ed71c482e2": map[string]interface{}{
"status": "HEALTHY",
"error": "",
},
"cel-cel.cel-1e8b33de-d54a-45cd-90da-ffffffc482e2": map[string]interface{}{
"status": "DEGRADED",
"error": "failed evaluation: failed eval: ERROR: <input>:1:30: failed to unmarshal JSON message: invalid character 'i' looking for beginning of value\n | bytes(get(state.url).Body).as(body,{\"events\":[body.decode_json()]})\n | .............................^",
"error": "failed evaluation", // Error prefix.
},
},
}, payload) {
Expand All @@ -410,7 +410,7 @@ func TestCheckinV2(t *testing.T) {
return false, oneStream
}

if !reflect.DeepEqual(map[string]interface{}{
if !similarMaps(map[string]interface{}{
"streams": map[string]interface{}{
"cel-cel.cel-1e8b33de-d54a-45cd-90da-23ed71c482e2": map[string]interface{}{
"status": "HEALTHY",
Expand Down Expand Up @@ -442,7 +442,7 @@ func TestCheckinV2(t *testing.T) {
return false, allStreams
}

if !reflect.DeepEqual(map[string]interface{}{
if !similarMaps(map[string]interface{}{
"streams": map[string]interface{}{
"cel-cel.cel-1e8b33de-d54a-45cd-90da-23ed71c482e2": map[string]interface{}{
"status": "HEALTHY",
Expand Down Expand Up @@ -496,6 +496,48 @@ func TestCheckinV2(t *testing.T) {
}
}

// similarMaps returns whether a and b are similar. Similarity is defined as
// being equal for string values except when the key is "error" in which case
// the error prefix must match (up to the first colon).
func similarMaps(a, b map[string]any) bool {
if len(a) != len(b) {
return false
}
for k, v1 := range a {
v2, ok := b[k]
if !ok || !sameType(v1, v2) {
return false
}
switch v1 := v1.(type) {
case string:
if k != "error" {
if v1 != v2 {
return false
}
continue
}
var ok1 bool
v1, _, ok1 = strings.Cut(v1, ":")
v2, _, ok2 := strings.Cut(v2.(string), ":")
if v1 != v2 || ok1 != ok2 {
return false
}
case map[string]any:
if !similarMaps(v1, v2.(map[string]any)) {
return false
}
default:
panic("not handled")
}
}
return true
}

func sameType[T any](v1 T, v2 any) bool {
_, ok := v2.(T)
return ok
}

func extractStateAndPayload(observed *proto.CheckinObserved, inputID string) (proto.State, map[string]interface{}) {
for _, unit := range observed.GetUnits() {
if unit.Id == inputID {
Expand Down

0 comments on commit be65f66

Please sign in to comment.