| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 1 | # Contributing to gophercloud |
| 2 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 3 | - [Getting started](#getting-started) |
| 4 | - [Tests](#tests) |
| 5 | - [Style guide](#basic-style-guide) |
| 6 | - [4 ways to get involved](#4-ways-to-get-involved) |
| 7 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 8 | ## Setting up your git workspace |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 9 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 10 | As a contributor you will need to setup your workspace in a slightly different |
| 11 | way than just downloading it. Here are the basic installation instructions: |
| 12 | |
| Jamie Hannaford | bc35d05 | 2014-10-07 16:27:30 +0200 | [diff] [blame^] | 13 | 1. Configure your `$GOPATH` and run `go get` as described in the main |
| 14 | [README](/#how-to-install). |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 15 | |
| 16 | 2. Move into the directory that houses your local repository: |
| 17 | |
| 18 | ```bash |
| Jamie Hannaford | bc35d05 | 2014-10-07 16:27:30 +0200 | [diff] [blame^] | 19 | cd ${GOPATH}/src/github.com/rackspace/gophercloud |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 20 | ``` |
| 21 | |
| 22 | 3. Fork the `rackspace/gophercloud` repository and update your remote refs. You |
| 23 | will need to rename the `origin` remote branch to `upstream`, and add your |
| 24 | fork as `origin` instead: |
| 25 | |
| 26 | ```bash |
| 27 | git remote rename origin upstream |
| 28 | git remote add origin git@github.com/<my_username>/gophercloud |
| 29 | ``` |
| 30 | |
| 31 | 4. Checkout the latest development branch ([click here](/branches) to see all |
| 32 | the branches): |
| 33 | |
| 34 | ```bash |
| 35 | git checkout v0.2.0 |
| 36 | ``` |
| 37 | |
| 38 | 5. If you're working on something (discussed more in detail below), you will |
| 39 | need to checkout a new feature branch: |
| 40 | |
| 41 | ```bash |
| 42 | git checkout -b my-new-feature |
| 43 | ``` |
| 44 | |
| 45 | Another thing to bear in mind is that you will need to add a few extra |
| 46 | environment variables for acceptance tests - this is documented in our |
| 47 | [acceptance tests readme](/acceptance). |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 48 | |
| 49 | ## Tests |
| 50 | |
| 51 | When working on a new or existing feature, testing will be the backbone of your |
| 52 | work since it helps uncover and prevent regressions in the codebase. There are |
| 53 | two types of test we use in gophercloud: unit tests and acceptance tests, which |
| 54 | are both described below. |
| 55 | |
| 56 | ### Unit tests |
| 57 | |
| 58 | Unit tests are the fine-grained tests that establish and ensure the behaviour |
| 59 | of individual units of functionality. We usually test on an |
| 60 | operation-by-operation basis (an operation typically being an API action) with |
| 61 | the use of mocking to set up explicit expectations. Each operation will set up |
| 62 | its HTTP response expectation, and then test how the system responds when fed |
| 63 | this controlled, pre-determined input. |
| 64 | |
| 65 | To make life easier, we've introduced a bunch of test helpers to simplify the |
| 66 | process of testing expectations with assertions: |
| 67 | |
| 68 | ```go |
| 69 | import ( |
| 70 | "testing" |
| 71 | |
| 72 | "github.com/rackspace/gophercloud/testhelper" |
| 73 | ) |
| 74 | |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 75 | func TestSomething(t *testing.T) { |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 76 | result, err := Operation() |
| 77 | |
| 78 | testhelper.AssertEquals(t, "foo", result.Bar) |
| 79 | testhelper.AssertNoErr(t, err) |
| 80 | } |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 81 | |
| 82 | func TestSomethingElse(t *testing.T) { |
| 83 | testhelper.CheckEquals(t, "expected", "actual") |
| 84 | } |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 85 | ``` |
| 86 | |
| 87 | `AssertEquals` and `AssertNoErr` will throw a fatal error if a value does not |
| 88 | match an expected value or if an error has been declared, respectively. You can |
| 89 | also use `CheckEquals` and `CheckNoErr` for the same purpose; the only difference |
| 90 | being that `t.Errorf` is raised rather than `t.Fatalf`. |
| 91 | |
| 92 | Here is a truncated example of mocked HTTP responses: |
| 93 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 94 | ```go |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 95 | import ( |
| 96 | "testing" |
| 97 | |
| 98 | th "github.com/rackspace/gophercloud/testhelper" |
| 99 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 100 | ) |
| 101 | |
| 102 | func TestGet(t *testing.T) { |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 103 | // Setup the HTTP request multiplexer and server |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 104 | th.SetupHTTP() |
| 105 | defer th.TeardownHTTP() |
| 106 | |
| 107 | th.Mux.HandleFunc("/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 108 | // Test we're using the correct HTTP method |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 109 | th.TestMethod(t, r, "GET") |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 110 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 111 | // Test we're setting the auth token |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 112 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 113 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 114 | // Set the appropriate headers for our mocked response |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 115 | w.Header().Add("Content-Type", "application/json") |
| 116 | w.WriteHeader(http.StatusOK) |
| 117 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 118 | // Set the HTTP body |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 119 | fmt.Fprintf(w, ` |
| 120 | { |
| 121 | "network": { |
| 122 | "status": "ACTIVE", |
| 123 | "name": "private-network", |
| 124 | "admin_state_up": true, |
| 125 | "tenant_id": "4fd44f30292945e481c7b8a0c8908869", |
| 126 | "shared": true, |
| 127 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 128 | } |
| 129 | } |
| 130 | `) |
| 131 | }) |
| 132 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 133 | // Call our API operation |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 134 | network, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
| 135 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 136 | // Assert no errors and equality |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 137 | th.AssertNoErr(t, err) |
| 138 | th.AssertEquals(t, n.Status, "ACTIVE") |
| 139 | } |
| 140 | ``` |
| 141 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 142 | ### Acceptance tests |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 143 | |
| 144 | As we've already mentioned, unit tests have a very narrow and confined focus - |
| 145 | they test small units of behaviour. Acceptance tests on the other hand have a |
| 146 | far larger scope: they are fully functional tests that test the entire API of a |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 147 | service in one fell swoop. They don't care about unit isolation or mocking |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 148 | expectations, they instead do a full run-through and consequently test how the |
| 149 | entire system _integrates_ together. When an API satisfies expectations, it |
| 150 | proves by default that the requirements for a contract have been met. |
| 151 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 152 | Please be aware that acceptance tests will hit a live API - and may incur |
| 153 | service charges from your provider. Although most tests handle their own |
| 154 | teardown procedures, it is always worth manually checking that resources are |
| 155 | deleted after the test suite finishes. |
| 156 | |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 157 | ### Running tests |
| 158 | |
| 159 | To run all tests: |
| 160 | |
| 161 | ```bash |
| 162 | go test ./... |
| 163 | ``` |
| 164 | |
| 165 | To run all tests with verbose output: |
| 166 | |
| 167 | ```bash |
| 168 | go test -v ./... |
| 169 | ``` |
| 170 | |
| 171 | To run tests that match certain [build tags](): |
| 172 | |
| 173 | ```bash |
| 174 | go test -tags "foo bar" ./... |
| 175 | ``` |
| 176 | |
| 177 | To run tests for a particular sub-package: |
| 178 | |
| 179 | ```bash |
| 180 | cd ./path/to/package && go test . |
| 181 | ``` |
| 182 | |
| 183 | ## Basic style guide |
| 184 | |
| 185 | We follow the standard formatting recommendations and language idioms set out |
| 186 | in the [Effective Go](https://golang.org/doc/effective_go.html) guide. It's |
| 187 | definitely worth reading - but the relevant sections are |
| 188 | [formatting](https://golang.org/doc/effective_go.html#formatting) |
| 189 | and [names](https://golang.org/doc/effective_go.html#names). |
| 190 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 191 | ## 5 ways to get involved |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 192 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 193 | There are five main ways you can get involved in our open-source project, and |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 194 | each is described briefly below. Once you've made up your mind and decided on |
| 195 | your fix, you will need to follow the same basic steps that all submissions are |
| 196 | required to adhere to: |
| 197 | |
| 198 | 1. [fork](https://help.github.com/articles/fork-a-repo/) the `rackspace/gophercloud` repository |
| 199 | 2. checkout a [new branch](https://github.com/Kunena/Kunena-Forum/wiki/Create-a-new-branch-with-git-and-manage-branches) |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 200 | 3. submit your branch as a [pull request](https://help.github.com/articles/creating-a-pull-request/) |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 201 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 202 | ### 1. Providing feedback |
| 203 | |
| 204 | On of the easiest ways to get readily involved in our project is to let us know |
| 205 | about your experiences using our SDK. Feedback like this is incredibly useful |
| 206 | to us, because it allows us to refine and change features based on what our |
| 207 | users want and expect of us. There are a bunch of ways to get in contact! You |
| 208 | can [ping us](mailto:sdk-support@rackspace.com) via e-mail, talk to us on irc |
| 209 | (#rackspace-dev on freenode), [tweet us](https://twitter.com/rackspace), or |
| 210 | submit an issue on our [bug tracker](/issues). Things you might like to tell us |
| 211 | are: |
| 212 | |
| 213 | * how easy was it to start using our SDK? |
| 214 | * did it meet your expectations? If not, why not? |
| 215 | * did our documentation help or hinder you? |
| 216 | * what could we improve in general? |
| 217 | |
| 218 | ### 2. Fixing bugs |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 219 | |
| 220 | If you want to start fixing open bugs, we'd really appreciate that! Bug fixing |
| 221 | is central to any project. The best way to get started is by heading to our |
| 222 | [bug tracker](https://github.com/rackspace/gophercloud/issues) and finding open |
| 223 | bugs that you think nobody is working on. It might be useful to comment on the |
| 224 | thread to see the current state of the issue and if anybody has made any |
| 225 | breakthroughs on it so far. |
| 226 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 227 | ### 3. Improving documentation |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 228 | |
| 229 | We have three forms of documentation: |
| 230 | |
| 231 | * short README documents that briefly introduce a topic |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 232 | * reference documentation on [godoc.org](http://godoc.org) that is automatically |
| 233 | generated from source code comments |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 234 | * user documentation on our [homepage](http://gophercloud.io) that includes |
| 235 | getting started guides, installation guides and code samples |
| 236 | |
| Jamie Hannaford | bc35d05 | 2014-10-07 16:27:30 +0200 | [diff] [blame^] | 237 | If you feel that a certain section could be improved - whether it's to clarify |
| 238 | ambiguity, correct a technical mistake, or to fix a grammatical error - please |
| 239 | feel entitled to do so! We welcome doc pull requests with the same childlike |
| 240 | enthusiasm as any other contribution! |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 241 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 242 | ### 4. Optimizing existing features |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 243 | |
| 244 | If you would like to improve or optimize an existing feature, please be aware |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 245 | that we adhere to [semantic versioning](http://semver.org) - which means that |
| 246 | we cannot introduce breaking changes to the API without a major version change |
| 247 | (v1.x -> v2.x). Making that leap is a big step, so we encourage contributors to |
| 248 | refactor rather than rewrite. Running tests will prevent regression and avoid |
| 249 | the possibility of breaking somebody's current implementation. |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 250 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 251 | Another tip is to keep the focus of your work as small as possible - try not to |
| 252 | introduce a change that affects lots and lots of files because it introduces |
| 253 | added risk and increases the cognitive load on the reviewers checking your |
| 254 | work. Change-sets which are easily understood and will not negatively impact |
| 255 | users are more likely to be integrated quickly. |
| 256 | |
| 257 | Lastly, if you're seeking to optimize a particular operation, you should try to |
| 258 | demonstrate a negative performance impact - perhaps using go's inbuilt |
| 259 | [benchmark capabilities](http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go). |
| 260 | |
| 261 | ### 5. Working on a new feature |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 262 | |
| 263 | If you've found something we've left out, definitely feel free to start work on |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 264 | introducing that feature. It's always useful to open an issue or submit a pull |
| 265 | request early on to indicate your intent to a core contributor - this enables |
| 266 | quick/early feedback and can help steer you in the right direction by avoiding |
| 267 | known issues. It might also help you avoid losing time implementing something |
| 268 | that might not ever work. One tip is to prefix your Pull Request issue title |
| 269 | with [wip] - then people know it's a work in progress. |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 270 | |
| 271 | You must ensure that all of your work is well tested - both in terms of unit |
| 272 | and acceptance tests. Untested code will not be merged because it introduces |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 273 | too much of a risk to end-users. |
| 274 | |
| 275 | Happy hacking! |