require 'support/integration_helper' require 'chef/knife/download_essentials' require 'chef/knife/diff_essentials' describe 'knife download' do extend IntegrationSupport include KnifeSupport context 'without versioned cookbooks' do when_the_chef_server "has one of each thing" do client 'x', '{}' cookbook 'x', '1.0.0', { 'metadata.rb' => 'version "1.0.0"' } data_bag 'x', { 'y' => '{}' } environment 'x', '{}' node 'x', '{}' role 'x', '{}' user 'x', '{}' when_the_repository 'has only top-level directories' do directory 'clients' directory 'cookbooks' directory 'data_bags' directory 'environments' directory 'nodes' directory 'roles' directory 'users' it 'knife download downloads everything' do knife('download /').should_succeed < 'y' } file 'cookbooks/x/blah.rb', '' file 'cookbooks/y/metadata.rb', 'version "1.0.0"' file 'data_bags/x/z.json', < 'y' } file 'roles/y.json', < 'y' } it 'knife download does nothing' do knife('download /').should_succeed '' knife('diff --name-status /').should_succeed < /USAGE/ end end end end # Test download of an item when the other end doesn't even have the container when_the_repository 'is empty' do when_the_chef_server 'has two data bag items' do data_bag 'x', { 'y' => {}, 'z' => {} } it 'knife download of one data bag item itself succeeds' do knife('download /data_bags/x/y.json').should_succeed < {}, 'modified' => { 'foo' => 'bar' }, 'unmodified' => {} } it 'knife download of the modified file succeeds' do knife('download /data_bags/x/modified.json').should_succeed < /USAGE/ end it 'knife download --purge . downloads everything' do knife('download --purge .').should_succeed < 'version "1.0.0"', 'y.rb' => 'hi' } it 'knife download of a modified file succeeds' do knife('download /cookbooks/x/metadata.rb').should_succeed "Updated /cookbooks/x/metadata.rb\n" knife('diff --name-status /cookbooks').should_succeed < 'version "1.0.0"', 'onlyin1.0.0.rb' => '' } cookbook 'x', '1.0.1', { 'metadata.rb' => 'version "1.0.1"', 'onlyin1.0.1.rb' => 'hi' } it 'knife download /cookbooks/x downloads the latest version' do knife('download --purge /cookbooks/x').should_succeed < 'version "1.0.0"', 'onlyin1.0.0.rb' => ''} cookbook 'x', '0.9.9', { 'metadata.rb' => 'version "0.9.9"', 'onlyin0.9.9.rb' => 'hi' } it 'knife download /cookbooks/x downloads the updated file' do knife('download --purge /cookbooks/x').should_succeed < 'version "1.0.1"', 'onlyin1.0.1.rb' => 'hi' } it 'knife download /cookbooks/x downloads the latest version' do knife('download --purge /cookbooks/x').should_succeed < 'version "0.9.9"', 'onlyin0.9.9.rb' => 'hi' } it 'knife download /cookbooks/x downloads the old version' do knife('download --purge /cookbooks/x').should_succeed < "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\n" knife('diff --name-status /environments/x.json').should_succeed '' end end when_the_repository 'has the same environment with the wrong name in the file' do file 'environments/x.json', { 'name' => 'y' } it 'knife download succeeds' do knife('download /environments/x.json').should_succeed "Updated /environments/x.json\n" knife('diff --name-status /environments/x.json').should_succeed '' end end when_the_repository 'has the same environment with no name in the file' do file 'environments/x.json', { 'description' => 'hi' } it 'knife download succeeds' do knife('download /environments/x.json').should_succeed "Updated /environments/x.json\n" knife('diff --name-status /environments/x.json').should_succeed '' end end end end # without versioned cookbooks with_versioned_cookbooks do when_the_chef_server "has one of each thing" do client 'x', '{}' cookbook 'x', '1.0.0', { 'metadata.rb' => 'version "1.0.0"' } data_bag 'x', { 'y' => '{}' } environment 'x', '{}' node 'x', '{}' role 'x', '{}' user 'x', '{}' when_the_repository 'has only top-level directories' do directory 'clients' directory 'cookbooks' directory 'data_bags' directory 'environments' directory 'nodes' directory 'roles' directory 'users' it 'knife download downloads everything' do knife('download /').should_succeed < 'y' } file 'cookbooks/x-1.0.0/blah.rb', '' file 'cookbooks/x-2.0.0/metadata.rb', 'version "2.0.0"' file 'cookbooks/y-1.0.0/metadata.rb', 'version "1.0.0"' file 'data_bags/x/z.json', < 'y' } file 'roles/y.json', < 'y' } it 'knife download does nothing' do knife('download /').should_succeed '' knife('diff --name-status /').should_succeed < /USAGE/ end end end end # Test download of an item when the other end doesn't even have the container when_the_repository 'is empty' do when_the_chef_server 'has two data bag items' do data_bag 'x', { 'y' => {}, 'z' => {} } it 'knife download of one data bag item itself succeeds' do knife('download /data_bags/x/y.json').should_succeed < {}, 'modified' => { 'foo' => 'bar' }, 'unmodified' => {} } it 'knife download of the modified file succeeds' do knife('download /data_bags/x/modified.json').should_succeed < /USAGE/ end it 'knife download --purge . downloads everything' do knife('download --purge .').should_succeed < 'version "1.0.0"', 'y.rb' => 'hi' } it 'knife download of a modified file succeeds' do knife('download /cookbooks/x-1.0.0/metadata.rb').should_succeed "Updated /cookbooks/x-1.0.0/metadata.rb\n" knife('diff --name-status /cookbooks').should_succeed < 'version "1.0.0"', 'onlyin1.0.0.rb' => '' } cookbook 'x', '1.0.1', { 'metadata.rb' => 'version "1.0.1"', 'onlyin1.0.1.rb' => 'hi' } it 'knife download /cookbooks/x downloads the latest version' do knife('download --purge /cookbooks').should_succeed < 'version "1.0.0"', 'onlyin1.0.0.rb' => ''} cookbook 'x', '0.9.9', { 'metadata.rb' => 'version "0.9.9"', 'onlyin0.9.9.rb' => 'hi' } it 'knife download /cookbooks downloads the updated file' do knife('download --purge /cookbooks').should_succeed < 'version "1.0.1"', 'onlyin1.0.1.rb' => 'hi' } it 'knife download /cookbooks/x downloads the latest version' do knife('download --purge /cookbooks').should_succeed < 'version "0.9.9"', 'onlyin0.9.9.rb' => 'hi' } it 'knife download --purge /cookbooks downloads the old version and deletes the new version' do knife('download --purge /cookbooks').should_succeed < "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\n" knife('diff --name-status /environments/x.json').should_succeed '' end end when_the_repository 'has the same environment with the wrong name in the file' do file 'environments/x.json', { 'name' => 'y' } it 'knife download succeeds' do knife('download /environments/x.json').should_succeed "Updated /environments/x.json\n" knife('diff --name-status /environments/x.json').should_succeed '' end end when_the_repository 'has the same environment with no name in the file' do file 'environments/x.json', { 'description' => 'hi' } it 'knife download succeeds' do knife('download /environments/x.json').should_succeed "Updated /environments/x.json\n" knife('diff --name-status /environments/x.json').should_succeed '' end end end end # with versioned cookbooks end