foreman_expire_hosts 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6fe2409547a823dfd323178e3d0919eac77bebc
4
- data.tar.gz: 87226ce69f178e588ca451eef22a0d8982bc1f99
3
+ metadata.gz: 4a6df2938b1dd23bcc51ceeec5b038f7b4cbbb86
4
+ data.tar.gz: 5d0d5c9d10dc61ca98425b68eb012bc06f4dea3d
5
5
  SHA512:
6
- metadata.gz: 815773fd3b98e15221daca80534cbf0ab0cc376ae4328409df26564a19bcfac2bea34d4aad22095e42439c7273726d909a005b8a6ae70ca7e4ede080e4436eea
7
- data.tar.gz: e556d4c3b0ff806fd126551cc7af2d36af1bc033e0ed0946cc947f64c8470a87cd3ce60555fd5f14d792bd99139df3c416e44f980df37db1c74dc2d41d4787bf
6
+ metadata.gz: 473e5e92b4df667cd758a6abb628cbf8d47a4521e8de5b184b792a41110a91c0c402a5e27876598d9e7c15e317a0b1e06637d6eda2ba219627a327ebadf3a9b0
7
+ data.tar.gz: 4070af0f6b9ab0703edba57c4fe6c086496c62abce17386906f122ae6f929557b28c1f1907579016e8cc6a69e7975a489f415c1bf44026c78870050a56efeae5
@@ -7,6 +7,7 @@ module ForemanExpireHosts
7
7
  end
8
8
 
9
9
  def multiple_actions_with_expire_hosts
10
+ return multiple_actions_without_expire_hosts unless authorized_for(:controller => :hosts, :action => :edit)
10
11
  multiple_actions_without_expire_hosts + [[_('Change Expiration'), select_multiple_expiration_hosts_path]]
11
12
  end
12
13
 
@@ -4,25 +4,25 @@ class ExpireHostsMailer < ApplicationMailer
4
4
 
5
5
  def deleted_hosts_notification(emails, hosts)
6
6
  @hosts = hosts
7
- mail(:to => emails, :subject => 'Deleted expired hosts in Foreman', :importance => 'High')
7
+ mail(:to => emails, :subject => _('Deleted expired hosts in Foreman'), :importance => 'High')
8
8
  end
9
9
 
10
10
  def failed_to_delete_hosts_notification(emails, hosts)
11
11
  @hosts = hosts
12
- mail(:to => emails, :subject => 'Failed to delete expired hosts in Foreman', :importance => 'High')
12
+ mail(:to => emails, :subject => _('Failed to delete expired hosts in Foreman'), :importance => 'High')
13
13
  end
14
14
 
15
15
  def stopped_hosts_notification(emails, delete_date, hosts)
16
16
  @hosts = hosts
17
17
  @delete_date = delete_date
18
- mail(:to => emails, :subject => 'Stopped expired hosts in Foreman', :importance => 'High') do |format|
18
+ mail(:to => emails, :subject => _('Stopped expired hosts in Foreman'), :importance => 'High') do |format|
19
19
  format.html { render :layout => 'application_mailer' }
20
20
  end
21
21
  end
22
22
 
23
23
  def failed_to_stop_hosts_notification(emails, hosts)
24
24
  @hosts = hosts
25
- mail(:to => emails, :subject => 'Failed to stop expired hosts in Foreman', :importance => 'High') do |format|
25
+ mail(:to => emails, :subject => _('Failed to stop expired hosts in Foreman'), :importance => 'High') do |format|
26
26
  format.html { render :layout => 'application_mailer' }
27
27
  end
28
28
  end
@@ -30,7 +30,7 @@ class ExpireHostsMailer < ApplicationMailer
30
30
  def expiry_warning_notification(emails, expiry_date, hosts)
31
31
  @hosts = hosts
32
32
  @expiry_date = expiry_date
33
- mail(:to => emails, :subject => 'Expiring hosts in foreman', :importance => 'High') do |format|
33
+ mail(:to => emails, :subject => _('Expiring hosts in foreman'), :importance => 'High') do |format|
34
34
  format.html { render :layout => 'application_mailer' }
35
35
  end
36
36
  end
@@ -16,7 +16,7 @@ module ForemanExpireHosts
16
16
  scope :with_expire_date, ->(date) { expiring.where('expired_on = ?', date) }
17
17
  scope :expired, -> { expiring.where('expired_on < ?', Date.today) }
18
18
  scope :expiring_today, -> { expiring.with_expire_date(Date.today) }
19
- scope :expired_past_grace_period, -> { expiring.where('expired_on < ?', Date.today + Setting[:days_to_delete_after_host_expiration].to_i) }
19
+ scope :expired_past_grace_period, -> { expiring.where('expired_on <= ?', Date.today - Setting[:days_to_delete_after_host_expiration].to_i) }
20
20
 
21
21
  scoped_search :on => :expired_on, :complete_value => :true, :rename => :expires, :only_explicit => true
22
22
  end
@@ -60,7 +60,7 @@ module ForemanExpireHosts
60
60
 
61
61
  def expired_past_grace_period?
62
62
  return false unless expires?
63
- expiration_grace_period_end_date < Date.today
63
+ expiration_grace_period_end_date <= Date.today
64
64
  end
65
65
 
66
66
  def pending_expiration_start_date
@@ -11,9 +11,9 @@ module HostStatus
11
11
  end
12
12
 
13
13
  def to_status(_options = {})
14
+ return EXPIRES_TODAY if host.expires_today?
14
15
  return EXPIRED if host.expired_past_grace_period?
15
16
  return IN_GRACE_PERIOD if host.expired?
16
- return EXPIRES_TODAY if host.expires_today?
17
17
  return PENDING if host.pending_expiration?
18
18
  OK
19
19
  end
@@ -24,12 +24,12 @@ module HostStatus
24
24
  HostStatus::Global::OK
25
25
  when EXPIRES_TODAY
26
26
  HostStatus::Global::WARN
27
+ when PENDING
28
+ HostStatus::Global::WARN
27
29
  when IN_GRACE_PERIOD
28
30
  HostStatus::Global::ERROR
29
31
  when EXPIRED
30
32
  HostStatus::Global::ERROR
31
- when PENDING
32
- HostStatus::Global::ERROR
33
33
  else
34
34
  HostStatus::Global::OK
35
35
  end
@@ -1,4 +1,4 @@
1
- <%= _('The following hosts have been expired in Foreman and will be stopped for now. These hosts will be destroyed on %s. Please change their expiry date and power them on if you want to keep the hosts.') % l @delete_date %>
1
+ <%= _('The following hosts have been expired in Foreman and will be stopped for now. These hosts will be destroyed on %s. Please change their expiry date and power them on if you want to keep the hosts.') % l(@delete_date) %>
2
2
  <br/><br/>
3
3
 
4
4
  <%= render 'hosts_table', :hosts => @hosts, :link_to_hosts => true %>
@@ -12,7 +12,7 @@ module ExpireHostsNotifications
12
12
  def catch_delivery_errors(message, hosts = [])
13
13
  yield
14
14
  rescue => error
15
- message = "#{message} for Hosts #{hosts.map(&:name).to_sentence}"
15
+ message = _("%{message} for Hosts %{hosts}") % {:message => message, :hosts => hosts.map(&:name).to_sentence}
16
16
  Foreman::Logging.exception(message, error)
17
17
  end
18
18
 
@@ -24,7 +24,7 @@ module ExpireHostsNotifications
24
24
  deletable_hosts.each do |deletable_host|
25
25
  Rails.logger.info "Deleting expired host #{deletable_host}"
26
26
  deletable_host.audit_comment = _('Destroyed since it got expired on %s') % deletable_host.expired_on
27
- if deletable_host # .destroy
27
+ if deletable_host.destroy
28
28
  deleted_hosts << deletable_host
29
29
  else
30
30
  failed_delete_hosts << deletable_host
@@ -1,3 +1,3 @@
1
1
  module ForemanExpireHosts
2
- VERSION = '2.0.1'.freeze
2
+ VERSION = '2.0.2'.freeze
3
3
  end
@@ -1,14 +1,26 @@
1
1
  FactoryGirl.modify do
2
2
  factory :host do
3
+ trait :without_validation do
4
+ # Turn off validation so we can create a host with an expiry date in the past
5
+ to_create {|instance| instance.save(validate: false) }
6
+ end
7
+
8
+ trait :expires_in_a_week do
9
+ expired_on Date.today + 7
10
+ end
11
+
3
12
  trait :expires_today do
13
+ without_validation
4
14
  expired_on Date.today
5
15
  end
6
16
 
7
17
  trait :expired_grace do
18
+ without_validation
8
19
  expired_on Date.today - 1
9
20
  end
10
21
 
11
22
  trait :expired do
23
+ without_validation
12
24
  expired_on Date.today - 356
13
25
  end
14
26
  end
@@ -0,0 +1,76 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
4
+ setup do
5
+ setup_settings
6
+ @owner = FactoryGirl.create(:user, :with_mail)
7
+ end
8
+
9
+ context '#delete_expired_hosts' do
10
+ setup do
11
+ @hosts = FactoryGirl.create_list(:host, 2, :expired, :owner => @owner)
12
+ @unaffected_hosts = FactoryGirl.create_list(:host, 2, :owner => @owner)
13
+ end
14
+
15
+ test 'should delete expired hosts' do
16
+ ExpireHostsMailer.expects(:deleted_hosts_notification).once
17
+ assert_difference 'Host.count', -2 do
18
+ ExpireHostsNotifications.delete_expired_hosts
19
+ end
20
+ end
21
+ end
22
+
23
+ context '#stop_expired_hosts' do
24
+ setup do
25
+ @power_mock = mock('power')
26
+ @power_mock.stubs(:ready?).returns(true)
27
+ @host = FactoryGirl.create(:host, :expired, :on_compute_resource, :owner => @owner)
28
+ @host.unstub(:queue_compute)
29
+ Host.any_instance.stubs(:power).returns(@power_mock)
30
+ @unaffected_hosts = FactoryGirl.create_list(:host, 2, :owner => @owner)
31
+ end
32
+
33
+ test 'should stop expired hosts' do
34
+ @power_mock.expects(:stop).returns(true)
35
+ ExpireHostsMailer.expects(:stopped_hosts_notification).once
36
+ ExpireHostsNotifications.stop_expired_hosts
37
+ end
38
+
39
+ test 'should send failure message if host cannot be stopped' do
40
+ @power_mock.expects(:stop).returns(false)
41
+ ExpireHostsMailer.expects(:failed_to_stop_hosts_notification).once
42
+ ExpireHostsNotifications.stop_expired_hosts
43
+ end
44
+ end
45
+
46
+ context '#deliver_expiry_warning_notification' do
47
+ setup do
48
+ Setting['notify1_days_before_host_expiry'] = 7
49
+ @hosts = FactoryGirl.create_list(:host, 2, :expires_in_a_week, :owner => @owner)
50
+ end
51
+
52
+ test 'should send a single notification' do
53
+ ExpireHostsMailer.expects(:expiry_warning_notification).once
54
+ ExpireHostsNotifications.deliver_expiry_warning_notification
55
+ end
56
+
57
+ test 'should send two notifications for two users' do
58
+ owner2 = FactoryGirl.create(:user, :with_mail)
59
+ host2 = FactoryGirl.create(:host, :expires_in_a_week, :owner => owner2)
60
+ ExpireHostsMailer.expects(:expiry_warning_notification).twice
61
+ ExpireHostsNotifications.deliver_expiry_warning_notification
62
+ end
63
+ end
64
+
65
+ context '#catch_delivery_errors' do
66
+ test 'should catch and log errors' do
67
+ host1 = OpenStruct.new(:name => 'Alpha')
68
+ host2 = OpenStruct.new(:name => 'Bravo')
69
+ hosts = [host1, host2]
70
+ Foreman::Logging.expects(:exception).with('failure for Hosts Alpha and Bravo', anything)
71
+ ExpireHostsNotifications.catch_delivery_errors('failure', hosts) do
72
+ raise "Test"
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,10 +1,24 @@
1
1
  # This calls the main test_helper in Foreman-core
2
2
  require 'test_helper'
3
+ require 'database_cleaner'
3
4
 
4
5
  # Add plugin to FactoryGirl's paths
5
6
  FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
6
7
  FactoryGirl.reload
7
8
 
9
+ # Foreman's setup doesn't handle cleaning up for Minitest::Spec
10
+ DatabaseCleaner.strategy = :transaction
11
+
8
12
  def setup_settings
9
13
  Setting::ExpireHosts.load_defaults
10
14
  end
15
+
16
+ class Minitest::Spec
17
+ before :each do
18
+ DatabaseCleaner.start
19
+ end
20
+
21
+ after :each do
22
+ DatabaseCleaner.clean
23
+ end
24
+ end
@@ -1,6 +1,8 @@
1
1
  require 'test_plugin_helper'
2
2
 
3
3
  class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
4
+ EXPIRATION_SCOPES = ['expiring', 'expired', 'expiring_today', 'expired_past_grace_period']
5
+
4
6
  setup do
5
7
  User.current = FactoryGirl.build(:user, :admin)
6
8
  setup_settings
@@ -53,6 +55,10 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
53
55
  test 'should not be pending expiration' do
54
56
  refute @host.pending_expiration?
55
57
  end
58
+
59
+ test 'should not be in any expiration scopes' do
60
+ exists_only_in_scopes(@host, [])
61
+ end
56
62
  end
57
63
 
58
64
  context 'a expired host' do
@@ -79,6 +85,10 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
79
85
  test 'should not be pending expiration' do
80
86
  refute @host.pending_expiration?
81
87
  end
88
+
89
+ test 'should only exist in correct scopes' do
90
+ exists_only_in_scopes(@host, ['expiring', 'expired', 'expired_past_grace_period'])
91
+ end
82
92
  end
83
93
 
84
94
  context 'a host expiring today' do
@@ -106,6 +116,10 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
106
116
  test 'should be pending expiration' do
107
117
  assert @host.pending_expiration?
108
118
  end
119
+
120
+ test 'should only exist in correct scopes' do
121
+ exists_only_in_scopes(@host, ['expiring', 'expiring_today'])
122
+ end
109
123
  end
110
124
 
111
125
  context 'a host in grace period' do
@@ -132,5 +146,21 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
132
146
  test 'should not be pending expiration' do
133
147
  refute @host.pending_expiration?
134
148
  end
149
+
150
+ test 'should only exist in correct scopes' do
151
+ exists_only_in_scopes(@host, ['expiring', 'expired'])
152
+ end
153
+ end
154
+
155
+ private
156
+
157
+ def exists_only_in_scopes(host, valid_scopes)
158
+ host.save(validate: false)
159
+ (EXPIRATION_SCOPES - valid_scopes).each do |scope|
160
+ refute Host::Managed.send(scope).exists?(host.id), "Host should not exist in #{scope} scope"
161
+ end
162
+ valid_scopes.each do |scope|
163
+ assert Host::Managed.send(scope).exists?(host.id), "Host should exist in #{scope} scope"
164
+ end
135
165
  end
136
166
  end
@@ -0,0 +1,64 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class ExpireHostMailerTest < ActionMailer::TestCase
4
+ setup do
5
+ setup_settings
6
+ @emails = ['test@example.com']
7
+ @hosts = FactoryGirl.create_list(:host, 2, :managed)
8
+ end
9
+
10
+ context 'deleted hosts notification' do
11
+ setup do
12
+ @mail = ExpireHostsMailer.deleted_hosts_notification(@emails, @hosts).deliver
13
+ end
14
+
15
+ test 'subject should be set' do
16
+ refute_nil @mail.subject
17
+ assert_includes @mail.subject, 'Deleted expired hosts in Foreman'
18
+ end
19
+ end
20
+
21
+ context 'failed to delete hosts notification' do
22
+ setup do
23
+ @mail = ExpireHostsMailer.failed_to_delete_hosts_notification(@emails, @hosts).deliver
24
+ end
25
+
26
+ test 'subject should be set' do
27
+ refute_nil @mail.subject
28
+ assert_includes @mail.subject, 'Failed to delete expired hosts in Foreman'
29
+ end
30
+ end
31
+
32
+ context 'stopped hosts notification' do
33
+ setup do
34
+ @mail = ExpireHostsMailer.stopped_hosts_notification(@emails, Date.today, @hosts).deliver
35
+ end
36
+
37
+ test 'subject should be set' do
38
+ refute_nil @mail.subject
39
+ assert_includes @mail.subject, 'Stopped expired hosts in Foreman'
40
+ end
41
+ end
42
+
43
+ context 'failed to stop hosts notification' do
44
+ setup do
45
+ @mail = ExpireHostsMailer.failed_to_stop_hosts_notification(@emails, @hosts).deliver
46
+ end
47
+
48
+ test 'subject should be set' do
49
+ refute_nil @mail.subject
50
+ assert_includes @mail.subject, 'Failed to stop expired hosts in Foreman'
51
+ end
52
+ end
53
+
54
+ context 'expiry warning notification' do
55
+ setup do
56
+ @mail = ExpireHostsMailer.expiry_warning_notification(@emails, Date.today, @hosts).deliver
57
+ end
58
+
59
+ test 'subject should be set' do
60
+ refute_nil @mail.subject
61
+ assert_includes @mail.subject, 'Expiring hosts in foreman'
62
+ end
63
+ end
64
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_expire_hosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nagarjuna Rachaneni
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-06-17 00:00:00.000000000 Z
12
+ date: 2016-07-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: deface
@@ -118,8 +118,10 @@ files:
118
118
  - lib/tasks/expired_hosts.rake
119
119
  - test/factories/foreman_expire_hosts_factories.rb
120
120
  - test/functional/concerns/hosts_controller_extensions_test.rb
121
+ - test/lib/expire_hosts_notifications_test.rb
121
122
  - test/test_plugin_helper.rb
122
123
  - test/unit/concerns/host_extensions_test.rb
124
+ - test/unit/expire_hosts_mailer_test.rb
123
125
  - test/unit/host_status/expiration_status_test.rb
124
126
  homepage: https://github.com/theforeman/foreman_expire_hosts
125
127
  licenses:
@@ -148,7 +150,9 @@ summary: Foreman plugin for limiting host lifetime
148
150
  test_files:
149
151
  - test/factories/foreman_expire_hosts_factories.rb
150
152
  - test/functional/concerns/hosts_controller_extensions_test.rb
153
+ - test/lib/expire_hosts_notifications_test.rb
151
154
  - test/test_plugin_helper.rb
152
155
  - test/unit/concerns/host_extensions_test.rb
156
+ - test/unit/expire_hosts_mailer_test.rb
153
157
  - test/unit/host_status/expiration_status_test.rb
154
158
  has_rdoc: