public
Description: Airtruk Enterprise Configuration Manager
Homepage:
Clone URL: git://github.com/penguincoder/airtruk.git
airtruk / airtruk_spec.rb
100644 135 lines (115 sloc) 4.539 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
ENV['RACK_ENV'] = 'test' # for some reason, set does not work...
 
require 'app'
require 'spec'
require 'spec/interop/test'
require 'rack/test'
 
def app
  Sinatra::Application
end
 
describe 'Airtruk Network Configurator' do
  include Rack::Test::Methods
  
  before(:all) do
    system("rake db:migrate > /dev/null 2>&1")
  end
  
  before(:each) do
    Machine.delete_all
    ShellScript.delete_all
  end
  
  after(:all) do
    File.unlink(File.join(File.dirname(__FILE__), 'db', 'test.sqlite3'))
  end
  
  it 'shows launch page' do
    get '/'
    last_response.should be_ok
  end
  
  it 'can create only unique machines' do
    hname = 'somerandomhostname'
    mcount = Machine.count
    Machine.create :hostname => hname
    Machine.count.should == mcount + 1
    Machine.create :hostname => hname
    Machine.count.should == mcount + 1
  end
  
  it 'should have contents in a shell script' do
    sscount = ShellScript.count
    ShellScript.create :filename => '/etc/resolv.conf', :contents => 'blarg!', :configuration_name => 'sample config 1'
    ShellScript.count.should == sscount + 1
    ShellScript.create :filename => '/etc/resolv.conf', :contents => nil
    ShellScript.count.should == sscount + 1
  end
  
  it 'should have has_and_belongs_to_many associations' do
    m = Machine.create :hostname => 'maia'
    ss = ShellScript.create :filename => '/etc/passwd', :contents => 'someuser:x:/home/someuser::::', :configuration_name => 'system accounts'
    m.shell_scripts << ss
    m.shell_scripts.size.should == 1
    ss.machines.size.should == 1
  end
  
  it 'should have executable shell scripts' do
    ss = ShellScript.create :filename => nil, :contents => "#!/bin/bash\necho hello world!", :configuration_name => 'hello world'
    ShellScript.executable.size.should == 1
  end
  
  it 'should have replaceable shell scripts' do
    ss = ShellScript.create :filename => '/etc/resolv.conf', :contents => "nameserver 192.168.1.1\ndomain olympus\nsearch olympus", :configuration_name => 'dns configuration'
    ShellScript.replaceable.size.should == 1
  end
  
  it 'can create a new machine' do
    get '/machines/new'
    last_response.should be_ok
    mcount = Machine.count
    post '/machines', { :machine => { :hostname => 'foobarbaz' } }
    Machine.count.should == mcount + 1
    last_response['Location'].should == '/'
  end
  
  it 'can update and destroy an existing machine' do
    m = Machine.create :hostname => 'foobarbaz'
    murl = "/machines/#{m.id}"
    get murl
    last_response.should be_ok
    put murl, { :machine => { :hostname => 'newhostname' } }
    m.reload
    m.hostname.should == 'newhostname'
    last_response['Location'].should == '/'
    delete murl
    Machine.find_by_id(m.id).should == nil
    last_response['Location'].should == '/'
  end
  
  it 'can create a new shell script' do
    get '/shell_scripts/new'
    last_response.should be_ok
    sscount = ShellScript.count
    post '/shell_scripts', { :shell_script => { :filename => 'foobarbaz', :contents => 'filecontents', :configuration_name => 'sample config' } }
    ShellScript.count.should == sscount + 1
    last_response['Location'].should == '/'
  end
  
  it 'can update and destroy an existing shell script' do
    s = ShellScript.create :filename => 'foobarbaz', :configuration_name => 'sampleconfig', :contents => 'filecontent'
    surl = "/shell_scripts/#{s.id}"
    get surl
    last_response.should be_ok
    put surl, { :shell_script => { :filename => 'newfilename' } }
    s.reload
    s.filename.should == 'newfilename'
    last_response['Location'].should == '/'
    delete surl
    ShellScript.find_by_id(s.id).should == nil
    last_response['Location'].should == '/'
  end
  
  it 'can request a configuration script for a machine' do
    m = Machine.create :hostname => 'localhost'
    s = ShellScript.create :contents => 'echo hello world!', :configuration_name => 'hello world'
    m.shell_scripts << s
    get "/autoscript/#{m.hostname}"
    last_response.should be_ok
    last_response.body.should == "#!/bin/bash\necho hello world!"
  end
  
  it 'can add and remove a shell script from a machine' do
    m = Machine.create :hostname => 'localhost'
    s = ShellScript.create :contents => 'echo hello world!', :configuration_name => 'hello world'
    get "/add_shell_script/#{m.id}/#{s.id}"
    m.shell_scripts.reload
    m.shell_scripts.include?(s).should == true
    get "/remove_shell_script/#{m.id}/#{s.id}"
    m.shell_scripts.reload
    m.shell_scripts.include?(s).should == false
  end
end