| jrperritt | 94ae0eb | 2016-07-11 22:53:34 -0500 | [diff] [blame^] | 1 | # Contributing to Gophercloud |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 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) |
| jrperritt | 94ae0eb | 2016-07-11 22:53:34 -0500 | [diff] [blame^] | 6 | - [3 ways to get involved](#5-ways-to-get-involved) |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 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 |
| Rickard von Essen | f66314b | 2016-02-13 11:35:00 +0100 | [diff] [blame] | 14 | [README](/README.md#how-to-install) but add `-tags "fixtures acceptance"` to |
| 15 | get dependencies for unit and acceptance tests. |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 16 | |
| 17 | 2. Move into the directory that houses your local repository: |
| 18 | |
| Jamie Hannaford | 54bbe99 | 2014-10-07 16:50:32 +0200 | [diff] [blame] | 19 | ```bash |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 20 | cd ${GOPATH}/src/github.com/gophercloud/gophercloud |
| Jamie Hannaford | 54bbe99 | 2014-10-07 16:50:32 +0200 | [diff] [blame] | 21 | ``` |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 22 | |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 23 | 3. Fork the `gophercloud/gophercloud` repository and update your remote refs. You |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 24 | will need to rename the `origin` remote branch to `upstream`, and add your |
| 25 | fork as `origin` instead: |
| 26 | |
| Jamie Hannaford | 54bbe99 | 2014-10-07 16:50:32 +0200 | [diff] [blame] | 27 | ```bash |
| 28 | git remote rename origin upstream |
| 29 | git remote add origin git@github.com/<my_username>/gophercloud |
| 30 | ``` |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 31 | |
| jrperritt | 1d8b6f1 | 2015-08-14 12:17:09 -0600 | [diff] [blame] | 32 | 4. Checkout the latest development branch: |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 33 | |
| Jamie Hannaford | 54bbe99 | 2014-10-07 16:50:32 +0200 | [diff] [blame] | 34 | ```bash |
| jrperritt | 1d8b6f1 | 2015-08-14 12:17:09 -0600 | [diff] [blame] | 35 | git checkout master |
| Jamie Hannaford | 54bbe99 | 2014-10-07 16:50:32 +0200 | [diff] [blame] | 36 | ``` |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 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 | |
| Jamie Hannaford | 54bbe99 | 2014-10-07 16:50:32 +0200 | [diff] [blame] | 41 | ```bash |
| 42 | git checkout -b my-new-feature |
| 43 | ``` |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 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 |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 53 | two types of test we use in Gophercloud: unit tests and acceptance tests, which |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 54 | are both described below. |
| 55 | |
| 56 | ### Unit tests |
| 57 | |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 58 | Unit tests are the fine-grained tests that establish and ensure the behavior |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 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 | |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 72 | "github.com/gophercloud/gophercloud/testhelper" |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 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 | |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 98 | th "github.com/gophercloud/gophercloud/testhelper" |
| 99 | fake "github.com/gophercloud/gophercloud/testhelper/client" |
| jrperritt | 94ae0eb | 2016-07-11 22:53:34 -0500 | [diff] [blame^] | 100 | "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 101 | ) |
| 102 | |
| 103 | func TestGet(t *testing.T) { |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 104 | // Setup the HTTP request multiplexer and server |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 105 | th.SetupHTTP() |
| 106 | defer th.TeardownHTTP() |
| 107 | |
| 108 | th.Mux.HandleFunc("/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 109 | // Test we're using the correct HTTP method |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 110 | th.TestMethod(t, r, "GET") |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 111 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 112 | // Test we're setting the auth token |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 113 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 114 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 115 | // Set the appropriate headers for our mocked response |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 116 | w.Header().Add("Content-Type", "application/json") |
| 117 | w.WriteHeader(http.StatusOK) |
| 118 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 119 | // Set the HTTP body |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 120 | fmt.Fprintf(w, ` |
| 121 | { |
| 122 | "network": { |
| 123 | "status": "ACTIVE", |
| 124 | "name": "private-network", |
| 125 | "admin_state_up": true, |
| 126 | "tenant_id": "4fd44f30292945e481c7b8a0c8908869", |
| 127 | "shared": true, |
| 128 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 129 | } |
| 130 | } |
| 131 | `) |
| 132 | }) |
| 133 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 134 | // Call our API operation |
| jrperritt | 94ae0eb | 2016-07-11 22:53:34 -0500 | [diff] [blame^] | 135 | network, err := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 136 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 137 | // Assert no errors and equality |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 138 | th.AssertNoErr(t, err) |
| 139 | th.AssertEquals(t, n.Status, "ACTIVE") |
| 140 | } |
| 141 | ``` |
| 142 | |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 143 | ### Acceptance tests |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 144 | |
| 145 | As we've already mentioned, unit tests have a very narrow and confined focus - |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 146 | they test small units of behavior. Acceptance tests on the other hand have a |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 147 | 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] | 148 | 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] | 149 | expectations, they instead do a full run-through and consequently test how the |
| 150 | entire system _integrates_ together. When an API satisfies expectations, it |
| 151 | proves by default that the requirements for a contract have been met. |
| 152 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 153 | Please be aware that acceptance tests will hit a live API - and may incur |
| 154 | service charges from your provider. Although most tests handle their own |
| 155 | teardown procedures, it is always worth manually checking that resources are |
| 156 | deleted after the test suite finishes. |
| 157 | |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 158 | ### Running tests |
| 159 | |
| 160 | To run all tests: |
| 161 | |
| 162 | ```bash |
| Rickard von Essen | f66314b | 2016-02-13 11:35:00 +0100 | [diff] [blame] | 163 | go test -tags fixtures ./... |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 164 | ``` |
| 165 | |
| 166 | To run all tests with verbose output: |
| 167 | |
| 168 | ```bash |
| Rickard von Essen | f66314b | 2016-02-13 11:35:00 +0100 | [diff] [blame] | 169 | go test -v -tags fixtures ./... |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 170 | ``` |
| 171 | |
| 172 | To run tests that match certain [build tags](): |
| 173 | |
| 174 | ```bash |
| Rickard von Essen | f66314b | 2016-02-13 11:35:00 +0100 | [diff] [blame] | 175 | go test -tags "fixtures foo bar" ./... |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 176 | ``` |
| 177 | |
| 178 | To run tests for a particular sub-package: |
| 179 | |
| 180 | ```bash |
| Rickard von Essen | f66314b | 2016-02-13 11:35:00 +0100 | [diff] [blame] | 181 | cd ./path/to/package && go test -tags fixtures . |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 182 | ``` |
| 183 | |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 184 | ## Style guide |
| 185 | |
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 186 | See [here](/STYLEGUIDE.md) |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 187 | |
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 188 | ## 3 ways to get involved |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 189 | |
| Jamie Hannaford | d2773f2 | 2014-10-07 16:06:23 +0200 | [diff] [blame] | 190 | 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] | 191 | each is described briefly below. Once you've made up your mind and decided on |
| 192 | your fix, you will need to follow the same basic steps that all submissions are |
| 193 | required to adhere to: |
| 194 | |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 195 | 1. [fork](https://help.github.com/articles/fork-a-repo/) the `gophercloud/gophercloud` repository |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 196 | 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] | 197 | 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] | 198 | |
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 199 | ### 1. Fixing bugs |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 200 | |
| 201 | If you want to start fixing open bugs, we'd really appreciate that! Bug fixing |
| 202 | is central to any project. The best way to get started is by heading to our |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 203 | [bug tracker](https://github.com/gophercloud/gophercloud/issues) and finding open |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 204 | bugs that you think nobody is working on. It might be useful to comment on the |
| 205 | thread to see the current state of the issue and if anybody has made any |
| 206 | breakthroughs on it so far. |
| 207 | |
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 208 | ### 2. Improving documentation |
| 209 | The best source of documentation is on [godoc.org](http://godoc.org). It is |
| 210 | automatically generated from the source code. |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 211 | |
| Jamie Hannaford | bc35d05 | 2014-10-07 16:27:30 +0200 | [diff] [blame] | 212 | If you feel that a certain section could be improved - whether it's to clarify |
| 213 | ambiguity, correct a technical mistake, or to fix a grammatical error - please |
| 214 | feel entitled to do so! We welcome doc pull requests with the same childlike |
| 215 | enthusiasm as any other contribution! |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 216 | |
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 217 | ###3. Working on a new feature |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 218 | |
| 219 | 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] | 220 | introducing that feature. It's always useful to open an issue or submit a pull |
| 221 | request early on to indicate your intent to a core contributor - this enables |
| 222 | quick/early feedback and can help steer you in the right direction by avoiding |
| 223 | known issues. It might also help you avoid losing time implementing something |
| 224 | that might not ever work. One tip is to prefix your Pull Request issue title |
| 225 | with [wip] - then people know it's a work in progress. |
| Jamie Hannaford | d5a1cb7 | 2014-10-07 14:31:27 +0200 | [diff] [blame] | 226 | |
| 227 | You must ensure that all of your work is well tested - both in terms of unit |
| 228 | and acceptance tests. Untested code will not be merged because it introduces |
| Jamie Hannaford | d2b6dfc | 2014-10-07 14:46:55 +0200 | [diff] [blame] | 229 | too much of a risk to end-users. |
| 230 | |
| 231 | Happy hacking! |