sites_controller_spec.rb 10.3 KB
Newer Older
PARISOT Clement's avatar
github    
PARISOT Clement committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright (c) 2009-2011 Cyril Rohr, INRIA Rennes - Bretagne Atlantique
#
# 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.

15
16
17
18
require 'spec_helper'

describe SitesController do
  render_views
Cyril Rohr's avatar
Cyril Rohr committed
19

Samir Noir's avatar
Samir Noir committed
20
21
22
  describe 'GET /sites' do
    it 'should get the correct collection of sites' do
      get :index, format: :json
23
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
24
25
26
      expect(json['total']).to eq 8
      expect(json['items'].length).to eq 8
      expect(json['items'][0]['uid']).to eq 'grenoble'
27
      expect(json['items'][0]['links']).to be_a(Array)
28
    end
Cyril Rohr's avatar
Cyril Rohr committed
29

Samir Noir's avatar
Samir Noir committed
30
    it 'should correctly set the URIs when X-Api-Path-Prefix is present' do
31
      @request.env['HTTP_X_API_PATH_PREFIX'] = 'sid'
Samir Noir's avatar
Samir Noir committed
32
      get :index, format: :json
33
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
34
      expect(json['links'].find { |l| l['rel'] == 'self' }['href']).to eq '/sid/sites'
35
    end
Cyril Rohr's avatar
Cyril Rohr committed
36

Samir Noir's avatar
Samir Noir committed
37
    it 'should correctly set the URIs when X-Api-Mount-Path is present' do
38
      @request.env['HTTP_X_API_MOUNT_PATH'] = '/sites'
Samir Noir's avatar
Samir Noir committed
39
      get :index, format: :json
40
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
41
      expect(json['links'].find { |l| l['rel'] == 'self' }['href']).to eq '/'
42
    end
Cyril Rohr's avatar
Cyril Rohr committed
43

Samir Noir's avatar
Samir Noir committed
44
    it 'should correctly set the URIs when X-Api-Mount-Path and X-Api-Path-Prefix are present' do
45
46
      @request.env['HTTP_X_API_PATH_PREFIX'] = 'sid'
      @request.env['HTTP_X_API_MOUNT_PATH'] = '/sites'
Samir Noir's avatar
Samir Noir committed
47
      get :index, format: :json
48
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
49
      expect(json['links'].find { |l| l['rel'] == 'self' }['href']).to eq '/sid/'
50
    end
Cyril Rohr's avatar
Cyril Rohr committed
51
  end # describe "GET /sites"
Cyril Rohr's avatar
Cyril Rohr committed
52

Samir Noir's avatar
Samir Noir committed
53
54
55
  describe 'GET /sites/{{site_id}}' do
    it 'should fail if the site does not exist' do
      get :show, params: { id: 'doesnotexist', format: :json }
56
      expect(response.status).to eq 404
57
    end
Cyril Rohr's avatar
Cyril Rohr committed
58

Samir Noir's avatar
Samir Noir committed
59
60
    it 'should return the site' do
      get :show, params: { id: 'rennes', format: :json }
61
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
62
      assert_expires_in(60, public: true)
63
      expect(json['uid']).to eq 'rennes'
Samir Noir's avatar
Samir Noir committed
64
65
66
67
68
      expect(json['links'].map { |l| l['rel'] }.sort).to eq %w[
        clusters
        deployments
        jobs
        metrics
Samir Noir's avatar
Samir Noir committed
69
        network_equipments
Samir Noir's avatar
Samir Noir committed
70
        parent
Samir Noir's avatar
Samir Noir committed
71
        pdus
Samir Noir's avatar
Samir Noir committed
72
        self
Samir Noir's avatar
Samir Noir committed
73
        servers
Samir Noir's avatar
Samir Noir committed
74
        status
Alexandre MERLIN's avatar
Alexandre MERLIN committed
75
        storage
Samir Noir's avatar
Samir Noir committed
76
77
78
        version
        versions
        vlans
79
      ]
Samir Noir's avatar
Samir Noir committed
80
      expect(json['links'].find do |l|
81
        l['rel'] == 'self'
Samir Noir's avatar
Samir Noir committed
82
83
      end['href']).to eq '/sites/rennes'
      expect(json['links'].find do |l|
84
        l['rel'] == 'clusters'
Samir Noir's avatar
Samir Noir committed
85
86
      end['href']).to eq '/sites/rennes/clusters'
      expect(json['links'].find do |l|
87
        l['rel'] == 'version'
Samir Noir's avatar
Samir Noir committed
88
      end['href']).to eq "/sites/rennes/versions/#{@latest_commit}"
89
    end
Samir Noir's avatar
Samir Noir committed
90

Samir Noir's avatar
Samir Noir committed
91
92
    it 'should return link for deployment' do
      get :show, params: { id: 'rennes', format: :json }
93
94
      expect(response.status).to eq 200
      expect(json['uid']).to eq 'rennes'
Samir Noir's avatar
Samir Noir committed
95
      expect(json['links'].find do |l|
96
        l['rel'] == 'deployments'
Samir Noir's avatar
Samir Noir committed
97
      end['href']).to eq '/sites/rennes/deployments'
Samir Noir's avatar
Samir Noir committed
98
    end
Samir Noir's avatar
Samir Noir committed
99

Samir Noir's avatar
Samir Noir committed
100
101
    it 'should return link /servers if present in site' do
      get :show, params: { id: 'nancy', format: :json }
102
103
      expect(response.status).to eq 200
      expect(json['uid']).to eq 'nancy'
Samir Noir's avatar
Samir Noir committed
104
      expect(json['links'].find do |l|
105
        l['rel'] == 'servers'
Samir Noir's avatar
Samir Noir committed
106
      end['href']).to eq '/sites/nancy/servers'
Samir Noir's avatar
Samir Noir committed
107
    end
108

Samir Noir's avatar
Samir Noir committed
109
    it 'should return the specified version, and the max-age value in the Cache-Control header should be big' do
Samir Noir's avatar
Samir Noir committed
110
      get :show, params: { id: 'rennes', format: :json, version: '2eefdbf0e48cad1bd2db4fa9c96397df168a9c68' }
111
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
112
      assert_expires_in(24 * 3600 * 30, public: true)
113
      expect(json['uid']).to eq 'rennes'
Samir Noir's avatar
Samir Noir committed
114
      expect(json['version']).to eq '2eefdbf0e48cad1bd2db4fa9c96397df168a9c68'
Samir Noir's avatar
Samir Noir committed
115
      expect(json['links'].find  do |l|
116
        l['rel'] == 'version'
Samir Noir's avatar
Samir Noir committed
117
      end['href']).to eq '/sites/rennes/versions/2eefdbf0e48cad1bd2db4fa9c96397df168a9c68'
118
    end
119
120
121
122
123
124
125
126
127
128

    it 'should return 404 if the specified branch does not exist' do
      get :show, params: { id: 'rennes', format: :json, branch: 'doesnotexist' }
      expect(response.status).to eq 404
    end

    it 'should return 404 if the specified version does not exist' do
      get :show, params: { id: 'rennes', format: :json, version: 'doesnotexist' }
      expect(response.status).to eq 404
    end
Samir Noir's avatar
Samir Noir committed
129
  end
Cyril Rohr's avatar
Cyril Rohr committed
130

Samir Noir's avatar
Samir Noir committed
131
  describe 'GET /sites/{{site_id}}/status (authenticated)' do
132
    before do
Samir Noir's avatar
Samir Noir committed
133
      authenticate_as('crohr')
134
135
    end

Samir Noir's avatar
Samir Noir committed
136
137
    it 'should return 200 and the site status' do
      get :status, params: { id: 'rennes', format: :json }
138
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
139
      expect(json['nodes'].length).to eq 75
Samir Noir's avatar
Samir Noir committed
140
      expect(json['nodes'].keys.map { |k| k.split('-')[0] }.uniq.sort).to eq %w[
Samir Noir's avatar
Samir Noir committed
141
142
143
        parapide
        parapluie
        parasilo
144
      ].sort
145
146
147
      expect(json['vlans']).to be_a(Hash)
      expect(json['vlans'].length).to eq 7
      expect(json['vlans']['1']['type']).to eq 'kavlan-local'
Samir Noir's avatar
Samir Noir committed
148
149
150
151
152
      expect(json['disks']).not_to be_nil
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['reservations']).not_to be_nil
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['free_slots']).not_to be_nil
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['busy_slots']).not_to be_nil
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['freeable_slots']).not_to be_nil
Samir Noir's avatar
Samir Noir committed
153
      expect(json.keys).to include('uid')
154
      expect(json['uid']).to eq @now.to_i
155
156
157
    end

    # GET /sites/{{site_id}}/status?network_address={{network_address}}
Samir Noir's avatar
Samir Noir committed
158
    it 'should return the status ONLY for the specified node' do
Samir Noir's avatar
Samir Noir committed
159
      get :status, params: { id: 'rennes', network_address: 'parapide-5.rennes.grid5000.fr', format: :json }
160
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
161
      expect(json['nodes'].keys.map { |k| k.split('.')[0] }.uniq.sort).to eq ['parapide-5']
162
      expect(json['disks']).to be_empty
Samir Noir's avatar
Samir Noir committed
163
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['reservations']).not_to be_nil
164
    end
165

166
    # GET /sites/{{site_id}}/status?disks=no
Samir Noir's avatar
Samir Noir committed
167
168
    it 'should return the status of nodes but not disks' do
      get :status, params: { id: 'rennes', disks: 'no', format: :json }
169
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
170
      expect(json['nodes'].length).to eq 75
Samir Noir's avatar
Samir Noir committed
171
      expect(json['nodes'].keys.map { |k| k.split('-')[0] }.uniq.sort).to eq %w[
Samir Noir's avatar
Samir Noir committed
172
173
174
        parapide
        parapluie
        parasilo
175
176
      ].sort
      expect(json['disks']).to be_nil
Samir Noir's avatar
Samir Noir committed
177
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['reservations']).not_to be_nil
178
179
    end

180
    # GET /sites/{{site_id}}/status?job_details=no
Samir Noir's avatar
Samir Noir committed
181
182
    it 'should return the status without the reservations' do
      get :status, params: { id: 'rennes', job_details: 'no', format: :json }
183
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
184
      expect(json['nodes'].length).to eq 75
Samir Noir's avatar
Samir Noir committed
185
      expect(json['nodes'].keys.map { |k| k.split('-')[0] }.uniq.sort).to eq %w[
Samir Noir's avatar
Samir Noir committed
186
187
188
        parapide
        parapluie
        parasilo
189
      ].sort
Samir Noir's avatar
Samir Noir committed
190
191
      expect(json['disks']).not_to be_nil
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['reservations']).to be_nil
192
193
    end

Samir Noir's avatar
Samir Noir committed
194
    it 'should fail gracefully in the event of a grit timeout' do
195
      expect_any_instance_of(Grid5000::Repository).to receive(:find_commit_for).and_raise(Rugged::RepositoryError)
Samir Noir's avatar
Samir Noir committed
196
      get :status, params: { id: 'rennes', job_details: 'no', format: :json }
197
198
      expect(response.status).to eq 503
    end
Cyril Rohr's avatar
Cyril Rohr committed
199
  end # "GET /sites/{{site_id}}/status"
Cyril Rohr's avatar
Cyril Rohr committed
200

Samir Noir's avatar
Samir Noir committed
201
  describe 'GET /sites/{{site_id}}/status (by anonymous)' do
202
    before do
Samir Noir's avatar
Samir Noir committed
203
204
      authenticate_as('anonymous')
      get :status, params: { id: 'rennes', format: :json }
205
206
207
      expect(response.status).to eq 200
    end

Samir Noir's avatar
Samir Noir committed
208
    it 'should not include reservations' do
Samir Noir's avatar
Samir Noir committed
209
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['reservations']).to be_nil
210
211
    end
  end
Samir Noir's avatar
Samir Noir committed
212
  describe 'GET /sites/{{site_id}}/status (unknown)' do
213
214
    # unknown users are authenticated users for which we don't have the precise login
    before do
Samir Noir's avatar
Samir Noir committed
215
216
      authenticate_as('unknown')
      get :status, params: { id: 'rennes', format: :json }
217
218
219
      expect(response.status).to eq 200
    end

Samir Noir's avatar
Samir Noir committed
220
    it 'should include reservations' do
Samir Noir's avatar
Samir Noir committed
221
      expect(json['nodes']['parapide-5.rennes.grid5000.fr']['reservations']).to_not be_nil
222
223
    end
  end
224
225
226
227
228

  describe 'GET /sites?deep=true' do
    it "should get the correct deep view of sites" do
      get :index, params: { format: :json, deep: true }
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
229
230
231
232
      expect(json['items'].length).to eq 8
      expect(json['items']['grenoble'].length).to eq 24
      expect(json['items']['grenoble']).to be_a(Hash)
      expect(json['items']['grenoble']['uid']).to eq 'grenoble'
233
234
235
236
237
    end

    it "should be the correct version" do
      get :index, params: { format: :json, deep: true }
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
238
      expect(json['version']).to eq @latest_commit
239
240
241
242
243
244
245
    end
  end

  describe "GET /sites/{{id}}?deep=true" do
    it "should get the correct deep view for one site" do
      get :show, params: { id: 'rennes', format: :json, deep: true }
      expect(response.status).to eq 200
Samir Noir's avatar
Samir Noir committed
246
247
      expect(json['total']).to eq 24
      expect(json['items'].length).to eq 24
248
      expect(json['items']['clusters']).to be_a(Hash)
Samir Noir's avatar
Samir Noir committed
249
      expect(json['items']['clusters']['parasilo']['uid']).to eq 'parasilo'
250
251
252
253
254
    end
  end

  describe "GET /sites/{{id}}?deep=true&job_id={{job_id}}" do
    it "should get the correct nodes collection for a job" do
Samir Noir's avatar
Samir Noir committed
255
      get :show, params: { id: 'rennes', job_id: '374187', format: :json, deep: true }
256
257
258
259
      expect(response.status).to eq 200
      expect(json['total']).to eq 3
      expect(json['items'].length).to eq 3
      expect(json['items']['clusters']).to be_a(Hash)
Samir Noir's avatar
Samir Noir committed
260
261
262
263
264
      expect(json['items']['clusters']['parapide']['uid']).to eq 'parapide'
      expect(json['items']['clusters']['parapide']['nodes']).to be_a(Array)
      expect(json['items']['clusters']['parapide']['nodes'].first['uid']).to eq 'parapide-1'
      expect(json['items']['clusters']['parapide']['nodes'].length).to eq 16
      expect(json['version']).to eq 'f449f0cb61b0cf5adf1ddbae47c9a409af9652f1'
265
266
    end
  end
267
end