Class: CloudManager::Configuration::Service::TestMachine

Inherits:
Machine
  • Object
show all
Defined in:
lib/cloud_manager/configuration/service/test.rb

Overview

This class represents a machine along with its configured tests.

Since:

  • 0.1.0

Instance Attribute Summary

Attributes inherited from Machine

#alias, #config, #parent_service

Instance Method Summary collapse

Constructor Details

#initialize(machine_alias, config, parent_service) ⇒ TestMachine

Returns a new instance of TestMachine.

Initializes a configured test machine.

Parameters:

  • machine_alias (String)

    the machine alias

  • config (Hash)

    the machine configuration

  • parent_service (Test)

    the initiator service

Since:

  • 0.1.0



60
61
62
63
64
65
# File 'lib/cloud_manager/configuration/service/test.rb', line 60

def initialize(machine_alias, config, parent_service)
  super(machine_alias, config, parent_service)
  @dns_names = nil

  raise "services.test.#{@alias} configuration is missing" unless @config.is_a?(Array)
end

Instance Method Details

#dns_namesArray<String>

Returns the DNS name list for the machine.

Returns:

  • (Array<String>)

    DNS names of the given machine

Since:

  • 0.1.0



89
90
91
92
# File 'lib/cloud_manager/configuration/service/test.rb', line 89

def dns_names
  get_dns_names! if @dns_names.nil?
  @dns_names
end

#get_dns_names!Array<String> (private)

Collects the DNS name list for the machine.

Returns:

  • (Array<String>)

    DNS names of the given machine

Since:

  • 0.1.0



99
100
101
# File 'lib/cloud_manager/configuration/service/test.rb', line 99

def get_dns_names!
  @dns_names = @parent_service.base_configuration.dns_names_of(@alias)
end

#testvoid

This method returns an undefined value.

Run the configured tests for the machine.

Raises:

  • (Exception)

    on invalid configuration

Since:

  • 0.1.0



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cloud_manager/configuration/service/test.rb', line 71

def test
  puts "    - testing machine #{@alias}"

  @config.each do |test_config|
    raise 'test type must be specified' unless test_config.has_key?('type')

    case test_config['type']
      when 'http'
        test_http(test_config)
      else
        raise "Unhandled test type: #{test_config['type']}"
    end
  end
end

#test_http(test_config) ⇒ void (private)

This method returns an undefined value.

Runs a http test according to the given configuration.

Parameters:

  • test_config (Hash)

    the test configuration

Since:

  • 0.1.0



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
# File 'lib/cloud_manager/configuration/service/test.rb', line 107

def test_http(test_config)
  puts '      - running http test...'

  defaults = Hash.new
  defaults['protocol_prefix'] = 'http://' # used for dns_names prefixing
  defaults['code'] = 200
  defaults['url'] = nil
  defaults['body_regex'] = '//'
  config = defaults.merge(test_config)

  # puts config.to_s
  urls = []

  if config['url'].nil?
    dns_names.each { |dns_name|
      urls.push(config['protocol_prefix'] + dns_name)
    }
  elsif config['url'].start_with?('/')
    dns_names.each { |dns_name|
      urls.push(config['protocol_prefix'] + dns_name + config['url'])
    }
  else
    urls.push(config['url'])
  end

  if urls.empty?
    puts '        > no urls to test'
  else
    urls.each { |url|
      print "        #{url} ... "

      RestClient.get(url) { |response, _request, _result|
        ok = (response.code == config['code'] and response.body =~ Regexp.new(config['body_regex'], Regexp::MULTILINE))
        puts ok ? 'OK' : 'failed'
      }
    }
  end
end