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

Additional details in simulator output #289

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions cmd/falco/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Flags:
-h, --help : Show this help
-r, --remote : Connect with Fastly API
-request : Simulate request config
-body : Output response body
-debug : Enable debug mode
--max_backends : Override max backends limitation
--max_acls : Override max acls limitation
Expand Down
1 change: 1 addition & 0 deletions cmd/falco/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ func (r *Runner) Simulate(rslv resolver.Resolver) error {
icontext.WithResolver(rslv),
icontext.WithMaxBackends(r.config.OverrideMaxBackends),
icontext.WithMaxAcls(r.config.OverrideMaxAcls),
icontext.WithOutputResponseBody(r.config.Simulator.OutputResponseBody),
}
if r.snippets != nil {
options = append(options, icontext.WithSnippets(r.snippets))
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type SimulatorConfig struct {
IncludePaths []string // Copy from root field

// Override Request configuration
OverrideRequest *RequestConfig
OverrideRequest *RequestConfig
OutputResponseBody bool `cli:"body" yaml:"output_response_body"`
}

// Testing configuration
Expand All @@ -52,7 +53,7 @@ type Config struct {
IncludePaths []string `cli:"I,include_path" yaml:"include_paths"`
Transforms []string `cli:"t,transformer" yaml:"transformers"`
Help bool `cli:"h,help"`
Version bool `cli:"V"`
Version bool `cli:"V,version"`
Remote bool `cli:"r,remote" yaml:"remote"`
Json bool `cli:"json"`
Request string `cli:"request"`
Expand Down
1 change: 1 addition & 0 deletions interpreter/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Context struct {
Gotos map[string]*ast.GotoStatement
SubroutineFunctions map[string]*ast.SubroutineDeclaration
OriginalHost string
OutputResponseBody bool

OverrideMaxBackends int
OverrideMaxAcls int
Expand Down
6 changes: 6 additions & 0 deletions interpreter/context/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ func WithOverrideHost(host string) Option {
c.OriginalHost = host
}
}

func WithOutputResponseBody(o bool) Option {
return func(c *Context) {
c.OutputResponseBody = o
}
}
2 changes: 1 addition & 1 deletion interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (i *Interpreter) ProcessInit(r *http.Request) error {
i.ctx.OriginalHost = r.Host
}

i.process = process.New()
i.process = process.New(i.ctx.OutputResponseBody)
i.ctx.Scope = context.InitScope
i.vars = variable.NewAllScopeVariables(i.ctx)

Expand Down
4 changes: 4 additions & 0 deletions interpreter/process/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Flow struct {
Line int `json:"line"`
Position int `json:"position"`
Subroutine string `json:"subroutine"`
Backend string `json:"backend,omitempty"`
Request *HttpFlow `json:"req,omitempty"`
BackendRequest *HttpFlow `json:"bereq,omitempty"`
BackendResponse *HttpFlow `json:"beresp,omitempty"`
Expand All @@ -29,6 +30,9 @@ func NewFlow(ctx *icontext.Context, sub *ast.SubroutineDeclaration) *Flow {
Position: token.Position,
Subroutine: sub.Name.Value,
}
if ctx.Backend != nil {
f.Backend = ctx.Backend.String()
}
if ctx.Request != nil {
f.Request = newFlowRequest(ctx.Request.Clone(c))
}
Expand Down
19 changes: 15 additions & 4 deletions interpreter/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

type Process struct {
includeBody bool

Flows []*Flow
Logs []*Log
Restarts int
Expand All @@ -22,11 +24,12 @@ type Process struct {
Response *http.Response
}

func New() *Process {
func New(includeBody bool) *Process {
return &Process{
Flows: []*Flow{},
Logs: []*Log{},
StartTime: time.Now().UnixMicro(),
Flows: []*Flow{},
Logs: []*Log{},
StartTime: time.Now().UnixMicro(),
includeBody: includeBody,
}
}

Expand Down Expand Up @@ -56,6 +59,11 @@ func (p *Process) Finalize(resp *http.Response) ([]byte, error) {
}
}

var body string
if p.includeBody {
body = buf.String()
}

return json.MarshalIndent(struct {
Flows []*Flow `json:"flows"`
Logs []*Log `json:"logs"`
Expand All @@ -68,6 +76,7 @@ func (p *Process) Finalize(resp *http.Response) ([]byte, error) {
ClientResponse struct {
StatusCode int `json:"status_code"`
ResponseBytes int `json:"body_bytes"`
ResponseBody string `json:"body,omitempty"`
Headers map[string]string `json:"headers"`
} `json:"client_response"`
}{
Expand All @@ -82,10 +91,12 @@ func (p *Process) Finalize(resp *http.Response) ([]byte, error) {
ClientResponse: struct {
StatusCode int `json:"status_code"`
ResponseBytes int `json:"body_bytes"`
ResponseBody string `json:"body,omitempty"`
Headers map[string]string `json:"headers"`
}{
StatusCode: statusCode,
ResponseBytes: len(buf.Bytes()),
ResponseBody: body,
Headers: headers,
},
}, "", " ")
Expand Down
Loading