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(assert): fix retry failure if using left arrow functions for assertion #397

Merged
merged 1 commit into from
Feb 21, 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
27 changes: 11 additions & 16 deletions template/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,36 +240,39 @@ func executeLeftArrowFunction(ctx context.Context, f Func, v reflect.Value, data
return reflect.ValueOf(res), nil
}

// NOTE: This function must return a copy to avoid the not found error when retrying.
func replaceFuncs(in reflect.Value, s *funcStash) (reflect.Value, error) {
v := reflectutil.Elem(in)

switch v.Kind() {
case reflect.Map:
vv := reflect.MakeMapWithSize(v.Type(), v.Len())
for _, k := range v.MapKeys() {
e := v.MapIndex(k)
if !isNil(e) {
x, err := replaceFuncs(e, s)
if err != nil {
return reflect.Value{}, err
}
v.SetMapIndex(k, x)
vv.SetMapIndex(k, x)
}
}
v = vv
case reflect.Slice:
vv := reflect.MakeSlice(v.Type(), v.Len(), v.Len())
for i := 0; i < v.Len(); i++ {
e := v.Index(i)
if !isNil(e) {
x, err := replaceFuncs(e, s)
if err != nil {
return reflect.Value{}, err
}
e.Set(x)
vv.Index(i).Set(x)
}
}
v = vv
case reflect.Struct:
if !v.CanSet() {
v = makePtr(v).Elem() // create pointer to enable to set values
}
vv := reflect.New(v.Type()).Elem()
for i := 0; i < v.NumField(); i++ {
if !token.IsExported(v.Type().Field(i).Name) {
continue // skip unexported field
Expand All @@ -279,11 +282,12 @@ func replaceFuncs(in reflect.Value, s *funcStash) (reflect.Value, error) {
if err != nil {
return reflect.Value{}, err
}
if err := reflectutil.Set(field, x); err != nil {
fieldName := structFieldName(v.Type().Field(i))
if err := reflectutil.Set(vv.Field(i), x); err != nil {
fieldName := structFieldName(vv.Type().Field(i))
return reflect.Value{}, errors.WithPath(err, fieldName)
}
}
v = vv
case reflect.Func:
return reflect.ValueOf(fmt.Sprintf("{{%s}}", s.save(v.Interface()))), nil
default:
Expand All @@ -295,15 +299,6 @@ func replaceFuncs(in reflect.Value, s *funcStash) (reflect.Value, error) {
if converted, err := convert(in.Type())(v, nil); err == nil {
v = converted
}
// keep the original address
if in.Type().Kind() == reflect.Ptr && v.Type().Kind() == reflect.Ptr {
if v.Elem().Type().AssignableTo(in.Elem().Type()) {
if err := reflectutil.Set(in.Elem(), v.Elem()); err != nil {
return reflect.Value{}, err
}
v = in
}
}
}
return v, nil
}
Expand Down
51 changes: 51 additions & 0 deletions template/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package template
import (
"context"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -448,6 +449,56 @@ func TestExecute(t *testing.T) {
})
}

func TestReplaceFuncs(t *testing.T) {
v := yaml.MapSlice{
yaml.MapItem{
Key: "foo",
Value: func() {},
},
yaml.MapItem{
Key: "bar",
Value: map[string]any{
"hoge": func() {},
},
},
yaml.MapItem{
Key: "baz",
Value: []any{
&callArg{
F: func() {},
Arg: "test",
},
},
},
}
if _, err := yaml.Marshal(v); err == nil {
t.Fatal("no error")
}

s := &funcStash{}
vv, err := replaceFuncs(reflect.ValueOf(v), s)
if err != nil {
t.Fatalf("failed to replace funcs: %s", err)
}
if b, err := yaml.Marshal(vv.Interface()); err != nil {
t.Fatalf("failed to marshal: %s", err)
} else if got, expect := string(b), strings.TrimLeft(`
foo: "{{func-0}}"
bar:
hoge: "{{func-1}}"
baz:
- f: "{{func-2}}"
arg: test
`, "\n"); got != expect {
t.Fatalf("expect %q but got %q", expect, got)
}

// don't break the original value
if _, err := yaml.Marshal(v); err == nil {
t.Fatal("no error")
}
}

func TestConvert(t *testing.T) {
convertToStr := convert(reflect.TypeOf(""))
t.Run("convert to string", func(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions template/lazy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func newWaitContext(ctx context.Context, base any) *waitContext {
case v := <-ready:
return v, true
case <-ctx.Done():
// ignore canceled if the value is already set
select {
case v := <-ready:
return v, true
default:
}
return nil, false
}
}),
Expand Down
18 changes: 18 additions & 0 deletions template/lazy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,28 @@ func TestWaitContext(t *testing.T) {
t.Fatalf("expect %q but got %q", expect, got)
}

// ignore canceled if the value is already set
cancel()
for i := 0; i < 10; i++ { // HACK: try many times for converage...
if got, expect := extractVal(t, "$.$", wc), "BAR"; got != expect {
t.Fatalf("expect %q but got %q", expect, got)
}
}

// don't set twice
if err := wc.set("BAR"); err == nil {
t.Fatal("no error")
}

// canceled
wc = newWaitContext(ctx, nil)
q, err := query.ParseString("$.$", queryutil.Options()...)
if err != nil {
t.Fatalf("failed to parse query string: %s", err)
}
if _, err := q.Extract(wc); err == nil {
t.Fatal("no error")
}
}

func extractVal(t *testing.T, s string, target any) any {
Expand Down
19 changes: 19 additions & 0 deletions test/e2e/testdata/testcases/mocks/retry/laf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mocks:
- protocol: http
response:
code: 200
body:
messages:
- id: 1
message: bar
- id: 2
message: foo
- protocol: http
response:
code: 200
body:
messages:
- id: 1
message: bar
- id: 2
message: foo
5 changes: 5 additions & 0 deletions test/e2e/testdata/testcases/retry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ scenarios:
success: false
output:
stdout: retry/step-include-failure.txt
- filename: retry/laf.yaml
mocks: retry/laf.yaml
success: false
output:
stdout: retry/laf.txt
23 changes: 23 additions & 0 deletions test/e2e/testdata/testcases/scenarios/retry/laf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: retry with lfa
steps:
- protocol: http
request:
method: GET
url: "http://{{env.TEST_HTTP_ADDR}}/messages"
expect:
code: OK
body:
messages:
'{{assert.and <-}}':
- '{{size($) == 2}}'
- '{{assert.contains <-}}':
id: '1'
message: foo
- '{{assert.contains <-}}':
id: '2'
message: bar
retry:
constant:
interval: 10ms
maxRetries: 1
51 changes: 51 additions & 0 deletions test/e2e/testdata/testcases/stdout/retry/laf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--- FAIL: testdata/testcases/scenarios/retry/laf.yaml (0.00s)
--- FAIL: testdata/testcases/scenarios/retry/laf.yaml/retry_with_lfa (0.00s)
--- FAIL: testdata/testcases/scenarios/retry/laf.yaml/retry_with_lfa/ (0.00s)
retry after 10ms
retry limit exceeded
request:
method: GET
url: http://[::]:12345/messages
header:
User-Agent:
- scenarigo/v1.0.0
response:
status: 200 OK
statusCode: 200
header:
Content-Length:
- "73"
Content-Type:
- application/json
Date:
- Mon, 01 Jan 0001 00:00:00 GMT
body:
messages:
- id: "1"
message: bar
- id: "2"
message: foo
elapsed time: 0.000000 sec
2 errors occurred: doesn't contain expected value: last error: expected 1 but got 2
12 | '{{assert.and <-}}':
13 | - '{{size($) == 2}}'
14 | - '{{assert.contains <-}}':
> 15 | id: '1'
^
16 | message: foo
17 | - '{{assert.contains <-}}':
18 | id: '2'

doesn't contain expected value: last error: expected bar but got foo
16 | message: foo
17 | - '{{assert.contains <-}}':
18 | id: '2'
> 19 | message: bar
^
20 | retry:
21 | constant:
22 | interval: 10ms
23 |
FAIL
FAIL testdata/testcases/scenarios/retry/laf.yaml 0.000s
FAIL
Loading