unit-hosting 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
  require 'rubygems'
4
4
  require 'mutter'
5
+ require 'unit-hosting/api'
5
6
 
6
7
  module UnitHosting
7
8
  module Groups
@@ -12,6 +13,7 @@ module UnitHosting
12
13
  end
13
14
 
14
15
  def tablize
16
+ return "no groups" if length == 0
15
17
  table = Mutter::Table.new(:delimiter => '|') do
16
18
  column :style => :green
17
19
  column
@@ -38,7 +40,7 @@ module UnitHosting
38
40
  end
39
41
 
40
42
  def update
41
- STDERR.puts "update #{instance_id}"
43
+ # STDERR.puts "update #{instance_id}"
42
44
  @vms = VmGroup.new(@instance_id,@key).vms
43
45
  self
44
46
  end
@@ -0,0 +1,50 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__),'..','lib'))
2
+ require 'rspec'
3
+ require 'webmock/rspec'
4
+ require "tempfile"
5
+
6
+ RSpec.configure do |config|
7
+ end
8
+
9
+ require 'simplecov'
10
+ require 'coveralls'
11
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
12
+ SimpleCov::Formatter::HTMLFormatter,
13
+ Coveralls::SimpleCov::Formatter
14
+ ]
15
+ SimpleCov.start
16
+
17
+ Coveralls.wear!
18
+
19
+ # http://qiita.com/gokoku_h/items/5e6903a240abc8a5cac6
20
+ require 'stringio'
21
+ def capture(stream)
22
+ begin
23
+ stream = stream.to_s
24
+ eval "$#{stream} = StringIO.new"
25
+ yield
26
+ result = eval("$#{stream}").string
27
+ ensure
28
+ eval "$#{stream} = #{stream.upcase}"
29
+ end
30
+ result
31
+ end
32
+
33
+ # https://gist.github.com/adamstegman/926858
34
+ # Redirects stderr and stdout to /dev/null.
35
+ def silence_output
36
+ @orig_stderr = $stderr
37
+ @orig_stdout = $stdout
38
+
39
+ # redirect stderr and stdout to /dev/null
40
+ $stderr = File.new('/dev/null', 'w')
41
+ $stdout = File.new('/dev/null', 'w')
42
+ end
43
+
44
+ # Replace stdout and stderr so anything else is output correctly.
45
+ def enable_output
46
+ $stderr = @orig_stderr
47
+ $stdout = @orig_stdout
48
+ @orig_stderr = nil
49
+ @orig_stdout = nil
50
+ end
@@ -0,0 +1,119 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'unit-hosting/agent'
4
+
5
+ describe UnitHosting::Agent do
6
+ ENDPOINT= "https://example.com/t/es/t"
7
+ describe "#initialize" do
8
+ it "initialize new UnitHosting::Agent class" do
9
+ expect(UnitHosting::Agent.new).to be_a UnitHosting::Agent
10
+ end
11
+ end
12
+ before {
13
+ @endpoint = "https://example.net"
14
+ @agent = UnitHosting::Agent.new
15
+ @agent.endpoint = @endpoint
16
+ }
17
+ describe "#login" do
18
+ before {
19
+ stub_request(:get, @endpoint + '/login').
20
+ to_return(:status =>200, :headers =>{:content_type =>'text/html; charset=utf-8'},
21
+ :body => <<HTML)
22
+ <form action="/login" method="post">
23
+ <input type="hidden" name="_csrf_token" value="" />
24
+ <input type="text" name="username" id="username" value="" />
25
+ <input type="password" name="password" id="password" value="" class="w100" />
26
+ <input type="submit" name="commit" value="" /></div>
27
+ </form>
28
+ HTML
29
+ }
30
+ context "success login" do
31
+ before {
32
+ stub_request(:post, @endpoint + "/login").
33
+ with(:body => {"_csrf_token"=>"", "password"=>"test-password", "username"=>"test-username"}).
34
+ to_return(:status =>200, :body => "OK", :headers => {:content_type =>'text/html; charset=utf-8'})
35
+ }
36
+ it "start API session" do
37
+ page = @agent.login("test-username","test-password")
38
+ expect(page).to be_a Mechanize::Page
39
+ expect(page.body).to eq "OK"
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "#groups" do
45
+ context "when there are three groups." do
46
+ before {
47
+ html = <<HTML
48
+ <table id="server-groups">
49
+ <tr><td><span class="instance_id"><a href="/my/group/test-sg-1">test-sg-1</a></span></td></tr>
50
+ <tr><td><span class="instance_id"><a href="/my/group/test-sg-2">test-sg-2</a></span></td></tr>
51
+ <tr><td><span class="instance_id"><a href="/my/group/test-sg-3">test-sg-3</a></span></td></tr>
52
+ </table>
53
+ HTML
54
+ stub_request(:get, @endpoint + "/my/group").
55
+ to_return(:status =>200, :body => html, :headers => {:content_type =>'text/html; charset=utf-8'})
56
+
57
+ (1..3).each { |n|
58
+ html = <<HTML
59
+ <span class="group-name">test-sg-#{n}</span>
60
+ <span class="api-key">#{n}dummyapikey1234567890</span>
61
+ HTML
62
+ stub_request(:get, @endpoint + "/my/group/test-sg-#{n}/info").
63
+ to_return(:status =>200, :body => html, :headers => {:content_type =>'text/html; charset=utf-8'})
64
+ }
65
+ }
66
+ it "returns list of groups" do
67
+ expect(@agent.groups.count).to be 3
68
+ expect(@agent.groups.first.name).to eq "test-sg-1"
69
+ end
70
+
71
+ end
72
+ end
73
+
74
+ describe "#logout" do
75
+ before {
76
+ stub_request(:get, @endpoint + "/logout").
77
+ to_return(:status =>200, :body => "", :headers => {:content_type =>'text/html; charset=utf-8'})
78
+ }
79
+ it "get /logout" do
80
+ @agent.logout
81
+ end
82
+ end
83
+ describe "#login?" do
84
+ context "when logged in" do
85
+ before {
86
+ stub_request(:get, @endpoint + "/dashboard").
87
+ to_return(:status =>200, :body => <<HTML, :headers => {:content_type =>'text/html; charset=utf-8'})
88
+ <html>
89
+ ログアウト
90
+ </html>
91
+ HTML
92
+
93
+ }
94
+ it "returns true" do
95
+ expect(@agent.login?).to be_true
96
+ end
97
+ end
98
+
99
+ context "when not logged in" do
100
+ before {
101
+ stub_request(:get, @endpoint + "/dashboard").
102
+ to_return(:status =>200, :body => <<HTML, :headers => {:content_type =>'text/html; charset=utf-8'})
103
+ <html>
104
+ ログイン
105
+ </html>
106
+ HTML
107
+ }
108
+ it "returns true" do
109
+ expect(@agent.login?).to be_false
110
+ end
111
+ end
112
+
113
+ end
114
+ end
115
+
116
+
117
+
118
+
119
+
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'unit-hosting/api/base'
4
+
5
+ describe UnitHosting::Api do
6
+ describe "#keypath" do
7
+ it "returns keypath" do
8
+ expect(UnitHosting::Api.keypath("foo")).to match /foo\.key$/
9
+ end
10
+ end
11
+ end
12
+
13
+ describe UnitHosting::Api::Base do
14
+ before {
15
+ @base = UnitHosting::Api::Base.new
16
+
17
+ @key_file = Tempfile.new('key')
18
+ @key_file.puts(<<"KEY")
19
+ <?xml version="1.0" encoding="UTF-8"?>
20
+ <server-group>
21
+ <instance_id>test-sg-1</instance_id>
22
+ <key>dummy-test-key-1</key>
23
+ </server-group>
24
+ KEY
25
+ @key_file.close
26
+ UnitHosting::Api.stub(:keypath).
27
+ with("foo").and_return(@key_file.path)
28
+ }
29
+ after {
30
+ @key_file.unlink
31
+ }
32
+ describe ".load" do
33
+ it "load a key file" do
34
+ expect(UnitHosting::Api::Base.load("foo")).to be_a UnitHosting::Api::Base
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'unit-hosting/api/vm_group'
4
+
5
+ describe UnitHosting::Api::VmGroup do
6
+ before {
7
+ @group = UnitHosting::Api::VmGroup.new
8
+ @recipe = UnitHosting::Api::VmRecipe.new
9
+ }
10
+ describe "#create_vm" do
11
+ it "calls Base::server_call('vmGroup.createVm')" do
12
+ expect(@group).to receive(:server_call).with("vmGroup.createVm",@recipe.params).
13
+ and_return("result" => "success")
14
+ @group.create_vm(@recipe)
15
+ end
16
+ end
17
+
18
+ describe "#vm_api_key" do
19
+
20
+ end
21
+
22
+ describe "#vm" do
23
+ context "when there is no such a VM" do
24
+ before {
25
+ @group.stub(:vms).and_return([
26
+ { :instance_id =>"test-vm-1"},
27
+ { :instance_id =>"test-vm-2"},
28
+ { :instance_id =>"test-vm-3"}
29
+ ])
30
+ }
31
+ it "returns Vm instance" do
32
+ expect(@group.vm("test-vm-123")).to be_a UnitHosting::Api::Vm
33
+ expect(@group.vm("test-vm-123").api_key).to eq nil
34
+ end
35
+ end
36
+ context "when there is no such a VM" do
37
+ before {
38
+ @group.stub(:vms).and_return([
39
+ { :instance_id =>"test-vm-1"},
40
+ { :instance_id =>"test-vm-2"},
41
+ { :instance_id =>"test-vm-3"}
42
+ ])
43
+ }
44
+ it "returns Vm instance" do
45
+ expect(@group.vm("test-vm-2")).to be_a UnitHosting::Api::Vm
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#networks" do
51
+ it "call vmGroup.getNetworks" do
52
+ expect(@group).to receive(:server_call).with("vmGroup.getNetworks").once
53
+ @group.networks
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,27 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'unit-hosting/api/vm_recipe'
4
+
5
+ describe UnitHosting::Api::VmRecipe do
6
+ before {
7
+ @recipe = UnitHosting::Api::VmRecipe.new
8
+ }
9
+ describe "#load_ssh_key" do
10
+ before {
11
+ @ssh_key_file = Tempfile.new('ssh_key')
12
+ @ssh_key_file.puts(<<"KEY")
13
+ dummy
14
+ ssh-key
15
+ file
16
+ KEY
17
+ @ssh_key_file.close
18
+ }
19
+ after {
20
+ @ssh_key_file.unlink
21
+ }
22
+ it "loads ssh key file " do
23
+ @recipe.load_ssh_key(@ssh_key_file.path)
24
+ expect(@recipe.ssh_key).to match /ssh-key/
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,148 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'unit-hosting/api/vm'
4
+
5
+ describe UnitHosting::Api::Vm do
6
+ before {
7
+ @vm = UnitHosting::Api::Vm.new("test-vm-1","test-1-dummy-key")
8
+ }
9
+ describe "#foo" do
10
+ it "calls Base::server_call('vm.foo')" do
11
+ expect(@vm).to receive(:server_call).with("vm.foo")
12
+ @vm.foo
13
+ end
14
+ it "calls Base::server_call('vm.foo','var' =>'val')" do
15
+ expect(@vm).to receive(:server_call).with("vm.foo",'var' =>'val')
16
+ @vm.foo "var"=>"val"
17
+ end
18
+ end
19
+
20
+ describe "#reboot" do
21
+ it "calls Base::server_call('vm.reboot')" do
22
+ expect(@vm).to receive(:server_call).with("vm.reboot")
23
+ @vm.reboot
24
+ end
25
+ end
26
+ describe "#start" do
27
+ it "calls Base::server_call('vm.start')" do
28
+ expect(@vm).to receive(:server_call).with("vm.start")
29
+ @vm.start
30
+ end
31
+ end
32
+ describe "#shutdown" do
33
+ it "calls Base::server_call('vm.shutdown')" do
34
+ expect(@vm).to receive(:server_call).with("vm.shutdown")
35
+ @vm.shutdown
36
+ end
37
+ end
38
+ describe "#power_off" do
39
+ it "calls Base::server_call('vm.powerOff')" do
40
+ expect(@vm).to receive(:server_call).with("vm.powerOff")
41
+ @vm.power_off
42
+ end
43
+ end
44
+ describe "#destroy" do
45
+ it "calls Base::server_call('vm.destroy')" do
46
+ expect(@vm).to receive(:server_call).with("vm.destroy")
47
+ @vm.destroy
48
+ end
49
+ end
50
+ describe "#status?" do
51
+ it "calls Base::server_call('vm.getStatus')" do
52
+ expect(@vm).to receive(:server_call).with("vm.getStatus")
53
+ @vm.status?
54
+ end
55
+ end
56
+
57
+ describe "#memory_unit_size" do
58
+ it "calls Base::server_call('vm.setMemoryUnitSize')" do
59
+ expect(@vm).to receive(:server_call).with("vm.setMemoryUnitSize","size" =>123)
60
+ @vm.memory_unit_size(123)
61
+ end
62
+ end
63
+ describe "#cup_unit_num" do
64
+ it "calls Base::server_call('vm.setCpuUnitNum')" do
65
+ expect(@vm).to receive(:server_call).with("vm.setCpuUnitNum","num" =>456)
66
+ @vm.cpu_unit_num(456)
67
+ end
68
+ end
69
+
70
+ describe "#memory_unit_size?" do
71
+ it "calls Base::server_call('vm.getMemoryUnitSize')" do
72
+ expect(@vm).to receive(:server_call).with("vm.getMemoryUnitSize")
73
+ @vm.memory_unit_size?
74
+ end
75
+ end
76
+ describe "#cup_unit_num?" do
77
+ it "calls Base::server_call('vm.setCpuUnitNum')" do
78
+ expect(@vm).to receive(:server_call).with("vm.getCpuUnitNum")
79
+ @vm.cpu_unit_num?
80
+ end
81
+ end
82
+ describe "#ips" do
83
+ it "calls Base::server_call('vm.getIpInfo')" do
84
+ expect(@vm).to receive(:server_call).with("vm.getIpInfo")
85
+ @vm.ips
86
+ end
87
+ end
88
+ describe "#display_name" do
89
+ it "calls Base::server_call('vm.getDisplayName')" do
90
+ expect(@vm).to receive(:server_call).with("vm.getDisplayName")
91
+ @vm.display_name
92
+ end
93
+ end
94
+ describe "#display_name=" do
95
+ it "calls Base::server_call('vm.setDisplayName')" do
96
+ expect(@vm).to receive(:server_call).with("vm.setDisplayName","display_name" =>"test-server")
97
+ @vm.display_name= "test-server"
98
+ end
99
+ end
100
+ describe "#replicate" do
101
+ it "calls Base::server_call('vm.replicate')" do
102
+ expect(@vm).to receive(:server_call).with("vm.replicate","display_name" =>"test-server")
103
+ @vm.replicate "test-server"
104
+ end
105
+ end
106
+ describe "#plugVif" do
107
+ it "calls Base::server_call('vm.plugVif')" do
108
+ expect(@vm).to receive(:server_call).with("vm.plugVif","network_uuid" =>"test-network-uuid","device" =>2)
109
+ @vm.plugVif("test-network-uuid",2)
110
+ end
111
+ end
112
+ describe "#vifs" do
113
+ it "calls Base::server_call('vm.getVifs')" do
114
+ expect(@vm).to receive(:server_call).with("vm.getVifs")
115
+ @vm.vifs
116
+ end
117
+ end
118
+ describe "#unplugVif" do
119
+ it "calls Base::server_call('vm.unplugVif')" do
120
+ expect(@vm).to receive(:server_call).with("vm.unplugVif","vif_uuid" =>"test-vif-uuid")
121
+ @vm.unplugVif "test-vif-uuid"
122
+ end
123
+ end
124
+ describe "#set_vm_data" do
125
+ it "calls Base::server_call('vm.setVmData')" do
126
+ expect(@vm).to receive(:server_call).with("vm.setVmData","key" =>"test-key-1","value" =>"test-val-1")
127
+ @vm.set_vm_data "test-key-1","test-val-1"
128
+ end
129
+ end
130
+ describe "#get_vm_data" do
131
+ it "calls Base::server_call('vm.getVmData')" do
132
+ expect(@vm).to receive(:server_call).with("vm.getVmData","key" =>"test-key-1")
133
+ @vm.get_vm_data "test-key-1"
134
+ end
135
+ end
136
+ describe "#set_pv_args" do
137
+ it "calls Base::server_call('vm.setPvArgs')" do
138
+ expect(@vm).to receive(:server_call).with("vm.setPvArgs","pv_args" =>"test-args-1")
139
+ @vm.set_pv_args "test-args-1"
140
+ end
141
+ end
142
+ describe "#get_pv_args" do
143
+ it "calls Base::server_call('vm.getPvArgs')" do
144
+ expect(@vm).to receive(:server_call).with("vm.setPvArgs","pv_args" =>"test-args-1")
145
+ @vm.set_pv_args "test-args-1"
146
+ end
147
+ end
148
+ end