siba 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/siba/console.rb CHANGED
@@ -34,7 +34,7 @@ Examples:
34
34
  siba restore mybak Restore the backup
35
35
  siba list Show available plugins
36
36
  siba scaffold source my-db Create a new gem for a source plugin
37
- (or archive, encryption or destination)
37
+ (or archive, encryption or destination)
38
38
 
39
39
  Note: single letter commands are also supported, like b for backup
40
40
 
@@ -48,6 +48,10 @@ Options:"
48
48
  SibaLogger.no_log = true
49
49
  end
50
50
 
51
+ o.on("--current-source", "Used with 'restore' command. Restores into the source location which is specified in the CURRENT options file instead of the ORIGINAL source location") do
52
+ options['cur'] = true
53
+ end
54
+
51
55
  o.on("-q", "--quiet", "No output to console") do
52
56
  SibaLogger.quiet = true
53
57
  end
@@ -193,7 +197,7 @@ Edit it and run backup command" unless path_to_yaml.nil?
193
197
  show_error "needless arguments: #{argv.join(', ')}" unless argv.empty?
194
198
  path_to_options = siba_file.file_expand_path path_to_options
195
199
  path_to_options += ".yml" unless path_to_options =~ /\.yml$/
196
- Siba::Restore.new.restore path_to_options
200
+ Siba::Restore.new.restore path_to_options, options["cur"] == true
197
201
  end
198
202
  end
199
203
  end
@@ -18,6 +18,14 @@ class MiniTest::Unit::TestCase
18
18
  verify_log false, level, false
19
19
  end
20
20
 
21
+ def must_log_msg(msg, level)
22
+ verify_log_msg msg, true, level
23
+ end
24
+
25
+ def wont_log_msg(msg, level)
26
+ verify_log_msg msg, false, level
27
+ end
28
+
21
29
  def show_log
22
30
  puts Siba::SibaLogger.messages.map{|a| a.msg}.join("\n")
23
31
  end
@@ -110,5 +118,18 @@ class MiniTest::Unit::TestCase
110
118
  raise message if log_count > 0
111
119
  end
112
120
  end
121
+
122
+ def verify_log_msg(msg, must_change, log_level)
123
+ log_count = Siba::SibaLogger.count_messages msg, log_level
124
+ message = "'#{log_level}' log messages with text '#{msg}'"
125
+
126
+ if must_change
127
+ message = "Expected " + message
128
+ raise message if log_count == 0
129
+ else
130
+ message = "Unexpected " + message
131
+ raise message if log_count > 0
132
+ end
133
+ end
113
134
  end
114
135
 
data/lib/siba/restore.rb CHANGED
@@ -6,7 +6,10 @@ module Siba
6
6
  include Siba::FilePlug
7
7
  include Siba::KernelPlug
8
8
 
9
- def restore(path_to_options_yml)
9
+ attr_accessor :current_source
10
+
11
+ def restore(path_to_options_yml, current_source)
12
+ @current_source = current_source
10
13
  run_restore path_to_options_yml
11
14
  ensure
12
15
  Siba.cleanup
@@ -21,7 +24,7 @@ private
21
24
  Siba.settings = options["settings"] || {}
22
25
  Siba.backup_name = File.basename path_to_options_yml, ".yml"
23
26
  TmpDir.test_access
24
- tasks = SibaTasks.new options, path_to_options_yml, true
27
+ tasks = SibaTasks.new options, path_to_options_yml, !current_source
25
28
  file_name = get_backup_choice tasks
26
29
  unless file_name.nil?
27
30
  if user_wants_to_proceed?
@@ -86,8 +89,14 @@ private
86
89
  end
87
90
 
88
91
  def user_wants_to_proceed?
89
- siba_kernel.printf "\nWarning: backup will be restored into your original source location.
90
- Your current source data will be overwritten and WILL BE LOST.
92
+ if current_source
93
+ source_msg = "CURRENT"
94
+ else
95
+ source_msg = "ORIGINAL"
96
+ end
97
+
98
+ siba_kernel.printf "\nWarning: backup will be restored into the #{source_msg} source location.
99
+ Your source data will be overwritten and WILL BE LOST.
91
100
  Type 'yes' if you want to proceed:
92
101
  (yes/n) > "
93
102
  user_choice = siba_kernel.gets.chomp.strip
@@ -33,6 +33,27 @@ module Siba
33
33
  end
34
34
  end
35
35
  end
36
+
37
+ # returns the number of log messages that contain given msg text
38
+ def count_messages(msg, severity=nil, exact_level=true)
39
+ return 0 if SibaLogger.messages.nil?
40
+ severity_i = SibaLogger.level_to_i severity unless severity.nil?
41
+ SibaLogger.messages.count do |i|
42
+ match_level = true
43
+ if severity_i
44
+ if exact_level
45
+ match_level = i.level == severity_i
46
+ else
47
+ match_level = i.level >= severity_i
48
+ end
49
+ end
50
+ if match_level
51
+ !((i.msg =~ /#{msg}/).nil?)
52
+ else
53
+ false
54
+ end
55
+ end
56
+ end
36
57
  end
37
58
 
38
59
  attr_accessor :show_finish_message
@@ -65,7 +65,7 @@ module Siba
65
65
  raise Siba::Error, "Failed to extract archive: #{path_to_archive}"
66
66
  end
67
67
 
68
- @tasks["source"] = OptionsBackup.load_source_from_backup source_dir
68
+ @tasks["source"] = OptionsBackup.load_source_from_backup source_dir if @tasks["source"].nil?
69
69
  @tasks["source"].restore source_dir
70
70
  end
71
71
  end
data/lib/siba/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Siba
4
- VERSION = "0.5.2"
4
+ VERSION = "0.5.3"
5
5
  end
@@ -42,7 +42,7 @@ describe Siba::Backup do
42
42
 
43
43
  Siba::SibaLogger.quiet = true
44
44
  SibaTest::KernelMock.gets_return_value = "yes"
45
- Siba::Restore.new.restore test_yml_path
45
+ Siba::Restore.new.restore test_yml_path, false
46
46
 
47
47
  File.directory?(src_dir).must_equal true
48
48
  File.file?(src_file).must_equal true
@@ -23,8 +23,19 @@ describe Siba::SibaTasks do
23
23
  tasks = Siba::SibaTasks.new options, path_to_test_yml, false
24
24
  tasks.backup
25
25
 
26
+ # restore into original source
26
27
  backup_file_name = Siba::FileHelper.entries(dest_dir)[0]
27
28
  tasks = Siba::SibaTasks.new options, path_to_test_yml, true
29
+ tasks.tasks["source"].must_be_nil # should not load source before restore
28
30
  tasks.restore backup_file_name
31
+ tasks.tasks["source"].wont_be_nil # source should be loaded on restore
32
+
33
+ # restore into curret source
34
+ backup_file_name = Siba::FileHelper.entries(dest_dir)[0]
35
+ tasks = Siba::SibaTasks.new options, path_to_test_yml, false
36
+ tasks.tasks["source"].wont_be_nil # should load current source on tasks init
37
+ source = tasks.tasks["source"]
38
+ tasks.restore backup_file_name
39
+ tasks.tasks["source"].must_equal source # should use current source during restore
29
40
  end
30
41
  end
@@ -11,7 +11,6 @@ describe Siba::SibaTasks do
11
11
  create_tasks "valid"
12
12
  end
13
13
 
14
-
15
14
  it "should get tasks" do
16
15
  siba_tasks = create_tasks "valid"
17
16
  siba_tasks.tasks["source"].must_be_instance_of Siba::SibaTask
@@ -54,6 +54,12 @@ describe Siba::Console do
54
54
  @console.parse [@unused, "--no-log"]
55
55
  Siba::SibaLogger.no_log.must_equal true
56
56
  end
57
+
58
+ it "should accept cur switch" do
59
+ @console.options["cur"].must_be_nil
60
+ @console.parse [@unused, "--current-source"]
61
+ @console.options["cur"].must_equal true
62
+ end
57
63
 
58
64
  it "should fail when using both log and no-log" do
59
65
  Siba::SibaLogger.no_log = false
@@ -7,12 +7,29 @@ describe Siba::Restore do
7
7
  @yml_path = File.expand_path('../yml', __FILE__)
8
8
  end
9
9
 
10
- it "should call restore" do
10
+ it "should call restore without loading source plugin" do
11
11
  Siba::LoggerPlug.close
12
12
  Siba::SibaLogger.quiet = true
13
13
  path_to_options_file = File.join @yml_path, "valid.yml"
14
- Siba::Restore.new.restore path_to_options_file
14
+ obj = Siba::Restore.new
15
+ obj.restore path_to_options_file, false
16
+ obj.current_source.must_equal false
15
17
  wont_log_from "warn"
18
+ must_log_msg "Loading destination", "debug"
19
+ wont_log_msg "Loading source", "debug"
20
+ Siba.tmp_dir_clean?.must_equal true
21
+ end
22
+
23
+ it "should call restore with loading source plugin" do
24
+ Siba::LoggerPlug.close
25
+ Siba::SibaLogger.quiet = true
26
+ path_to_options_file = File.join @yml_path, "valid.yml"
27
+ obj = Siba::Restore.new
28
+ obj.restore path_to_options_file, true
29
+ obj.current_source.must_equal true
30
+ wont_log_from "warn"
31
+ must_log_msg "Loading destination", "debug"
32
+ must_log_msg "Loading source", "debug"
16
33
  Siba.tmp_dir_clean?.must_equal true
17
34
  end
18
35
  end
@@ -5,75 +5,76 @@ require 'helper/require_unit'
5
5
  describe Siba::SibaLogger do
6
6
  before do
7
7
  @logger = Siba::SibaLogger.new "Test", nil
8
+ @cls = Siba::SibaLogger
8
9
  end
9
10
 
10
11
  describe "when access class" do
11
12
  it "must access quiet" do
12
- Siba::SibaLogger.quiet = true
13
- Siba::SibaLogger.quiet.must_equal true
13
+ @cls.quiet = true
14
+ @cls.quiet.must_equal true
14
15
  end
15
16
 
16
17
  it "must access verbose" do
17
- Siba::SibaLogger.verbose = true
18
- Siba::SibaLogger.verbose.must_equal true
18
+ @cls.verbose = true
19
+ @cls.verbose.must_equal true
19
20
  end
20
21
 
21
22
  it "must access no_log" do
22
- Siba::SibaLogger.no_log = true
23
- Siba::SibaLogger.no_log.must_equal true
23
+ @cls.no_log = true
24
+ @cls.no_log.must_equal true
24
25
  end
25
26
 
26
27
  it "must contain LogLevels" do
27
- Siba::SibaLogger::LogLevels.must_include "debug"
28
- Siba::SibaLogger::LogLevels.must_include "info"
29
- Siba::SibaLogger::LogLevels.must_include "warn"
30
- Siba::SibaLogger::LogLevels.must_include "error"
31
- Siba::SibaLogger::LogLevels.must_include "fatal"
32
- Siba::SibaLogger::LogLevels.must_include "unknown"
28
+ @cls::LogLevels.must_include "debug"
29
+ @cls::LogLevels.must_include "info"
30
+ @cls::LogLevels.must_include "warn"
31
+ @cls::LogLevels.must_include "error"
32
+ @cls::LogLevels.must_include "fatal"
33
+ @cls::LogLevels.must_include "unknown"
33
34
  end
34
35
 
35
36
  it "must check given log level" do
36
- Siba::SibaLogger.log_level?("info").must_equal true
37
- Siba::SibaLogger.log_level?("chelyabinsk").must_equal false
37
+ @cls.log_level?("info").must_equal true
38
+ @cls.log_level?("chelyabinsk").must_equal false
38
39
  end
39
40
 
40
41
  it "must have check_log_level method" do
41
- Siba::SibaLogger::check_log_level "info"
42
+ @cls::check_log_level "info"
42
43
  end
43
44
 
44
45
  it "check_log_level must raise for unsupported level" do
45
- -> { Siba::SibaLogger::check_log_level("weird") }.must_raise RuntimeError
46
+ -> { @cls::check_log_level("weird") }.must_raise RuntimeError
46
47
  end
47
48
 
48
49
  it "must return log level integer" do
49
- Siba::SibaLogger.level_to_i("debug").must_equal 0
50
- Siba::SibaLogger.level_to_i("info").must_equal 1
51
- Siba::SibaLogger.level_to_i("warn").must_equal 2
52
- Siba::SibaLogger.level_to_i("error").must_equal 3
53
- Siba::SibaLogger.level_to_i("fatal").must_equal 4
54
- Siba::SibaLogger.level_to_i("unknown").must_equal 5
50
+ @cls.level_to_i("debug").must_equal 0
51
+ @cls.level_to_i("info").must_equal 1
52
+ @cls.level_to_i("warn").must_equal 2
53
+ @cls.level_to_i("error").must_equal 3
54
+ @cls.level_to_i("fatal").must_equal 4
55
+ @cls.level_to_i("unknown").must_equal 5
55
56
  end
56
57
 
57
58
  it "must call count" do
58
- Siba::SibaLogger.count.must_equal 1
59
+ @cls.count.must_equal 1
59
60
  end
60
61
 
61
62
  it "count should work when logger is closed" do
62
63
  Siba::LoggerPlug.close
63
- Siba::SibaLogger.messages = nil
64
- Siba::SibaLogger.count("warn").must_equal 0
64
+ @cls.messages = nil
65
+ @cls.count("warn").must_equal 0
65
66
  end
66
67
  end
67
68
 
68
69
  describe "when access logger" do
69
70
  it "must initialize SibaLogger" do
70
71
  @logger.wont_be_nil
71
- Siba::SibaLogger.count("info").must_equal 1, "Must contain 'log start' message"
72
+ @cls.count("info").must_equal 1, "Must contain 'log start' message"
72
73
  end
73
74
 
74
75
  it "must initialize variables" do
75
- Siba::SibaLogger.messages.must_be_instance_of Array
76
- Siba::SibaLogger.count.must_equal 1
76
+ @cls.messages.must_be_instance_of Array
77
+ @cls.count.must_equal 1
77
78
  end
78
79
 
79
80
  it "should call log methods" do
@@ -90,13 +91,13 @@ describe Siba::SibaLogger do
90
91
  @logger.info "msg2"
91
92
  @logger.warn "msg3"
92
93
 
93
- Siba::SibaLogger.messages.size.must_equal 4
94
- Siba::SibaLogger.messages[1].must_be_instance_of Siba::LogMessage
95
- Siba::SibaLogger.messages[1].msg.must_equal "msg1"
96
- Siba::SibaLogger.messages[1].level.must_equal Siba::SibaLogger.level_to_i("debug")
97
- Siba::SibaLogger.messages[1].time.must_be_instance_of Time
98
- Siba::SibaLogger.messages[3].msg.must_equal "msg3"
99
- Siba::SibaLogger.messages[3].level.must_equal Siba::SibaLogger.level_to_i("warn")
94
+ @cls.messages.size.must_equal 4
95
+ @cls.messages[1].must_be_instance_of Siba::LogMessage
96
+ @cls.messages[1].msg.must_equal "msg1"
97
+ @cls.messages[1].level.must_equal @cls.level_to_i("debug")
98
+ @cls.messages[1].time.must_be_instance_of Time
99
+ @cls.messages[3].msg.must_equal "msg3"
100
+ @cls.messages[3].level.must_equal @cls.level_to_i("warn")
100
101
  end
101
102
 
102
103
  it "should raise when called missing method" do
@@ -104,16 +105,16 @@ describe Siba::SibaLogger do
104
105
  end
105
106
 
106
107
  it "should log on different levels" do
107
- Siba::SibaLogger::LogLevels.each do |level|
108
- Siba::SibaLogger.count(level, true).must_equal (level == "info" ? 1 : 0)
108
+ @cls::LogLevels.each do |level|
109
+ @cls.count(level, true).must_equal (level == "info" ? 1 : 0)
109
110
  end
110
111
 
111
- Siba::SibaLogger::LogLevels.each do |level|
112
+ @cls::LogLevels.each do |level|
112
113
  @logger.send(level,"#{level} message")
113
114
  end
114
115
 
115
- Siba::SibaLogger::LogLevels.each do |level|
116
- Siba::SibaLogger.count(level, true).must_equal (level == "info" ? 2 : 1)
116
+ @cls::LogLevels.each do |level|
117
+ @cls.count(level, true).must_equal (level == "info" ? 2 : 1)
117
118
  end
118
119
  end
119
120
 
@@ -129,33 +130,68 @@ describe Siba::SibaLogger do
129
130
  @logger.fatal "msg9"
130
131
  @logger.unknown "msg10"
131
132
 
132
- Siba::SibaLogger.count.must_equal 11
133
- Siba::SibaLogger.count("debug", false).must_equal 11
134
- Siba::SibaLogger.count("debug").must_equal 1
135
- Siba::SibaLogger.count("info", false).must_equal 10
136
- Siba::SibaLogger.count("info").must_equal 3
137
- Siba::SibaLogger.count("warn", false).must_equal 7
138
- Siba::SibaLogger.count("error", false).must_equal 6
139
- Siba::SibaLogger.count("error").must_equal 3
140
- Siba::SibaLogger.count("fatal", false).must_equal 3
141
- Siba::SibaLogger.count("fatal", true).must_equal 2
133
+ @cls.count.must_equal 11
134
+ @cls.count("debug", false).must_equal 11
135
+ @cls.count("debug").must_equal 1
136
+ @cls.count("info", false).must_equal 10
137
+ @cls.count("info").must_equal 3
138
+ @cls.count("warn", false).must_equal 7
139
+ @cls.count("error", false).must_equal 6
140
+ @cls.count("error").must_equal 3
141
+ @cls.count("fatal", false).must_equal 3
142
+ @cls.count("fatal", true).must_equal 2
143
+ end
144
+
145
+ it "should count messages in logs" do
146
+ @logger.debug "debug1"
147
+ @logger.debug "shared"
148
+ @logger.info "shared"
149
+ @logger.info "info1"
150
+ @logger.info "info2"
151
+ @logger.error "shared"
152
+ @logger.error "err1"
153
+ @logger.error "err2"
154
+ @logger.error "err3"
155
+
156
+ @cls.count_messages("debug1").must_equal 1
157
+ @cls.count_messages("debug1", "debug").must_equal 1
158
+ @cls.count_messages("debug", "debug").must_equal 1
159
+ @cls.count_messages("hola", "debug").must_equal 0
160
+ @cls.count_messages("hola").must_equal 0
161
+
162
+ @cls.count_messages("info1").must_equal 1
163
+ @cls.count_messages("info2").must_equal 1
164
+ @cls.count_messages("info").must_equal 2
165
+ @cls.count_messages("info", "debug").must_equal 0
166
+ @cls.count_messages("info", "debug", false).must_equal 2
167
+ @cls.count_messages("info", "info").must_equal 2
168
+ @cls.count_messages("info", "warn").must_equal 0
169
+
170
+ @cls.count_messages("err").must_equal 3
171
+ @cls.count_messages("err", "error").must_equal 3
172
+ @cls.count_messages("err1", "error").must_equal 1
173
+
174
+ @cls.count_messages("shared", "info").must_equal 1
175
+ @cls.count_messages("shared", "debug").must_equal 1
176
+ @cls.count_messages("shared", "error").must_equal 1
177
+ @cls.count_messages("shared").must_equal 3
142
178
  end
143
179
 
144
180
  it "must call log_exception" do
145
181
  @logger.log_exception Exception.new "hello"
146
- Siba::SibaLogger.count("debug",true).must_equal 1
182
+ @cls.count("debug",true).must_equal 1
147
183
  end
148
184
 
149
185
  it "must call log backtrace with_exception" do
150
186
  ex = Exception.new "hello"
151
187
  ex.set_backtrace ["one","two","tree"]
152
188
  @logger.log_exception ex
153
- Siba::SibaLogger.count("debug",true).must_equal 2
189
+ @cls.count("debug",true).must_equal 2
154
190
  end
155
191
 
156
192
  it "must close logger" do
157
193
  @logger.close
158
- Siba::SibaLogger.count.must_equal 2
194
+ @cls.count.must_equal 2
159
195
  ->{@logger.info "hi"}.must_raise Siba::Error, "Error if trying to use closed log"
160
196
  end
161
197
 
@@ -168,7 +204,7 @@ describe Siba::SibaLogger do
168
204
  it "should access :show_finish_message" do
169
205
  @logger.show_finish_message = false
170
206
  @logger.close
171
- Siba::SibaLogger.messages.size.must_equal 1
207
+ @cls.messages.size.must_equal 1
172
208
  end
173
209
  end
174
210
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: siba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &74959570 !ruby/object:Gem::Requirement
16
+ requirement: &82437310 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.10'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *74959570
24
+ version_requirements: *82437310
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &77393270 !ruby/object:Gem::Requirement
27
+ requirement: &82437080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.9'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *77393270
35
+ version_requirements: *82437080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-minitest
38
- requirement: &77393040 !ruby/object:Gem::Requirement
38
+ requirement: &82436850 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0.4'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *77393040
46
+ version_requirements: *82436850
47
47
  description: ! 'SIBA is a backup and restore utility. It implements daily backup rotation
48
48
  scheme. It retains full year history of backups by keeping 23 files in total: for
49
49
  the last 6 days, 5 weeks and 12 months. Backups are archived and encrypted. Various