diff options
author | Tim Smith <tsmith@chef.io> | 2018-07-13 10:37:34 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-13 10:37:34 -0700 |
commit | d99bc319e244e4d4fac57bc1853ea82f8b97c0e9 (patch) | |
tree | 77eac01f6baac6486308a30c8c3982f03309fb39 /spec | |
parent | 8baff8ae8f6dc0e88c1a467566b1ac595ceef267 (diff) | |
parent | 6fb425078b5986998c37450445f5b3ded61e02ac (diff) | |
download | chef-d99bc319e244e4d4fac57bc1853ea82f8b97c0e9.tar.gz |
Merge pull request #7455 from coderanger/config-commands
Add knife config get/use-profile commands
Diffstat (limited to 'spec')
-rw-r--r-- | spec/integration/knife/config_get_profile_spec.rb | 112 | ||||
-rw-r--r-- | spec/integration/knife/config_list_profiles_spec.rb | 188 | ||||
-rw-r--r-- | spec/integration/knife/config_use_profile_spec.rb | 100 |
3 files changed, 400 insertions, 0 deletions
diff --git a/spec/integration/knife/config_get_profile_spec.rb b/spec/integration/knife/config_get_profile_spec.rb new file mode 100644 index 0000000000..e97b24b869 --- /dev/null +++ b/spec/integration/knife/config_get_profile_spec.rb @@ -0,0 +1,112 @@ +# +# Copyright 2018, Noah Kantrowitz +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "support/shared/integration/integration_helper" +require "support/shared/context/config" + +describe "knife config get-profile", :workstation do + include IntegrationSupport + include KnifeSupport + + include_context "default config options" + include_context "with a chef repo" + + let(:cmd_args) { [] } + + subject do + cmd = knife("config", "get-profile", *cmd_args, instance_filter: lambda { |instance| + # Fake the failsafe check because this command doesn't actually process knife.rb. + $__KNIFE_INTEGRATION_FAILSAFE_CHECK << " ole" + }) + cmd.stdout + end + + around do |ex| + # Store and reset the value of some env vars. + old_chef_home = ENV["CHEF_HOME"] + old_knife_home = ENV["KNIFE_HOME"] + old_home = ENV["HOME"] + old_wd = Dir.pwd + ChefConfig::PathHelper.per_tool_home_environment = "KNIFE_HOME" + # Clear these out because they are cached permanently. + ChefConfig::PathHelper.class_exec { remove_class_variable(:@@home_dir) } + Chef::Knife::ConfigGetProfile.reset_config_loader! + begin + ex.run + ensure + ENV["CHEF_HOME"] = old_chef_home + ENV["KNIFE_HOME"] = old_knife_home + ENV["HOME"] = old_home + Dir.chdir(old_wd) + ENV[ChefConfig.windows? ? "CD" : "PWD"] = Dir.pwd + ChefConfig::PathHelper.per_tool_home_environment = nil + end + end + + before do + # Always run from the temp folder. This can't be in the `around` block above + # because it has to run after the before set in the "with a chef repo" shared context. + directory("repo") + Dir.chdir(path_to("repo")) + ENV[ChefConfig.windows? ? "CD" : "PWD"] = Dir.pwd + ENV["HOME"] = path_to(".") + end + + context "with no configuration" do + it { is_expected.to eq "default\n" } + end + + context "with --profile" do + let(:cmd_args) { %w{--profile production} } + it { is_expected.to eq "production\n" } + end + + context "with an environment variable" do + around do |ex| + old_chef_profile = ENV["CHEF_PROFILE"] + begin + ENV["CHEF_PROFILE"] = "staging" + ex.run + ensure + ENV["CHEF_PROFILE"] = old_chef_profile + end + end + + it { is_expected.to eq "staging\n" } + end + + context "with a context file" do + before { file(".chef/context", "development\n") } + it { is_expected.to eq "development\n" } + end + + context "with a context file under $CHEF_HOME" do + before do + file("chefhome/.chef/context", "other\n") + ENV["CHEF_HOME"] = path_to("chefhome") + end + + it { is_expected.to eq "other\n" } + end + + context "with a context file under $KNIFE_HOME" do + before do + file("knifehome/.chef/context", "other\n") + ENV["KNIFE_HOME"] = path_to("knifehome") + end + + it { is_expected.to eq "other\n" } + end +end diff --git a/spec/integration/knife/config_list_profiles_spec.rb b/spec/integration/knife/config_list_profiles_spec.rb new file mode 100644 index 0000000000..32846f9999 --- /dev/null +++ b/spec/integration/knife/config_list_profiles_spec.rb @@ -0,0 +1,188 @@ +# +# Copyright 2018, Noah Kantrowitz +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "support/shared/integration/integration_helper" +require "support/shared/context/config" + +describe "knife config list-profiles", :workstation do + include IntegrationSupport + include KnifeSupport + + include_context "default config options" + include_context "with a chef repo" + + let(:cmd_args) { [] } + let(:knife_list_profiles) do + knife("config", "list-profiles", *cmd_args, instance_filter: proc { + # Clear the stub set up in KnifeSupport. + allow(File).to receive(:file?).and_call_original + }) + end + subject { knife_list_profiles.stdout } + + around do |ex| + # Store and reset the value of some env vars. + old_home = ENV["HOME"] + old_wd = Dir.pwd + # Clear these out because they are cached permanently. + ChefConfig::PathHelper.class_exec { remove_class_variable(:@@home_dir) } + Chef::Knife::ConfigListProfiles.reset_config_loader! + begin + ex.run + ensure + ENV["HOME"] = old_home + Dir.chdir(old_wd) + ENV[ChefConfig.windows? ? "CD" : "PWD"] = Dir.pwd + end + end + + before do + # Always run from the temp folder. This can't be in the `around` block above + # because it has to run after the before set in the "with a chef repo" shared context. + directory("repo") + Dir.chdir(path_to("repo")) + ENV[ChefConfig.windows? ? "CD" : "PWD"] = Dir.pwd + ENV["HOME"] = path_to(".") + end + + # NOTE: The funky formatting with # at the end of the line of some of the + # output examples are because of how the format strings are built, there is + # substantial trailing whitespace in most cases which many editors "helpfully" remove. + + context "with no credentials file" do + subject { knife_list_profiles.stderr } + it { is_expected.to eq "FATAL: No profiles found, #{path_to(".chef/credentials")} does not exist or is empty\n" } + end + + context "with an empty credentials file" do + before { file(".chef/credentials", "") } + subject { knife_list_profiles.stderr } + it { is_expected.to eq "FATAL: No profiles found, #{path_to(".chef/credentials")} does not exist or is empty\n" } + end + + context "with a simple default profile" do + before { file(".chef/credentials", <<~EOH) } + [default] + client_name = "testuser" + client_key = "testkey.pem" + chef_server_url = "https://example.com/organizations/testorg" + EOH + it { is_expected.to eq <<~EOH.delete("#") } + Profile Client Key Server # + ----------------------------------------------------------------------------------# + *default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg# + EOH + end + + context "with multiple profiles" do + before { file(".chef/credentials", <<~EOH) } + [default] + client_name = "testuser" + client_key = "testkey.pem" + chef_server_url = "https://example.com/organizations/testorg" + + [prod] + client_name = "testuser" + client_key = "testkey.pem" + chef_server_url = "https://example.com/organizations/prod" + + [qa] + client_name = "qauser" + client_key = "~/src/qauser.pem" + chef_server_url = "https://example.com/organizations/testorg" + EOH + it { is_expected.to eq <<~EOH.delete("#") } + Profile Client Key Server # + ----------------------------------------------------------------------------------# + *default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg# + prod testuser ~/.chef/testkey.pem https://example.com/organizations/prod # + qa qauser ~/src/qauser.pem https://example.com/organizations/testorg# + EOH + end + + context "with a non-default active profile" do + let(:cmd_args) { %w{--profile prod} } + before { file(".chef/credentials", <<~EOH) } + [default] + client_name = "testuser" + client_key = "testkey.pem" + chef_server_url = "https://example.com/organizations/testorg" + + [prod] + client_name = "testuser" + client_key = "testkey.pem" + chef_server_url = "https://example.com/organizations/prod" + + [qa] + client_name = "qauser" + client_key = "~/src/qauser.pem" + chef_server_url = "https://example.com/organizations/testorg" + EOH + it { is_expected.to eq <<~EOH.delete("#") } + Profile Client Key Server # + ----------------------------------------------------------------------------------# + default testuser ~/.chef/testkey.pem https://example.com/organizations/testorg# + *prod testuser ~/.chef/testkey.pem https://example.com/organizations/prod # + qa qauser ~/src/qauser.pem https://example.com/organizations/testorg# + EOH + end + + context "with a minimal profile" do + before { file(".chef/credentials", <<~EOH) } + [default] + chef_server_url = "https://example.com/organizations/testorg" + EOH + it { is_expected.to match %r{^*default .*? https://example.com/organizations/testorg$} } + end + + context "with -i" do + let(:cmd_args) { %w{-i} } + before { file(".chef/credentials", <<~EOH) } + [default] + chef_server_url = "https://example.com/organizations/testorg" + EOH + it { is_expected.to eq <<~EOH.delete("#") } + Profile Client Key Server # + ----------------------------------------------------------------# + *default https://example.com/organizations/testorg# + EOH + end + + context "with --format=json" do + let(:cmd_args) { %w{--format=json node_name} } + before { file(".chef/credentials", <<~EOH) } + [default] + client_name = "testuser" + client_key = "testkey.pem" + chef_server_url = "https://example.com/organizations/testorg" + + [prod] + client_name = "testuser" + client_key = "testkey.pem" + chef_server_url = "https://example.com/organizations/prod" + + [qa] + client_name = "qauser" + client_key = "~/src/qauser.pem" + chef_server_url = "https://example.com/organizations/testorg" + EOH + it { + expect(JSON.parse(subject)).to eq [ + { "profile" => "default", "active" => true, "client_name" => "testuser", "client_key" => path_to(".chef/testkey.pem"), "server_url" => "https://example.com/organizations/testorg" }, + { "profile" => "prod", "active" => false, "client_name" => "testuser", "client_key" => path_to(".chef/testkey.pem"), "server_url" => "https://example.com/organizations/prod" }, + { "profile" => "qa", "active" => false, "client_name" => "qauser", "client_key" => path_to("src/qauser.pem"), "server_url" => "https://example.com/organizations/testorg" }, + ] } + end +end diff --git a/spec/integration/knife/config_use_profile_spec.rb b/spec/integration/knife/config_use_profile_spec.rb new file mode 100644 index 0000000000..a021dbbe6f --- /dev/null +++ b/spec/integration/knife/config_use_profile_spec.rb @@ -0,0 +1,100 @@ +# +# Copyright 2018, Noah Kantrowitz +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "support/shared/integration/integration_helper" +require "support/shared/context/config" + +describe "knife config use-profile", :workstation do + include IntegrationSupport + include KnifeSupport + + include_context "default config options" + include_context "with a chef repo" + + let(:cmd_args) { [] } + + let(:knife_use_profile) do + knife("config", "use-profile", *cmd_args, instance_filter: lambda { |instance| + # Fake the failsafe check because this command doesn't actually process knife.rb. + $__KNIFE_INTEGRATION_FAILSAFE_CHECK << " ole" + }) + end + + subject { knife_use_profile.stdout } + + around do |ex| + # Store and reset the value of some env vars. + old_chef_home = ENV["CHEF_HOME"] + old_knife_home = ENV["KNIFE_HOME"] + old_home = ENV["HOME"] + old_wd = Dir.pwd + ChefConfig::PathHelper.per_tool_home_environment = "KNIFE_HOME" + # Clear these out because they are cached permanently. + ChefConfig::PathHelper.class_exec { remove_class_variable(:@@home_dir) } + Chef::Knife::ConfigUseProfile.reset_config_loader! + begin + ex.run + ensure + ENV["CHEF_HOME"] = old_chef_home + ENV["KNIFE_HOME"] = old_knife_home + ENV["HOME"] = old_home + Dir.chdir(old_wd) + ENV[ChefConfig.windows? ? "CD" : "PWD"] = Dir.pwd + ChefConfig::PathHelper.per_tool_home_environment = nil + end + end + + before do + # Always run from the temp folder. This can't be in the `around` block above + # because it has to run after the before set in the "with a chef repo" shared context. + directory("repo") + Dir.chdir(path_to("repo")) + ENV[ChefConfig.windows? ? "CD" : "PWD"] = Dir.pwd + ENV["HOME"] = path_to(".") + end + + context "with no argument" do + subject { knife_use_profile.stderr } + it { is_expected.to eq "FATAL: You must specify a profile\n" } + end + + context "with an argument" do + let(:cmd_args) { %w{production} } + it do + is_expected.to eq "Set default profile to production\n" + expect(File.read(path_to(".chef/context"))).to eq "production\n" + end + end + + context "with $CHEF_HOME" do + let(:cmd_args) { %w{staging} } + before { ENV["CHEF_HOME"] = path_to("chefhome"); file("chefhome/tmp", "") } + it do + is_expected.to eq "Set default profile to staging\n" + expect(File.read(path_to("chefhome/.chef/context"))).to eq "staging\n" + expect(File.exist?(path_to(".chef/context"))).to be_falsey + end + end + + context "with $KNIFE_HOME" do + let(:cmd_args) { %w{development} } + before { ENV["KNIFE_HOME"] = path_to("knifehome"); file("knifehome/tmp", "") } + it do + is_expected.to eq "Set default profile to development\n" + expect(File.read(path_to("knifehome/.chef/context"))).to eq "development\n" + expect(File.exist?(path_to(".chef/context"))).to be_falsey + end + end +end |