blob: 0d85ca8333728eaed906559149535c6a313f0b39 [file] [log] [blame] [view]
jrperritt94ae0eb2016-07-11 22:53:34 -05001# Contributing to Gophercloud
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +02002
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +02003- [Getting started](#getting-started)
4- [Tests](#tests)
5- [Style guide](#basic-style-guide)
jrperritt94ae0eb2016-07-11 22:53:34 -05006- [3 ways to get involved](#5-ways-to-get-involved)
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +02007
Jamie Hannafordd2773f22014-10-07 16:06:23 +02008## Setting up your git workspace
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +02009
Jamie Hannafordd2773f22014-10-07 16:06:23 +020010As a contributor you will need to setup your workspace in a slightly different
11way than just downloading it. Here are the basic installation instructions:
12
Jamie Hannafordbc35d052014-10-07 16:27:30 +0200131. Configure your `$GOPATH` and run `go get` as described in the main
Rickard von Essenf66314b2016-02-13 11:35:00 +010014[README](/README.md#how-to-install) but add `-tags "fixtures acceptance"` to
15get dependencies for unit and acceptance tests.
Jamie Hannafordd2773f22014-10-07 16:06:23 +020016
172. Move into the directory that houses your local repository:
18
Jamie Hannaford54bbe992014-10-07 16:50:32 +020019 ```bash
jrperritt55fb5842016-04-13 14:16:08 -050020 cd ${GOPATH}/src/github.com/gophercloud/gophercloud
Jamie Hannaford54bbe992014-10-07 16:50:32 +020021 ```
Jamie Hannafordd2773f22014-10-07 16:06:23 +020022
jrperritt55fb5842016-04-13 14:16:08 -0500233. Fork the `gophercloud/gophercloud` repository and update your remote refs. You
Jamie Hannafordd2773f22014-10-07 16:06:23 +020024will need to rename the `origin` remote branch to `upstream`, and add your
25fork as `origin` instead:
26
Jamie Hannaford54bbe992014-10-07 16:50:32 +020027 ```bash
28 git remote rename origin upstream
29 git remote add origin git@github.com/<my_username>/gophercloud
30 ```
Jamie Hannafordd2773f22014-10-07 16:06:23 +020031
jrperritt1d8b6f12015-08-14 12:17:09 -0600324. Checkout the latest development branch:
Jamie Hannafordd2773f22014-10-07 16:06:23 +020033
Jamie Hannaford54bbe992014-10-07 16:50:32 +020034 ```bash
jrperritt1d8b6f12015-08-14 12:17:09 -060035 git checkout master
Jamie Hannaford54bbe992014-10-07 16:50:32 +020036 ```
Jamie Hannafordd2773f22014-10-07 16:06:23 +020037
385. If you're working on something (discussed more in detail below), you will
39need to checkout a new feature branch:
40
Jamie Hannaford54bbe992014-10-07 16:50:32 +020041 ```bash
42 git checkout -b my-new-feature
43 ```
Jamie Hannafordd2773f22014-10-07 16:06:23 +020044
45Another thing to bear in mind is that you will need to add a few extra
46environment variables for acceptance tests - this is documented in our
47[acceptance tests readme](/acceptance).
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +020048
49## Tests
50
51When working on a new or existing feature, testing will be the backbone of your
52work since it helps uncover and prevent regressions in the codebase. There are
jrperritt55fb5842016-04-13 14:16:08 -050053two types of test we use in Gophercloud: unit tests and acceptance tests, which
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +020054are both described below.
55
56### Unit tests
57
jrperritt55fb5842016-04-13 14:16:08 -050058Unit tests are the fine-grained tests that establish and ensure the behavior
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +020059of individual units of functionality. We usually test on an
60operation-by-operation basis (an operation typically being an API action) with
61the use of mocking to set up explicit expectations. Each operation will set up
62its HTTP response expectation, and then test how the system responds when fed
63this controlled, pre-determined input.
64
65To make life easier, we've introduced a bunch of test helpers to simplify the
66process of testing expectations with assertions:
67
68```go
69import (
70 "testing"
71
jrperritt55fb5842016-04-13 14:16:08 -050072 "github.com/gophercloud/gophercloud/testhelper"
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +020073)
74
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +020075func TestSomething(t *testing.T) {
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +020076 result, err := Operation()
77
78 testhelper.AssertEquals(t, "foo", result.Bar)
79 testhelper.AssertNoErr(t, err)
80}
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +020081
82func TestSomethingElse(t *testing.T) {
83 testhelper.CheckEquals(t, "expected", "actual")
84}
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +020085```
86
87`AssertEquals` and `AssertNoErr` will throw a fatal error if a value does not
88match an expected value or if an error has been declared, respectively. You can
89also use `CheckEquals` and `CheckNoErr` for the same purpose; the only difference
90being that `t.Errorf` is raised rather than `t.Fatalf`.
91
92Here is a truncated example of mocked HTTP responses:
93
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +020094```go
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +020095import (
96 "testing"
97
jrperritt55fb5842016-04-13 14:16:08 -050098 th "github.com/gophercloud/gophercloud/testhelper"
99 fake "github.com/gophercloud/gophercloud/testhelper/client"
jrperritt94ae0eb2016-07-11 22:53:34 -0500100 "github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200101)
102
103func TestGet(t *testing.T) {
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200104 // Setup the HTTP request multiplexer and server
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200105 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 Hannafordd2b6dfc2014-10-07 14:46:55 +0200110 th.TestMethod(t, r, "GET")
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200111
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200112 // Test we're setting the auth token
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200113 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
114
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200115 // Set the appropriate headers for our mocked response
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200116 w.Header().Add("Content-Type", "application/json")
117 w.WriteHeader(http.StatusOK)
118
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200119 // Set the HTTP body
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200120 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 Hannafordd2b6dfc2014-10-07 14:46:55 +0200134 // Call our API operation
jrperritt94ae0eb2016-07-11 22:53:34 -0500135 network, err := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200136
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200137 // Assert no errors and equality
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200138 th.AssertNoErr(t, err)
139 th.AssertEquals(t, n.Status, "ACTIVE")
140}
141```
142
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200143### Acceptance tests
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200144
145As we've already mentioned, unit tests have a very narrow and confined focus -
jrperritt55fb5842016-04-13 14:16:08 -0500146they test small units of behavior. Acceptance tests on the other hand have a
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200147far larger scope: they are fully functional tests that test the entire API of a
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200148service in one fell swoop. They don't care about unit isolation or mocking
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200149expectations, they instead do a full run-through and consequently test how the
150entire system _integrates_ together. When an API satisfies expectations, it
151proves by default that the requirements for a contract have been met.
152
Jamie Hannafordd2773f22014-10-07 16:06:23 +0200153Please be aware that acceptance tests will hit a live API - and may incur
154service charges from your provider. Although most tests handle their own
155teardown procedures, it is always worth manually checking that resources are
156deleted after the test suite finishes.
157
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200158### Running tests
159
160To run all tests:
161
162```bash
Rickard von Essenf66314b2016-02-13 11:35:00 +0100163go test -tags fixtures ./...
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200164```
165
166To run all tests with verbose output:
167
168```bash
Rickard von Essenf66314b2016-02-13 11:35:00 +0100169go test -v -tags fixtures ./...
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200170```
171
172To run tests that match certain [build tags]():
173
174```bash
Rickard von Essenf66314b2016-02-13 11:35:00 +0100175go test -tags "fixtures foo bar" ./...
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200176```
177
178To run tests for a particular sub-package:
179
180```bash
Rickard von Essenf66314b2016-02-13 11:35:00 +0100181cd ./path/to/package && go test -tags fixtures .
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200182```
183
jrperritt55fb5842016-04-13 14:16:08 -0500184## Style guide
185
jrperritt9b7b9e62016-07-11 22:30:50 -0500186See [here](/STYLEGUIDE.md)
jrperritt55fb5842016-04-13 14:16:08 -0500187
jrperritt9b7b9e62016-07-11 22:30:50 -0500188## 3 ways to get involved
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200189
Jamie Hannafordd2773f22014-10-07 16:06:23 +0200190There are five main ways you can get involved in our open-source project, and
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200191each is described briefly below. Once you've made up your mind and decided on
192your fix, you will need to follow the same basic steps that all submissions are
193required to adhere to:
194
jrperritt55fb5842016-04-13 14:16:08 -05001951. [fork](https://help.github.com/articles/fork-a-repo/) the `gophercloud/gophercloud` repository
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +02001962. checkout a [new branch](https://github.com/Kunena/Kunena-Forum/wiki/Create-a-new-branch-with-git-and-manage-branches)
Jamie Hannafordd2773f22014-10-07 16:06:23 +02001973. submit your branch as a [pull request](https://help.github.com/articles/creating-a-pull-request/)
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200198
jrperritt9b7b9e62016-07-11 22:30:50 -0500199### 1. Fixing bugs
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200200
201If you want to start fixing open bugs, we'd really appreciate that! Bug fixing
202is central to any project. The best way to get started is by heading to our
jrperritt55fb5842016-04-13 14:16:08 -0500203[bug tracker](https://github.com/gophercloud/gophercloud/issues) and finding open
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200204bugs that you think nobody is working on. It might be useful to comment on the
205thread to see the current state of the issue and if anybody has made any
206breakthroughs on it so far.
207
jrperritt9b7b9e62016-07-11 22:30:50 -0500208### 2. Improving documentation
209The best source of documentation is on [godoc.org](http://godoc.org). It is
210automatically generated from the source code.
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200211
Jamie Hannafordbc35d052014-10-07 16:27:30 +0200212If you feel that a certain section could be improved - whether it's to clarify
213ambiguity, correct a technical mistake, or to fix a grammatical error - please
214feel entitled to do so! We welcome doc pull requests with the same childlike
215enthusiasm as any other contribution!
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200216
jrperritt9b7b9e62016-07-11 22:30:50 -0500217###3. Working on a new feature
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200218
219If you've found something we've left out, definitely feel free to start work on
Jamie Hannafordd2773f22014-10-07 16:06:23 +0200220introducing that feature. It's always useful to open an issue or submit a pull
221request early on to indicate your intent to a core contributor - this enables
222quick/early feedback and can help steer you in the right direction by avoiding
223known issues. It might also help you avoid losing time implementing something
224that might not ever work. One tip is to prefix your Pull Request issue title
225with [wip] - then people know it's a work in progress.
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200226
227You must ensure that all of your work is well tested - both in terms of unit
228and acceptance tests. Untested code will not be merged because it introduces
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200229too much of a risk to end-users.
230
231Happy hacking!