|
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | # you may not use this file except in compliance with the License. |
8 | 8 | # You may obtain a copy of the License at |
9 | | -# |
| 9 | +# |
10 | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
11 | | -# |
| 11 | +# |
12 | 12 | # Unless required by applicable law or agreed to in writing, software |
13 | 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
14 | 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
24 | 24 |
|
25 | 25 | Chef::Config[:node_name] = "webmonkey.example.com" |
26 | 26 | @knife = Chef::Knife::NodeBulkDelete.new |
27 | | - @knife.config = { |
28 | | - :print_after => nil |
29 | | - } |
30 | | - @knife.name_args = ["."] |
31 | | - @knife.stub!(:output).and_return(true) |
32 | | - @knife.stub!(:confirm).and_return(true) |
| 27 | + @knife.name_args = ["."] |
| 28 | + @stdout = StringIO.new |
| 29 | + @knife.ui.stub!(:stdout).and_return(@stdout) |
| 30 | + @knife.ui.stub!(:confirm).and_return(true) |
33 | 31 | @nodes = Hash.new |
34 | 32 | %w{adam brent jacob}.each do |node_name| |
35 | | - node = Chef::Node.new() |
36 | | - node.name(node_name) |
37 | | - node.stub!(:destroy).and_return(true) |
38 | | - @nodes[node_name] = node |
| 33 | + @nodes[node_name] = "http://localhost:4000/nodes/#{node_name}" |
39 | 34 | end |
40 | | - Chef::Node.stub!(:list).and_return(@nodes) |
41 | 35 | end |
42 | 36 |
|
43 | | - describe "run" do |
44 | | - |
45 | | - it "should get the list of inflated nodes" do |
| 37 | + describe "when creating the list of nodes" do |
| 38 | + it "fetches the node list" do |
| 39 | + expected = @nodes.inject({}) do |inflatedish, (name, uri)| |
| 40 | + inflatedish[name] = Chef::Node.new.tap {|n| n.name(name)} |
| 41 | + inflatedish |
| 42 | + end |
46 | 43 | Chef::Node.should_receive(:list).and_return(@nodes) |
47 | | - @knife.run |
| 44 | + # I hate not having == defined for anything :( |
| 45 | + actual = @knife.all_nodes |
| 46 | + actual.keys.should =~ expected.keys |
| 47 | + actual.values.map {|n| n.name }.should =~ %w[adam brent jacob] |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + describe "run" do |
| 52 | + before do |
| 53 | + @inflatedish_list = @nodes.keys.inject({}) do |nodes_by_name, name| |
| 54 | + node = Chef::Node.new() |
| 55 | + node.name(name) |
| 56 | + node.stub!(:destroy).and_return(true) |
| 57 | + nodes_by_name[name] = node |
| 58 | + nodes_by_name |
| 59 | + end |
| 60 | + @knife.stub!(:all_nodes).and_return(@inflatedish_list) |
48 | 61 | end |
49 | 62 |
|
50 | 63 | it "should print the nodes you are about to delete" do |
51 | | - @knife.should_receive(:output).with(@knife.format_list_for_display(@nodes)) |
52 | 64 | @knife.run |
| 65 | + @stdout.string.should match(/#{@knife.ui.list(@nodes.keys.sort, :columns_down)}/) |
53 | 66 | end |
54 | 67 |
|
55 | 68 | it "should confirm you really want to delete them" do |
56 | | - @knife.should_receive(:confirm) |
| 69 | + @knife.ui.should_receive(:confirm) |
57 | 70 | @knife.run |
58 | 71 | end |
59 | 72 |
|
60 | 73 | it "should delete each node" do |
61 | | - @nodes.each_value do |n| |
| 74 | + @inflatedish_list.each_value do |n| |
62 | 75 | n.should_receive(:destroy) |
63 | 76 | end |
64 | 77 | @knife.run |
65 | 78 | end |
66 | 79 |
|
67 | 80 | it "should only delete nodes that match the regex" do |
68 | 81 | @knife.name_args = ['adam'] |
69 | | - @nodes['adam'].should_receive(:destroy) |
70 | | - @nodes['brent'].should_not_receive(:destroy) |
71 | | - @nodes['jacob'].should_not_receive(:destroy) |
| 82 | + @inflatedish_list['adam'].should_receive(:destroy) |
| 83 | + @inflatedish_list['brent'].should_not_receive(:destroy) |
| 84 | + @inflatedish_list['jacob'].should_not_receive(:destroy) |
72 | 85 | @knife.run |
73 | 86 | end |
74 | 87 |
|
|
77 | 90 | lambda { @knife.run }.should raise_error(SystemExit) |
78 | 91 | end |
79 | 92 |
|
80 | | - describe "with -p or --print-after" do |
81 | | - it "should pretty print the node, formatted for display" do |
82 | | - @knife.config[:print_after] = true |
83 | | - @nodes.each_value do |n| |
84 | | - @knife.should_receive(:output).with(@knife.format_for_display(n)) |
85 | | - end |
86 | | - @knife.run |
87 | | - end |
88 | | - end |
89 | 93 | end |
90 | 94 | end |
91 | 95 |
|
|
0 commit comments