Skip to content

Instantly share code, notes, and snippets.

View atheiman's full-sized avatar
😬

Austin Heiman atheiman

😬
View GitHub Profile
@atheiman
atheiman / include_recipe_spec.rb
Last active November 21, 2017 15:42
Stubbing `include_recipe` while ensuring correct recipes are included. complete example: https://github.com/atheiman/test-cookbook/pull/4
# test_cookbook/recipes/default.rb
include_recipe 'test_cookbook::included_recipe'
include_recipe 'apt'
# test_cookbook/spec/recipes/default_spec.rb
describe 'test_cookbook::default' do
before(:all) { @included_recipes = [] }
before do
@stubbed_recipes = %w[test_cookbook::included_recipe apt]
@atheiman
atheiman / home_dir_spec.rb
Created November 17, 2017 22:19
Stubbing methods interacting with /etc/passwd
describe 'cookbook::recipe' do
cached(:chef_run) do
ChefSpec::ServerRunner.new.converge(described_recipe)
end
it 'creates home directories' do
%w[/home/testuser1 /home/testuser2].each do |d|
expect(chef_run).to create_directory(d)
end
end
@atheiman
atheiman / with_retries.rb
Last active November 28, 2017 14:32
Execute a block with retries for exceptions
def doubles?
a = rand(1..6)
b = rand(1..6)
puts "#{a} and #{b}"
raise 'Not doubles!' unless a == b
true
end
def with_retries(retries: 2, sleep: 1)
attempt = 1

Keybase proof

I hereby claim:

  • I am atheiman on github.
  • I am austinheiman (https://keybase.io/austinheiman) on keybase.
  • I have a public key ASC2ZiWvCqeAOkMAORKrAfBc6FUubaaHaXHScebkbcth0Ao

To claim this, I am signing this object:

# chef-nodes is useful to share a nodes attributes with other nodes. if you want to share _converged_ node
# data (more than just ip and hostname from chef-nodes) then you can use test-kitchen >= 1.20.0 which adds
# support for downloading files from nodes after converge via .kitchen.yml:
#
# provisioner:
# nodes_path: test/fixtures/nodes
# downloads:
# /tmp/kitchen/nodes: test/fixtures/
#
# this will download node data after converge and drop it into test/fixtures/nodes/node-a.json, node-b.json, etc

Chef Cookbook Generator

Build Status

Template for creating new cookbooks with chef generate cookbook COOKBOOK_NAME --generator-cookbook:

# install `chef` utility from chef-dk if you dont already have it
gem install --no-document chef-dk
# You run an e-commerce website and want to record the last N order ids in a log. Implement a data structure to accomplish this, with the following API:
# record(order_id): adds the order_id to the log
# get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
require 'rspec'
class RingBuffer < Array
attr_reader :max
@atheiman
atheiman / docker_run_nginx.sh
Last active February 11, 2024 01:28
Run nginx in docker container to serve PWD
# Run nginx docker container in background
docker run --name nginx --rm -d -p 8080:80 -v ${PWD}:/usr/share/nginx/html:ro nginx
# Load a file in curent directory
curl http://localhost:8080/index.html
# Stop the docker container (the container will be removed / cleaned up)
docker stop nginx
Expand me More details