github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/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/chnsz/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/chnsz/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/chnsz/golangsdk 63 ``` 64 65 2. Move into the directory that houses your local repository: 66 67 ```bash 68 cd ${GOPATH}/src/github.com/chnsz/golangsdk 69 ``` 70 71 3. Fork the `chnsz/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. 94 Once finished, commit them. 95 96 ```bash 97 git status 98 git add path/to/changed/file.go 99 git commit 100 ``` 101 102 7. Submit your branch as a [Pull Request](https://help.github.com/articles/creating-a-pull-request/). 103 When submitting a Pull Request, please follow our [Style Guide](/STYLEGUIDE.md). 104 105 > Further information about using Git can be found [here](https://git-scm.com/book/en/v2). 106 107 Happy Hacking! 108 109 ## Tests 110 111 When working on a new or existing feature, testing will be the backbone of your 112 work since it helps uncover and prevent regressions in the codebase. There are 113 two types of test we use in Gophercloud: unit tests and acceptance tests, which 114 are both described below. 115 116 ### Unit tests 117 118 Unit tests are the fine-grained tests that establish and ensure the behavior 119 of individual units of functionality. We usually test on an 120 operation-by-operation basis (an operation typically being an API action) with 121 the use of mocking to set up explicit expectations. Each operation will set up 122 its HTTP response expectation, and then test how the system responds when fed 123 this controlled, pre-determined input. 124 125 To make life easier, we've introduced a bunch of test helpers to simplify the 126 process of testing expectations with assertions: 127 128 ```go 129 import ( 130 "testing" 131 132 "github.com/chnsz/golangsdk/testhelper" 133 ) 134 135 func TestSomething(t *testing.T) { 136 result, err := Operation() 137 138 testhelper.AssertEquals(t, "foo", result.Bar) 139 testhelper.AssertNoErr(t, err) 140 } 141 142 func TestSomethingElse(t *testing.T) { 143 testhelper.CheckEquals(t, "expected", "actual") 144 } 145 ``` 146 147 `AssertEquals` and `AssertNoErr` will throw a fatal error if a value does not 148 match an expected value or if an error has been declared, respectively. You can 149 also use `CheckEquals` and `CheckNoErr` for the same purpose; the only difference 150 being that `t.Errorf` is raised rather than `t.Fatalf`. 151 152 Here is a truncated example of mocked HTTP responses: 153 154 ```go 155 import ( 156 "testing" 157 158 th "github.com/chnsz/golangsdk/testhelper" 159 fake "github.com/chnsz/golangsdk/testhelper/client" 160 "github.com/chnsz/golangsdk/openstack/networking/v2/networks" 161 ) 162 163 func TestGet(t *testing.T) { 164 // Setup the HTTP request multiplexer and server 165 th.SetupHTTP() 166 defer th.TeardownHTTP() 167 168 th.Mux.HandleFunc("/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 169 // Test we're using the correct HTTP method 170 th.TestMethod(t, r, "GET") 171 172 // Test we're setting the auth token 173 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 174 175 // Set the appropriate headers for our mocked response 176 w.Header().Add("Content-Type", "application/json") 177 w.WriteHeader(http.StatusOK) 178 179 // Set the HTTP body 180 fmt.Fprintf(w, ` 181 { 182 "network": { 183 "status": "ACTIVE", 184 "name": "private-network", 185 "admin_state_up": true, 186 "tenant_id": "4fd44f30292945e481c7b8a0c8908869", 187 "shared": true, 188 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" 189 } 190 } 191 `) 192 }) 193 194 // Call our API operation 195 network, err := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() 196 197 // Assert no errors and equality 198 th.AssertNoErr(t, err) 199 th.AssertEquals(t, n.Status, "ACTIVE") 200 } 201 ``` 202 203 ### Acceptance tests 204 205 As we've already mentioned, unit tests have a very narrow and confined focus - 206 they test small units of behavior. Acceptance tests on the other hand have a 207 far larger scope: they are fully functional tests that test the entire API of a 208 service in one fell swoop. They don't care about unit isolation or mocking 209 expectations, they instead do a full run-through and consequently test how the 210 entire system _integrates_ together. When an API satisfies expectations, it 211 proves by default that the requirements for a contract have been met. 212 213 Please be aware that acceptance tests will hit a live API - and may incur 214 service charges from your provider. Although most tests handle their own 215 teardown procedures, it is always worth manually checking that resources are 216 deleted after the test suite finishes. 217 218 We provide detailed information about how to set up local acceptance test 219 environments in our [acceptance tests readme](/acceptance). 220 221 ### Running tests 222 223 To run all tests: 224 225 ```bash 226 go test ./acceptance/... 227 ``` 228 229 To run all tests with verbose output: 230 231 ```bash 232 go test -v ./acceptance/... 233 ``` 234 235 To run tests for a particular sub-package: 236 237 ```bash 238 cd ./acceptance/openstack/sub-package && go test ./... 239 ``` 240 241 ## Style guide 242 243 See [here](/STYLEGUIDE.md)