dk-dumpdb 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,103 @@
1
+ require 'assert'
2
+ require 'dk-dumpdb/db'
3
+
4
+ class Dk::Dumpdb::Db
5
+
6
+ class UnitTests < Assert::Context
7
+ desc "Dk::Dumpdb::Db"
8
+ setup do
9
+ @db_class = Dk::Dumpdb::Db
10
+ end
11
+ subject{ @db_class }
12
+
13
+ should "know its default value" do
14
+ assert_equal '', DEFAULT_VALUE
15
+ end
16
+
17
+ end
18
+
19
+ class InitTests < UnitTests
20
+ desc "when init"
21
+ setup do
22
+ @dump_file_name = Factory.file_name
23
+ @host = Factory.string
24
+ @port = Factory.integer
25
+ @user = Factory.string
26
+ @pw = Factory.string
27
+ @db_name = Factory.string
28
+ @output_root = Factory.dir_path
29
+ @custom_value = Factory.string
30
+
31
+ @current_time = Factory.time
32
+ Assert.stub(Time, :now){ @current_time }
33
+
34
+ @db = @db_class.new(@dump_file_name, {
35
+ :host => @host,
36
+ :port => @port,
37
+ :user => @user,
38
+ :pw => @pw,
39
+ :db => @db_name,
40
+ :output_root => @output_root,
41
+ :custom_value => @custom_value
42
+ })
43
+ end
44
+ subject{ @db }
45
+
46
+ should have_imeths :host, :port, :user, :pw, :db
47
+ should have_imeths :output_root, :output_dir, :dump_file
48
+ should have_imeths :to_hash
49
+
50
+ should "know its attributes" do
51
+ assert_equal @host, subject.host
52
+ assert_equal @port, subject.port
53
+ assert_equal @user, subject.user
54
+ assert_equal @pw, subject.pw
55
+ assert_equal @db_name, subject.db
56
+ assert_equal @output_root, subject.output_root
57
+
58
+ exp = File.join(@output_root, "#{@host}__#{@db_name}__#{@current_time.to_f}")
59
+ assert_equal exp, subject.output_dir
60
+
61
+ exp = File.join(subject.output_dir, @dump_file_name)
62
+ assert_equal exp, subject.dump_file
63
+ end
64
+
65
+ should "allow custom attributes" do
66
+ assert_true subject.respond_to?(:custom_value)
67
+ assert_equal @custom_value, subject.custom_value
68
+ end
69
+
70
+ should "default its attributes" do
71
+ db = @db_class.new
72
+
73
+ assert_equal DEFAULT_VALUE, db.host
74
+ assert_equal DEFAULT_VALUE, db.port
75
+ assert_equal DEFAULT_VALUE, db.user
76
+ assert_equal DEFAULT_VALUE, db.pw
77
+ assert_equal DEFAULT_VALUE, db.db
78
+ assert_equal DEFAULT_VALUE, db.output_root
79
+ assert_equal @current_time.to_f.to_s, db.output_dir
80
+
81
+ exp = File.join(db.output_dir, 'dump.output')
82
+ assert_equal exp, db.dump_file
83
+ end
84
+
85
+ should "know if it is equal to another db" do
86
+ equal_db = @db_class.new(@dump_file_name, {
87
+ :host => @host,
88
+ :port => @port,
89
+ :user => @user,
90
+ :pw => @pw,
91
+ :db => @db_name,
92
+ :output_root => @output_root,
93
+ :custom_value => @custom_value
94
+ })
95
+ assert_equal subject, equal_db
96
+
97
+ not_equal_db = @db_class.new(@dump_file_name, {})
98
+ assert_not_equal subject, not_equal_db
99
+ end
100
+
101
+ end
102
+
103
+ end
@@ -0,0 +1,16 @@
1
+ require 'assert'
2
+
3
+ module Dk::Dumpdb
4
+
5
+ class UnitTests < Assert::Context
6
+ desc "Dk::Dumpdb"
7
+ subject{ Dk::Dumpdb }
8
+
9
+ should "know its scp args param name" do
10
+ exp = "dk-dumpdb_scp_args"
11
+ assert_equal exp, subject::SCP_ARGS_PARAM_NAME
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,165 @@
1
+ require 'assert'
2
+ require 'dk-dumpdb/script'
3
+
4
+ require 'much-plugin'
5
+ require 'dk-dumpdb/config'
6
+ require 'dk-dumpdb/task'
7
+
8
+ module Dk::Dumpdb::Script
9
+
10
+ class UnitTests < Assert::Context
11
+ desc "Dk::Dumpdb::Script"
12
+ subject{ Dk::Dumpdb::Script }
13
+
14
+ should "use much-plugin" do
15
+ assert_includes MuchPlugin, subject
16
+ end
17
+
18
+ end
19
+
20
+ class MixinTests < UnitTests
21
+ desc "mixin"
22
+ setup do
23
+ @config_proc = Proc.new do
24
+ ssh{ "user@host" }
25
+
26
+ dump_file{ "dump.bz2" }
27
+
28
+ source do
29
+ { :user => 'admin',
30
+ :pw => 'secret',
31
+ :db => 'myapp_db',
32
+ :output_root => '/some/source/dir'
33
+ }
34
+ end
35
+ target do
36
+ { :host => 'localhost',
37
+ :user => 'admin',
38
+ :db => 'myapp_db',
39
+ :output_root => '/some/target/dir'
40
+ }
41
+ end
42
+
43
+ dump{ "mysqldump -u :user -p\":pw\" :db | bzip2 > :dump_file" }
44
+ restore{ "bunzip2 -c :dump_file | mysql -u :user -p\":pw\" :db" }
45
+ end
46
+ @script_class = Class.new do
47
+ include Dk::Dumpdb::Script
48
+ end
49
+ end
50
+ subject{ @script_class }
51
+
52
+ should have_imeths :config_blocks, :config, :task_description, :task_desc
53
+
54
+ should "know its config blocks" do
55
+ assert_empty subject.config_blocks
56
+
57
+ subject.config(&@config_proc)
58
+ assert_equal [@config_proc], subject.config_blocks
59
+ end
60
+
61
+ should "define a Task for the script class" do
62
+ task_class = @script_class::Task
63
+
64
+ assert_includes Dk::Dumpdb::Task, task_class
65
+ assert_equal @script_class, task_class.script_class
66
+ end
67
+
68
+ should "set a description on its Task" do
69
+ assert_nil subject.task_description
70
+ assert_nil subject.task_desc
71
+
72
+ exp = Factory.string
73
+ subject.task_description exp
74
+ assert_equal exp, subject.task_description
75
+ assert_equal exp, subject.task_desc
76
+
77
+ exp = Factory.string
78
+ subject.task_desc exp
79
+ assert_equal exp, subject.task_description
80
+ assert_equal exp, subject.task_desc
81
+ end
82
+
83
+ end
84
+
85
+ class InitTests < MixinTests
86
+ desc "when init"
87
+ setup do
88
+ now = Factory.time
89
+ Assert.stub(Time, :now){ now }
90
+
91
+ @script_class.config(&@config_proc)
92
+ @script = @script_class.new
93
+ end
94
+ subject{ @script }
95
+
96
+ should have_readers :params
97
+ should have_imeths :config
98
+ should have_imeths :ssh, :dump_file, :source, :target, :copy_dump_cmd_args
99
+ should have_imeths :dump_cmds, :restore_cmds
100
+ should have_imeths :source_dump_file, :target_dump_file
101
+ should have_imeths :source_hash, :target_hash
102
+ should have_imeths :ssh?
103
+ should have_imeths :dump_cmd, :restore_cmd
104
+
105
+ should "know its params" do
106
+ exp = {}
107
+ assert_equal exp, subject.params
108
+
109
+ params = { Factory.string => Factory.string }
110
+ script = @script_class.new(params)
111
+ assert_equal params, script.params
112
+ end
113
+
114
+ should "know its config" do
115
+ assert_instance_of Dk::Dumpdb::Config, subject.config
116
+ end
117
+
118
+ should "know its config values" do
119
+ c = subject.config
120
+ assert_equal c.ssh.value(subject), subject.ssh
121
+ assert_equal c.dump_file.value(subject), subject.dump_file
122
+ assert_equal c.source.value(subject), subject.source
123
+ assert_equal c.target.value(subject), subject.target
124
+
125
+ assert_equal c.copy_dump_cmd_args.value(subject), subject.copy_dump_cmd_args
126
+
127
+ assert_equal c.dump_cmds.value(subject), subject.dump_cmds
128
+ assert_equal c.restore_cmds.value(subject), subject.restore_cmds
129
+ end
130
+
131
+ should "demeter its source/target" do
132
+ assert_equal subject.source.dump_file, subject.source_dump_file
133
+ assert_equal subject.target.dump_file, subject.target_dump_file
134
+ assert_equal subject.source.to_hash, subject.source_hash
135
+ assert_equal subject.target.to_hash, subject.target_hash
136
+ end
137
+
138
+ should "know if it should use ssh or not" do
139
+ assert_true subject.ssh?
140
+
141
+ none = Class.new{ include Dk::Dumpdb::Script }
142
+ assert_false none.new.ssh?
143
+
144
+ empty = Class.new do
145
+ include Dk::Dumpdb::Script
146
+ config do
147
+ ssh{ '' }
148
+ end
149
+ end
150
+ assert_false empty.new.ssh?
151
+ end
152
+
153
+ should "build dump/restore cmd strs" do
154
+ cmd_str = Factory.string
155
+
156
+ exp = subject.config.dump_cmd(subject, &proc{ cmd_str })
157
+ assert_equal exp, subject.dump_cmd(&proc{ cmd_str })
158
+
159
+ exp = subject.config.restore_cmd(subject, &proc{ cmd_str })
160
+ assert_equal exp, subject.restore_cmd(&proc{ cmd_str })
161
+ end
162
+
163
+ end
164
+
165
+ end
@@ -0,0 +1,57 @@
1
+ require 'assert'
2
+ require 'dk-dumpdb/task/copy_dump'
3
+
4
+ require 'test/support/task/internal_task'
5
+
6
+ class Dk::Dumpdb::Task::CopyDump
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "Dk::Dumpdb::Task::CopyDump"
10
+ setup do
11
+ @task_class = Dk::Dumpdb::Task::CopyDump
12
+ end
13
+ subject{ @task_class}
14
+
15
+ should "be an internal task" do
16
+ assert_includes Dk::Dumpdb::Task::InternalTask, subject
17
+ end
18
+
19
+ should "know its description" do
20
+ exp = "(dk-dumpdb) copy the given script's dump file from source to target"
21
+ assert_equal exp, subject.description
22
+ end
23
+
24
+ end
25
+
26
+ class InitTests < UnitTests
27
+ include Dk::Dumpdb::Task::InternalTask::TestHelpers
28
+
29
+ desc "when init"
30
+ setup do
31
+ now = Factory.time
32
+ Assert.stub(Time, :now){ now }
33
+
34
+ set_dk_dumpdb_script_param
35
+ @runner = test_runner(@task_class, :params => @params)
36
+ end
37
+
38
+ end
39
+
40
+ class RunTests < InitTests
41
+ desc "and run"
42
+ setup do
43
+ @runner.run
44
+ end
45
+ subject{ @runner }
46
+
47
+ should "run a cmd to copy the dump" do
48
+ assert_equal 1, subject.runs.size
49
+ cp_dump = subject.runs.first
50
+
51
+ exp = @params['script'].copy_dump_cmd_args
52
+ assert_match /#{exp}\Z/, cp_dump.cmd_str
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,58 @@
1
+ require 'assert'
2
+ require 'dk-dumpdb/task/dump'
3
+
4
+ require 'test/support/task/internal_task'
5
+
6
+ class Dk::Dumpdb::Task::Dump
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "Dk::Dumpdb::Task::Dump"
10
+ setup do
11
+ @task_class = Dk::Dumpdb::Task::Dump
12
+ end
13
+ subject{ @task_class}
14
+
15
+ should "be an internal task" do
16
+ assert_includes Dk::Dumpdb::Task::InternalTask, subject
17
+ end
18
+
19
+ should "know its description" do
20
+ exp = "(dk-dumpdb) run the given script's dump cmds"
21
+ assert_equal exp, subject.description
22
+ end
23
+
24
+ end
25
+
26
+ class InitTests < UnitTests
27
+ include Dk::Dumpdb::Task::InternalTask::TestHelpers
28
+
29
+ desc "when init"
30
+ setup do
31
+ now = Factory.time
32
+ Assert.stub(Time, :now){ now }
33
+
34
+ @dump_cmds = dump_cmds = Factory.integer(3).times.map{ Factory.string }
35
+ set_dk_dumpdb_script_param do
36
+ dump_cmds.each do |cmd_str|
37
+ dump{ cmd_str }
38
+ end
39
+ end
40
+ @runner = test_runner(@task_class, :params => @params)
41
+ end
42
+
43
+ end
44
+
45
+ class RunTests < InitTests
46
+ desc "and run"
47
+ setup do
48
+ @runner.run
49
+ end
50
+ subject{ @runner }
51
+
52
+ should "run all dump cmds" do
53
+ assert_equal @dump_cmds, subject.runs.map(&:cmd_str)
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,106 @@
1
+ require 'assert'
2
+ require 'dk-dumpdb/task/internal_task'
3
+
4
+ require 'dk'
5
+ require 'dk/task'
6
+ require 'much-plugin'
7
+ require 'dk-dumpdb'
8
+
9
+ module Dk::Dumpdb::Task::InternalTask
10
+
11
+ class UnitTests < Assert::Context
12
+ desc "Dk::Dumpdb::Task::InternalTask"
13
+ setup do
14
+ @module = Dk::Dumpdb::Task::InternalTask
15
+ end
16
+ subject{ @module }
17
+
18
+ should "use much-plugin" do
19
+ assert_includes MuchPlugin, subject
20
+ end
21
+
22
+ end
23
+
24
+ class MixinTests < UnitTests
25
+ desc "mixin"
26
+ setup do
27
+ @task_class = Class.new do
28
+ include Dk::Dumpdb::Task::InternalTask
29
+ attr_writer :cp_cmd_args
30
+ def run!
31
+ source_cmd! "a source cmd"
32
+ copy_cmd! @cp_cmd_args
33
+ target_cmd! "a target cmd"
34
+ end
35
+ end
36
+ end
37
+ subject{ @task_class }
38
+
39
+ should "be a Dk task" do
40
+ assert_includes Dk::Task, subject
41
+ end
42
+
43
+ end
44
+
45
+ class InitTests < MixinTests
46
+ include Dk::Task::TestHelpers
47
+
48
+ desc "when init"
49
+ setup do
50
+ @cp_args = "#{Factory.file_path} #{Factory.file_path}"
51
+ @params = {}
52
+ end
53
+
54
+ should "run source/target/copy cmds as local Dk cmds if not an ssh script" do
55
+ @params['script'] = Factory.script
56
+ runner = test_runner(@task_class, :params => @params)
57
+ task = runner.task
58
+
59
+ task.cp_cmd_args = @cp_args
60
+ runner.run
61
+ assert_equal 3, runner.runs.size
62
+
63
+ source_cmd, copy_cmd, target_cmd = runner.runs
64
+ assert_false source_cmd.ssh?
65
+ assert_false target_cmd.ssh?
66
+ assert_false copy_cmd.ssh?
67
+
68
+ assert_equal "a source cmd", source_cmd.cmd_str
69
+ assert_equal "a target cmd", target_cmd.cmd_str
70
+
71
+ exp = "cp #{@cp_args}"
72
+ assert_equal exp, copy_cmd.cmd_str
73
+ end
74
+
75
+ should "run source/copy cmds as remote Dk cmds if an ssh script" do
76
+ @params['script'] = Factory.script{ ssh{ "hostname" } }
77
+
78
+ if Factory.boolean
79
+ @params[Dk::Dumpdb::SCP_ARGS_PARAM_NAME] = Factory.string
80
+ end
81
+
82
+ runner = test_runner(@task_class, :params => @params)
83
+ task = runner.task
84
+
85
+ task.cp_cmd_args = @cp_args
86
+ runner.run
87
+ assert_equal 3, runner.runs.size
88
+
89
+ source_ssh, copy_cmd, target_cmd = runner.runs
90
+ assert_true source_ssh.ssh?
91
+ assert_false target_cmd.ssh?
92
+ assert_false copy_cmd.ssh?
93
+
94
+ assert_equal [@params['script'].ssh], source_ssh.cmd_opts[:hosts]
95
+
96
+ assert_equal "a source cmd", source_ssh.cmd_str
97
+ assert_equal "a target cmd", target_cmd.cmd_str
98
+
99
+ exp = "scp #{@params[Dk::Dumpdb::SCP_ARGS_PARAM_NAME]} " \
100
+ "#{@params['script'].ssh}:#{@cp_args}"
101
+ assert_equal exp, copy_cmd.cmd_str
102
+ end
103
+
104
+ end
105
+
106
+ end