blob: b2713f541b5478821b1f862c2ffb7d47269acdbb [file] [log] [blame] [view]
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +02001# Contributing to gophercloud
2
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +02003- [Getting started](#getting-started)
4- [Tests](#tests)
5- [Style guide](#basic-style-guide)
Jamie Hannaford54bbe992014-10-07 16:50:32 +02006- [5 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"
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200100)
101
102func TestGet(t *testing.T) {
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200103 // Setup the HTTP request multiplexer and server
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200104 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 Hannafordd2b6dfc2014-10-07 14:46:55 +0200109 th.TestMethod(t, r, "GET")
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200110
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200111 // Test we're setting the auth token
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200112 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
113
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200114 // Set the appropriate headers for our mocked response
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200115 w.Header().Add("Content-Type", "application/json")
116 w.WriteHeader(http.StatusOK)
117
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200118 // Set the HTTP body
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200119 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 Hannafordd2b6dfc2014-10-07 14:46:55 +0200133 // Call our API operation
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200134 network, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
135
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200136 // Assert no errors and equality
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200137 th.AssertNoErr(t, err)
138 th.AssertEquals(t, n.Status, "ACTIVE")
139}
140```
141
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200142### Acceptance tests
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200143
144As we've already mentioned, unit tests have a very narrow and confined focus -
jrperritt55fb5842016-04-13 14:16:08 -0500145they test small units of behavior. Acceptance tests on the other hand have a
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200146far larger scope: they are fully functional tests that test the entire API of a
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200147service in one fell swoop. They don't care about unit isolation or mocking
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200148expectations, they instead do a full run-through and consequently test how the
149entire system _integrates_ together. When an API satisfies expectations, it
150proves by default that the requirements for a contract have been met.
151
Jamie Hannafordd2773f22014-10-07 16:06:23 +0200152Please be aware that acceptance tests will hit a live API - and may incur
153service charges from your provider. Although most tests handle their own
154teardown procedures, it is always worth manually checking that resources are
155deleted after the test suite finishes.
156
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200157### Running tests
158
159To run all tests:
160
161```bash
Rickard von Essenf66314b2016-02-13 11:35:00 +0100162go test -tags fixtures ./...
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200163```
164
165To run all tests with verbose output:
166
167```bash
Rickard von Essenf66314b2016-02-13 11:35:00 +0100168go test -v -tags fixtures ./...
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200169```
170
171To run tests that match certain [build tags]():
172
173```bash
Rickard von Essenf66314b2016-02-13 11:35:00 +0100174go test -tags "fixtures foo bar" ./...
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200175```
176
177To run tests for a particular sub-package:
178
179```bash
Rickard von Essenf66314b2016-02-13 11:35:00 +0100180cd ./path/to/package && go test -tags fixtures .
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200181```
182
jrperritt55fb5842016-04-13 14:16:08 -0500183## Style guide
184
jrperritt9b7b9e62016-07-11 22:30:50 -0500185See [here](/STYLEGUIDE.md)
jrperritt55fb5842016-04-13 14:16:08 -0500186
jrperritt9b7b9e62016-07-11 22:30:50 -0500187## 3 ways to get involved
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200188
Jamie Hannafordd2773f22014-10-07 16:06:23 +0200189There are five main ways you can get involved in our open-source project, and
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200190each is described briefly below. Once you've made up your mind and decided on
191your fix, you will need to follow the same basic steps that all submissions are
192required to adhere to:
193
jrperritt55fb5842016-04-13 14:16:08 -05001941. [fork](https://help.github.com/articles/fork-a-repo/) the `gophercloud/gophercloud` repository
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +02001952. 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 +02001963. submit your branch as a [pull request](https://help.github.com/articles/creating-a-pull-request/)
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200197
jrperritt9b7b9e62016-07-11 22:30:50 -0500198### 1. Fixing bugs
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200199
200If you want to start fixing open bugs, we'd really appreciate that! Bug fixing
201is central to any project. The best way to get started is by heading to our
jrperritt55fb5842016-04-13 14:16:08 -0500202[bug tracker](https://github.com/gophercloud/gophercloud/issues) and finding open
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200203bugs that you think nobody is working on. It might be useful to comment on the
204thread to see the current state of the issue and if anybody has made any
205breakthroughs on it so far.
206
jrperritt9b7b9e62016-07-11 22:30:50 -0500207### 2. Improving documentation
208The best source of documentation is on [godoc.org](http://godoc.org). It is
209automatically generated from the source code.
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200210
Jamie Hannafordbc35d052014-10-07 16:27:30 +0200211If you feel that a certain section could be improved - whether it's to clarify
212ambiguity, correct a technical mistake, or to fix a grammatical error - please
213feel entitled to do so! We welcome doc pull requests with the same childlike
214enthusiasm as any other contribution!
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200215
jrperritt9b7b9e62016-07-11 22:30:50 -0500216###3. Working on a new feature
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200217
218If you've found something we've left out, definitely feel free to start work on
Jamie Hannafordd2773f22014-10-07 16:06:23 +0200219introducing that feature. It's always useful to open an issue or submit a pull
220request early on to indicate your intent to a core contributor - this enables
221quick/early feedback and can help steer you in the right direction by avoiding
222known issues. It might also help you avoid losing time implementing something
223that might not ever work. One tip is to prefix your Pull Request issue title
224with [wip] - then people know it's a work in progress.
Jamie Hannafordd5a1cb72014-10-07 14:31:27 +0200225
226You must ensure that all of your work is well tested - both in terms of unit
227and acceptance tests. Untested code will not be merged because it introduces
Jamie Hannafordd2b6dfc2014-10-07 14:46:55 +0200228too much of a risk to end-users.
229
230Happy hacking!