Coder Social home page Coder Social logo

goodman's Introduction

Goodman Godoc Reference Travis Status

Goodman is a Dredd hook handler implementation in Go. The API may change, please Vendor this library.

About

This package contains a Go Dredd hook handler which provides a bridge between the Dredd API Testing Framework and Go environment to ease implementation of testing hooks provided by Dredd. Write Dredd hooks in Go to glue together API Blueprint with your Go project

Not sure what these Dredd Hooks are? Read the Dredd documentation on them

The following are a few examples of what hooks can be used for:

  • loading db fixtures
  • cleanup after test step or steps
  • handling authentication and sessions
  • passing data between transactions (saving state from responses to stash)
  • modifying request generated from blueprint
  • changing generated expectations
  • setting custom expectations
  • debugging via logging stuff

Installing

Must use Dredd v1.1.0 or greater

go get github.com/snikch/goodman/cmd/goodman

Usage

1). Create a hook file in hooks.go

package main

import (
  "fmt"

  "github.com/snikch/goodman/hooks"
  trans "github.com/snikch/goodman/transaction"
)

func main() {
      h := hooks.NewHooks()
      server := hooks.NewServer(hooks.NewHooksRunner(h))
      h.Before("/message > GET", func(t *trans.Transaction) {
          fmt.Println("before modification")
      })
      server.Serve()
      defer server.Listener.Close()
})

2). Compile your hooks program

go build -o hooks path/to/hooks.go

3). Run it with dredd

dredd apiary.apib localhost:3000 --language go --hookfiles ./hooks

API

The hooks.Server struct provides the following methods to hook into the following dredd transactions: before, after, before_all, after_all, before_each, after_each, before_validation, and before_each_validation. The before, before_validation and after hooks are identified by transaction name.

How to Contribute

  1. Fork it
  2. Create your feature branch (git checkout -b my-newfeature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push (git push origin my-new-feature)
  5. Create a new Pull Request

Tests

The test suite consists of go test suite and aruba/cucumber tests

Running the tests

  • go tests go test github.com/snikch/{,/hooks,/transaction}

  • aruba tests

    • Install local dredd copy npm install
    • Install aruba ruby gem bundle install
    • Run test suite bundle exec cucumber

goodman's People

Contributors

ddelnano avatar grncdr avatar honzajavorek avatar ibraimedina avatar ingmarstein avatar snikch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

goodman's Issues

Create Dockerfile for goodman

Would it be possible to create a Dockerfile with dredd and goodman installed (npm and go too) so folks can just do a docker run rather than having to manually install and configure things themselves?

Unused hooks report invalid arguments

summary

When I make a simple hook server with the goodman library...
I expect hooks I don't implement to be ignored.
Instead I get warnings
Even though the hooks I did implement work properly.

details

my hook server

package main

import (
    "fmt"

    "github.com/snikch/goodman/hooks"
    trans "github.com/snikch/goodman/transaction"
)

func main() {
    h := hooks.NewHooks()
    server := hooks.NewServer(h)

    h.BeforeAll(func(t []*trans.Transaction) {
        fmt.Println("THIS IS A DREDD TEST HOOK WRITTEN IN GOLANG BeforeAll starting.")
    })

    server.Serve()
    defer server.Listener.Close()
}

unexpected error messages

info: Hook handler stdout: Completed

info: Hook handler stderr: 2016/07/14 11:57:40 Starting hooks server in go routine

info: Hook handler stderr: 2016/07/14 11:57:40 method After reply type not a pointer: hooks.Callback
2016/07/14 11:57:40 method AfterAll has wrong number of ins: 2
2016/07/14 11:57:40 method AfterEach has wrong number of ins: 2
2016/07/14 11:57:40 method Before reply type not a pointer: hooks.Callback
2016/07/14 11:57:40 method BeforeAll has wrong number of ins: 2
2016/07/14 11:57:40 method BeforeEach has wrong number of ins: 2
2016/07/14 11:57:40 method BeforeEachValidation has wrong number of ins: 2
2016/07/14 11:57:40 method BeforeValidation reply type not a pointer: hooks.Callback

Successful portion

info: Hook handler stdout: Starting

info: Hook handler stdout: Accepting connection

info: Connected to the hook handler, waiting 0.1s to start testing.
info: Hook handler stdout: THIS IS A DREDD TEST HOOK WRITTEN IN GOLANG BeforeAll starting.

Unable to un-skip tests

I am trying to add tests for non-2xx codes but I am having two problems

First, I am not able to refer to my transaction by name (i.e. URL > 404 > POST), so I have given up with that. (I am using Swagger specification, I believe the only example in goodman is using the API Blueprint specification)

So my current strategy is to check for the transaction within the BeforeEach hook, and specify t.Skip = false if it is a transaction that should be tested. In fact I am unable to un-skip anything:

Example:

h.BeforeEach(func(t *trans.Transaction) { t.Skip = false fmt.Println("Don't skip") }

Output

info: Hooks handler stdout: Don't skip

skip: GET /api/76c75fb2
info: Hooks handler stderr: 2017/08/08 10:54:35 Shutting down hooks servers

What am I doing wrong?

Modify the Request Path

Hey, I'm trying to modify the request path inside a Before() hook, but it's not wokring, this is how the code looks like:

	h.Before("/resource/{id} > GET > 200 > application/json", func(t *trans.Transaction) {
                newId := createResource()
		uri := strings.Replace(t.FullPath, "xxx", newId, 1)
		t.FullPath = uri
		t.Request.URI = uri
	})

But dredd it's still calling GET /resource/xxx. Am I doing something wrong?

Installing goodman using dependencies managers

Hello @snikch and @ddelnano,

TL;DR: I think go get github.com/snikch/goodman/cmd/goodman would be a better recommendation to install goodman because it behaves in a standard way.

I am getting started with goodman, and I was surprized by the installation instructions:

go get github.com/snikch/goodman
go build -o $GOPATH/bin/goodman github.com/snikch/goodman/cmd/goodman

Specifically, I was surprized by the mandatory go build: usually, go get does install the binary.

Further on, that additional step turned to be annoying because it doesn't play well with dependencies managers. (I am using govendor but I think the comment is relevant to any dependencies managers which rely on the standard go get / go install under the hood.)

What do I mean by "doesn't play well"? If I add goodman to my project dependencies (govendor fetch github.com/snikch/goodman in my case), then I expect it to be installed when I install all the dependencies (govendor install +vendor in my case). That is particularly true in my build pipeline, where I wish I hadn't to add goodman-specific steps to install everything I need to run my test suite.

It makes sense to me to keep the library at the root level and the command in cmd/, I mean I think it's a least a legitimate choice.

But considering that using Dredd hooks requires both the library and the binary to be installed, I wish the default recommendation would take care of that (which it does! ...in a non-standard way).

Using go get github.com/snikch/goodman/cmd/goodman (resp. govendor install github.com/snikch/goodman/cmd/goodman or whatever you dependencies manager requires) does the trick. And would be a better installation recommendation for the README/wiki IMHO.

The main package depends on the library (goodman) which depends on goodman/transaction. When requiring github.com/snikch/goodman/cmd/goodman, go get performs go fetch and go install for main and everything falls in place nicely.

Am I missing something? What you you think? I can open a PR if you want : )

Hook files with flags

How do I alter the behavior of the hook files, provided by a specific flag? Say that I want to set the database connection to A if I am running it locally, and set it to database connection B if I am running it against a development server.

Error when API sets a cookie

Whenever my API responds with a header Set-Cookie, I get the error info: Hook handler stderr: panic: RPC client threw error gob: type not registered for interface: []interface {}

I have narrowed this down to the Set-Cookie header. If I name the header anything else, it doesn't give me that error, but when I name it Set-Cookie I always get that error.

Could releases be tagged?

Hello again!

In the same line as #17 (using dependencies managers) but independently: could the goodman releases be tagged? (Please!)

Since I'm asking, could the release numbers adhere to the Semantic Versioning conventions? That makes automated updates so much safer that it reduces drastically the maintenance efforts related to dependencies management when using a dependencies manager.

I would also suggest a look at http://keepachangelog.com, I'm using it and I find that the extra work required maintain a change log is minimal (example). And it makes things much clearer, even for me as a maintainer.

Tests

Is there a way how to run tests for this repository?

I am working on testing infrastructure, which would be able to verify whether new Dredd releases work seamlessly with every hooks handler implementation. To include the goodman project, I need this repository to have Travis CI with the Cucumber test suite.

Without the Travis CI integration, I won't be able to test new Dredd releases with goodman and I won't be able to be confident about further integrating goodman with Dredd (dredd init, documentation, ...). And that would be a shame!

Example tests failing

When running make -C example dredd the tests fail.
Dredd 2.2.5 or using 1.6.1 from node_modules folder
Go 1.7
Node 7.3.0
NPM 3.10.10

Can someone re-run the Travis CI build/test to confirm that is it just my issue?

goodman ➤ make -C example dredd                                                                                                                                                                                                                                                 git:master* ../node_modules/.bin/dredd api.apib http://localhost:8080 --language go --server ./server --hookfiles hooks/hooks
info: Starting server with command: ./server
info: Waiting 3 seconds for server command to start...
info: Beginning Dredd testing...
info: Found Hookfiles: hooks/hooks
info: Spawning `go` hooks handler
info: Hook handler stdout: Sending to channel

Completed

info: Hook handler stderr: 2016/12/23 10:32:10 Starting hooks server in go routine

info: Hook handler stderr: 2016/12/23 10:32:10 method After reply type not a pointer: hooks.Callback
2016/12/23 10:32:10 method AfterAll has wrong number of ins: 2
2016/12/23 10:32:10 method AfterEach has wrong number of ins: 2
2016/12/23 10:32:10 method Before reply type not a pointer: hooks.Callback
2016/12/23 10:32:10 method BeforeAll has wrong number of ins: 2

info: Hook handler stderr: 2016/12/23 10:32:10 method BeforeEach has wrong number of ins: 2
2016/12/23 10:32:10 method BeforeEachValidation has wrong number of ins: 2
2016/12/23 10:32:10 method BeforeValidation reply type not a pointer: hooks.Callback

info: Hook handler stdout: Starting

info: Hook handler stdout: Accepting connection

info: Connected to the hook handler, waiting 0.1s to start testing.
info: Hook handler stderr: 2016/12/23 10:32:11 table users already exists

info: Hook handler stdout: Hooks client failed with exit status 1

info: Hook handler stderr: panic: RPC client threw error unexpected EOF

goroutine 1 [running]:
panic(0x2d6e20, 0xc420075f90
info: Hook handler stderr: )
	/usr/local/Cellar/go/1.7/libexec/src/runtime/panic.go:500 +0x1a1
github.com/snikch/goodman.(*Run).RunBeforeAll(0xc4200ef500, 0xc420070460)
	/Users/timothy.blackwell/go/src/github.com/snikch/goodman/runner.go:32 +0x1bc
github.com/snikch/goodman.(*Server).RunBeforeAll(0xc42004bee8, 0xc420070460)
	/Users/timothy.blackwell/go/src/github.com/snikch/goodman/server.go:135 +0x5e
github.com/snikch/goodman.(*Server).ProcessMessage(0xc42004bee8, 0xc420070420, 0x500, 0x2c6e20)
	/Users/timothy.blackwell/go/src/github.com/snikch/goodman/server.go:98 +0x5a2
github.com/snikch/goodman.(*Server).Run(0xc42004bee8, 0x0, 0x0)
	/Users/timothy.blackwell/go/src/github.com/snikch/goodman/server.go:70 +0x484
main.main()
	/Users/timothy.blackwell/go/src/github.com/snikch/goodman/cmd/goodman/main.go:60 +0x6d7

error: Hook handler '/Users/timothy.blackwell/go/bin/goodman' exited with status: 2
warn: Hook handling timed out.
error: Hook handler '/Users/timothy.blackwell/go/bin/goodman' exited with status: 2
info: Sending SIGTERM to the backend server
info: Backend server was killed
make: *** [dredd] Error 1 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.