Skip to content

Commit

Permalink
More tests for upload, bump and promote
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Cowie committed Oct 25, 2014
1 parent e9aa8a9 commit c9156b6
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 12 deletions.
25 changes: 21 additions & 4 deletions spec/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,36 @@ def fixtures_path
File.expand_path(File.dirname(__FILE__) + "/unit/fixtures/")
end

def cookbook_path
File.expand_path('cookbooks', tempdir)
end

def environment_path
File.expand_path('environments', tempdir)
end

def stdout
stdout_io.string
end

def set_chef_config
knife.config[:chef_repo_path] = tempdir
knife.config[:cookbook_path] = File.join(fixtures_path, 'cookbooks')
knife.config[:environment_path] = File.join(fixtures_path, 'environments')
knife.config[:cookbook_path] = File.join(tempdir, 'cookbooks')
knife.config[:environment_path] = File.join(tempdir, 'environments')
knife.config[:chef_server_url] = "http://localhost:4000"
knife.config[:client_key] = File.join(fixtures_path, 'test_client.pem')
knife.config[:client_key] = File.join(tempdir, 'test_client.pem')
knife.config[:client_name] = "test-client"
knife.config[:node_name] = "test-node"
knife.config[:cache_type] = 'BasicFile'
knife.config[:cache_options] = {:path => File.join(fixtures_path, 'checksums')}
knife.config[:cache_options] = {:path => File.join(tempdir, 'checksums')}
end


def copy_test_data
FileUtils.cp_r "#{fixtures_path}/.", tempdir
end

def cleanup_test_data
FileUtils.rm_r Dir.glob("#{tempdir}/*")
end
end
1 change: 0 additions & 1 deletion spec/unit/fixtures/environments/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "example",
"description": "This is an example environment",
"cookbook_versions": {
"example": ">= 1.0.0"
},
"json_class": "Chef::Environment",
"chef_type": "environment",
Expand Down
51 changes: 48 additions & 3 deletions spec/unit/spork_bump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,75 @@
require 'spec_helper'
require 'chef/knife/spork-bump'
require 'knife-spork/runner'
require 'fileutils'

module KnifeSpork
describe SporkBump do

let(:stdout_io) { StringIO.new }
let(:stderr_io) { StringIO.new }
let(:default_cookbook_path) do
File.expand_path('cookbooks', fixtures_path)
end

subject(:knife) do
SporkBump.new(argv).tap do |c|
allow(c.ui).to receive(:stdout).and_return(stdout_io)
end
end

before(:all) do
copy_test_data
end

after(:all) do
cleanup_test_data
end

let(:argv) { ["example"] }

let(:metadata_file) { "#{cookbook_path}/example/metadata.rb" }

describe '#run' do
before(:each) { set_chef_config }
it 'calls bump method' do
expect(knife).to receive(:bump)
knife.run
end
end

describe '#bump' do
before(:each) { set_chef_config }
it 'automatically bumps patch level' do
knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first))
knife.send(:bump)
File.read(metadata_file).should include "version \"0.0.2\""
end

it 'manually bumps patch version level' do
knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first))
knife.instance_variable_set(:@name_args, ["example","patch"])
knife.send(:bump)
File.read(metadata_file).should include "version \"0.0.3\""
end

it 'manually bumps minor version level' do
knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first))
knife.instance_variable_set(:@name_args, ["example","minor"])
knife.send(:bump)
File.read(metadata_file).should include "version \"0.1.0\""
end

it 'manually bumps major version level' do
knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first))
knife.instance_variable_set(:@name_args, ["example","major"])
knife.send(:bump)
File.read(metadata_file).should include "version \"1.0.0\""
end

it 'manually sets version to 0.0.1' do
knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first))
knife.instance_variable_set(:@name_args, ["example", "manual", "0.0.1"])
knife.send(:bump)
File.read(metadata_file).should include "version \"0.0.1\""
end
end
end
end
51 changes: 49 additions & 2 deletions spec/unit/spork_promote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ module KnifeSpork

let(:stdout_io) { StringIO.new }
let(:stderr_io) { StringIO.new }
let(:default_cookbook_path) do
File.expand_path('cookbooks', fixtures_path)

before(:all) do
copy_test_data
end

after(:all) do
cleanup_test_data
end

subject(:knife) do
Expand All @@ -34,6 +39,8 @@ module KnifeSpork
end
end

let(:environment_file) { "#{environment_path}/example.json" }

let(:argv) { ["example", "example"] }

describe '#run' do
Expand All @@ -42,6 +49,46 @@ module KnifeSpork
expect(knife).to receive(:promote)
knife.run
end

it 'calls save_environment_changes method by default' do
expect(knife).to receive(:save_environment_changes)
knife.run
end

it 'calls save_environment_changes_remote method when --remote is specified' do
knife.config[:remote] = true
expect(knife).to receive(:save_environment_changes_remote)
knife.run
end
end

describe '#promote' do
before(:each) { set_chef_config }
it 'updates version constraint for cookbook' do
knife.instance_variable_set(:@environments, ["example"])
knife.instance_variable_set(:@cookbook, "example")
expect(knife).to receive(:update_version_constraints)
knife.send(:promote, knife.load_environment_from_file("example"), "example")
end
end

describe '#save_environment_changes' do
before(:each) { set_chef_config }
it 'updates the constraint in the environment file' do
knife.instance_variable_set(:@environments, ["example"])
knife.instance_variable_set(:@cookbook, "example")
knife.send(:save_environment_changes, "example", knife.pretty_print_json(knife.load_environment_from_file("example").to_hash))
File.read(environment_file).should include "\"example\": \"= 0.0.1\""
end
end

describe '#save_environment_changes_remote' do
before(:each) { set_chef_config }
it 'saves the environment change to the server' do
knife.instance_variable_set(:@environments, ["example"])
knife.instance_variable_set(:@cookbook, "example")
knife.send(:save_environment_changes_remote, "example")
end
end
end
end
9 changes: 7 additions & 2 deletions spec/unit/spork_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ module KnifeSpork

let(:stdout_io) { StringIO.new }
let(:stderr_io) { StringIO.new }
let(:default_cookbook_path) do
File.expand_path('cookbooks', fixtures_path)

before(:all) do
copy_test_data
end

after(:all) do
cleanup_test_data
end

subject(:knife) do
Expand Down

0 comments on commit c9156b6

Please sign in to comment.