| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame^] | 1 | # Contributing to gophercloud |
| 2 | |
| 3 | ## Getting started |
| 4 | |
| 5 | There should be no fundamental differences of setup between contributors and |
| 6 | normal end-users. The only thing to bear in mind is that you will need to add a |
| 7 | few extra environment variables for acceptance tests - this is documented in |
| 8 | our [acceptance tests readme](/acceptance). |
| 9 | |
| 10 | ## Tests |
| 11 | |
| 12 | When working on a new or existing feature, testing will be the backbone of your |
| 13 | work since it helps uncover and prevent regressions in the codebase. There are |
| 14 | two types of test we use in gophercloud: unit tests and acceptance tests, which |
| 15 | are both described below. |
| 16 | |
| 17 | ### Unit tests |
| 18 | |
| 19 | Unit tests are the fine-grained tests that establish and ensure the behaviour |
| 20 | of individual units of functionality. We usually test on an |
| 21 | operation-by-operation basis (an operation typically being an API action) with |
| 22 | the use of mocking to set up explicit expectations. Each operation will set up |
| 23 | its HTTP response expectation, and then test how the system responds when fed |
| 24 | this controlled, pre-determined input. |
| 25 | |
| 26 | To make life easier, we've introduced a bunch of test helpers to simplify the |
| 27 | process of testing expectations with assertions: |
| 28 | |
| 29 | ```go |
| 30 | import ( |
| 31 | "testing" |
| 32 | |
| 33 | "github.com/rackspace/gophercloud/testhelper" |
| 34 | ) |
| 35 | |
| 36 | |
| 37 | func TestSomething(t *testing.T) { |
| 38 | testhelper.AssertEquals(t, "expected", "actual") |
| 39 | } |
| 40 | |
| 41 | func TestErrors(t *testing.T) { |
| 42 | result, err := Operation() |
| 43 | |
| 44 | testhelper.AssertEquals(t, "foo", result.Bar) |
| 45 | testhelper.AssertNoErr(t, err) |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | `AssertEquals` and `AssertNoErr` will throw a fatal error if a value does not |
| 50 | match an expected value or if an error has been declared, respectively. You can |
| 51 | also use `CheckEquals` and `CheckNoErr` for the same purpose; the only difference |
| 52 | being that `t.Errorf` is raised rather than `t.Fatalf`. |
| 53 | |
| 54 | Here is a truncated example of mocked HTTP responses: |
| 55 | |
| 56 | ``` |
| 57 | import ( |
| 58 | "testing" |
| 59 | |
| 60 | th "github.com/rackspace/gophercloud/testhelper" |
| 61 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 62 | ) |
| 63 | |
| 64 | func TestGet(t *testing.T) { |
| 65 | th.SetupHTTP() |
| 66 | defer th.TeardownHTTP() |
| 67 | |
| 68 | th.Mux.HandleFunc("/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 69 | // Test we're using the correct HTTP method |
| 70 | th.TestMethod(t, r, "GET") |
| 71 | |
| 72 | // Test we're setting the auth token |
| 73 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 74 | |
| 75 | // Set the appropriate headers for our mocked response |
| 76 | w.Header().Add("Content-Type", "application/json") |
| 77 | w.WriteHeader(http.StatusOK) |
| 78 | |
| 79 | // Set the HTTP body |
| 80 | fmt.Fprintf(w, ` |
| 81 | { |
| 82 | "network": { |
| 83 | "status": "ACTIVE", |
| 84 | "name": "private-network", |
| 85 | "admin_state_up": true, |
| 86 | "tenant_id": "4fd44f30292945e481c7b8a0c8908869", |
| 87 | "shared": true, |
| 88 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 89 | } |
| 90 | } |
| 91 | `) |
| 92 | }) |
| 93 | |
| 94 | // Call our API operation |
| 95 | network, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
| 96 | |
| 97 | // Assert no errors and equality |
| 98 | th.AssertNoErr(t, err) |
| 99 | th.AssertEquals(t, n.Status, "ACTIVE") |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | ### Acceptance tests + build tags |
| 104 | |
| 105 | As we've already mentioned, unit tests have a very narrow and confined focus - |
| 106 | they test small units of behaviour. Acceptance tests on the other hand have a |
| 107 | far larger scope: they are fully functional tests that test the entire API of a |
| 108 | service in one fell swoop. They don't care about unit independence or mocking |
| 109 | expectations, they instead do a full run-through and consequently test how the |
| 110 | entire system _integrates_ together. When an API satisfies expectations, it |
| 111 | proves by default that the requirements for a contract have been met. |
| 112 | |
| 113 | ### Running tests |
| 114 | |
| 115 | To run all tests: |
| 116 | |
| 117 | ```bash |
| 118 | go test ./... |
| 119 | ``` |
| 120 | |
| 121 | To run all tests with verbose output: |
| 122 | |
| 123 | ```bash |
| 124 | go test -v ./... |
| 125 | ``` |
| 126 | |
| 127 | To run tests that match certain [build tags](): |
| 128 | |
| 129 | ```bash |
| 130 | go test -tags "foo bar" ./... |
| 131 | ``` |
| 132 | |
| 133 | To run tests for a particular sub-package: |
| 134 | |
| 135 | ```bash |
| 136 | cd ./path/to/package && go test . |
| 137 | ``` |
| 138 | |
| 139 | ## Basic style guide |
| 140 | |
| 141 | We follow the standard formatting recommendations and language idioms set out |
| 142 | in the [Effective Go](https://golang.org/doc/effective_go.html) guide. It's |
| 143 | definitely worth reading - but the relevant sections are |
| 144 | [formatting](https://golang.org/doc/effective_go.html#formatting) |
| 145 | and [names](https://golang.org/doc/effective_go.html#names). |
| 146 | |
| 147 | ## 4 ways to get involved |
| 148 | |
| 149 | There are four main ways you can get involved in our open-source project, and |
| 150 | each is described briefly below. Once you've made up your mind and decided on |
| 151 | your fix, you will need to follow the same basic steps that all submissions are |
| 152 | required to adhere to: |
| 153 | |
| 154 | 1. [fork](https://help.github.com/articles/fork-a-repo/) the `rackspace/gophercloud` repository |
| 155 | 2. checkout a [new branch](https://github.com/Kunena/Kunena-Forum/wiki/Create-a-new-branch-with-git-and-manage-branches) |
| 156 | 3. ensure all your commits are [well-organized](https://help.github.com/articles/about-git-rebase/) |
| 157 | 4. submit your branch as a [pull request](https://help.github.com/articles/creating-a-pull-request/) |
| 158 | |
| 159 | ### 1. Fixing bugs |
| 160 | |
| 161 | If you want to start fixing open bugs, we'd really appreciate that! Bug fixing |
| 162 | is central to any project. The best way to get started is by heading to our |
| 163 | [bug tracker](https://github.com/rackspace/gophercloud/issues) and finding open |
| 164 | bugs that you think nobody is working on. It might be useful to comment on the |
| 165 | thread to see the current state of the issue and if anybody has made any |
| 166 | breakthroughs on it so far. |
| 167 | |
| 168 | ### 2. Improving documentation |
| 169 | |
| 170 | We have three forms of documentation: |
| 171 | |
| 172 | * short README documents that briefly introduce a topic |
| 173 | * reference documentation on [godoc.org](http://godoc.org) that automatically |
| 174 | generates from source code comments |
| 175 | * user documentation on our [homepage](http://gophercloud.io) that includes |
| 176 | getting started guides, installation guides and code samples |
| 177 | |
| 178 | If you feel that a certain section could be improved - whether its to clarify |
| 179 | ambiguity or a fix a grammatical mistake - please feel entitled to do so! We |
| 180 | welcome doc pull requests with the same childlike enthusiasm as any other |
| 181 | contribution! |
| 182 | |
| 183 | ### 3. Optimizing existing features |
| 184 | |
| 185 | If you would like to improve or optimize an existing feature, please be aware |
| 186 | that we adhere to semantic versioning] - which means that we cannot introduce |
| 187 | breaking changes to the API without a major version change (v1.x -> v2.x). |
| 188 | Making that leap is a big step, so we encourage contributors to refactor rather |
| 189 | than rewrite. Running tests will prevent regression and avoid the possibility |
| 190 | of breaking somebody's current implementation. |
| 191 | |
| 192 | ### 4. Working on a new feature |
| 193 | |
| 194 | If you've found something we've left out, definitely feel free to start work on |
| 195 | introducing that feature. It's always useful to open an issue first to indicate |
| 196 | your intent to a core contributor - this enables quick feedback and can help |
| 197 | steer you in the right direction by avoiding known issues. It might also help |
| 198 | you avoid losing time implementing something that might not ever work. |
| 199 | |
| 200 | You must ensure that all of your work is well tested - both in terms of unit |
| 201 | and acceptance tests. Untested code will not be merged because it introduces |
| 202 | too much of a risk to end-users. Happy hacking! |