| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  | require 'db/mongo/models/image' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RSpec.describe Devops::Model::Image, type: :model do | 
					
						
							|  |  |  |   let(:image) { build(:image) } | 
					
						
							| 
									
										
										
										
											2016-01-12 12:01:07 +03:00
										 |  |  |   let(:string_with_dash) { 'asd-asd' } | 
					
						
							|  |  |  |   let(:string_with_slash) { 'asd/asd' } | 
					
						
							|  |  |  |   let(:string_with_parenthesis) { 'centos 6.5 x86_64 (development instance)' } | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   before do | 
					
						
							|  |  |  |     allow(Provider::ProviderFactory).to receive(:providers).and_return(%w(openstack ec2 static')) | 
					
						
							| 
									
										
										
										
											2016-01-12 12:01:07 +03:00
										 |  |  |     allow_any_instance_of(Validators::Image::ImageInFilter).to receive(:available_images).and_return([{'id' => 'test_image'}, {'id' => string_with_dash}, {'id' => string_with_slash}]) | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'is valid with correct attrs' do | 
					
						
							|  |  |  |     expect(image).to be_valid | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:29:49 +03:00
										 |  |  |   describe 'validation', stubbed_logger: true do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     include_examples 'field type validation', :id, :not_nil, :non_empty_string, :field_validator | 
					
						
							| 
									
										
										
										
											2015-11-30 16:56:54 +03:00
										 |  |  |     include_examples 'field type validation', :remote_user, :not_nil, :non_empty_string, :field_validator | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     include_examples 'field type validation', :name, :not_nil, :non_empty_string, :field_validator | 
					
						
							|  |  |  |     include_examples 'field type validation', :bootstrap_template, :maybe_nil, :non_empty_string, :only_word_symbols, :field_validator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'id should contain only letters, digits and dashes' do | 
					
						
							| 
									
										
										
										
											2016-01-12 12:01:07 +03:00
										 |  |  |       expect(build(:image, id: string_with_dash)).to be_valid | 
					
						
							|  |  |  |       expect(build(:image, id: string_with_slash)).not_to be_valid | 
					
						
							|  |  |  |       expect(build(:image, id: string_with_parenthesis)).not_to be_valid | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "id should be included in image filters" do | 
					
						
							|  |  |  |       expect(build(:image, id: 'wrong')).not_to be_valid | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 12:01:07 +03:00
										 |  |  |     it 'name may contain everything' do | 
					
						
							|  |  |  |       expect(build(:image, name: string_with_dash)).to be_valid | 
					
						
							|  |  |  |       expect(build(:image, name: string_with_slash)).to be_valid | 
					
						
							|  |  |  |       expect(build(:image, name: string_with_parenthesis)).to be_valid | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'name length should be less or equal than 100' do | 
					
						
							|  |  |  |       expect(build(:image, name: 'a'*100)).to be_valid | 
					
						
							|  |  |  |       expect(build(:image, name: 'a'*101)).not_to be_valid | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'bootstrap_template should be included in available bootstrap templates' do | 
					
						
							|  |  |  |       expect(build(:image, bootstrap_template: 'wrong')).not_to be_valid | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-11-30 16:56:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it 'remote_user should contain only a-zA-Z0-9_-.' do | 
					
						
							|  |  |  |       expect(build(:image, remote_user: 'aA0-.')).to be_valid | 
					
						
							|  |  |  |       expect(build(:image, remote_user: 'name/')).not_to be_valid | 
					
						
							|  |  |  |       expect(build(:image, remote_user: 'name!')).not_to be_valid | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it '#to_hash_without_id returns provider, name, remote_user and bootstrap_template' do | 
					
						
							|  |  |  |     expect(image.to_hash_without_id.keys).to match_array(%w(provider name remote_user bootstrap_template)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | end |