File: capistrano_support_test.rb

package info (click to toggle)
ruby-whenever 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 328 kB
  • sloc: ruby: 2,722; makefile: 2
file content (147 lines) | stat: -rw-r--r-- 6,584 bytes parent folder | download | duplicates (2)
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
require 'test_helper'
require 'whenever/capistrano/v2/support'

class CapistranoSupportTestSubject
  include Whenever::CapistranoSupport
end

class CapistranoTestCase < Whenever::TestCase
  setup do
    @capistrano = CapistranoSupportTestSubject.new
    configuration = mock()
    configuration.stubs(:load).yields(@capistrano)
    Whenever::CapistranoSupport.load_into(configuration)
  end
end

class CapistranoSupportTest < CapistranoTestCase
  should "return fetch(:whenever_options) from #whenever_options" do
    @capistrano.expects(:fetch).with(:whenever_options)
    @capistrano.whenever_options
  end

  should "return whenever_options[:roles] as an array from #whenever_roles with one role" do
    @capistrano.stubs(:whenever_options).returns({:roles => :role1})
    assert_equal [:role1], @capistrano.whenever_roles
  end

  should "return an empty array from #whenever_roles with no defined roles" do
    @capistrano.stubs(:whenever_options).returns({})
    assert_equal [], @capistrano.whenever_roles
  end

  should "return the list of servers returned by find_servers from #whenever_servers" do
    @capistrano.stubs(:whenever_options).returns({})
    @capistrano.stubs(:find_servers).returns([:server1, :server2])

    assert_equal [:server1, :server2], @capistrano.whenever_servers
  end

  should "#whenever_prepare_for_rollback: set path to previous_release if there is a previous release" do
    args = {}
    @capistrano.stubs(:fetch).with(:previous_release).returns("/some/path/20121221010000")
    assert_equal({:path => "/some/path/20121221010000"}, @capistrano.whenever_prepare_for_rollback(args))
  end

  should "#whenever_prepare_for_rollback: set path to release_path and flags to whenever_clear_flags if there is no previous release" do
    args = {}
    @capistrano.stubs(:fetch).with(:previous_release).returns(nil)
    @capistrano.stubs(:fetch).with(:release_path).returns("/some/path/20121221010000")
    @capistrano.stubs(:fetch).with(:whenever_clear_flags).returns("--clear-crontab whenever_identifier")
    assert_equal({:path => "/some/path/20121221010000", :flags => "--clear-crontab whenever_identifier"}, @capistrano.whenever_prepare_for_rollback(args))
  end

  should "#whenever_run_commands: require :command arg" do
    assert_raises ArgumentError do
      @capistrano.whenever_run_commands(:options => {}, :path => {}, :flags => {})
    end
  end

  should "#whenever_run_commands: require :path arg" do
    assert_raises ArgumentError do
      @capistrano.whenever_run_commands(:options => {}, :command => {}, :flags => {})
    end
  end

  should "#whenever_run_commands: require :flags arg" do
    assert_raises ArgumentError do
      @capistrano.whenever_run_commands(:options => {}, :path => {}, :command => {})
    end
  end
end

class ServerRolesTest < CapistranoTestCase
  setup do
    @mock_servers = ["foo", "bar"]
    @capistrano.stubs(:whenever_servers).returns(@mock_servers)

    @mock_server1, @mock_server2, @mock_server3 = mock("Server1"), mock("Server2"), mock("Server3")
    @mock_server1.stubs(:host).returns("server1.foo.com")
    @mock_server2.stubs(:host).returns("server2.foo.com")
    @mock_server3.stubs(:host => "server3.foo.com", :port => 1022, :user => 'test')
    @mock_servers = [@mock_server1, @mock_server2]
  end

  should "return a map of servers to their role(s)" do
    @capistrano.stubs(:whenever_roles).returns([:role1, :role2])
    @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1])
    @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
    assert_equal({"foo" => [:role1], "bar" => [:role2]}, @capistrano.whenever_server_roles)
  end

  should "exclude non-requested roles" do
    @capistrano.stubs(:whenever_roles).returns([:role1, :role2])
    @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1, :role3])
    @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
    assert_equal({"foo" => [:role1], "bar" => [:role2]}, @capistrano.whenever_server_roles)
  end

  should "include all roles for servers w/ >1 when they're requested" do
    @capistrano.stubs(:whenever_roles).returns([:role1, :role2, :role3])
    @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1, :role3])
    @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
    assert_equal({"foo" => [:role1, :role3], "bar" => [:role2]}, @capistrano.whenever_server_roles)
  end

  should "call run for each host w/ appropriate role args" do
    @capistrano.stubs(:role_names_for_host).with(@mock_server1).returns([:role1])
    @capistrano.stubs(:role_names_for_host).with(@mock_server2).returns([:role2])
    @capistrano.stubs(:whenever_servers).returns(@mock_servers)
    roles = [:role1, :role2]
    @capistrano.stubs(:whenever_options).returns({:roles => roles})

    @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role1', {:roles => roles, :hosts => @mock_server1})
    @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role2', {:roles => roles, :hosts => @mock_server2})

    @capistrano.whenever_run_commands(:command => "whenever",
                                      :path => "/foo/bar",
                                      :flags => "--flag1 --flag2")
  end

  should "call run w/ all role args for servers w/ >1 role" do
    @capistrano.stubs(:role_names_for_host).with(@mock_server1).returns([:role1, :role3])
    @capistrano.stubs(:whenever_servers).returns([@mock_server1])
    roles = [:role1, :role2, :role3]
    @capistrano.stubs(:whenever_options).returns({:roles => roles})

    @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role1,role3', {:roles => roles, :hosts => @mock_server1})

    @capistrano.whenever_run_commands(:command => "whenever",
                                      :path => "/foo/bar",
                                      :flags => "--flag1 --flag2")
  end

  should "call run w/ proper server options (port, user)" do
    @capistrano.stubs(:role_names_for_host).with(@mock_server3).returns([:role3])
    @capistrano.stubs(:whenever_servers).returns([@mock_server3])
    @capistrano.stubs(:whenever_options).returns({:roles => [:role3]})

    @capistrano.expects(:run).once.with do |command, options|
      options[:hosts].user == "test" && options[:hosts].port == 1022
    end

    @capistrano.whenever_run_commands(:command => "whenever",
                                      :path => "/foo/bar",
                                      :flags => "--flag1 --flag2")
  end
end