File: vm_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 (828 lines) | stat: -rw-r--r-- 30,303 bytes parent folder | download
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
require 'spec_helper'

describe Fission::VM do
  before do
    @vm = Fission::VM.new('foo')
    @conf_file_path = File.join(@vm.path, 'foo.vmx')
    @vmrun_cmd = Fission.config['vmrun_cmd']
    @conf_file_response_mock = double('conf_file_response')
  end

  describe 'new' do
    it 'should set the vm name' do
      Fission::VM.new('foo').name.should == 'foo'
    end
  end

  describe 'delete' do
    before do
      @vm_deleter           = double('vm deleter')
      @delete_response_mock = double('vm delete response')
      Fission::Action::VM::Deleter.stub(:new).and_return(@vm_deleter)
      @vm_deleter.should_receive(:delete).
                  and_return(@delete_response_mock)
    end

    it 'should return an unsuccessful response when unable to delete the vm' do
      @delete_response_mock.stub_as_unsuccessful
      @vm.delete.should be_an_unsuccessful_response
    end

    it 'should return a successful response when deleting' do
      @delete_response_mock.stub_as_successful
      @vm.delete.should be_a_successful_response
    end
  end

  describe 'start' do
    before do
      @vm_starter          = double('vm starter')
      @start_response_mock = double('vm start response')
      Fission::Action::VM::Starter.stub(:new).and_return(@vm_starter)
    end

    it 'should return an unsuccessful response when unable to start the vm' do
      @vm_starter.should_receive(:start).
                  and_return(@start_response_mock)
      @start_response_mock.stub_as_unsuccessful
      @vm.start.should be_an_unsuccessful_response
    end

    it 'should return a successful response when starting headless' do
      @vm_starter.should_receive(:start).
                  with({:headless => true}).
                  and_return(@start_response_mock)
      @start_response_mock.stub_as_successful
      @vm.start(:headless => true).should be_a_successful_response
    end

    it 'should return a successful response when starting' do
      @vm_starter.should_receive(:start).
                  and_return(@start_response_mock)
      @start_response_mock.stub_as_successful
      @vm.start.should be_a_successful_response
    end
  end

  describe 'stop' do
    before do
      @vm_stopper         = double('vm stopper')
      @stop_response_mock = double('vm stop response')
      Fission::Action::VM::Stopper.stub(:new).and_return(@vm_stopper)
    end

    it 'should return a successful response when stopping' do
      @vm_stopper.should_receive(:stop).
                  and_return(@stop_response_mock)
      @stop_response_mock.stub_as_successful
      @vm.stop.should be_a_successful_response
    end

    it 'should return an unsuccessful response when unable to stop the vm' do
      @vm_stopper.should_receive(:stop).
                  and_return(@stop_response_mock)
      @stop_response_mock.stub_as_unsuccessful
      @vm.stop.should be_an_unsuccessful_response
    end

    it 'should return a successful response when stopping hard' do
      @vm_stopper.should_receive(:stop).
                  with({:hard => true}).
                  and_return(@stop_response_mock)
      @stop_response_mock.stub_as_successful
      @vm.stop(:hard => true).should be_a_successful_response
    end
  end

  describe 'suspend' do
    before do
      @vm_suspender          = double('vm suspender')
      @suspend_response_mock = double('vm suspend response')
      Fission::Action::VM::Suspender.stub(:new).and_return(@vm_suspender)
      @vm_suspender.should_receive(:suspend).
                    and_return(@suspend_response_mock)
    end

    it 'should return a successful response when suspending' do
      @suspend_response_mock.stub_as_successful
      @vm.suspend.should be_a_successful_response
    end

    it 'should return an unsuccessful response when unable to suspend the vm' do
      @suspend_response_mock.stub_as_unsuccessful
      @vm.suspend.should be_an_unsuccessful_response
    end
  end

  describe 'snapshots' do
    before do
      @snapshot_lister_mock = double('snapshot lister')
      @snapshots_response_mock = double('snapshots mock')
      Fission::Action::Snapshot::Lister.stub(:new).and_return(@snapshot_lister_mock)
      @snapshot_lister_mock.should_receive(:snapshots).
                            and_return(@snapshots_response_mock)
    end

    it "should return an unsuccessful repsonse when unable to list the snapshots" do
      @snapshots_response_mock.stub_as_unsuccessful
      @vm.snapshots.should be_an_unsuccessful_response
    end

    it 'should return a successful response with the list of snapshots' do
      @snapshots_response_mock.stub_as_successful ['snap_1', 'snap_2']
      response = @vm.snapshots
      response.should be_a_successful_response
      response.data.should == ['snap_1', 'snap_2']
    end
  end

  describe 'create_snapshot' do
    before do
      @snapshot_creator_mock         = double('snapshot creator')
      @snapshot_create_response_mock = double('snapshot create response')
      Fission::Action::Snapshot::Creator.stub(:new).and_return(@snapshot_creator_mock)
      @snapshot_creator_mock.should_receive(:create_snapshot).
                             with('snap_1').
                             and_return(@snapshot_create_response_mock)
    end

    it 'should return an unsuccessful response when unable to create the snapshot' do
      @snapshot_create_response_mock.stub_as_unsuccessful
      @vm.create_snapshot('snap_1').should be_an_unsuccessful_response
    end

    it 'should return a successful response when creating the snapshot' do
      @snapshot_create_response_mock.stub_as_successful
      @vm.create_snapshot('snap_1').should be_a_successful_response
    end
  end

  describe 'delete_snapshot' do
    before do
      @snapshot_deleter_mock         = double('snapshot deleter')
      @snapshot_delete_response_mock = double('snapshot delete response')

      Fission::Action::Snapshot::Deleter.stub(:new).
                                       and_return(@snapshot_deleter_mock)
      @snapshot_deleter_mock.should_receive(:delete_snapshot).
                             with('snap_1').
                             and_return(@snapshot_delete_response_mock)
    end

    it 'should return an unsuccessful response when unable to delete the snapshot' do
      @snapshot_delete_response_mock.stub_as_unsuccessful
      @vm.delete_snapshot('snap_1').should be_an_unsuccessful_response
    end

    it 'should return a successful response when deleteing the snapshot' do
      @snapshot_delete_response_mock.stub_as_successful
      @vm.delete_snapshot('snap_1').should be_a_successful_response
    end
  end

  describe 'revert_to_snapshot' do
    before do
      @snapshot_reverter_mock       = double('snapshot reverter')
      @snapshot_revert_response_mock = 'snapshot revert response'

      Fission::Action::Snapshot::Reverter.stub(:new).
                                        and_return(@snapshot_reverter_mock)
      @snapshot_reverter_mock.should_receive(:revert_to_snapshot).
                              with('snap_1').
                              and_return(@snapshot_revert_response_mock)
    end

    it 'should return an unsuccessful response when unable to revert the snapshot' do
      @snapshot_revert_response_mock.stub_as_unsuccessful
      @vm.revert_to_snapshot('snap_1').should be_an_unsuccessful_response
    end

    it 'should return a successful response when reverting the snapshot' do
      @snapshot_revert_response_mock.stub_as_successful
      @vm.revert_to_snapshot('snap_1').should be_a_successful_response
    end
  end

  describe 'exists?' do
    before do
      @conf_file_response = double('exists')
      @vm.stub(:conf_file).and_return(@conf_file_response)
    end

    it 'should return true if the VM exists' do
      @conf_file_response.stub_as_successful '/vms/foo/foo.vmx'
      @vm.exists?.should == true
    end

    it 'should return false if the VM does not exist' do
      @conf_file_response.stub_as_unsuccessful
      @vm.exists?.should == false
    end
  end

  describe 'hardware_info' do
    before do
      @vm_config_data_response_mock = double('vm config data response')
      @vm.stub(:conf_file_data).and_return(@vm_config_data_response_mock)
      @config_data = { 'numvcpus'         => '2',
                       'replay.supported' => "TRUE",
                       'replay.filename'  => '',
                       'memsize'          => '2048',
                       'scsi0:0.redo'     => '' }
    end

    context 'when successful getting the vm config data' do
      before do
        @vm_config_data_response_mock.stub_as_successful @config_data
      end

      context 'when the number of cpus is not specified in the conf file' do
        before do
         @config_data.delete 'numvcpus'
        end

        it 'should return a successful response with a single cpu' do
          response = @vm.hardware_info

          response.should be_a_successful_response
          response.data.should have_key 'cpus'
          response.data['cpus'].should == 1
        end
      end

      it 'should return a successful response with the number of cpus' do
        response = @vm.hardware_info

        response.should be_a_successful_response
        response.data.should have_key 'cpus'
        response.data['cpus'].should == 2
      end

      it 'should return a successful response with the amount of memory' do
        response = @vm.hardware_info

        response.should be_a_successful_response
        response.data.should have_key 'memory'
        response.data['memory'].should == 2048
      end
    end

    context 'when unsuccessfully getting the vm config data' do
      it 'should return an unsuccessful response' do
        @vm_config_data_response_mock.stub_as_unsuccessful
        @vm.hardware_info.should be_an_unsuccessful_response
      end
    end

  end

  describe 'mac_addresses' do
    before do
      @network_info_mock = double('network_info')
      @vm.should_receive(:network_info).and_return(@network_info_mock)
    end

    it 'should return a successful response with the list of mac addresses' do
      network_data = { 'ethernet0' => { 'mac_address' => '00:0c:29:1d:6a:64',
                                        'ip_address'  => '127.0.0.1' },
                       'ethernet1' => { 'mac_address' => '00:0c:29:1d:6a:75',
                                        'ip_address'  => '127.0.0.2' } }
      @network_info_mock.stub_as_successful network_data

      response = @vm.mac_addresses

      response.should be_a_successful_response
      response.data.should =~ ['00:0c:29:1d:6a:64', '00:0c:29:1d:6a:75']
    end

    it 'should return a successful response with an empty list if no mac addresses were found' do
      @network_info_mock.stub_as_successful Hash.new

      response = @vm.mac_addresses

      response.should be_a_successful_response
      response.data.should == []
    end

    it 'should return an unsuccessful response if there was an error getting the mac addresses' do
      @network_info_mock.stub_as_unsuccessful

      response = @vm.mac_addresses

      response.should be_an_unsuccessful_response
      response.data.should be_nil
    end

  end

  describe 'network_info' do
    before do
      @lease_1_response_mock = double('lease_1_response')
      @lease_2_response_mock = double('lease_1_response')
      @vm_config_data_response_mock = double('vm config data response')

      @vm.stub(:conf_file_data).and_return(@vm_config_data_response_mock)
      @config_data = { 'ide1:0.deviceType'                   => 'cdrom-image',
                       'ethernet0.present'                    => 'TRUE',
                       'ethernet1.address'                    => '00:0c:29:1d:6a:75',
                       'ethernet0.connectionType'             => 'nat',
                       'ethernet0.generatedAddress'           => '00:0c:29:1d:6a:64',
                       'ethernet0.virtualDev'                 => 'e1000',
                       'ethernet0.wakeOnPcktRcv'              => 'FALSE',
                       'ethernet0.addressType'                => 'generated',
                       'ethernet0.linkStatePropagatio.enable' => 'TRUE',
                       'ethernet0.generatedAddressenable'     => 'TRUE',
                       'ethernet1.generatedAddressenable'     => 'TRUE' }
    end

    context 'when successful getting the vm config data' do
      before do
        @vm_config_data_response_mock.stub_as_successful @config_data
      end

      it 'should return a successful response with the list of interfaces, macs, and ips' do
        @lease_1 = Fission::Lease.new :ip_address  => '127.0.0.1',
                                      :mac_address => '00:0c:29:1d:6a:64'
        @lease_1_response_mock.stub_as_successful @lease_1

        @lease_2 = Fission::Lease.new :ip_address  => '127.0.0.2',
                                      :mac_address => '00:0c:29:1d:6a:75'
        @lease_2_response_mock.stub_as_successful @lease_2

        Fission::Lease.should_receive(:find_by_mac_address).
                       with('00:0c:29:1d:6a:64').
                       and_return(@lease_1_response_mock)
        Fission::Lease.should_receive(:find_by_mac_address).
                       with('00:0c:29:1d:6a:75').
                       and_return(@lease_2_response_mock)

        response = @vm.network_info
        response.should be_a_successful_response
        response.data.should == { 'ethernet0' => { 'mac_address'  => '00:0c:29:1d:6a:64',
                                                   'ip_address'   => '127.0.0.1' },
                                  'ethernet1' => { 'mac_address'  => '00:0c:29:1d:6a:75',
                                                   'ip_address'   => '127.0.0.2' } }
      end

      it 'should return a successful response with an empty list if there are no macs' do
        @config_data.delete 'ethernet0.generatedAddress'
        @config_data.delete 'ethernet1.address'

        response = @vm.network_info
        response.should be_a_successful_response
        response.data.should == {}
      end

      it 'should return a successful response without ip addresses if none were found' do
        @lease_1_response_mock.stub_as_successful nil
        @lease_2_response_mock.stub_as_successful nil

        Fission::Lease.should_receive(:find_by_mac_address).
                       with('00:0c:29:1d:6a:64').
                       and_return(@lease_1_response_mock)
        Fission::Lease.should_receive(:find_by_mac_address).
                       with('00:0c:29:1d:6a:75').
                       and_return(@lease_2_response_mock)

        response = @vm.network_info
        response.should be_a_successful_response
        response.data.should == { 'ethernet0' => { 'mac_address'  => '00:0c:29:1d:6a:64',
                                                   'ip_address'   => nil },
                                  'ethernet1' => { 'mac_address'  => '00:0c:29:1d:6a:75',
                                                   'ip_address'   => nil } }
      end

      context 'when unsuccessful getting the ip info' do
        it 'should return an unsuccessful response if there was an error getting the ip information' do
          @lease_1_response_mock.stub_as_unsuccessful
          @lease_2_response_mock.stub_as_successful nil

          Fission::Lease.should_receive(:find_by_mac_address).
                         with('00:0c:29:1d:6a:64').
                         and_return(@lease_1_response_mock)
          Fission::Lease.should_receive(:find_by_mac_address).
                         with('00:0c:29:1d:6a:75').
                         and_return(@lease_2_response_mock)

          @vm.network_info.should be_an_unsuccessful_response
        end
      end

    end

    context 'when unsuccessfully getting the vm config data' do
      it 'should return an unsuccessful response' do
        @vm_config_data_response_mock.stub_as_unsuccessful
        @vm.guestos.should be_an_unsuccessful_response
      end
    end

  end

  describe 'guestos' do
    before do
      @vm_config_data_response_mock = double('vm config data response')
      @vm.stub(:conf_file_data).and_return(@vm_config_data_response_mock)
      @config_data = { 'cleanShutdown'   => 'TRUE',
                       'guestOS'         => 'debian5',
                       'replay.filename' => '',
                       'scsi0:0.redo'    => '' }
    end

    context 'when successful getting the vm config data' do
      it 'should return a successful response with a string when a guestos is defined' do
        @vm_config_data_response_mock.stub_as_successful @config_data

        response = @vm.guestos
        response.should be_a_successful_response
        response.data.should == 'debian5'
      end

      it 'should return a successful response with an empty string if guestos is not set' do
        @config_data.delete 'guestOS'
        @vm_config_data_response_mock.stub_as_successful @config_data

        response = @vm.guestos
        response.should be_a_successful_response
        response.data.should == ''
      end
    end

    context 'when unsuccessfully getting the vm config data' do
      it 'should return an unsuccessful response' do
        @vm_config_data_response_mock.stub_as_unsuccessful
        @vm.guestos.should be_an_unsuccessful_response
      end
    end
  end

  describe 'uuids' do
    before do
      @vm_config_data_response_mock = double('vm config data response')
      @vm.stub(:conf_file_data).and_return(@vm_config_data_response_mock)
      @config_data = { 'uuid.location' => '56 4d d8 9c f8 ec 95 73-2e ea a0 f3 7a 1d 6f c8',
                       'uuid.bios' => '56 4d d8 9c f8 ec 95 73-2e ea a0 f3 7a 1d 6f c8',
                       'checkpoint.vmState' => '',
                       'cleanShutdown' => 'TRUE',
                       'replay.supported' => "TRUE",
                       'replay.filename' => '',
                       'scsi0:0.redo' =>'' }
    end

    context 'when successful getting the vm config data' do
      it 'should return a successful response with a hash when uuids are defined' do
        @vm_config_data_response_mock.stub_as_successful @config_data

        response = @vm.uuids
        response.should be_a_successful_response
        response.data.should == { 'bios'     => '56 4d d8 9c f8 ec 95 73-2e ea a0 f3 7a 1d 6f c8',
                                  'location' => '56 4d d8 9c f8 ec 95 73-2e ea a0 f3 7a 1d 6f c8' }
      end

      it 'should return a successful response with empty hash if no uuids are defined' do
        ['location', 'bios'].each { |i| @config_data.delete "uuid.#{i}" }
        @vm_config_data_response_mock.stub_as_successful @config_data

        response = @vm.uuids
        response.should be_a_successful_response
        response.data.should == {}
      end
    end

    context 'when unsuccessfully getting the vm config data' do
      it 'should return an unsuccessful response' do
        @vm_config_data_response_mock.stub_as_unsuccessful
        @vm.uuids.should be_an_unsuccessful_response
      end
    end

  end

  describe 'path' do
    it 'should return the path of the VM' do
      vm_path = File.join(Fission.config['vm_dir'], 'foo.vmwarevm').gsub '\\', ''
      Fission::VM.new('foo').path.should == vm_path
    end
  end

  describe 'state' do
    before do
      @vm_1 = Fission::VM.new 'foo'
      @vm_2 = Fission::VM.new 'bar'

      @all_running_response_mock = double('all_running')
      @suspended_response_mock = double('suspended')

      Fission::VM.stub(:all_running).and_return(@all_running_response_mock)
      @all_running_response_mock.stub_as_successful [@vm_2]
    end

    it "should return a successful response and 'not running' when the VM is off" do
      response = @vm.state
      response.should be_a_successful_response
      response.data.should == 'not running'
    end

    it "should return a successful resopnse and 'running' when the VM is running" do
      @all_running_response_mock.stub_as_successful [@vm_1, @vm_2]

      response = @vm.state
      response.should be_a_successful_response
      response.data.should == 'running'
    end

    it "should return a successful response and 'suspended' when the VM is suspended" do
      @suspended_response_mock.stub_as_successful true

      @vm.stub(:suspended?).and_return(@suspended_response_mock)

      response = @vm.state
      response.should be_a_successful_response
      response.data.should == 'suspended'
    end

    it 'should return an unsuccessful response if there was an error getting the running VMs' do
      @all_running_response_mock.stub_as_unsuccessful
      @vm.state.should be_an_unsuccessful_response
    end

    it 'should return an unsuccessful repsonse if there was an error determining if the VM is suspended' do
      @suspended_response_mock.stub_as_unsuccessful
      @vm.stub(:suspended?).and_return(@suspended_response_mock)
      @vm.state.should be_an_unsuccessful_response
    end
  end

  describe 'running?' do
    before do
      @all_running_response_mock = double('all_running')

      Fission::VM.stub(:all_running).and_return(@all_running_response_mock)
    end

    it 'should return a successful response and false when the vm is not running' do
      @all_running_response_mock.stub_as_successful []
      response = @vm.running?
      response.should be_a_successful_response
      response.data.should == false
    end

    it 'should return a successful response and true if the vm is running' do
      @all_running_response_mock.stub_as_successful [Fission::VM.new('foo')]

      response = @vm.running?
      response.should be_a_successful_response
      response.data.should == true
    end

    it 'should return an unsuccessful repsponse if there is an error getting the list of running vms' do
      @all_running_response_mock.stub_as_unsuccessful
      @vm.running?.should be_an_unsuccessful_response
    end

  end

  describe 'suspend_file_exists?' do
    before do
      FakeFS.activate!
      FileUtils.mkdir_p @vm.path
    end

    after do
      FakeFS.deactivate!
      FakeFS::FileSystem.clear
    end

    it 'should return true if the suspend file exists' do
      FileUtils.touch(File.join(@vm.path, "#{@vm.name}.vmem"))
      @vm.suspend_file_exists?.should == true
    end

    it 'should return false if the suspend file does not exist' do
      @vm.suspend_file_exists?.should == false
    end

  end

  describe 'suspended?' do
    before do
      @running_response_mock = double('running?')
      @vm.stub(:running?).and_return(@running_response_mock)
    end

    describe 'when the vm is not running' do
      before do
        @running_response_mock.stub_as_successful false
      end

      it 'should return a successful response and true if a .vmem file exists in the vm dir' do
        @vm.stub(:suspend_file_exists?).and_return(true)

        response = @vm.suspended?
        response.should be_a_successful_response
        response.data.should == true
      end

      it 'should return a successful response and false if a .vmem file is not found in the vm dir' do
        @vm.stub(:suspend_file_exists?).and_return(false)

        response = @vm.suspended?
        response.should be_a_successful_response
        response.data.should == false
      end
    end

    it 'should return a successful response and false if the vm is running' do
      @running_response_mock.stub_as_successful true

      response = @vm.suspended?
      response.should be_a_successful_response
      response.data.should == false
    end

    it 'should return an unsuccessful repsponse if there is an error getting the list of running vms' do
      @running_response_mock.stub_as_unsuccessful
      @vm.suspended?.should be_an_unsuccessful_response
    end

  end

  describe 'conf_file_data' do
    before do
      @vm_config_mock          = double('vm config')
      @vm_config_response_mock = double('vm config response')

      Fission::VMConfiguration.should_receive(:new).with(@vm).
                                                    and_return(@vm_config_mock)
    end

    it 'should return a successful response with the data' do
      @vm_config_response_mock.stub_as_successful({ 'numvcpus' => '2' })

      @vm_config_mock.should_receive(:config_data).
                      and_return(@vm_config_response_mock)
      config_data = @vm.conf_file_data
      config_data.should be_a_successful_response
      config_data.data.should == { 'numvcpus' => '2' }
    end

    it 'should return an unsuccessful response' do
      @vm_config_mock.should_receive(:config_data).
                      and_return(@vm_config_response_mock)
      @vm_config_response_mock.stub_as_unsuccessful
      @vm.conf_file_data.should be_an_unsuccessful_response
    end
  end

  describe 'conf_file' do
    before do
      FakeFS.activate!
      @vm_root_dir = Fission::VM.new('foo').path
      FileUtils.mkdir_p(@vm_root_dir)
    end

    after do
      FakeFS.deactivate!
      FakeFS::FileSystem.clear
    end

    it 'should return a successful response with the path to the conf file' do
      file_path = File.join(@vm_root_dir, 'foo.vmx')
      FileUtils.touch(file_path)

      response = Fission::VM.new('foo').conf_file
      response.should be_a_successful_response
      response.data.should == file_path
    end

    it 'should return an unsuccessful response with an error if no vmx file was found' do
      response = Fission::VM.new('foo').conf_file
      response.successful?.should == false
      response.message.should match /Unable to find a config file for VM 'foo' \(in '#{File.join(@vm_root_dir, '\*\.vmx')}'\)/m
    end

    describe 'when the VM name and conf file name do not match' do
      it 'should return the path to the conf file' do
        file_path = File.join(@vm_root_dir, 'bar.vmx')
        FileUtils.touch(file_path)

        response = Fission::VM.new('foo').conf_file
        response.should be_a_successful_response
        response.data.should == file_path
      end
    end

    describe 'if multiple vmx files are found' do
      it 'should use return a successful response with the conf file which matches the VM name if it exists' do
        ['foo.vmx', 'bar.vmx'].each do |file|
          FileUtils.touch(File.join(@vm_root_dir, file))
        end

        response = Fission::VM.new('foo').conf_file
        response.should be_a_successful_response
        response.data.should == File.join(@vm_root_dir, 'foo.vmx')
      end

      it 'should return an unsuccessful object if none of the conf files matches the VM name' do
        ['bar.vmx', 'baz.vmx'].each do |file|
          FileUtils.touch(File.join(@vm_root_dir, file))
        end
        Fission::VM.new('foo').conf_file

        response = Fission::VM.new('foo').conf_file
        response.successful?.should == false
        error_regex = /Multiple config files found for VM 'foo' \('bar\.vmx', 'baz\.vmx' in '#{@vm_root_dir}'/m
        response.message.should match error_regex
      end
    end

  end

  describe "self.all" do
    before do
      @lister            = double('lister')
      @all_response_mock = double('all response')
      Fission::Action::VM::Lister.stub(:new).and_return(@lister)
      @lister.should_receive(:all).
              and_return(@all_response_mock)
    end

    it 'should return an unsuccessful response when unable to delete the vm' do
      @all_response_mock.stub_as_unsuccessful
      Fission::VM.all.should be_an_unsuccessful_response
    end

    it 'should return a successful response when deleting' do
      @all_response_mock.stub_as_successful
      Fission::VM.all.should be_a_successful_response
    end
  end

  describe 'self.all_running' do
    before do
      @lister                    = double('lister')
      @all_running_response_mock = double('all running response')
      Fission::Action::VM::Lister.stub(:new).and_return(@lister)
      @lister.should_receive(:all_running).
              and_return(@all_running_response_mock)
    end

    it 'should return an unsuccessful response when unable to delete the vm' do
      @all_running_response_mock.stub_as_unsuccessful
      Fission::VM.all_running.should be_an_unsuccessful_response
    end

    it 'should return a successful response when deleting' do
      @all_running_response_mock.stub_as_successful
      Fission::VM.all_running.should be_a_successful_response
    end
  end

  describe "self.clone" do
    before do
      @vm_1                = double('vm 1')
      @vm_2                = double('vm 2')
      @vm_cloner           = double('vm cloner')
      @clone_response_mock = double('vm clone response')
      Fission::Action::VM::Cloner.should_receive(:new).
                                with(@vm_1, @vm_2).
                                and_return(@vm_cloner)
      Fission::VM.should_receive(:new).with('vm_1').and_return(@vm_1)
      Fission::VM.should_receive(:new).with('vm_2').and_return(@vm_2)
      @vm_cloner.should_receive(:clone).
                 and_return(@clone_response_mock)
    end

    it 'should return an unsuccessful response when unable to clone the vm' do
      @clone_response_mock.stub_as_unsuccessful
      Fission::VM.clone('vm_1', 'vm_2').should be_an_unsuccessful_response
    end

    it 'should return a successful response when cloning' do
      @clone_response_mock.stub_as_successful
      Fission::VM.clone('vm_1', 'vm_2').should be_a_successful_response
    end
  end

  describe 'self.all_with_status' do
    before do
      @lister                   = double('lister')
      @all_status_response_mock = double('all status response')
      Fission::Action::VM::Lister.stub(:new).and_return(@lister)
      @lister.should_receive(:all_with_status).
              and_return(@all_status_response_mock)
    end

    it 'should return an unsuccessful response when unable to delete the vm' do
      @all_status_response_mock.stub_as_unsuccessful
      Fission::VM.all_with_status.should be_an_unsuccessful_response
    end

    it 'should return a successful response when deleting' do
      @all_status_response_mock.stub_as_successful
      Fission::VM.all_with_status.should be_a_successful_response
    end

  end

end