README.md in heroku_hatchet-0.2.0 vs README.md in heroku_hatchet-1.0.0

- old
+ new

@@ -30,162 +30,233 @@ ## Run the Tests $ bundle exec rake test +## Why Test a Buildpack? -## Writing Tests +To prevent regressions and to make pushing out new features faster and easier. -Hatchet is meant for running integration tests, which means we actually have to deploy a real live honest to goodness app on Heroku and see how it behaves. +## What can Hatchet Test? -First you'll need a repo to an app you know works on Heroku, add it to the proper folder in repos. Such as `repos/rails3/`. Once you've done that make a corresponding test file in the `test` dir such as `test/repos/rails3`. I've already got a project called "codetriage" and the test is "triage_test.rb". +Hatchet can easily test deployment of buildpacks, getting the build output, and running arbitrary interactive processes such as `heroku run bash`. -Now that you have an app, we'll need to create a heroku instance, and deploy our code to that instance you can do that with this code: +## Testing a Buildpack - Hatchet::App.new("repos/rails3/codetriage").deploy do |app| - ## - end +Hatchet was built for testing the Ruby buildpack, but you can use it to test any buildpack you desire provided you don't mind writing your tests written in Ruby. -The first argument to the app is the directory where you can find the code. Once your test is done, the app will automatically be destroyed. +You will need copies of applications that can be deployed by your buildpack. You can see the ones for the Hatchet unit tests (and the Ruby buildpack) https://github.com/sharpstone. Hatchet does not require that you keep these apps checked into your git repo which would make fetching your buildpack slow instead declare them in a `hatchet.json` file (see below). -Now that you've deployed your app you'll want to make sure that the deploy worked correctly. Since we're using `test/unit` you can use regular assertions such as `assert` and `refute`. Since we're yielding to an `app` variable we can check the `deployed?` status of the app: +Hatchet will automate retrieving these files `$ hatchet install`, as well as deploying them using your local copy of the buildpack, retrieving the build output and running commands against deploying applications. - Hatchet::App.new("repos/rails3/codetriage").deploy do |app| - assert app.deployed? - end -The primary purpose of the buildpack is configuring and deploying apps, so if it deployed chances are the buildpack is working correctly, but sometimes you may want more information. You can run arbitrary commands such as `heroku bash` and then check for the existence of a file. +## Hatchet.json - Hatchet::App.new("repos/rails3/codetriage").deploy do |app| - app.run("bash") do |cmd| - assert cmd.run("ls public/assets").include?("application.css") - end - end +Hatchet expects a json file in the root of your buildpack called `hatchet.json`. You can configure install options using the `"hatchet"` key. In this example we're telling hatchet to install the given repos to our `test/fixtures` directory instead of the default current directory. -Anything you put in `cmd.run` at this point will be executed from with in the app that you are in. +``` +{ + "hatchet": {"directory": "test/fixtures"}, + "rails3": ["sharpstone/rails3_mri_193"], + "rails2": ["sharpstone/rails2blog"], + "bundler": ["sharpstone/no_lockfile"] +} +``` - cmd.run("cat") - cmd.run("cd") - cmd.run("cd .. ; ls | grep foo") +When you run `$ hatchet install` it will grab the git repos from github and place them on your local machine in a file structure that looks like this: -It behaves exactly as if you were in a remote shell. If you really wanted you could even run the tests: +``` +test/ + fixtures/ + repos/ + rails3/ + rails3_mri_193/ + rails2/ + rails2blog/ + bundler/ + no_lockfile/ +``` - cmd.run("rake test") +Now in your test you can reference one of these applications by using it's git name: -But since cmd.run doesn't return the exit status now, that wouldn't be -so useful (also there is a default timeout to all commands). If you want -you can configure the timeout by passing in a second parameter +```ruby +Hatchet::AnvilApp.new('no_lockfile') +``` - cmd.run("rake test", 180.seconds) +If you have conflicting names, use full paths. +A word of warning on including repos inside of your test +directory, if you're using a runner that looks for patterns such as +`*_test.rb` to run your hatchet tests, it may incorrectly think you want +to run the tests inside of the repos. To get rid of this +problem move your repos direcory out of `test/` or be more specific +with your tests such as moving them to a `test/hatchet` directory and +changing your pattern if you are using `Rake::TestTask` it might look like this: -## Running One off Commands + t.pattern = 'test/hatchet/**/*_test.rb' -If you only want to run one command you can call `app.run` without -passing a block +A note on external repos: since you're basing tests on these repos, it +is in your best interest to not change them or your tests may +spontaneously fail. In the future we may create a hatchet.lockfile or +something to declare the commit - Hatchet::AnvilApp.new("/codetriage").deploy do |app| - assert_match "1.9.3", app.run("ruby -v") - end +## Deploying apps +Now that you've got your apps locally you can have hatchet deploy them for you. Hatchet can deploy using one of two ways Anvil and Git. A `Hatchet::AnvilApp` will deploy using Anvil against the current directory. This means that the buildpack you have locally will be used when deploying, due to this we recommend using Anvil to run your tests. -## Testing A Different Buildpack +A `Hatchet::GitApp` will deploy using the standard `git push heroku master`, if you use this option you need to have a publicly accessible copy of your buildpack. Using Git to test your buildpack may be slow and require you to frequently push your buildpack to a public git repo. For this reason we recommend using Anvil to run your tests: -You can specify buildpack to deploy with like so: +```ruby +Hatchet::AnvilApp.new("rails3_mri_193").deploy do |app| - Hatchet::App.new("repos/rails3/codetriage", buildpack: "https://github.com/schneems/heroku-buildpack-ruby.git").deploy do |app| +end +``` -## Hatchet Config +Deploys are expected to work, if the `ENV['HATCHET_RETRIES']` is set, then deploys will be automatically retried that number of times. Due to testing using a network and random Anvil failures, setting this value to `3` retries seems to work well. If an app cannot be deployed within its allotted number of retries an error will be raised. -Hatchet is designed to test buildpacks, and requires full repositories -to deploy to Heroku. Web application repos, especially Rails repos, aren't known for -being small, if you're testing a custom buildpack and have -`BUILDPACK_URL` set in your app config, it needs to be cloned each time -you deploy your app. If you've `git add`-ed a bunch of repos then this -clone would be pretty slow, we're not going to do this. Do not commit -your repos to git. +If you are testing an app that is supposed to fail deployment you can set the `allow_failure: true` flag when creating the app: -Instead we will keep a structured file called -inventively `hatchet.json` at the root of your project. This file will -describe the structure of your repos, have the name of the repo, and a -git url. We will use it to sync remote git repos with your local -project. It might look something like this +```ruby +Hatchet::AnvilApp.new("no_lockfile", allow_failure: true).deploy do |app| +``` - { - "hatchet": {}, - "rails3": ["git@github.com:codetriage/codetriage.git"], - "rails2": ["git@github.com:heroku/rails2blog.git"] - } +After the block finishes your app will be removed from heroku. If you are investigating a deploy, you can add the `debug: true` flag to your app: -the 'hatchet' object accessor is reserved for hatchet settings. -. To copy each repo in your `hatchet.json` -run the command: +```ruby +Hatchet::AnvilApp.new("rails3_mri_193", debug: true).deploy do |app| +``` - $ hatchet install +Now after Hatchet is done deploying your app it will remain on Heroku. It will also output the name of the app into your test logs so that you can `heroku run bash` into it for detailed postmortem. -The above `hatchet.json` will produce a directory structure like this: +If you are wanting to run a test against a specific app without deploying to it, you can set the app name like this: - repos/ - rails3/ - codetriage/ - #... - rails2/ - rails2blog/ - # ... +```ruby +app = Hatchet::AnvilApp.new("rails3_mri_193", name: "testapp") +``` -While you are running your tests if you reference a repo that isn't -synced locally Hatchet will raise an error. Since you're using a -standard file for your repos, you can now reference the name of the git -repo, provided you don't have conflicting names: +Deploying the app takes a few minutes, so you may want to skip that part to make debugging a problem easier since you're iterating much faster. - Hatchet::App.new("codetriage").deploy do |app| -If you do have conflicting names, use full paths. +If you need to deploy using a buildpack that is not in the root of your directory you can specify a path in the `buildpack` option: -A word of warning on including rails/ruby repos inside of your test -directory, if you're using a runner that looks for patterns such as -`*_test.rb` to run your hatchet tests, it may incorrectly think you want -to run the tests inside of the rails repositories. To get rid of this -problem move your repos direcory out of `test/` or be more specific -with your tests such as moving them to a `test/hatchet` directory and -changing your pattern if you are using `Rake::TestTask` it might look like this: - t.pattern = 'test/hatchet/**/*_test.rb' +```ruby +buildpack_path = File.expand_path 'test/fixtures/buildpacks/heroku-buildpack-ruby' -A note on external repos: since you're basing tests on these repos, it -is in your best interest to not change them or your tests may -spontaneously fail. In the future we may create a hatchet.lockfile or -something to declare the commit +def test_deploy + Hatchet::AnvilApp.new("rails3_mri_193", buildpack: buildpack_path).deploy do |app| + # ... +``` -## Hatchet CLI +If you are using a `Hatchet::GitApp` this is where you specify the publicly avaialble location of your buildpack, such as `https://github.com/heroku/heroku-buildpack-ruby.git#mybranch` -Hatchet has a CLI for installing and maintaining external repos you're -using to test against. If you have Hatchet installed as a gem run - $ hatchet --help +## Getting Deploy Output -For more info on commands. If you're using the source code you can run -the command by going to the source code directory and running: +After Hatchet deploys your app you can get the output by using `app.output` - $ ./bin/hatchet --help +```ruby +Hatchet::AnvilApp.new("rails3_mri_193").deploy do |app| + puts app.output +end +``` +If you told Hatchet to `allow_failure: true` then the full output of the failed build will be in `app.output` even though the app was not deployed. It is a good idea to test against the output for text that should be present. Using a testing framework such as `Test::Unit` a failed test output may look like this -## Retries +```ruby +Hatchet::AnvilApp.new("no_lockfile", allow_failure: true).deploy do |app| + assert_match "Gemfile.lock required", app.output +end +``` -Phantom errors happen. To auto retry deploy failures set the environment variable `HATCHET_RETRIES=3` which will auto retry deploys 3 times. By default deploys will not be retried. Once the number of retries has occurred the last exception will be raised. +Since an error will be raised on failed deploys you don't need to check for a deployed status (the error will automatically fail the test for you). -## The Future +## Running Processes -### Speed +Often times asserting output of a build can only get you so far, and you will need to actually run a task on the dyno. To run a non-interactive command such as `heroku run ls` you can do this using the `app.run()` command and do not pass it a block -Efforts may be spent optimizing / parallelizing the process, almost all of the time of the test is spent waiting for IO, so hopefully we should be able to parallelize many tests / deploys at the same time. The hardest part of this (i believe) would be splitting out the different runs into different log streams so that the output wouldn't be completely useless. +```ruby +Hatchet::AnvilApp.new("rails3_mri_193").deploy do |app| + assert_match "applications.css", app.run("ls public/assets") +``` -Right now running 1 deploy test takes about 3 min on my machine. +This is useful for checking the existence of generated files such as assets. If you need to run an interactive session such as `heroku run bash` or `heroku run rails console` you can use the run command and pass a block: -## Git Based Deploys +```ruby +Hatchet::AnvilApp.new("rails3_mri_193").deploy do |app| + app.run("bash") do |bash| + bash.run("ls") {|result| assert_match "Gemfile.lock", result } + bash.run("cat Procfile") {|result| assert_match "web:", result } + end +end +``` -It would be great to allow hatchet to deploy apps off of git url, however if we do that we could open ourselves up to false negatives, if we are pointing at an external repo that gets broken. +or +```ruby +Hatchet::AnvilApp.new("rails3_mri_193").deploy do |app| + app.run("rails console") do |console| + console.run("a = 1 + 2") {|result| assert_match "3", result } + console.run("'foo' * a") {|result| assert_match "foofoofoo", result } + end +end +``` -## Features? +This functionality is provided by [repl_runner](http://github.com/schneems/repl_runner). Please read the docs on that readme for more info. The only interactive commands that are supported out of the box are `rails console`, `bash`, and `irb` it is fairly easy to add your own though: -What else do we want to test? Config vars, addons, etc. Let's write some tests. +``` +ReplRunner.register_commands(:python) do |config| + config.terminate_command "exit()" # the command you use to end the 'python' console + config.startup_timeout 60 # seconds to boot + config.return_char "\n" # the character that submits the command +end +``` + +If you have questions on setting running other interactive commands message [@schneems](http://twitter.com/schneems) + +## Writing Tests + +Hatchet is test framework agnostic. [This project](https://github.com/heroku/hatchet) uses `Test::Unit` to run it's own tests. While the [heroku-ruby-buildpack](https://github.com/heroku/heroku-buildpack-ruby) uses rspec. + +Rspec has a number of advantages, the ability to run `focused: true` to only run the exact test you want as well as the ability to tag tests. Rspec also has a number of useful plugins, one especialy useful one is `gem 'rspec-retry'` which will re-run any failed tests a given number of times (I recommend setting this to at least 2) this decrease the number of false negatives your tests will have. + +Whatever testing framework you chose, we recommend using a parallel test runner when running the full suite [parallel_tests](https://github.com/grosser/parallel_tests) works with rspec and test::unit and is amazing. + +If you're unfamiliar with the ruby testing eco-system or want some help with boilerplate and work for Heroku: [@schneems](http://twitter.com/schneems) can help you get started. Looking at existing projects is a good place to get started + +## Testing on Travis + +Once you've got your tests working locally, you'll likely want to get them running on Travis because a) CI is awesome, and b) you can use pull requests to run your all your tests in parallel without having to kill your network connection. + +To run on travis you will need to configure your `.travis.yml` to run the appropriate commands and to set up encrypted data so you can run tests against a valid heroku user. + +For reference see the `.travis.yml` from [hatchet](https://github.com/heroku/hatchet/blob/master/.travis.yml) and the [heroku-ruby-buildpack](https://github.com/heroku/heroku-buildpack-ruby/blob/master/.travis.yml). To make running on travis easier there is a rake task in Hatchet that can be run before your tests are executed + +``` +before_script: bundle exec rake hatchet:setup_travis +``` + +I recommend signing up for a new heroku account for running your tests on travis, otherwise you will quickly excede your API limit. Once you have the new api token you can use this technique to [securely send travis the data](http://about.travis-ci.org/docs/user/build-configuration/#Secure-environment-variables). + + +## Extra App Commands + +``` +app.add_database # adds a database to specified app +app.heroku # returns a Herou Api client https://github.com/heroku/heroku.rb +``` + +## Hatchet CLI + +Hatchet has a CLI for installing and maintaining external repos you're +using to test against. If you have Hatchet installed as a gem run + + $ hatchet --help + +For more info on commands. If you're using the source code you can run +the command by going to the source code directory and running: + + $ ./bin/hatchet --help + + + +