require_relative '../spec_helper'
require_relative '../../libraries/helper'

describe CloudConductor::Helper do
  describe '#generate_password' do
    before do
      @helper = Object.new
      @helper.extend CloudConductor::Helper
    end

    it 'return generate_password string that length is 64 characters' do
      node = { 'cloudconductor' => { 'salt' => 'db310da43d290e5dd54e5e2c5926385c' } }
      allow(@helper).to receive(:node).and_return(node)
      expect(@helper.generate_password).to match(/[0-9a-f]{64}/)
    end

    it 'return random string that generated by salt' do
      node = { 'cloudconductor' => { 'salt' => 'db310da43d290e5dd54e5e2c5926385c' } }
      allow(@helper).to receive(:node).and_return(node)
      expect(@helper.generate_password).to eq('4d6240ace778fbf59ef9784869d54b40282a323adb7b762fb4e7ee7e1b93c0df')
    end

    it 'return same random string if passed same keys' do
      node = { 'cloudconductor' => { 'salt' => 'db310da43d290e5dd54e5e2c5926385c' } }
      allow(@helper).to receive(:node).and_return(node)
      random1 = @helper.generate_password('key')
      random2 = @helper.generate_password('key')

      expect(random1).to eq(random2)
    end

    it 'return different random string if passed different keys' do
      node = { 'cloudconductor' => { 'salt' => 'db310da43d290e5dd54e5e2c5926385c' } }
      allow(@helper).to receive(:node).and_return(node)
      random1 = @helper.generate_password('key1')
      random2 = @helper.generate_password('key2')

      expect(random1).not_to eq(random2)
    end
  end
  describe '#pick_servers_as_role' do
    before do
      @helper = Object.new
      @helper.extend CloudConductor::Helper
      node = {
        'cloudconductor' => {
          'servers' => {
            'web' => { 'roles' => 'web', 'private_ip' => '127.0.0.1' },
            'ap1' => { 'roles' => 'ap', 'private_ip' => '127.0.0.2' },
            'ap2' => { 'roles' => 'ap', 'private_ip' => '127.0.0.3' },
            'db1' => { 'roles' => 'db', 'private_ip' => '127.0.0.4' },
            'db2' => { 'roles' => 'db', 'private_ip' => '127.0.0.5' }
          }
        }
      }
      allow(@helper).to receive(:node).and_return(node)
    end
    it 'return hash of argument roll' do
      expect(@helper.pick_servers_as_role('web')).to eq(
        ['roles' => 'web', 'private_ip' => '127.0.0.1', 'hostname' => 'web']
      )
      expect(@helper.pick_servers_as_role('ap')).to eq(
        [{ 'roles' => 'ap', 'private_ip' => '127.0.0.2', 'hostname' => 'ap1' },
         { 'roles' => 'ap', 'private_ip' => '127.0.0.3', 'hostname' => 'ap2' }]
      )
      expect(@helper.pick_servers_as_role('db')).to eq(
        [{ 'roles' => 'db', 'private_ip' => '127.0.0.4', 'hostname' => 'db1' },
         { 'roles' => 'db', 'private_ip' => '127.0.0.5', 'hostname' => 'db2' }]
      )
    end
  end
  describe '#ap_servers' do
    before do
      @helper = Object.new
      @helper.extend CloudConductor::Helper
      node = {
        'cloudconductor' => {
          'servers' => {
            'web' => { 'roles' => 'web', 'private_ip' => '127.0.0.1' },
            'ap1' => { 'roles' => 'ap', 'private_ip' => '127.0.0.2' },
            'ap2' => { 'roles' => 'ap', 'private_ip' => '127.0.0.3' },
            'db' => { 'roles' => 'db', 'private_ip' => '127.0.0.4' }
          }
        }
      }
      allow(@helper).to receive(:node).and_return(node)
    end
    it 'return hash of ap roll only' do
      expect(@helper.ap_servers).to eq(
        [{ 'roles' => 'ap', 'private_ip' => '127.0.0.2', 'hostname' => 'ap1' },
         { 'roles' => 'ap', 'private_ip' => '127.0.0.3', 'hostname' => 'ap2' }]
      )
    end
  end
  describe '#db_servers' do
    before do
      @helper = Object.new
      @helper.extend CloudConductor::Helper
      node = {
        'cloudconductor' => {
          'servers' => {
            'ap' => { 'roles' => 'ap', 'private_ip' => '127.0.0.2' },
            'db1' => { 'roles' => 'db', 'private_ip' => '127.0.0.4' },
            'db2' => { 'roles' => 'db', 'private_ip' => '127.0.0.5' }
          }
        }
      }
      allow(@helper).to receive(:node).and_return(node)
    end
    it 'return hash of db roll only' do
      expect(@helper.db_servers).to eq(
        [{ 'roles' => 'db', 'private_ip' => '127.0.0.4', 'hostname' => 'db1' },
         { 'roles' => 'db', 'private_ip' => '127.0.0.5', 'hostname' => 'db2' }]
      )
    end
  end
  describe '#first_ap_server' do
    before do
      @helper = Object.new
      @helper.extend CloudConductor::Helper
      node = {
        'cloudconductor' => {
          'servers' => {
            'ap1' => { 'roles' => 'ap', 'private_ip' => '127.0.0.2' },
            'ap2' => { 'roles' => 'ap', 'private_ip' => '127.0.0.3' }
          }
        }
      }
      allow(@helper).to receive(:node).and_return(node)
    end
    it 'return hash of ap roll of first' do
      expect(@helper.first_ap_server).to eq(
        'roles' => 'ap', 'private_ip' => '127.0.0.2', 'hostname' => 'ap1'
      )
    end
  end
  describe '#first_db_server' do
    before do
      @helper = Object.new
      @helper.extend CloudConductor::Helper
      node = {
        'cloudconductor' => {
          'servers' => {
            'db1' => { 'roles' => 'db', 'private_ip' => '127.0.0.2' },
            'db2' => { 'roles' => 'db', 'private_ip' => '127.0.0.3' }
          }
        }
      }
      allow(@helper).to receive(:node).and_return(node)
    end
    it 'return hash of db roll of first' do
      expect(@helper.first_db_server).to eq(
        'roles' => 'db', 'private_ip' => '127.0.0.2', 'hostname' => 'db1'
      )
    end
  end
end
