# Refactoring Infrastructure Code

**URL:** <https://discourse.chef.io/t/refactoring-infrastructure-code/9039>\
**Category:** Chef Infra (archive)\
**Created:** [July 28, 2016, 10:27pm UTC](https://discourse.chef.io/t/refactoring-infrastructure-code/9039 "2016-07-28T22:27:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nell\_Shamrell](https://avatars.discourse-cdn.com/v4/letter/n/85f322/32.png) [@Nell\_Shamrell](https://discourse.chef.io/u/Nell_Shamrell)\
**Post date:** [July 28, 2016, 10:27pm UTC](https://discourse.chef.io/t/refactoring-infrastructure-code/9039/1 "2016-07-28T22:27:01Z")

</div>

Hello all!

I am beginning work on my talk on Infrastructure Code Refactoring for DevOpsDays Chicago (hope to meet many of you there!). Although I can speak to my own experience, I want to make sure to not limit it my own POV. I would LOVE to hear other people’s experiences - how have you found refactoring infrastructure code (whether it’s Chef, Terraform, etc.) to be different than refactoring application code? How do you deal with the risk of making changes on live infrastructure with a refactor? What do you do when a refactor goes wrong?

Feel free to reply here or email me directly at [nellshamrell@gmail.com](mailto:nellshamrell@gmail.com).

Thanks so much!

---

<div class="post-metadata">

**Author:** ![Ranjib](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ranjib/32/1040_2.png) [@Ranjib](https://discourse.chef.io/u/Ranjib)\
**Post date:** [July 28, 2016, 10:42pm UTC](https://discourse.chef.io/t/refactoring-infrastructure-code/9039/2 "2016-07-28T22:42:07Z")

</div>

Every time I do it, large or small refactorings, its fairly similar to any other app codes. I use three set of tests,

1. Chefspec based unit tests, that i use to ensure the resultant resources are same, event when i change the abstractions, conditionals etc. Also ensure the code compiles (valid ruby), attributes are in place (valid resource/method stuff), attribute values are propagated as intended. This is fast, and part of the red-green style TDD workflow

2. LXC based smoke tests (checks basic integrity of the code, without mocking anything)… can be as simple as `lxc-attach -n chef12.5 -- chef-client -o recipe[zookeeper]`. Takes more time. But gives you confidence that the code converges, URL, network stuff (SHA for remote files) are good, services are starting fine (port based checks etc)

3. Deploy in staging or UAT server. Or canary deployment to a handful of servers in prod etc. I use either cokbook versions or some kind of attribute gating

hth  
ranjib
