Class: CloudstackSpec::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudstack_spec/setup.rb

Class Method Summary collapse

Class Method Details

.create_initialObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cloudstack_spec/setup.rb', line 11

def self.create_initial
  print "Create base for cloudstack_spec? y/n: "
  auto_config = $stdin.gets.chomp
  if auto_config =~ (/(true|t|yes|y|1)$/i)
    [ 'spec', "spec/lib" ].each { |dir| safe_mkdir(dir) }
    safe_create_config
    safe_create_spec
    safe_create_spec_helper
#        safe_create_rakefile
    puts "Make sure to Update file: spec/config.yml"
  else
    exit
  end
end

.runObject



6
7
8
9
# File 'lib/cloudstack_spec/setup.rb', line 6

def self.run

  create_initial
end

.safe_create_configObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cloudstack_spec/setup.rb', line 26

def self.safe_create_config
  # create config.yml file.
  content = <<-EOF
cloudstack:
  url:        http://127.0.0.1:8080/client/api
  api_key:    c0dD7KjmFF4ixekZyQZJa7xPrW6y5-egZU1FGqq2u0DITs1qSluyP3giLDJ2msg0y1gPeARq4ZyvK-j48QP8hQ
  secret_key: GYfIZjsjSIHKO_227M6tvLns86QJEzqJVzc8xobqfgg03YeDUYk2QTGBgB6IeaHfSROW61uPm-koO2iDDvq5HQ
  use_ssl:    false

EOF

  if File.exists? "spec/config.yml"
    old_content = File.read("spec/config.yml")
    if old_content != content
      $stderr.puts "!! spec/config.yml already exists and differs from template"
    end
  else
    File.open("spec/config.yml", 'w') do |f|
      f.puts content
    end
    puts " + spec/config.yml"
  end
end

.safe_create_rakefileObject



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
145
146
147
148
149
150
151
# File 'lib/cloudstack_spec/setup.rb', line 112

def self.safe_create_rakefile
  content = <<-EOF
require 'rake'
require 'rspec/core/rake_task'

task :spec    => 'spec:all'
task :default => :spec

namespace :spec do
  targets = []
  Dir.glob('./spec/*').each do |dir|
next unless File.directory?(dir)
targets << File.basename(dir)
  end

  task :all     => targets
  task :default => :all

  targets.each do |target|
desc "Run serverspec tests to #{target}"
RSpec::Core::RakeTask.new(target.to_sym) do |t|
  ENV['TARGET_HOST'] = target
  t.pattern = "spec/#{target}/*_spec.rb"
end
  end
end

EOF
  if File.exists? 'Rakefile'
    old_content = File.read('Rakefile')
    if old_content != content
      $stderr.puts "!! Rakefile already exists and differs from template"
    end
  else
    File.open('Rakefile', 'w') do |f|
      f.puts content
    end
    puts ' + Rakefile'
  end
end

.safe_create_specObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cloudstack_spec/setup.rb', line 50

def self.safe_create_spec
  content = <<-EOF
require 'spec_helper'

describe zone do
  it { should exist }
  it { should be_allocated }
#  its(:local_storage) { should_not be_set }
#  its(:security_group) { should_not be_set }
#  its(:network_type) { should match("Advanced") }
end

%w(consoleproxy secondarystoragevm).each do |svm|
  describe system_vm(svm) do
it { should exist }
it { should be_running }
it { should be_reachable }
  end
end

EOF

  if File.exists? "spec/lib/001_zone_spec.rb"
    old_content = File.read("spec/lib/001_zone_spec.rb")
    if old_content != content
      $stderr.puts "!! spec/lib/zone_spec.rb already exists and differs from template"
    end
  else
    File.open("spec/lib/001_zone_spec.rb", 'w') do |f|
      f.puts content
    end
    puts " + spec/lib/001_zone_spec.rb"
  end
end

.safe_create_spec_helperObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cloudstack_spec/setup.rb', line 96

def self.safe_create_spec_helper
  requirements = []
  content = ERB.new(spec_helper_template, nil, '-').result(binding)
  if File.exists? 'spec/spec_helper.rb'
    old_content = File.read('spec/spec_helper.rb')
    if old_content != content
      $stderr.puts "!! spec/spec_helper.rb already exists and differs from template"
    end
  else
    File.open('spec/spec_helper.rb', 'w') do |f|
      f.puts content
    end
    puts ' + spec/spec_helper.rb'
  end
end

.safe_mkdir(dir) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/cloudstack_spec/setup.rb', line 85

def self.safe_mkdir(dir)
  if File.exists? dir
    unless File.directory? dir
      $stderr.puts "!! #{dir} already exists and is not a directory"
    end
  else
    FileUtils.mkdir dir
    puts " + #{dir}/"
  end
end

.spec_helper_templateObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cloudstack_spec/setup.rb', line 153

def self.spec_helper_template
  template = <<-EOF
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'yaml'
require 'cloudstack_ruby_client'
require 'uuid'
require 'cloudstack_spec'
require 'rspec/expectations'

require 'serverspec'
#require 'specinfra'
set :backend, :exec



RSpec.configure do |config|
  config.run_all_when_everything_filtered = true
#  config.filter_run :focus

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'defined'
  config.formatter = 'documentation'
  config.color = true
end

EOF
  template
end