From 865ae8a61ccb3395c7f738871be0bf7a2d1b02ce Mon Sep 17 00:00:00 2001 From: John Keiser Date: Sun, 31 Aug 2014 10:07:21 -0700 Subject: Add org.json, members.json, invitations.json for full org download and upload support --- spec/integration/knife/delete_spec.rb | 29 +++- spec/integration/knife/download_spec.rb | 76 ++++++++++ spec/integration/knife/list_spec.rb | 103 +++++++++++++- spec/integration/knife/upload_spec.rb | 153 +++++++++++++++++++++ .../shared/integration/integration_helper.rb | 12 +- 5 files changed, 365 insertions(+), 8 deletions(-) (limited to 'spec') diff --git a/spec/integration/knife/delete_spec.rb b/spec/integration/knife/delete_spec.rb index dc52cc71a4..a9ba9dc3e1 100644 --- a/spec/integration/knife/delete_spec.rb +++ b/spec/integration/knife/delete_spec.rb @@ -962,9 +962,12 @@ EOM end end - when_the_chef_server "is in Enterprise mode", :osc_compat => false, :single_org => false do + when_the_chef_server "is in Enterprise mode", :focus, :osc_compat => false, :single_org => false do before do - organization 'foo' + organization 'foo' do + container 'x', {} + group 'x', {} + end end before :each do @@ -974,5 +977,27 @@ EOM it 'knife delete /acls/containers/environments.json fails with a reasonable error' do knife('delete /acls/containers/environments.json').should_fail "ERROR: /acls/containers/environments.json (remote) cannot be deleted.\n" end + + it 'knife delete /containers/x.json succeeds' do + knife('delete /containers/x.json').should_succeed "Deleted /containers/x.json\n" + knife('raw /containers/x.json').should_fail(/404/) + end + + it 'knife delete /groups/x.json succeeds' do + knife('delete /groups/x.json').should_succeed "Deleted /groups/x.json\n" + knife('raw /groups/x.json').should_fail(/404/) + end + + it 'knife delete /org.json fails with a reasonable error' do + knife('delete /org.json').should_fail "ERROR: /org.json (remote) cannot be deleted.\n" + end + + it 'knife delete /invitations.json fails with a reasonable error' do + knife('delete /invitations.json').should_fail "ERROR: /invitations.json (remote) cannot be deleted.\n" + end + + it 'knife delete /members.json fails with a reasonable error' do + knife('delete /members.json').should_fail "ERROR: /members.json (remote) cannot be deleted.\n" + end end end diff --git a/spec/integration/knife/download_spec.rb b/spec/integration/knife/download_spec.rb index 4126acc9fc..17779d1f47 100644 --- a/spec/integration/knife/download_spec.rb +++ b/spec/integration/knife/download_spec.rb @@ -1091,4 +1091,80 @@ EOM end end end + + when_the_chef_server "is in Enterprise mode", :osc_compat => false, :single_org => false do + before do + organization 'foo' do + container 'x', {} + group 'x', {} + end + end + + before :each do + Chef::Config.chef_server_url = URI.join(Chef::Config.chef_server_url, '/organizations/foo') + end + + when_the_repository 'is empty' do + it 'knife download / downloads everything' do + knife('download /').should_succeed < false, :single_org => false do + before do + organization 'foo' + end + + before :each do + Chef::Config.chef_server_url = URI.join(Chef::Config.chef_server_url, '/organizations/foo') + end + + it 'knife list -R / returns everything' do + knife('list -R /').should_succeed < false, :single_org => false do + before do + user 'foo', {} + user 'bar', {} + user 'foobar', {} + organization 'foo', { 'full_name' => 'Something'} + end + + before :each do + Chef::Config.chef_server_url = URI.join(Chef::Config.chef_server_url, '/organizations/foo') + end + + context 'and has nothing but a single group named blah' do + group 'blah', {} + + when_the_repository 'has one of each thing' do + + before do + # TODO We have to upload acls for an existing group due to a lack of + # dependency detection during upload. Fix that! + file 'acls/groups/blah.json', {} + file 'clients/x.json', { 'public_key' => ChefZero::PUBLIC_KEY } + file 'containers/x.json', {} + file 'cookbooks/x/metadata.rb', cb_metadata("x", "1.0.0") + file 'data_bags/x/y.json', {} + file 'environments/x.json', {} + file 'groups/x.json', {} + file 'invitations.json', [ 'foo' ] + file 'members.json', [ 'bar' ] + file 'nodes/x.json', {} + file 'org.json', { 'full_name' => 'wootles' } + file 'roles/x.json', {} + end + + it 'knife upload / uploads everything' do + knife('upload /').should_succeed < 'Something' } + end + + it 'knife upload / emits a warning for bar and adds foo and foobar' do + knife('upload /').should_succeed '' + api.get('/')['full_name'].should == 'Something' + end + end + + when_the_repository 'has an org.json that changes full_name' do + before do + file 'org.json', { 'full_name' => 'Something Else'} + end + + it 'knife upload / emits a warning for bar and adds foo and foobar' do + knife('upload /').should_succeed "Updated /org.json\n" + api.get('/')['full_name'].should == 'Something Else' + end + end + + context 'and has invited foo and bar is already a member' do + org_invite 'foo' + org_member 'bar' + + when_the_repository 'wants to invite foo, bar and foobar' do + before do + file 'invitations.json', [ 'foo', 'bar', 'foobar' ] + end + + it 'knife upload / emits a warning for bar and invites foobar' do + knife('upload /').should_succeed "Updated /invitations.json\n", :stderr => "WARN: Could not invite bar to organization foo: User bar is already in organization foo\n" + api.get('association_requests').map { |a| a['username'] }.should == [ 'foo', 'foobar' ] + api.get('users').map { |a| a['user']['username'] }.should == [ 'bar' ] + end + end + + when_the_repository 'wants to make foo, bar and foobar members' do + before do + file 'members.json', [ 'foo', 'bar', 'foobar' ] + end + + it 'knife upload / emits a warning for bar and adds foo and foobar' do + knife('upload /').should_succeed "Updated /members.json\n" + api.get('association_requests').map { |a| a['username'] }.should == [ ] + api.get('users').map { |a| a['user']['username'] }.should == [ 'bar', 'foo', 'foobar' ] + end + end + + when_the_repository 'wants to invite foo and have bar as a member' do + before do + file 'invitations.json', [ 'foo' ] + file 'members.json', [ 'bar' ] + end + + it 'knife upload / does nothing' do + knife('upload /').should_succeed '' + api.get('association_requests').map { |a| a['username'] }.should == [ 'foo' ] + api.get('users').map { |a| a['user']['username'] }.should == [ 'bar' ] + end + end + end + + context 'and has invited bar and foo' do + org_invite 'bar', 'foo' + + when_the_repository 'wants to invite foo and bar (different order)' do + before do + file 'invitations.json', [ 'foo', 'bar' ] + end + + it 'knife upload / does nothing' do + knife('upload /').should_succeed '' + api.get('association_requests').map { |a| a['username'] }.should == [ 'bar', 'foo' ] + api.get('users').map { |a| a['user']['username'] }.should == [ ] + end + end + end + + context 'and has already added bar and foo as members of the org' do + org_member 'bar', 'foo' + + when_the_repository 'wants to add foo and bar (different order)' do + before do + file 'members.json', [ 'foo', 'bar' ] + end + + it 'knife upload / does nothing' do + knife('upload /').should_succeed '' + api.get('association_requests').map { |a| a['username'] }.should == [ ] + api.get('users').map { |a| a['user']['username'] }.should == [ 'bar', 'foo' ] + end + end + end + end + end end diff --git a/spec/support/shared/integration/integration_helper.rb b/spec/support/shared/integration/integration_helper.rb index a6595e778d..465633b9e0 100644 --- a/spec/support/shared/integration/integration_helper.rb +++ b/spec/support/shared/integration/integration_helper.rb @@ -20,9 +20,9 @@ require 'tmpdir' require 'fileutils' require 'chef/config' -require 'chef_zero/rspec' - require 'chef/json_compat' +require 'chef/server_api' +require 'chef_zero/rspec' require 'support/shared/integration/knife_support' require 'support/shared/integration/app_server_support' require 'spec_helper' @@ -53,6 +53,10 @@ module IntegrationSupport includer_class.extend(ClassMethods) end + def api + Chef::ServerAPI.new + end + def directory(relative_path, &block) old_parent_path = @parent_path @parent_path = path_to(relative_path) @@ -67,10 +71,8 @@ module IntegrationSupport FileUtils.mkdir_p(dir) unless dir == '.' File.open(filename, 'w') do |file| raw = case contents - when Hash + when Hash, Array JSON.pretty_generate(contents) - when Array - contents.join("\n") else contents end -- cgit v1.2.1