<commit_before># Copyright (c) 2009-2013 VMware, Inc.
# Copyright (c) 2012 Piston Cloud Computing, Inc.

require 'spec_helper'

describe Bosh::OpenStackCloud::Cloud do
  describe :new do
    let(:cloud_options) { mock_cloud_options }
    let(:openstack_parms) {
      {
        :provider => 'OpenStack',
        :openstack_auth_url => 'http://127.0.0.1:5000/v2.0/tokens',
        :openstack_username => 'admin',
        :openstack_api_key => 'nova',
        :openstack_tenant => 'admin',
        :openstack_region => 'RegionOne',
        :openstack_endpoint_type => nil,
        :connection_options => connection_options,
      }
    }
    let(:volume_parms) {
      {
        :provider => 'OpenStack',
        :openstack_auth_url => 'http://127.0.0.1:5000/v2.0/tokens',
        :openstack_username => 'admin',
        :openstack_api_key => 'nova',
        :openstack_tenant => 'admin',
        :openstack_endpoint_type => nil,
        :connection_options => connection_options,
      }
    }
    let(:connection_options) { nil }
    let(:compute) { double('Fog::Compute') }
    let(:image) { double('Fog::Image') }
    let(:volume) { double('Fog::Volume') }

    it 'should create a Fog connection' do
      Fog::Compute.stub(:new).with(openstack_parms).and_return(compute)
      Fog::Image.should_receive(:new).with(openstack_parms).and_return(image)
      Fog::Volume.stub(:new).with(volume_parms).and_return(volume)
      cloud = Bosh::Clouds::Provider.create(cloud_options, 'fake-director-uuid')

      expect(cloud.openstack).to eql(compute)
      expect(cloud.glance).to eql(image)
      expect(cloud.volume).to eql(volume)
    end

    context 'with connection options' do
      let(:connection_options) {
        JSON.generate({
          'ssl_verify_peer' => false,
        })
      }

      it 'should add optional options to the Fog connection' do
        cloud_options['properties']['openstack']['connection_options'] = connection_options
        Fog::Compute.stub(:new).with(openstack_parms).and_return(compute)
        Fog::Image.should_receive(:new).with(openstack_parms).and_return(image)
        Fog::Volume.stub(:new).with(volume_parms).and_return(volume)
        cloud = Bosh::Clouds::Provider.create(cloud_options, 'fake-director-uuid')

        expect(cloud.openstack).to eql(compute)
        expect(cloud.glance).to eql(image)
        expect(cloud.volume).to eql(volume)
      end
    end
  end

  describe 'creating via provider' do
    it 'can be created using Bosh::Cloud::Provider' do
      Fog::Compute.stub(:new)
      Fog::Image.stub(:new)
      Fog::Volume.stub(:new)
      cloud = Bosh::Clouds::Provider.create(mock_cloud_options, 'fake-director-uuid')
      cloud.should be_an_instance_of(Bosh::OpenStackCloud::Cloud)
    end

    it 'raises ArgumentError on initializing with blank options' do
    	options = Hash.new('options')
    	expect {
    		Bosh::OpenStackCloud::Cloud.new(options)
    	}.to raise_error(ArgumentError, /Invalid OpenStack configuration/)
    end

    it 'raises ArgumentError on initializing with non Hash options' do
    	options = 'this is a string'
    	expect {
    		Bosh::OpenStackCloud::Cloud.new(options)
    	}.to raise_error(ArgumentError, /Invalid OpenStack configuration/)
    end

    it 'raises a CloudError exception if cannot connect to the OpenStack Compute API' do<commit_msg>Formatting.<commit_after>require 'spec_helper'

describe Bosh::OpenStackCloud::Cloud do
  describe :new do
    let(:cloud_options) { mock_cloud_options }
    let(:openstack_parms) {
      {
        :provider => 'OpenStack',
        :openstack_auth_url => 'http://127.0.0.1:5000/v2.0/tokens',
        :openstack_username => 'admin',
        :openstack_api_key => 'nova',
        :openstack_tenant => 'admin',
        :openstack_region => 'RegionOne',
        :openstack_endpoint_type => nil,
        :connection_options => connection_options,
      }
    }
    let(:volume_parms) {
      {
        :provider => 'OpenStack',
        :openstack_auth_url => 'http://127.0.0.1:5000/v2.0/tokens',
        :openstack_username => 'admin',
        :openstack_api_key => 'nova',
        :openstack_tenant => 'admin',
        :openstack_endpoint_type => nil,
        :connection_options => connection_options,
      }
    }
    let(:connection_options) { nil }
    let(:compute) { double('Fog::Compute') }
    let(:image) { double('Fog::Image') }
    let(:volume) { double('Fog::Volume') }

    it 'should create a Fog connection' do
      Fog::Compute.stub(:new).with(openstack_parms).and_return(compute)
      Fog::Image.should_receive(:new).with(openstack_parms).and_return(image)
      Fog::Volume.stub(:new).with(volume_parms).and_return(volume)
      cloud = Bosh::Clouds::Provider.create(cloud_options, 'fake-director-uuid')

      expect(cloud.openstack).to eql(compute)
      expect(cloud.glance).to eql(image)
      expect(cloud.volume).to eql(volume)
    end

    context 'with connection options' do
      let(:connection_options) {
        JSON.generate({
          'ssl_verify_peer' => false,
        })
      }

      it 'should add optional options to the Fog connection' do
        cloud_options['properties']['openstack']['connection_options'] = connection_options
        Fog::Compute.stub(:new).with(openstack_parms).and_return(compute)
        Fog::Image.should_receive(:new).with(openstack_parms).and_return(image)
        Fog::Volume.stub(:new).with(volume_parms).and_return(volume)
        cloud = Bosh::Clouds::Provider.create(cloud_options, 'fake-director-uuid')

        expect(cloud.openstack).to eql(compute)
        expect(cloud.glance).to eql(image)
        expect(cloud.volume).to eql(volume)
      end
    end
  end

  describe 'creating via provider' do
    it 'can be created using Bosh::Cloud::Provider' do
      Fog::Compute.stub(:new)
      Fog::Image.stub(:new)
      Fog::Volume.stub(:new)
      cloud = Bosh::Clouds::Provider.create(mock_cloud_options, 'fake-director-uuid')
      cloud.should be_an_instance_of(Bosh::OpenStackCloud::Cloud)
    end

    it 'raises ArgumentError on initializing with blank options' do
      options = Hash.new('options')
      expect {
        Bosh::OpenStackCloud::Cloud.new(options)
      }.to raise_error(ArgumentError, /Invalid OpenStack configuration/)
    end

    it 'raises ArgumentError on initializing with non Hash options' do
      options = 'this is a string'
      expect {
        Bosh::OpenStackCloud::Cloud.new(options)
      }.to raise_error(ArgumentError, /Invalid OpenStack configuration/)
    end

    it 'raises a CloudError exception if cannot connect to the OpenStack Compute API' do
{'fraction_non_alphanumeric': 0.13107822410147993, 'fraction_numerical': 0.009060706735125339, 'mean_word_length': 3.2509627727856225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6634969', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2072, 'n_words': 380}