github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/.github/CONTRIBUTING.md (about) 1 # Contributing to Golangsdk 2 3 - [3 ways to get involved](#3-ways-to-get-involved) 4 - [Getting started](#getting-started) 5 - [Tests](#tests) 6 - [Style guide](#basic-style-guide) 7 8 ## 3 ways to get involved 9 10 There are three main ways you can get involved in our open-source project, and 11 each is described briefly below. 12 13 ### 1. Fixing bugs 14 15 If you want to start fixing open bugs, we'd really appreciate that! Bug fixing 16 is central to any project. The best way to get started is by heading to our 17 [bug tracker](https://github.com/huaweicloud/golangsdk/issues) and finding open 18 bugs that you think nobody is working on. It might be useful to comment on the 19 thread to see the current state of the issue and if anybody has made any 20 breakthroughs on it so far. 21 22 ### 2. Improving documentation 23 24 Golangsdk's documentation is automatically generated from the source code 25 and can be read online at [godoc.org](https://godoc.org/github.com/huaweicloud/golangsdk). 26 27 If you feel that a certain section could be improved - whether it's to clarify 28 ambiguity, correct a technical mistake, or to fix a grammatical error - please 29 feel entitled to do so! We welcome doc pull requests with the same childlike 30 enthusiasm as any other contribution! 31 32 ### 3. Working on a new feature 33 34 If you've found something we've left out, definitely feel free to start work on 35 introducing that feature. It's always useful to open an issue or submit a pull 36 request early on to indicate your intent to a core contributor - this enables 37 quick/early feedback and can help steer you in the right direction by avoiding 38 known issues. It might also help you avoid losing time implementing something 39 that might not ever work. One tip is to prefix your Pull Request issue title 40 with [wip] - then people know it's a work in progress. 41 42 We ask that you do not submit a feature that you have not spent time researching 43 and testing first-hand in an actual Huawei clouds environment. While we appreciate 44 the contribution, submitting code which you are unfamiliar with is a risk to the 45 users who will ultimately use it. See our [acceptance tests readme](/acceptance) 46 for information about how you can create a local development environment to 47 better understand the feature you're working on. 48 49 Please do not hesitate to ask questions or request clarification. Your 50 contribution is very much appreciated and we are happy to work with you to get 51 it merged. 52 53 ## Getting Started 54 55 As a contributor you will need to setup your workspace in a slightly different 56 way than just downloading it. Here are the basic instructions: 57 58 1. Configure your `$GOPATH` and run `go get` as described in the main 59 [README](/README.md#how-to-install). 60 61 ```bash 62 go get github.com/huaweicloud/golangsdk 63 ``` 64 65 2. Move into the directory that houses your local repository: 66 67 ```bash 68 cd ${GOPATH}/src/github.com/huaweicloud/golangsdk 69 ``` 70 71 3. Fork the `huaweicloud/golangsdk` repository and update your remote refs. You 72 will need to rename the `origin` remote branch to `upstream`, and add your 73 fork as `origin` instead: 74 75 ```bash 76 git remote rename origin upstream 77 git remote add origin git@github.com:<my_username>/golangsdk.git 78 ``` 79 80 4. Checkout the latest development branch: 81 82 ```bash 83 git checkout master 84 ``` 85 86 5. If you're working on something (discussed more in detail below), you will 87 need to checkout a new feature branch: 88 89 ```bash 90 git checkout -b my-new-feature 91 ``` 92 93 6. Use a standard text editor or IDE of your choice to make your changes to the code or documentation. Once finished, commit them. 94 95 ```bash 96 git status 97 git add path/to/changed/file.go 98 git commit 99 ``` 100 101 7. Submit your branch as a [Pull Request](https://help.github.com/articles/creating-a-pull-request/). When submitting a Pull Request, please follow our [Style Guide](https://github.com/huaweicloud/golangsdk/blob/master/STYLEGUIDE.md). 102 103 > Further information about using Git can be found [here](https://git-scm.com/book/en/v2). 104 105 Happy Hacking! 106 107 ## Tests 108 109 When working on a new or existing feature, testing will be the backbone of your 110 work since it helps uncover and prevent regressions in the codebase. There are 111 two types of test we use in Gophercloud: unit tests and acceptance tests, which 112 are both described below. 113 114 ### Unit tests 115 116 Unit tests are the fine-grained tests that establish and ensure the behavior 117 of individual units of functionality. We usually test on an 118 operation-by-operation basis (an operation typically being an API action) with 119 the use of mocking to set up explicit expectations. Each operation will set up 120 its HTTP response expectation, and then test how the system responds when fed 121 this controlled, pre-determined input. 122 123 To make life easier, we've introduced a bunch of test helpers to simplify the 124 process of testing expectations with assertions: 125 126 ```go 127 import ( 128 "testing" 129 130 "github.com/huaweicloud/golangsdk/testhelper" 131 ) 132 133 func TestSomething(t *testing.T) { 134 result, err := Operation() 135 136 testhelper.AssertEquals(t, "foo", result.Bar) 137 testhelper.AssertNoErr(t, err) 138 } 139 140 func TestSomethingElse(t *testing.T) { 141 testhelper.CheckEquals(t, "expected", "actual") 142 } 143 ``` 144 145 `AssertEquals` and `AssertNoErr` will throw a fatal error if a value does not 146 match an expected value or if an error has been declared, respectively. You can 147 also use `CheckEquals` and `CheckNoErr` for the same purpose; the only difference 148 being that `t.Errorf` is raised rather than `t.Fatalf`. 149 150 Here is a truncated example of mocked HTTP responses: 151 152 ```go 153 import ( 154 "testing" 155 156 th "github.com/huaweicloud/golangsdk/testhelper" 157 fake "github.com/huaweicloud/golangsdk/testhelper/client" 158 "github.com/huaweicloud/golangsdk/openstack/networking/v2/networks" 159 ) 160 161 func TestGet(t *testing.T) { 162 // Setup the HTTP request multiplexer and server 163 th.SetupHTTP() 164 defer th.TeardownHTTP() 165 166 th.Mux.HandleFunc("/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 167 // Test we're using the correct HTTP method 168 th.TestMethod(t, r, "GET") 169 170 // Test we're setting the auth token 171 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 172 173 // Set the appropriate headers for our mocked response 174 w.Header().Add("Content-Type", "application/json") 175 w.WriteHeader(http.StatusOK) 176 177 // Set the HTTP body 178 fmt.Fprintf(w, ` 179 { 180 "network": { 181 "status": "ACTIVE", 182 "name": "private-network", 183 "admin_state_up": true, 184 "tenant_id": "4fd44f30292945e481c7b8a0c8908869", 185 "shared": true, 186 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" 187 } 188 } 189 `) 190 }) 191 192 // Call our API operation 193 network, err := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() 194 195 // Assert no errors and equality 196 th.AssertNoErr(t, err) 197 th.AssertEquals(t, n.Status, "ACTIVE") 198 } 199 ``` 200 201 ### Acceptance tests 202 203 As we've already mentioned, unit tests have a very narrow and confined focus - 204 they test small units of behavior. Acceptance tests on the other hand have a 205 far larger scope: they are fully functional tests that test the entire API of a 206 service in one fell swoop. They don't care about unit isolation or mocking 207 expectations, they instead do a full run-through and consequently test how the 208 entire system _integrates_ together. When an API satisfies expectations, it 209 proves by default that the requirements for a contract have been met. 210 211 Please be aware that acceptance tests will hit a live API - and may incur 212 service charges from your provider. Although most tests handle their own 213 teardown procedures, it is always worth manually checking that resources are 214 deleted after the test suite finishes. 215 216 We provide detailed information about how to set up local acceptance test 217 environments in our [acceptance tests readme](/acceptance). 218 219 ### Running tests 220 221 To run all tests: 222 223 ```bash 224 go test ./acceptance/... 225 ``` 226 227 To run all tests with verbose output: 228 229 ```bash 230 go test -v ./acceptance/... 231 ``` 232 233 To run tests for a particular sub-package: 234 235 ```bash 236 cd ./acceptance/openstack/sub-package && go test ./... 237 ``` 238 239 ## Style guide 240 241 See [here](/STYLEGUIDE.md)