github.com/jrasell/terraform@v0.6.17-0.20160523115548-2652f5232949/.github/CONTRIBUTING.md (about) 1 # Contributing to Terraform 2 3 **First:** if you're unsure or afraid of _anything_, just ask 4 or submit the issue or pull request anyways. You won't be yelled at for 5 giving your best effort. The worst that can happen is that you'll be 6 politely asked to change something. We appreciate any sort of contributions, 7 and don't want a wall of rules to get in the way of that. 8 9 However, for those individuals who want a bit more guidance on the 10 best way to contribute to the project, read on. This document will cover 11 what we're looking for. By addressing all the points we're looking for, 12 it raises the chances we can quickly merge or address your contributions. 13 14 Specifically, we have provided checklists below for each type of issue and pull 15 request that can happen on the project. These checklists represent everything 16 we need to be able to review and respond quickly. 17 18 ## HashiCorp vs. Community Providers 19 20 We separate providers out into what we call "HashiCorp Providers" and 21 "Community Providers". 22 23 HashiCorp providers are providers that we'll dedicate full time resources to 24 improving, supporting the latest features, and fixing bugs. These are providers 25 we understand deeply and are confident we have the resources to manage 26 ourselves. 27 28 Community providers are providers where we depend on the community to 29 contribute fixes and enhancements to improve. HashiCorp will run automated 30 tests and ensure these providers continue to work, but will not dedicate full 31 time resources to add new features to these providers. These providers are 32 available in official Terraform releases, but the functionality is primarily 33 contributed. 34 35 The current list of HashiCorp Providers is as follows: 36 37 * `aws` 38 * `azurerm` 39 * `google` 40 41 Our testing standards are the same for both HashiCorp and Community providers, 42 and HashiCorp runs full acceptance test suites for every provider nightly to 43 ensure Terraform remains stable. 44 45 We make the distinction between these two types of providers to help 46 highlight the vast amounts of community effort that goes in to making Terraform 47 great, and to help contributers better understand the role HashiCorp employees 48 play in the various areas of the code base. 49 50 ## Issues 51 52 ### Issue Reporting Checklists 53 54 We welcome issues of all kinds including feature requests, bug reports, and 55 general questions. Below you'll find checklists with guidlines for well-formed 56 issues of each type. 57 58 #### Bug Reports 59 60 - [ ] __Test against latest release__: Make sure you test against the latest 61 released version. It is possible we already fixed the bug you're experiencing. 62 63 - [ ] __Search for possible duplicate reports__: It's helpful to keep bug 64 reports consolidated to one thread, so do a quick search on existing bug 65 reports to check if anybody else has reported the same thing. You can scope 66 searches by the label "bug" to help narrow things down. 67 68 - [ ] __Include steps to reproduce__: Provide steps to reproduce the issue, 69 along with your `.tf` files, with secrets removed, so we can try to 70 reproduce it. Without this, it makes it much harder to fix the issue. 71 72 - [ ] __For panics, include `crash.log`__: If you experienced a panic, please 73 create a [gist](https://gist.github.com) of the *entire* generated crash log 74 for us to look at. Double check no sensitive items were in the log. 75 76 #### Feature Requests 77 78 - [ ] __Search for possible duplicate requests__: It's helpful to keep requests 79 consolidated to one thread, so do a quick search on existing requests to 80 check if anybody else has reported the same thing. You can scope searches by 81 the label "enhancement" to help narrow things down. 82 83 - [ ] __Include a use case description__: In addition to describing the 84 behavior of the feature you'd like to see added, it's helpful to also lay 85 out the reason why the feature would be important and how it would benefit 86 Terraform users. 87 88 #### Questions 89 90 - [ ] __Search for answers in Terraform documentation__: We're happy to answer 91 questions in GitHub Issues, but it helps reduce issue churn and maintainer 92 workload if you work to find answers to common questions in the 93 documentation. Often times Question issues result in documentation updates 94 to help future users, so if you don't find an answer, you can give us 95 pointers for where you'd expect to see it in the docs. 96 97 ### Issue Lifecycle 98 99 1. The issue is reported. 100 101 2. The issue is verified and categorized by a Terraform collaborator. 102 Categorization is done via GitHub labels. We generally use a two-label 103 system of (1) issue/PR type, and (2) section of the codebase. Type is 104 usually "bug", "enhancement", "documentation", or "question", and section 105 can be any of the providers or provisioners or "core". 106 107 3. Unless it is critical, the issue is left for a period of time (sometimes 108 many weeks), giving outside contributors a chance to address the issue. 109 110 4. The issue is addressed in a pull request or commit. The issue will be 111 referenced in the commit message so that the code that fixes it is clearly 112 linked. 113 114 5. The issue is closed. Sometimes, valid issues will be closed to keep 115 the issue tracker clean. The issue is still indexed and available for 116 future viewers, or can be re-opened if necessary. 117 118 ## Pull Requests 119 120 Thank you for contributing! Here you'll find information on what to include in 121 your Pull Request to ensure it is accepted quickly. 122 123 * For pull requests that follow the guidelines, we expect to be able to review 124 and merge very quickly. 125 * Pull requests that don't follow the guidelines will be annotated with what 126 they're missing. A community or core team member may be able to swing around 127 and help finish up the work, but these PRs will generally hang out much 128 longer until they can be completed and merged. 129 130 ### Pull Request Lifecycle 131 132 1. You are welcome to submit your pull request for commentary or review before 133 it is fully completed. Please prefix the title of your pull request with 134 "[WIP]" to indicate this. It's also a good idea to include specific 135 questions or items you'd like feedback on. 136 137 2. Once you believe your pull request is ready to be merged, you can remove any 138 "[WIP]" prefix from the title and a core team member will review. Follow 139 [the checklists below](#checklists-for-contribution) to help ensure that 140 your contribution will be merged quickly. 141 142 3. One of Terraform's core team members will look over your contribution and 143 either provide comments letting you know if there is anything left to do. We 144 do our best to provide feedback in a timely manner, but it may take some 145 time for us to respond. 146 147 4. Once all outstanding comments and checklist items have been addressed, your 148 contribution will be merged! Merged PRs will be included in the next 149 Terraform release. The core team takes care of updating the CHANGELOG as 150 they merge. 151 152 5. In rare cases, we might decide that a PR should be closed. We'll make sure 153 to provide clear reasoning when this happens. 154 155 ### Checklists for Contribution 156 157 There are several different kinds of contribution, each of which has its own 158 standards for a speedy review. The following sections describe guidelines for 159 each type of contribution. 160 161 #### Documentation Update 162 163 Because [Terraform's website][website] is in the same repo as the code, it's 164 easy for anybody to help us improve our docs. 165 166 - [ ] __Reasoning for docs update__: Including a quick explanation for why the 167 update needed is helpful for reviewers. 168 - [ ] __Relevant Terraform version__: Is this update worth deploying to the 169 site immediately, or is it referencing an upcoming version of Terraform and 170 should get pushed out with the next release? 171 172 #### Enhancement/Bugfix to a Resource 173 174 Working on existing resources is a great way to get started as a Terraform 175 contributor because you can work within existing code and tests to get a feel 176 for what to do. 177 178 - [ ] __Acceptance test coverage of new behavior__: Existing resources each 179 have a set of [acceptance tests][acctests] covering their functionality. 180 These tests should exercise all the behavior of the resource. Whether you are 181 adding something or fixing a bug, the idea is to have an acceptance test that 182 fails if your code were to be removed. Sometimes it is sufficient to 183 "enhance" an existing test by adding an assertion or tweaking the config 184 that is used, but often a new test is better to add. You can copy/paste an 185 existing test and follow the conventions you see there, modifying the test 186 to exercise the behavior of your code. 187 - [ ] __Documentation updates__: If your code makes any changes that need to 188 be documented, you should include those doc updates in the same PR. The 189 [Terraform website][website] source is in this repo and includes 190 instructions for getting a local copy of the site up and running if you'd 191 like to preview your changes. 192 - [ ] __Well-formed Code__: Do your best to follow existing conventions you 193 see in the codebase, and ensure your code is formatted with `go fmt`. (The 194 Travis CI build will fail if `go fmt` has not been run on incoming code.) 195 The PR reviewers can help out on this front, and may provide comments with 196 suggestions on how to improve the code. 197 198 #### New Resource 199 200 Implementing a new resource is a good way to learn more about how Terraform 201 interacts with upstream APIs. There are plenty of examples to draw from in the 202 existing resources, but you still get to implement something completely new. 203 204 - [ ] __Acceptance tests__: New resources should include acceptance tests 205 covering their behavior. See [Writing Acceptance 206 Tests](#writing-acceptance-tests) below for a detailed guide on how to 207 approach these. 208 - [ ] __Documentation__: Each resource gets a page in the Terraform 209 documentation. The [Terraform website][website] source is in this 210 repo and includes instructions for getting a local copy of the site up and 211 running if you'd like to preview your changes. For a resource, you'll want 212 to add a new file in the appropriate place and add a link to the sidebar for 213 that page. 214 - [ ] __Well-formed Code__: Do your best to follow existing conventions you 215 see in the codebase, and ensure your code is formatted with `go fmt`. (The 216 Travis CI build will fail if `go fmt` has not been run on incoming code.) 217 The PR reviewers can help out on this front, and may provide comments with 218 suggestions on how to improve the code. 219 220 #### New Provider 221 222 Implementing a new provider gives Terraform the ability to manage resources in 223 a whole new API. It's a larger undertaking, but brings major new functionaliy 224 into Terraform. 225 226 - [ ] __Acceptance tests__: Each provider should include an acceptance test 227 suite with tests for each resource should include acceptance tests covering 228 its behavior. See [Writing Acceptance Tests](#writing-acceptance-tests) below 229 for a detailed guide on how to approach these. 230 - [ ] __Documentation__: Each provider has a section in the Terraform 231 documentation. The [Terraform website][website] source is in this repo and 232 includes instructions for getting a local copy of the site up and running if 233 you'd like to preview your changes. For a provider, you'll want to add new 234 index file and individual pages for each resource. 235 - [ ] __Well-formed Code__: Do your best to follow existing conventions you 236 see in the codebase, and ensure your code is formatted with `go fmt`. (The 237 Travis CI build will fail if `go fmt` has not been run on incoming code.) 238 The PR reviewers can help out on this front, and may provide comments with 239 suggestions on how to improve the code. 240 241 #### Core Bugfix/Enhancement 242 243 We are always happy when any developer is interested in diving into Terraform's 244 core to help out! Here's what we look for in smaller Core PRs. 245 246 - [ ] __Unit tests__: Terraform's core is covered by hundreds of unit tests at 247 several different layers of abstraction. Generally the best place to start 248 is with a "Context Test". These are higher level test that interact 249 end-to-end with most of Terraform's core. They are divided into test files 250 for each major action (plan, apply, etc.). Getting a failing test is a great 251 way to prove out a bug report or a new enhancement. With a context test in 252 place, you can work on implementation and lower level unit tests. Lower 253 level tests are largely context dependent, but the Context Tests are almost 254 always part of core work. 255 - [ ] __Documentation updates__: If the core change involves anything that 256 needs to be reflected in our documentation, you can make those changes in 257 the same PR. The [Terraform website][website] source is in this repo and 258 includes instructions for getting a local copy of the site up and running if 259 you'd like to preview your changes. 260 - [ ] __Well-formed Code__: Do your best to follow existing conventions you 261 see in the codebase, and ensure your code is formatted with `go fmt`. (The 262 Travis CI build will fail if `go fmt` has not been run on incoming code.) 263 The PR reviewers can help out on this front, and may provide comments with 264 suggestions on how to improve the code. 265 266 #### Core Feature 267 268 If you're interested in taking on a larger core feature, it's a good idea to 269 get feedback early and often on the effort. 270 271 - [ ] __Early validation of idea and implementation plan__: Terraform's core 272 is complicated enough that there are often several ways to implement 273 something, each of which has different implications and tradeoffs. Working 274 through a plan of attack with the team before you dive into implementation 275 will help ensure that you're working in the right direction. 276 - [ ] __Unit tests__: Terraform's core is covered by hundreds of unit tests at 277 several different layers of abstraction. Generally the best place to start 278 is with a "Context Test". These are higher level test that interact 279 end-to-end with most of Terraform's core. They are divided into test files 280 for each major action (plan, apply, etc.). Getting a failing test is a great 281 way to prove out a bug report or a new enhancement. With a context test in 282 place, you can work on implementation and lower level unit tests. Lower 283 level tests are largely context dependent, but the Context Tests are almost 284 always part of core work. 285 - [ ] __Documentation updates__: If the core change involves anything that 286 needs to be reflected in our documentation, you can make those changes in 287 the same PR. The [Terraform website][website] source is in this repo and 288 includes instructions for getting a local copy of the site up and running if 289 you'd like to preview your changes. 290 - [ ] __Well-formed Code__: Do your best to follow existing conventions you 291 see in the codebase, and ensure your code is formatted with `go fmt`. (The 292 Travis CI build will fail if `go fmt` has not been run on incoming code.) 293 The PR reviewers can help out on this front, and may provide comments with 294 suggestions on how to improve the code. 295 296 ### Writing Acceptance Tests 297 298 Terraform includes an acceptance test harness that does most of the repetitive 299 work involved in testing a resource. 300 301 #### Acceptance Tests Often Cost Money to Run 302 303 Because acceptance tests create real resources, they often cost money to run. 304 Because the resources only exist for a short period of time, the total amount 305 of money required is usually a relatively small. Nevertheless, we don't want 306 financial limitations to be a barrier to contribution, so if you are unable to 307 pay to run acceptance tests for your contribution, simply mention this in your 308 pull request. We will happily accept "best effort" implementations of 309 acceptance tests and run them for you on our side. This might mean that your PR 310 takes a bit longer to merge, but it most definitely is not a blocker for 311 contributions. 312 313 #### Running an Acceptance Test 314 315 Acceptance tests can be run using the `testacc` target in the Terraform 316 `Makefile`. The individual tests to run can be controlled using a regular 317 expression. Prior to running the tests provider configuration details such as 318 access keys must be made available as environment variables. 319 320 For example, to run an acceptance test against the Azure Resource Manager 321 provider, the following environment variables must be set: 322 323 ```sh 324 export ARM_SUBSCRIPTION_ID=... 325 export ARM_CLIENT_ID=... 326 export ARM_CLIENT_SECRET=... 327 export ARM_TENANT_ID=... 328 ``` 329 330 Tests can then be run by specifying the target provider and a regular 331 expression defining the tests to run: 332 333 ```sh 334 $ make testacc TEST=./builtin/providers/azurerm TESTARGS='-run=TestAccAzureRMPublicIpStatic_update' 335 ==> Checking that code complies with gofmt requirements... 336 go generate ./... 337 TF_ACC=1 go test ./builtin/providers/azurerm -v -run=TestAccAzureRMPublicIpStatic_update -timeout 120m 338 === RUN TestAccAzureRMPublicIpStatic_update 339 --- PASS: TestAccAzureRMPublicIpStatic_update (177.48s) 340 PASS 341 ok github.com/hashicorp/terraform/builtin/providers/azurerm 177.504s 342 ``` 343 344 Entire resource test suites can be targeted by using the naming convention to 345 write the regular expression. For example, to run all tests of the 346 `azurerm_public_ip` resource rather than just the update test, you can start 347 testing like this: 348 349 ```sh 350 $ make testacc TEST=./builtin/providers/azurerm TESTARGS='-run=TestAccAzureRMPublicIpStatic' 351 ==> Checking that code complies with gofmt requirements... 352 go generate ./... 353 TF_ACC=1 go test ./builtin/providers/azurerm -v -run=TestAccAzureRMPublicIpStatic -timeout 120m 354 === RUN TestAccAzureRMPublicIpStatic_basic 355 --- PASS: TestAccAzureRMPublicIpStatic_basic (137.74s) 356 === RUN TestAccAzureRMPublicIpStatic_update 357 --- PASS: TestAccAzureRMPublicIpStatic_update (180.63s) 358 PASS 359 ok github.com/hashicorp/terraform/builtin/providers/azurerm 318.392s 360 ``` 361 362 #### Writing an Acceptance Test 363 364 Terraform has a framework for writing acceptance tests which minimises the 365 amount of boilerplate code necessary to use common testing patterns. The entry 366 point to the framework is the `resource.Test()` function. 367 368 Tests are divided into `TestStep`s. Each `TestStep` proceeds by applying some 369 Terraform configuration using the provider under test, and then verifying that 370 results are as expected by making assertions using the provider API. It is 371 common for a single test function to excercise both the creation of and updates 372 to a single resource. Most tests follow a similar structure. 373 374 1. Pre-flight checks are made to ensure that sufficient provider configuration 375 is available to be able to proceed - for example in an acceptance test 376 targetting AWS, `AWS_ACCESS_KEY_ID` and `AWS_SECRET_KEY` must be set prior 377 to running acceptance tests. This is common to all tests exercising a single 378 provider. 379 380 Each `TestStep` is defined in the call to `resource.Test()`. Most assertion 381 functions are defined out of band with the tests. This keeps the tests 382 readable, and allows reuse of assertion functions across different tests of the 383 same type of resource. The definition of a complete test looks like this: 384 385 ```go 386 func TestAccAzureRMPublicIpStatic_update(t *testing.T) { 387 resource.Test(t, resource.TestCase{ 388 PreCheck: func() { testAccPreCheck(t) }, 389 Providers: testAccProviders, 390 CheckDestroy: testCheckAzureRMPublicIpDestroy, 391 Steps: []resource.TestStep{ 392 resource.TestStep{ 393 Config: testAccAzureRMVPublicIpStatic_basic, 394 Check: resource.ComposeTestCheckFunc( 395 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 396 ), 397 }, 398 }, 399 }) 400 } 401 ``` 402 403 When executing the test, the following steps are taken for each `TestStep`: 404 405 1. The Terraform configuration required for the test is applied. This is 406 responsible for configuring the resource under test, and any dependencies it 407 may have. For example, to test the `azurerm_public_ip` resource, an 408 `azurerm_resource_group` is required. This results in configuration which 409 looks like this: 410 411 ```hcl 412 resource "azurerm_resource_group" "test" { 413 name = "acceptanceTestResourceGroup1" 414 location = "West US" 415 } 416 417 resource "azurerm_public_ip" "test" { 418 name = "acceptanceTestPublicIp1" 419 location = "West US" 420 resource_group_name = "${azurerm_resource_group.test.name}" 421 public_ip_address_allocation = "static" 422 } 423 ``` 424 425 1. Assertions are run using the provider API. These use the provider API 426 directly rather than asserting against the resource state. For example, to 427 verify that the `azurerm_public_ip` described above was created 428 successfully, a test function like this is used: 429 430 ```go 431 func testCheckAzureRMPublicIpExists(name string) resource.TestCheckFunc { 432 return func(s *terraform.State) error { 433 // Ensure we have enough information in state to look up in API 434 rs, ok := s.RootModule().Resources[name] 435 if !ok { 436 return fmt.Errorf("Not found: %s", name) 437 } 438 439 publicIPName := rs.Primary.Attributes["name"] 440 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 441 if !hasResourceGroup { 442 return fmt.Errorf("Bad: no resource group found in state for public ip: %s", availSetName) 443 } 444 445 conn := testAccProvider.Meta().(*ArmClient).publicIPClient 446 447 resp, err := conn.Get(resourceGroup, publicIPName, "") 448 if err != nil { 449 return fmt.Errorf("Bad: Get on publicIPClient: %s", err) 450 } 451 452 if resp.StatusCode == http.StatusNotFound { 453 return fmt.Errorf("Bad: Public IP %q (resource group: %q) does not exist", name, resourceGroup) 454 } 455 456 return nil 457 } 458 } 459 ``` 460 461 Notice that the only information used from the Terraform state is the ID of 462 the resource - though in this case it is necessary to split the ID into 463 constituent parts in order to use the provider API. For computed properties, 464 we instead assert that the value saved in the Terraform state was the 465 expected value if possible. The testing framework providers helper functions 466 for several common types of check - for example: 467 468 ```go 469 resource.TestCheckResourceAttr("azurerm_public_ip.test", "domain_name_label", "mylabel01"), 470 ``` 471 472 1. The resources created by the test are destroyed. This step happens 473 automatically, and is the equivalent of calling `terraform destroy`. 474 475 1. Assertions are made against the provider API to verify that the resources 476 have indeed been removed. If these checks fail, the test fails and reports 477 "dangling resources". The code to ensure that the `azurerm_public_ip` shown 478 above looks like this: 479 480 ```go 481 func testCheckAzureRMPublicIpDestroy(s *terraform.State) error { 482 conn := testAccProvider.Meta().(*ArmClient).publicIPClient 483 484 for _, rs := range s.RootModule().Resources { 485 if rs.Type != "azurerm_public_ip" { 486 continue 487 } 488 489 name := rs.Primary.Attributes["name"] 490 resourceGroup := rs.Primary.Attributes["resource_group_name"] 491 492 resp, err := conn.Get(resourceGroup, name, "") 493 494 if err != nil { 495 return nil 496 } 497 498 if resp.StatusCode != http.StatusNotFound { 499 return fmt.Errorf("Public IP still exists:\n%#v", resp.Properties) 500 } 501 } 502 503 return nil 504 } 505 ``` 506 507 These functions usually test only for the resource directly under test: we 508 skip the check that the `azurerm_resource_group` has been destroyed when 509 testing `azurerm_resource_group`, under the assumption that 510 `azurerm_resource_group` is tested independently in its own acceptance 511 tests. 512 513 [website]: https://github.com/hashicorp/terraform/tree/master/website 514 [acctests]: https://github.com/hashicorp/terraform#acceptance-tests 515 [ml]: https://groups.google.com/group/terraform-tool