File: suspend_spec.rb

package info (click to toggle)
ruby-fission 0.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 624 kB
  • sloc: ruby: 4,664; makefile: 10
file content (105 lines) | stat: -rw-r--r-- 3,225 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'

describe Fission::Command::Suspend do
  include_context 'command_setup'

  before do
    @target_vm = ['foo']
    Fission::VM.stub(:new).and_return(@vm_mock)

    @suspend_response_mock = double('suspend_response')

    @vm_mock.stub(:name).and_return(@target_vm.first)
  end

  describe 'command_name' do
    it 'should return the pretty command name' do
      Fission::Command::Suspend.new.command_name.should == 'suspend'
    end
  end

  describe 'execute' do
    subject { Fission::Command::Suspend }

    [ [], ['--bar'] ].each do |args|
      it_should_not_accept_arguments_of args, 'suspend'
    end

    it 'should suspend the vm' do
      @suspend_response_mock.stub_as_successful

      @vm_mock.should_receive(:suspend).and_return(@suspend_response_mock)

      command = Fission::Command::Suspend.new @target_vm
      command.execute

      @string_io.string.should match /Suspending '#{@target_vm.first}'/
      @string_io.string.should match /VM '#{@target_vm.first}' suspended/
    end

    it 'should output an error and exit if there was an error suspending the vm' do
      @suspend_response_mock.stub_as_unsuccessful

      @vm_mock.should_receive(:suspend).and_return(@suspend_response_mock)

      command = Fission::Command::Suspend.new @target_vm
      lambda { command.execute }.should raise_error SystemExit

      @string_io.string.should match /Suspending '#{@target_vm.first}'/
      @string_io.string.should match /There was an error suspending the VM.+it blew up.+/m
    end

    describe 'with --all' do
      before do
        @vm_mock_1 = double('vm_mock_1')
        @vm_mock_2 = double('vm_mock_2')

        @vm_mock_1.stub(:name).and_return('vm_1')
        @vm_mock_2.stub(:name).and_return('vm_2')

        @vm_items = {'vm_1' => @vm_mock_1,
                     'vm_2' => @vm_mock_2
        }
      end

      it 'should output an error and exit if there was an error getting the list of running vms' do
        @all_running_response_mock.stub_as_unsuccessful

        Fission::VM.should_receive(:all_running).and_return(@all_running_response_mock)

        command = Fission::Command::Suspend.new ['--all']
        lambda { command.execute }.should raise_error SystemExit

        @string_io.string.should match /There was an error getting the list of running VMs.+it blew up.+/m
      end

      it 'should suspend all running VMs' do
        @all_running_response_mock.stub_as_successful @vm_items.values
        @suspend_response_mock.stub_as_successful

        Fission::VM.should_receive(:all_running).and_return(@all_running_response_mock)

        @vm_items.each_pair do |name, mock|
          mock.should_receive(:suspend).and_return(@suspend_response_mock)
        end

        command = Fission::Command::Suspend.new ['--all']
        command.execute

        @vm_items.keys.each do |vm|
          @string_io.string.should match /Suspending '#{vm}'/
          @string_io.string.should match /VM '#{vm}' suspended/
        end
      end
    end

  end

  describe 'help' do
    it 'should output info for this command' do
      output = Fission::Command::Suspend.help

      output.should match /fission suspend \[TARGET_VM \| --all\]/
    end
  end
end