github.com/hashicorp/packer@v1.14.3/.github/CONTRIBUTING.md (about) 1 # Contributing to Packer 2 3 **First:** if you're unsure or afraid of _anything_, just ask or submit the 4 issue or pull request anyway. You won't be yelled at for giving your best 5 effort. The worst that can happen is that you'll be politely asked to change 6 something. We appreciate any sort of contributions, and don't want a wall of 7 rules to get in the way of that. 8 9 However, for those individuals who want a bit more guidance on the best way to 10 contribute to the project, read on. This document will cover what we're looking 11 for. By addressing all the points we're looking for, it raises the chances we 12 can quickly merge or address your contributions. 13 14 When contributing in any way to the Packer project (new issue, PR, etc), please 15 be aware that our team identifies with many gender pronouns. Please remember to 16 use nonbinary pronouns (they/them) and gender neutral language ("Hello folks") 17 when addressing our team. For more reading on our code of conduct, please see the 18 [HashiCorp community guidelines](https://www.hashicorp.com/community-guidelines). 19 20 ## Issues 21 22 ### Reporting an Issue 23 24 - Make sure you test against the latest released version. It is possible we 25 already fixed the bug you're experiencing. 26 27 - Run the command with debug output with the environment variable `PACKER_LOG`. 28 For example: `PACKER_LOG=1 packer build template.pkr.hcl`. Take the _entire_ 29 output and create a [gist](https://gist.github.com) for linking to in your 30 issue. Packer should strip sensitive keys from the output, but take a look 31 through just in case. 32 33 - Provide a reproducible test case. If a contributor can't reproduce an issue, 34 then it dramatically lowers the chances it'll get fixed. And in some cases, 35 the issue will eventually be closed. 36 37 - Respond promptly to any questions made by the Packer team to your issue. Stale 38 issues will be closed. 39 40 ### Issue Lifecycle 41 42 1. The issue is reported. 43 44 2. The issue is verified and categorized by a Packer collaborator. 45 Categorization is done via tags. For example, bugs are marked as "bugs" and 46 simple fixes are marked as "good first issue". 47 48 3. Unless it is critical, the issue is left for a period of time (sometimes many 49 weeks), giving outside contributors a chance to address the issue. 50 51 4. The issue is addressed in a pull request or commit. The issue will be 52 referenced in the commit message so that the code that fixes it is clearly 53 linked. 54 55 5. Sometimes, if you have a specialized environment or use case, the maintainers 56 may ask for your help to test the patch. You are able to download an 57 experimental binary of Packer containing the Pull Request's patch via from 58 the Pull Request page on GitHub. You can do this by scrolling to the 59 "checks" section on GitHub, and clicking "details" on the 60 "store_artifacts" check. This will take you to Packer's Circle CI page for 61 the build, and you will be able to click a tab named "Artifacts" which will 62 contain zipped Packer binaries for each major OS architecture. 63 64 6. The issue is closed. 65 66 ## Setting up Go 67 68 If you have never worked with Go before, you will have to install its 69 runtime in order to build packer. 70 71 1. This project always releases from the latest version of golang. 72 [Install go](https://golang.org/doc/install#install) To properly build from 73 source, you need to have golang >= v1.20 74 75 ## Setting up Packer for dev 76 77 If/when you have go installed you can already clone packer and `make` in 78 order to compile and test Packer. These instructions target 79 POSIX-like environments (macOS, Linux, Cygwin, etc.) so you may need to 80 adjust them for Windows or other shells. 81 82 83 1. Create a directory in your GOPATH for the code `mkdir -p $(go env GOPATH)/src/github.com/hashicorp && cd $_` 84 and clone the packer repository from GitHub into your GOPATH `git clone https://github.com/hashicorp/packer.git` 85 then change into the packer directory `cd packer` 86 87 2. When working on Packer, first `cd $GOPATH/src/github.com/hashicorp/packer` 88 so you can run `make` and easily access other files. Run `make help` to get 89 information about make targets. 90 91 3. Make your changes to the Packer source. You can run `make` in 92 `$GOPATH/src/github.com/hashicorp/packer` to run tests and build the Packer 93 binary. Any compilation errors will be shown when the binaries are 94 rebuilding. If you don't have `make` you can simply run 95 `go build -o bin/packer .` from the project root. 96 97 4. After running building Packer successfully, use 98 `$GOPATH/src/github.com/hashicorp/packer/bin/packer` to build a machine and 99 verify your changes work. For instance: 100 `$GOPATH/src/github.com/hashicorp/packer/bin/packer build template.pkr.hcl`. 101 102 5. If everything works well and the tests pass, run `go fmt` on your code before 103 submitting a pull-request. 104 105 ### Windows Systems 106 107 On windows systems you need at least the [MinGW Tools](http://www.mingw.org/), e.g. install via [choco](https://chocolatey.org/): 108 109 ``` 110 choco install mingw -y 111 ``` 112 113 This installs the GCC compiler, as well as a `mingw32-make` which can be used wherever 114 this documentation mentions `make` 115 116 when building using `go` you also need to mention the windows 117 executable extension 118 119 ``` 120 go build -o bin/packer.exe 121 ``` 122 123 ### Opening a Pull Request 124 125 Thank you for contributing! When you are ready to open a pull-request, you will 126 need to [fork 127 Packer](https://github.com/hashicorp/packer#fork-destination-box), push your 128 changes to your fork, and then open a pull-request. 129 130 For example, my GitHub username is `cbednarski`, so I would do the following: 131 132 ``` 133 git checkout -b f-my-feature 134 # Develop a patch. 135 git push https://github.com/cbednarski/Packer f-my-feature 136 ``` 137 138 From there, open your fork in your browser to open a new pull-request. 139 140 **Note:** Go infers package names from their file paths. This means `go build` 141 will break if you `git clone` your fork instead of using `go get` on the main 142 Packer project. 143 144 **Note:** See '[Working with 145 forks](https://help.github.com/articles/working-with-forks/)' for a better way 146 to use `git push ...`. 147 148 ### Pull Request Lifecycle 149 150 1. You are welcome to submit your pull request for commentary or review before 151 it is fully completed. Please prefix the title of your pull request with 152 "[WIP]" to indicate this. It's also a good idea to include specific questions 153 or items you'd like feedback on. 154 155 2. Once you believe your pull request is ready to be merged, you can remove any 156 "[WIP]" prefix from the title and a core team member will review. 157 158 3. One of Packer's core team members will look over your contribution and 159 either merge, or provide comments letting you know if there is anything left 160 to do. We do our best to provide feedback in a timely manner, but it may take 161 some time for us to respond. We may also have questions that we need answered 162 about the code, either because something doesn't make sense to us or because 163 we want to understand your thought process. 164 165 4. If we have requested changes, you can either make those changes or, if you 166 disagree with the suggested changes, we can have a conversation about our 167 reasoning and agree on a path forward. This may be a multi-step process. Our 168 view is that pull requests are a chance to collaborate, and we welcome 169 conversations about how to do things better. It is the contributor's 170 responsibility to address any changes requested. While reviewers are happy to 171 give guidance, it is unsustainable for us to perform the coding work necessary 172 to get a PR into a mergeable state. 173 174 5. Once all outstanding comments and checklist items have been addressed, your 175 contribution will be merged! Merged PRs will be included in the next 176 Packer release. The core team takes care of updating the 177 [CHANGELOG.md](../CHANGELOG.md) as they merge. 178 179 6. In rare cases, we might decide that a PR should be closed without merging. 180 We'll make sure to provide clear reasoning when this happens. 181 182 ### Tips for Working on Packer 183 184 #### Getting Your Pull Requests Merged Faster 185 186 It is much easier to review pull requests that are: 187 188 1. Well-documented: Try to explain in the pull request comments what your 189 change does, why you have made the change, and provide instructions for how 190 to produce the new behavior introduced in the pull request. If you can, 191 provide screen captures or terminal output to show what the changes look 192 like. This helps the reviewers understand and test the change. 193 194 2. Small: Try to only make one change per pull request. If you found two bugs 195 and want to fix them both, that's _awesome_, but it's still best to submit 196 the fixes as separate pull requests. This makes it much easier for reviewers 197 to keep in their heads all of the implications of individual code changes, 198 and that means the PR takes less effort and energy to merge. In general, the 199 smaller the pull request, the sooner reviewers will be able to make time to 200 review it. 201 202 3. Passing Tests: Based on how much time we have, we may not review pull 203 requests which aren't passing our tests. (Look below for advice on how to 204 run unit tests). If you need help figuring out why tests are failing, please 205 feel free to ask, but while we're happy to give guidance it is generally 206 your responsibility to make sure that tests are passing. If your pull request 207 changes an interface or invalidates an assumption that causes a bunch of 208 tests to fail, then you need to fix those tests before we can merge your PR. 209 210 If we request changes, try to make those changes in a timely manner. Otherwise, 211 PRs can go stale and be a lot more work for all of us to merge in the future. 212 213 Even with everyone making their best effort to be responsive, it can be 214 time-consuming to get a PR merged. It can be frustrating to deal with 215 the back-and-forth as we make sure that we understand the changes fully. Please 216 bear with us, and please know that we appreciate the time and energy you put 217 into the project. 218 219 ### PR Checks 220 221 The following checks run when a PR is opened: 222 223 - Contributor License Agreement (CLA): If this is your first contribution to Packer you will be asked to sign the CLA. 224 - Tests: tests include unit tests, documentation checks, and code formatting checks, and all checks must pass before a PR can be merged. 225 226 #### Working on forks 227 228 The easiest way to work on a fork is to set it as a remote of the Packer 229 project. After following the steps in "Setting up Go to work on Packer": 230 231 1. Navigate to the code: 232 233 `cd $GOPATH/src/github.com/hashicorp/packer` 234 235 2. Add the remote by running: 236 237 `git remote add <name of remote> <github url of fork>` 238 239 For example: 240 241 `git remote add mwhooker https://github.com/mwhooker/packer.git` 242 243 3. Checkout a feature branch: 244 245 `git checkout -b new-feature` 246 247 4. Make changes. 248 5. (Optional) Push your changes to the fork: 249 250 `git push -u <name of remote> new-feature` 251 252 This way you can push to your fork to create a PR, but the code on disk still 253 lives in the spot where the go cli tools are expecting to find it. 254 255 #### Go modules & go vendor 256 257 If you are submitting a change that requires new or updated dependencies, 258 please include them in `go.mod`/`go.sum` and in the `vendor/` folder. This 259 helps everything get tested properly in CI. 260 261 Note that you will need to use [go 262 mod](https://github.com/golang/go/wiki/Modules) to do this. This step is 263 recommended but not required. 264 265 Use `go get <project>` to add dependencies to the project and `go mod vendor` 266 to make vendored copy of dependencies. See [go mod quick 267 start](https://github.com/golang/go/wiki/Modules#quick-start) for examples. 268 269 Please only apply the minimal vendor changes to get your PR to work. Packer 270 does not attempt to track the latest version for each dependency. 271 272 #### Code generation 273 274 Packer relies on `go generate` to generate a [peg parser for boot 275 commands](https://github.com/hashicorp/packer/blob/master/packer-plugin-sdk/bootcommand/boot_command.go), 276 [docs](https://github.com/hashicorp/packer/blob/master/website/pages/partials/builder/amazon/chroot/_Config-not-required.mdx) 277 and HCL2's bridging code. Packer's testing suite will run `make generate-check` 278 to check that all the generated files Packer needs are what they should be. 279 `make generate` re-generates all these file and can take a while depending on 280 your machine's performances. To make it faster it is recommended to run 281 localized code generation. Say you are working on the Amazon builder: running 282 `go generate ./builder/amazon/...` will do that for you. Make sure that the 283 latest code generation tool is installed by running `make install-gen-deps`. 284 285 #### Code linting 286 287 Packer relies on [golangci-lint](https://github.com/golangci/golangci-lint) for linting its Go code base, excluding any generated code created by `go generate`. Linting is executed on new files during Travis builds via `make ci`; the linting of existing code base is only executed when running `make lint`. Linting a large project like Packer is an iterative process so existing code base will have issues that are actively being fixed; pull-requests that fix existing linting issues are always welcomed :smile:. 288 289 The main configuration for golangci-lint is the `.golangci.yml` in the project root. See `golangci-lint --help` for a list of flags that can be used to override the default configuration. 290 291 Run golangci-lint on the entire Packer code base. 292 293 ``` 294 make lint 295 ``` 296 297 Run golangci-lint on a single pkg or directory; PKG_NAME expands to /builder/amazon/... 298 299 ``` 300 make lint PKG_NAME=builder/amazon 301 ``` 302 303 Note: linting on Travis uses the `--new-from-rev` flag to only lint new files added within a branch or pull-request. To run this check locally you can use the `ci-lint` make target. See [golangci-lint in CI](https://github.com/golangci/golangci-lint#faq) for more information. 304 305 ``` 306 make ci-lint 307 ``` 308 309 #### Running Unit Tests 310 311 You can run tests for individual packages using commands like this: 312 313 ``` 314 make test TEST=./builder/amazon/... 315 ``` 316 317 #### Running Builder Acceptance Tests 318 319 Packer has [acceptance tests](https://en.wikipedia.org/wiki/Acceptance_testing) 320 for various builders. These typically require an API key (AWS, GCE), or 321 additional software to be installed on your computer (VirtualBox, VMware). 322 323 If you're working on a new builder or builder feature and want to verify it is 324 functioning (and also hasn't broken anything else), we recommend creating or 325 running the acceptance tests. 326 327 **Warning:** The acceptance tests create/destroy/modify _real resources_, which 328 may incur costs for real money. In the presence of a bug, it is possible that 329 resources may be left behind, which can cost money even though you were not 330 using them. We recommend running tests in an account used only for that purpose 331 so it is easy to see if there are any dangling resources, and so production 332 resources are not accidentally destroyed or overwritten during testing. 333 334 To run the acceptance tests, invoke `make testacc`: 335 336 ``` 337 make testacc TEST=./builder/amazon/ebs 338 ... 339 ``` 340 341 The `TEST` variable lets you narrow the scope of the acceptance tests to a 342 specific package / folder. The `TESTARGS` variable is recommended to filter down 343 to a specific resource to test, since testing all of them at once can sometimes 344 take a very long time. 345 346 To run only a specific test, use the `-run` argument: 347 348 ``` 349 make testacc TEST=./builder/amazon/ebs TESTARGS="-run TestBuilderAcc_forceDeleteSnapshot" 350 ``` 351 352 Acceptance tests typically require other environment variables to be set for 353 things such as API tokens and keys. Each test should error and tell you which 354 credentials are missing, so those are not documented here. 355 356 #### Running Provisioner Acceptance Tests 357 358 **Warning:** The acceptance tests create/destroy/modify _real resources_, which 359 may incur costs for real money. In the presence of a bug, it is possible that 360 resources may be left behind, which can cost money even though you were not 361 using them. We recommend running tests in an account used only for that purpose 362 so it is easy to see if there are any dangling resources, and so production 363 resources are not accidentally destroyed or overwritten during testing. 364 Also, these typically require an API key (AWS, GCE), or additional software 365 to be installed on your computer (VirtualBox, VMware). 366 367 To run the Provisioners Acceptance Tests you should use the 368 **ACC_TEST_BUILDERS** environment variable to tell the tests which builder the 369 test should be run against. 370 371 Examples of usage: 372 373 - Run the Shell provisioner acceptance tests against the Amazon EBS builder. 374 ``` 375 ACC_TEST_BUILDERS=amazon-ebs go test ./provisioner/shell/... -v -timeout=1h 376 ``` 377 - Do the same but using the Makefile 378 ``` 379 ACC_TEST_BUILDERS=amazon-ebs make provisioners-acctest TEST=./provisioner/shell 380 ``` 381 - Run all provisioner acceptance tests against the Amazon EBS builder. 382 ``` 383 ACC_TEST_BUILDERS=amazon-ebs make provisioners-acctest TEST=./... 384 ``` 385 - Run all provisioner acceptance tests against all builders whenever they are compatible. 386 ``` 387 ACC_TEST_BUILDERS=all make provisioners-acctest TEST=./... 388 ``` 389 390 The **ACC_TEST_BUILDERS** env variable accepts a list of builders separated by 391 commas. (e.g. `ACC_TEST_BUILDERS=amazon-ebs,virtualbox-iso`) 392 393 394 #### Writing Provisioner Acceptance Tests 395 396 Packer has implemented a `ProvisionerTestCase` structure to help write 397 provisioner acceptance tests. 398 399 ```go 400 type ProvisionerTestCase struct { 401 // Check is called after this step is executed in order to test that 402 // the step executed successfully. If this is not set, then the next 403 // step will be called 404 Check func(*exec.Cmd, string) error 405 // IsCompatible checks whether a provisioner is able to run against a 406 // given builder type and guest operating system, and returns a boolean. 407 // if it returns true, the test combination is okay to run. If false, the 408 // test combination is not okay to run. 409 IsCompatible func(builderType string, BuilderGuestOS string) bool 410 // Name is the name of the test case. Be simple but unique and descriptive. 411 Name string 412 // Setup, if non-nil, will be called once before the test case 413 // runs. This can be used for some setup like setting environment 414 // variables, or for validation prior to the 415 // test running. For example, you can use this to make sure certain 416 // binaries are installed, or text fixtures are in place. 417 Setup func() error 418 // Teardown will be called before the test case is over regardless 419 // of if the test succeeded or failed. This should return an error 420 // in the case that the test can't guarantee all resources were 421 // properly cleaned up. 422 Teardown builderT.TestTeardownFunc 423 // Template is the provisioner template to use. 424 // The provisioner template fragment must be a json-formatted string 425 // containing the provisioner definition but no other portions of a packer 426 // template. For 427 // example: 428 // 429 // ```json 430 // { 431 // "type": "shell-local", 432 // "inline", ["echo hello world"] 433 // } 434 //``` 435 // 436 // is a valid entry for "template" here, but the complete Packer template: 437 // 438 // ```json 439 // { 440 // "provisioners": [ 441 // { 442 // "type": "shell-local", 443 // "inline", ["echo hello world"] 444 // } 445 // ] 446 // } 447 // ``` 448 // 449 // is invalid as input. 450 // 451 // You may provide multiple provisioners in the same template. For example: 452 // ```json 453 // { 454 // "type": "shell-local", 455 // "inline", ["echo hello world"] 456 // }, 457 // { 458 // "type": "shell-local", 459 // "inline", ["echo hello world 2"] 460 // } 461 // ``` 462 Template string 463 // Type is the type of provisioner. 464 Type string 465 } 466 467 ``` 468 469 To start writing a new provisioner acceptance test, you should add a test file 470 named `provisioner_acc_test.go` in the same folder as your provisioner is 471 defined. Create a test case by implementing the above struct, and run it 472 by calling `provisioneracc.TestProvisionersAgainstBuilders(testCase, t)` 473 474 The following example has been adapted from a shell-local provisioner test: 475 476 ``` 477 import ( 478 "github.com/hashicorp/packer-plugin-sdk/acctest/provisioneracc" 479 "github.com/hashicorp/packer-plugin-sdk/acctest/testutils" 480 ) 481 482 // ... 483 484 func TestAccShellProvisioner_basic(t *testing.T) { 485 // Create a json template fragment containing just the provisioners you want 486 // to run. 487 templateString := `{ 488 "type": "shell-local", 489 "script": "test-fixtures/script.sh", 490 "max_retries" : 5 491 }` 492 493 // instantiate a test case. 494 testCase := &provisioneracc.ProvisionerTestCase{ 495 IsCompatible: func() bool {return true}, 496 Name: "shell-local-provisioner-basic", 497 Teardown: func() error { 498 testutils.CleanupFiles("test-fixtures/file.txt") 499 return nil 500 }, 501 Template: templateString, 502 Type: "shell-local", 503 Check: func(buildcommand *exec.Cmd, logfile string) error { 504 if buildcommand.ProcessState != nil { 505 if buildcommand.ProcessState.ExitCode() != 0 { 506 return fmt.Errorf("Bad exit code. Logfile: %s", logfile) 507 } 508 } 509 filecontents, err := loadFile("file.txt") 510 if err != nil { 511 return err 512 } 513 if !strings.Contains(filecontents, "hello") { 514 return fmt.Errorf("file contents were wrong: %s", filecontents) 515 } 516 return nil 517 }, 518 } 519 520 provisioneracc.TestProvisionersAgainstBuilders(testCase, t) 521 } 522 523 ``` 524 525 526 After writing the struct and implementing the interface, now is time to write the test that will run all 527 of this code you wrote. Your test should be like: 528 529 ```go 530 func TestShellProvisioner(t *testing.T) { 531 acc.TestProvisionersPreCheck("shell", t) 532 acc.TestProvisionersAgainstBuilders(new(ShellProvisionerAccTest), t) 533 } 534 ``` 535 536 The method `TestProvisionersAgainstBuilders` will run the provisioner against 537 all available and compatible builders. If there are not builders compatible with 538 the test you want to run, you can add a builder using the following steps: 539 540 Create a subdirectory in provisioneracc/test-fixtures for the type of builder 541 you are adding. In this subdirectory, add one json file containing a single 542 builder fragment. For example, one of our amazon-ebs builders is defined in 543 provisioneracc/test-fixtures/amazon-ebs/amazon-ebs.txt and contains: 544 545 ```json 546 { 547 "type": "amazon-ebs", 548 "ami_name": "packer-acc-test", 549 "instance_type": "t2.micro", 550 "region": "us-east-1", 551 "ssh_username": "ubuntu", 552 "source_ami_filter": { 553 "filters": { 554 "virtualization-type": "hvm", 555 "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*", 556 "root-device-type": "ebs" 557 }, 558 "owners": ["099720109477"], 559 "most_recent": true 560 }, 561 "force_deregister" : true, 562 "tags": { 563 "packer-test": "true" 564 } 565 } 566 ``` 567 568 note that this fragment does not contain anything other than a single builder 569 definition. The testing framework will combine this with the provisioner 570 fragment to create a working json template. 571 572 In order to tell the testing framework how to use this builder fragment, you 573 need to implement a `BuilderFixture` struct: 574 575 ```go 576 type BuilderFixture struct { 577 // Name is the name of the builder fixture. 578 // Be simple and descriptive. 579 Name string 580 // Setup creates necessary extra test fixtures, and renders their values 581 // into the BuilderFixture.Template. 582 Setup func() 583 // Template is the path to a builder template fragment. 584 // The builder template fragment must be a json-formatted file containing 585 // the builder definition but no other portions of a packer template. For 586 // example: 587 // 588 // ```json 589 // { 590 // "type": "null", 591 // "communicator", "none" 592 // } 593 //``` 594 // 595 // is a valid entry for "template" here, but the complete Packer template: 596 // 597 // ```json 598 // { 599 // "builders": [ 600 // "type": "null", 601 // "communicator": "none" 602 // ] 603 // } 604 // ``` 605 // 606 // is invalid as input. 607 // 608 // Only provide one builder template fragment per file. 609 TemplatePath string 610 611 // GuestOS says what guest os type the builder template fragment creates. 612 // Valid values are "windows", "linux" or "darwin" guests. 613 GuestOS string 614 615 // HostOS says what host os type the builder is capable of running on. 616 // Valid values are "any", windows", or "posix". If you set "posix", then 617 // this builder can run on a "linux" or "darwin" platform. If you set 618 // "any", then this builder can be used on any platform. 619 HostOS string 620 621 Teardown builderT.TestTeardownFunc 622 } 623 ``` 624 Implement this struct to the file "provisioneracc/builders.go", then add 625 the new implementation to the `BuildersAccTest` map in 626 `provisioneracc/provisioners.go` 627 628 Once you finish these steps, you should be ready to run your new provisioner 629 acceptance test by setting the name used in the BuildersAccTest map as your 630 `ACC_TEST_BUILDERS` environment variable. 631 632 #### Debugging Plugins 633 634 Each packer plugin runs in a separate process and communicates via RPC over a 635 socket therefore using a debugger will not work (be complicated at least). 636 637 But most of the Packer code is really simple and easy to follow with PACKER_LOG 638 turned on. If that doesn't work adding some extra debug print outs when you have 639 homed in on the problem is usually enough.