Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
embedding templates and consuming them
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham Arora committed Oct 17, 2020
1 parent 0495b17 commit 94f5ca8
Show file tree
Hide file tree
Showing 47 changed files with 2,624 additions and 15 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ require (
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/aws/aws-sdk-go v1.29.25
github.com/go-test/deep v1.0.5
github.com/gobuffalo/here v0.6.2 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/jmespath/go-jmespath v0.3.0 // indirect
github.com/kylelemons/godebug v1.1.0
github.com/markbates/pkger v0.17.1
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/spf13/pflag v1.0.5
Expand Down
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ github.com/aws/aws-sdk-go v1.29.25 h1:03yt6K6vCxfxFHT7mXGqFeISsNDE27LO2u+5AQBnTz
github.com/aws/aws-sdk-go v1.29.25/go.mod h1:1KvfttTE3SPKMpo8g2c6jL3ZKfXtFvKscTgahTma5Xg=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-test/deep v1.0.5 h1:AKODKU3pDH1RzZzm6YZu77YWtEAq6uh1rLIAQlay2qc=
github.com/go-test/deep v1.0.5/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gobuffalo/here v0.6.2 h1:ZtCqC7F9ou3moLbYfHM1Tj+gwHGgWhjyRjVjsir9BE0=
github.com/gobuffalo/here v0.6.2/go.mod h1:D75Sq0p2BVHdgQu3vCRsXbg85rx943V19urJpqAVWjI=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
Expand All @@ -21,8 +27,16 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5i
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno=
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
Expand Down Expand Up @@ -51,7 +65,12 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
52 changes: 39 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"syscall"

"github.com/intuit/replay-zero/templates"
"github.com/markbates/pkger"
flag "github.com/spf13/pflag"

"github.com/ztrue/shutdown"
Expand All @@ -31,7 +31,7 @@ var (
listenPort int
defaultTargetPort int
batchSize int
output string
template string
extension string
debug bool
streamRoleArn string
Expand Down Expand Up @@ -82,10 +82,10 @@ func readFlags() {
}
flag.BoolVarP(&flags.version, "version", "V", false, "Print version info and exit")
flag.IntVarP(&flags.listenPort, "listen-port", "l", 9000, "The port the Replay Zero proxy will listen on")
flag.IntVarP(&flags.defaultTargetPort, "target-port", "t", 8080, "The port the Replay Zero proxy will forward to on localhost")
flag.IntVarP(&flags.defaultTargetPort, "target-port", "p", 8080, "The port the Replay Zero proxy will forward to on localhost")
flag.BoolVar(&flags.debug, "debug", false, "Set logging to also print debug messages")
flag.IntVarP(&flags.batchSize, "batch-size", "b", 1, "Buffer events before writing out to a file")
flag.StringVarP(&flags.output, "output", "o", "karate", "Either [karate] or [path/to/custom/template]")
flag.StringVarP(&flags.template, "template", "t", "karate", "Either [karate] or [gatling] or [path/to/custom/template]")
flag.StringVarP(&flags.extension, "extension", "e", "", "For custom output template")
flag.StringVarP(&flags.streamName, "stream-name", "s", "", "AWS Kinesis Stream name (streaming mode only)")
flag.StringVarP(&flags.streamRoleArn, "stream-role-arn", "r", "", "AWS Kinesis Stream ARN (streaming mode only)")
Expand All @@ -105,26 +105,52 @@ func readFlags() {

// check if template exist at the provided path
// TODO: add validation for correctness of template
if flags.output != "karate" {
_, err := ioutil.ReadFile(flags.output)
if !(flags.template == "karate" || flags.template == "gatling") {
_, err := ioutil.ReadFile(flags.template)
if err != nil {
log.Printf("Failed to load template")
panic(err)
}
if flags.extension == "" {
log.Printf("For custom template, output extension is expected to be passed using --extension flag.")
log.Fatal("For custom template, output extension is expected to be passed using --extension or -e flag.")
os.Exit(0)
}
}
}

func getFormat(output string, extension string) outputFormat {
switch output {
// get embeded template file content
func getPkgTemplate(filePath string) string {
f, err := pkger.Open(filePath)
if err != nil {
panic(err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
panic(err)
}
b := make([]byte, info.Size())
content, err := f.Read(b)
if err != nil {
panic(err)
}
return string(b[:content])
}

func getFormat(template string, extension string) outputFormat {
switch template {
case "karate":
return outputFormat{
template: templates.KarateBase,
template: getPkgTemplate("/templates/karate_default.template"),
extension: "feature",
}
case "gatling":
return outputFormat{
template: getPkgTemplate("/templates/gatling_default.template"),
extension: "scala",
}
default:
dat, err := ioutil.ReadFile(output)
dat, err := ioutil.ReadFile(template)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -219,8 +245,8 @@ func main() {
log.Println("Running ONLINE, sending recorded events to Kinesis")
h = getOnlineHandler(flags.streamName, flags.streamRoleArn)
} else {
log.Printf("Running OFFLINE, writing out events to %s files\n", flags.output)
h = getOfflineHandler(flags.output, flags.extension)
log.Printf("Running OFFLINE, writing out events to %s files\n", flags.template)
h = getOfflineHandler(flags.template, flags.extension)
}

shutdown.Add(func() {
Expand Down
4 changes: 2 additions & 2 deletions offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type offlineHandler struct {
templateFuncMap template.FuncMap
}

func getOfflineHandler(output string, extension string) eventHandler {
func getOfflineHandler(template string, extension string) eventHandler {
return &offlineHandler{
format: getFormat(output, extension),
format: getFormat(template, extension),
defaultBatchSize: flags.batchSize,
currentBatchSize: flags.batchSize,
writerFactory: getFileWriter,
Expand Down
34 changes: 34 additions & 0 deletions templates/karate_default.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Replay Zero at {{ now }}
Feature:

Background:
* url 'http://localhost:8080'
{{ range $index, $event := . }}
Scenario: test scenario {{.PairID}}
Given path '{{ $event.Endpoint }}'
{{/* add request headers if present */ -}}
{{ range $header := $event.ReqHeaders -}}
And header {{$header.Name}} = '{{$header.Value}}'
{{end }}
{{- /* add request body if present */ -}}
{{ if $event.ReqBody -}}
And request
"""
{{ $event.ReqBody }}
"""
{{- end }}

When method {{ $event.HTTPMethod }}
Then status {{ $event.ResponseCode }}
{{/* assert on response headers if present */ -}}
{{ range $header := $event.RespHeaders -}}
And match header {{$header.Name}} == '{{$header.Value}}'
{{end }}
{{- /* assert on response body if present */ -}}
{{ if $event.RespBody -}}
And match response ==
"""
{{ $event.RespBody }}
"""
{{- end }}
{{ end }}
28 changes: 28 additions & 0 deletions vendor/github.com/gobuffalo/here/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions vendor/github.com/gobuffalo/here/.goreleaser.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/gobuffalo/here/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions vendor/github.com/gobuffalo/here/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 94f5ca8

Please sign in to comment.