summaryrefslogtreecommitdiff
path: root/chef/spec/unit/chef_fs/file_system/chef_server_root_dir_spec.rb
blob: b60193c4aebf7fd25f47e367108286a72b50dcc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2012 Opscode, 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 'spec_helper'
require 'chef/chef_fs/file_system/chef_server_root_dir'

describe Chef::ChefFS::FileSystem::ChefServerRootDir do
  shared_examples 'a json endpoint dir leaf' do
    it 'parent is endpoint' do
      endpoint_leaf.parent.should == endpoint
    end
    it 'name is correct' do
      endpoint_leaf.name.should == "#{endpoint_leaf_name}.json"
    end
    it 'path is correct' do
      endpoint_leaf.path.should == "/#{endpoint_name}/#{endpoint_leaf_name}.json"
    end
    it 'path_for_printing is correct' do
      endpoint_leaf.path_for_printing.should == "remote/#{endpoint_name}/#{endpoint_leaf_name}.json"
    end
    it 'is not a directory' do
      endpoint_leaf.dir?.should be_false
    end
    it 'exists' do
      should_receive_children
      endpoint_leaf.exists?.should be_true
    end
    it 'read returns content' do
      @rest.should_receive(:get_rest).with("#{endpoint_name}/#{endpoint_leaf_name}").once.and_return(
        {
          'a' => 'b'
        })
      endpoint_leaf.read.should == '{
  "a": "b"
}'
    end
  end

  shared_examples 'a json rest endpoint dir' do
    it 'parent is root' do
      endpoint.parent.should == root_dir
    end
    it 'has correct name' do
      endpoint.name.should == endpoint_name
    end
    it 'has correct path' do
      endpoint.path.should == "/#{endpoint_name}"
    end
    it 'has correct path_for_printing' do
      endpoint.path_for_printing.should == "remote/#{endpoint_name}"
    end
    it 'is a directory' do
      endpoint.dir?.should be_true
    end
    it 'exists' do
      endpoint.exists?.should be_true
    end
    it 'can have json files as children' do
      endpoint.can_have_child?('blah.json', false).should be_true
    end
    it 'cannot have non-json files as children' do
      endpoint.can_have_child?('blah', false).should be_false
    end
    it 'cannot have directories as children' do
      endpoint.can_have_child?('blah', true).should be_false
      endpoint.can_have_child?('blah.json', true).should be_false
    end
    let(:should_receive_children) {
      @rest.should_receive(:get_rest).with(endpoint_name).once.and_return(
        {
          "achild" => "http://opscode.com/achild",
          "bchild" => "http://opscode.com/bchild"
        })
    }
    it 'has correct children' do
      should_receive_children
      endpoint.children.map { |child| child.name }.should =~ %w(achild.json bchild.json)
    end
    context 'achild in endpoint.children' do
      let(:endpoint_leaf_name) { 'achild' }
      let(:endpoint_leaf) do
        should_receive_children
        endpoint.children.select { |child| child.name == 'achild.json' }.first
      end
      it_behaves_like 'a json endpoint dir leaf'
    end
    context 'endpoint.child(achild)' do
      let(:endpoint_leaf_name) { 'achild' }
      let(:endpoint_leaf) { endpoint.child('achild.json') }
      it_behaves_like 'a json endpoint dir leaf'
    end
    context 'nonexistent child()' do
      let(:nonexistent_child) { endpoint.child('blah.json') }
      it 'has correct parent, name, path and path_for_printing' do
        nonexistent_child.parent.should == endpoint
        nonexistent_child.name.should == "blah.json"
        nonexistent_child.path.should == "#{endpoint.path}/blah.json"
        nonexistent_child.path_for_printing.should == "#{endpoint.path_for_printing}/blah.json"
      end
      it 'does not exist' do
        should_receive_children
        nonexistent_child.exists?.should be_false
      end
      it 'is not a directory' do
        nonexistent_child.dir?.should be_false
      end
      it 'read returns NotFoundError' do
        @rest.should_receive(:get_rest).with("#{endpoint_name}/blah").once.and_raise(Net::HTTPServerException.new(nil,Net::HTTPResponse.new(nil,'404',nil)))
        expect { nonexistent_child.read }.to raise_error(Chef::ChefFS::FileSystem::NotFoundError)
      end
    end
  end

  let(:root_dir) {
    Chef::ChefFS::FileSystem::ChefServerRootDir.new('remote',
    {
      :chef_server_url => 'url',
      :node_name => 'username',
      :client_key => 'key'
    }, 'everything')
  }
  before(:each) do
    @rest = double("rest")
    Chef::REST.stub(:new).with('url','username','key') { @rest }
  end
  context 'the root directory' do
    it 'has no parent' do
      root_dir.parent.should == nil
    end
    it 'is a directory' do
      root_dir.dir?.should be_true
    end
    it 'exists' do
      root_dir.exists?.should be_true
    end
    it 'has name ""' do
      root_dir.name.should == ""
    end
    it 'has path /' do
      root_dir.path.should == '/'
    end
    it 'has path_for_printing remote/' do
      root_dir.path_for_printing.should == 'remote/'
    end
    it 'has correct children' do
      root_dir.children.map { |child| child.name }.should =~ %w(clients cookbooks data_bags environments nodes roles users)
    end
    it 'can have children with the known names' do
      %w(clients cookbooks data_bags environments nodes roles users).each { |child| root_dir.can_have_child?(child, true).should be_true }
    end
    it 'cannot have files as children' do
      %w(clients cookbooks data_bags environments nodes roles users).each { |child| root_dir.can_have_child?(child, false).should be_false }
      root_dir.can_have_child?('blah', false).should be_false
    end
    it 'cannot have other child directories than the known names' do
      root_dir.can_have_child?('blah', true).should be_false
    end
    it 'child() responds to children' do
      %w(clients cookbooks data_bags environments nodes roles users).each { |child| root_dir.child(child).exists?.should be_true }
    end
    context 'nonexistent child()' do
      let(:nonexistent_child) { root_dir.child('blah') }
      it 'has correct parent, name, path and path_for_printing' do
        nonexistent_child.parent.should == root_dir
        nonexistent_child.name.should == "blah"
        nonexistent_child.path.should == "/blah"
        nonexistent_child.path_for_printing.should == "remote/blah"
      end
      it 'does not exist' do
        nonexistent_child.exists?.should be_false
      end
      it 'is not a directory' do
        nonexistent_child.dir?.should be_false
      end
      it 'read returns NotFoundError' do
        expect { nonexistent_child.read }.to raise_error(Chef::ChefFS::FileSystem::NotFoundError)
      end
    end
  end

  context 'clients in children' do
    let(:endpoint_name) { 'clients' }
    let(:endpoint) { root_dir.children.select { |child| child.name == 'clients' }.first }

    it_behaves_like 'a json rest endpoint dir'
  end

  context 'root.child(clients)' do
    let(:endpoint_name) { 'clients' }
    let(:endpoint) { root_dir.child('clients') }

    it_behaves_like 'a json rest endpoint dir'
  end

  context 'root.child(environments)' do
    let(:endpoint_name) { 'environments' }
    let(:endpoint) { root_dir.child('environments') }

    it_behaves_like 'a json rest endpoint dir'
  end

  context 'root.child(nodes)' do
    let(:endpoint_name) { 'nodes' }
    let(:endpoint) { root_dir.child('nodes') }

    it_behaves_like 'a json rest endpoint dir'
  end

  context 'root.child(roles)' do
    let(:endpoint_name) { 'roles' }
    let(:endpoint) { root_dir.child('roles') }

    it_behaves_like 'a json rest endpoint dir'
  end

  context 'root.child(users)' do
    let(:endpoint_name) { 'users' }
    let(:endpoint) { root_dir.child('users') }

    it_behaves_like 'a json rest endpoint dir'
  end
end