summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2019-10-14 17:08:14 -0700
committerGitHub <noreply@github.com>2019-10-14 17:08:14 -0700
commit8357a03fec18c3b1690895debb27093924fc50d4 (patch)
tree53f0e907dba1a2f9516c6be266dc05b4b845235c
parent5deba6e63e368d929e7878c4ff68070b8c62a61e (diff)
parent2f8e934cb814ed937068386219ad2b799e27d96b (diff)
downloadchef-8357a03fec18c3b1690895debb27093924fc50d4.tar.gz
Merge pull request #8971 from MsysTechnologiesllc/VSingh/MSYS-1122_knife_supermarket_list_enhancement
Enhance knife supermarket list & search
-rw-r--r--lib/chef/knife/supermarket_list.rb26
-rw-r--r--lib/chef/knife/supermarket_search.rb5
-rw-r--r--spec/unit/knife/supermarket_list_spec.rb70
-rw-r--r--spec/unit/knife/supermarket_search_spec.rb85
4 files changed, 177 insertions, 9 deletions
diff --git a/lib/chef/knife/supermarket_list.rb b/lib/chef/knife/supermarket_list.rb
index 700d928af8..ec57830b00 100644
--- a/lib/chef/knife/supermarket_list.rb
+++ b/lib/chef/knife/supermarket_list.rb
@@ -37,23 +37,35 @@ class Chef
default: "https://supermarket.chef.io",
proc: Proc.new { |supermarket| Chef::Config[:knife][:supermarket_site] = supermarket }
+ option :sort_by,
+ long: "--sort-by SORT",
+ description: "Use to sort the records",
+ in: %w{recently_updated recently_added most_downloaded most_followed}
+
+ option :owned_by,
+ short: "-o USER",
+ long: "--owned-by USER",
+ description: "Show cookbooks that are owned by the USER"
+
def run
if config[:with_uri]
- cookbooks = {}
- get_cookbook_list.each { |k, v| cookbooks[k] = v["cookbook"] }
- ui.output(format_for_display(cookbooks))
+ ui.output(format_for_display(get_cookbook_list))
else
- ui.msg(ui.list(get_cookbook_list.keys.sort, :columns_down))
+ ui.msg(ui.list(get_cookbook_list.keys, :columns_down))
end
end
- def get_cookbook_list(items = 10, start = 0, cookbook_collection = {})
+ # In order to avoid pagination items limit set to 9999999
+ def get_cookbook_list(items = 9999999, start = 0, cookbook_collection = {})
cookbooks_url = "#{config[:supermarket_site]}/api/v1/cookbooks?items=#{items}&start=#{start}"
+ cookbooks_url << "&order=#{config[:sort_by]}" if config[:sort_by]
+ cookbooks_url << "&user=#{config[:owned_by]}" if config[:owned_by]
cr = noauth_rest.get(cookbooks_url)
+
cr["items"].each do |cookbook|
- cookbook_collection[cookbook["cookbook_name"]] = cookbook
+ cookbook_collection[cookbook["cookbook_name"]] = cookbook["cookbook"]
end
- new_start = start + cr["items"].length
+ new_start = start + items
if new_start < cr["total"]
get_cookbook_list(items, new_start, cookbook_collection)
else
diff --git a/lib/chef/knife/supermarket_search.rb b/lib/chef/knife/supermarket_search.rb
index 40614b4070..f3a57d872a 100644
--- a/lib/chef/knife/supermarket_search.rb
+++ b/lib/chef/knife/supermarket_search.rb
@@ -35,13 +35,14 @@ class Chef
output(search_cookbook(name_args[0]))
end
- def search_cookbook(query, items = 10, start = 0, cookbook_collection = {})
+ # In order to avoid pagination items limit set to 9999999
+ def search_cookbook(query, items = 9999999, start = 0, cookbook_collection = {})
cookbooks_url = "#{config[:supermarket_site]}/api/v1/search?q=#{query}&items=#{items}&start=#{start}"
cr = noauth_rest.get(cookbooks_url)
cr["items"].each do |cookbook|
cookbook_collection[cookbook["cookbook_name"]] = cookbook
end
- new_start = start + cr["items"].length
+ new_start = start + items
if new_start < cr["total"]
search_cookbook(query, items, new_start, cookbook_collection)
else
diff --git a/spec/unit/knife/supermarket_list_spec.rb b/spec/unit/knife/supermarket_list_spec.rb
new file mode 100644
index 0000000000..4181e42b81
--- /dev/null
+++ b/spec/unit/knife/supermarket_list_spec.rb
@@ -0,0 +1,70 @@
+#
+# Author:: Vivek Singh (<vivek.singh@msystechnologies.com>)
+# Copyright:: Copyright 2018-2019, Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# 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 "chef/knife/supermarket_list"
+require "spec_helper"
+
+describe Chef::Knife::SupermarketList do
+ let(:knife) { described_class.new }
+ let(:noauth_rest) { double("no auth rest") }
+ let(:stdout) { StringIO.new }
+ let(:cookbooks_data) {
+ [
+ { "cookbook_name" => "1password", "cookbook_maintainer" => "jtimberman", "cookbook_description" => "Installs 1password", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/1password" },
+ { "cookbook_name" => "301", "cookbook_maintainer" => "markhuge", "cookbook_description" => "Installs/Configures 301", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/301" },
+ { "cookbook_name" => "3cx", "cookbook_maintainer" => "obay", "cookbook_description" => "Installs/Configures 3cx", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/3cx" },
+ { "cookbook_name" => "7dtd", "cookbook_maintainer" => "gregf", "cookbook_description" => "Installs/Configures the 7 Days To Die server", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/7dtd" },
+ { "cookbook_name" => "7-zip", "cookbook_maintainer" => "sneal", "cookbook_description" => "Installs/Configures the 7-zip file archiver", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/7-zip" },
+ ]
+ }
+
+ let(:response_text) {
+ {
+ "start" => 0,
+ "total" => 5,
+ "items" => cookbooks_data,
+ }
+ }
+
+ describe "run" do
+ before do
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
+ allow(knife).to receive(:noauth_rest).and_return(noauth_rest)
+ expect(noauth_rest).to receive(:get).and_return(response_text)
+ knife.configure_chef
+ end
+
+ it "should display all supermarket cookbooks" do
+ knife.run
+ cookbooks_data.each do |item|
+ expect(stdout.string).to match /#{item["cookbook_name"]}\s/
+ end
+ end
+
+ describe "with -w or --with-uri" do
+ it "should display the cookbook uris" do
+ knife.config[:with_uri] = true
+ knife.run
+ cookbooks_data.each do |item|
+ expect(stdout.string).to match /#{item["cookbook_name"]}\s/
+ expect(stdout.string).to match /#{item["cookbook"]}\s/
+ end
+ end
+ end
+ end
+end
diff --git a/spec/unit/knife/supermarket_search_spec.rb b/spec/unit/knife/supermarket_search_spec.rb
new file mode 100644
index 0000000000..4ffc35c59a
--- /dev/null
+++ b/spec/unit/knife/supermarket_search_spec.rb
@@ -0,0 +1,85 @@
+#
+# Author:: Vivek Singh (<vivek.singh@msystechnologies.com>)
+# Copyright:: Copyright 2018-2019, Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# 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 "chef/knife/supermarket_search"
+require "spec_helper"
+
+describe Chef::Knife::SupermarketSearch do
+ let(:knife) { described_class.new }
+ let(:noauth_rest) { double("no auth rest") }
+ let(:stdout) { StringIO.new }
+ let(:cookbooks_data) {
+ [
+ { "cookbook_name" => "mysql", "cookbook_maintainer" => "sous-chefs", "cookbook_description" => "Provides mysql_service, mysql_config, and mysql_client resources", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/mysql" },
+ { "cookbook_name" => "mw_mysql", "cookbook_maintainer" => "car", "cookbook_description" => "Installs/Configures mw_mysql", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/mw_mysql" },
+ { "cookbook_name" => "L7-mysql", "cookbook_maintainer" => "szelcsanyi", "cookbook_description" => "Installs/Configures MySQL server", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/l7-mysql" },
+ { "cookbook_name" => "mysql-sys", "cookbook_maintainer" => "ovaistariq", "cookbook_description" => "Installs the mysql-sys tool. Description of the tool is available here https://github.com/MarkLeith/mysql-sys", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/mysql-sys" },
+ { "cookbook_name" => "cg_mysql", "cookbook_maintainer" => "phai", "cookbook_description" => "Installs/Configures mysql with master and slave", "cookbook" => "https://supermarket.chef.io/api/v1/cookbooks/cg_mysql" },
+ ]
+ }
+
+ let(:response_text) {
+ {
+ "start" => 0,
+ "total" => 5,
+ "items" => cookbooks_data,
+ }
+ }
+
+ let(:empty_response_text) {
+ {
+ "start" => 0,
+ "total" => 0,
+ "items" => [],
+ }
+ }
+
+ describe "run" do
+ before do
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
+ allow(knife).to receive(:noauth_rest).and_return(noauth_rest)
+ knife.configure_chef
+ end
+
+ context "when name_args is present" do
+ before do
+ expect(noauth_rest).to receive(:get).and_return(response_text)
+ end
+
+ it "should display cookbooks with given name value" do
+ knife.name_args = ["mysql"]
+ knife.run
+ cookbooks_data.each do |item|
+ expect(stdout.string).to match /#{item["cookbook_name"]}\s/
+ end
+ end
+ end
+
+ context "when name_args is empty string" do
+ before do
+ expect(noauth_rest).to receive(:get).and_return(empty_response_text)
+ end
+
+ it "display nothing with name arg empty string" do
+ knife.name_args = [""]
+ knife.run
+ expect(stdout.string).to eq("\n")
+ end
+ end
+ end
+end