@@ -25,70 +25,97 @@ func TestDumpCmd(t *testing.T) {
2525 wantErr bool
2626 expectedDumpOptions core.DumpOptions
2727 expectedTimerOptions core.TimerOptions
28+ expectedPruneOptions * core.PruneOptions
2829 }{
29- {"missing server and target options" , []string {"" }, "" , true , core.DumpOptions {}, core.TimerOptions {}},
30- {"invalid target URL" , []string {"--server" , "abc" , "--target" , "def" }, "" , true , core.DumpOptions {DBConn : database.Connection {Host : "abc" }}, core.TimerOptions {}},
30+ // invalid ones
31+ {"missing server and target options" , []string {"" }, "" , true , core.DumpOptions {}, core.TimerOptions {}, nil },
32+ {"invalid target URL" , []string {"--server" , "abc" , "--target" , "def" }, "" , true , core.DumpOptions {DBConn : database.Connection {Host : "abc" }}, core.TimerOptions {}, nil },
33+
34+ // file URL
3135 {"file URL" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" }, "" , false , core.DumpOptions {
3236 Targets : []storage.Storage {file .New (* fileTargetURL )},
3337 MaxAllowedPacket : defaultMaxAllowedPacket ,
3438 Compressor : & compression.GzipCompressor {},
3539 DBConn : database.Connection {Host : "abc" },
36- }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin }},
40+ }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin }, nil },
41+ {"file URL with prune" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--retention" , "1h" }, "" , false , core.DumpOptions {
42+ Targets : []storage.Storage {file .New (* fileTargetURL )},
43+ MaxAllowedPacket : defaultMaxAllowedPacket ,
44+ Compressor : & compression.GzipCompressor {},
45+ DBConn : database.Connection {Host : "abc" },
46+ }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin }, & core.PruneOptions {Targets : []storage.Storage {file .New (* fileTargetURL )}, Retention : "1h" }},
47+
48+ // config file
49+ {"config file" , []string {"--config-file" , "testdata/config.yml" }, "" , false , core.DumpOptions {
50+ Targets : []storage.Storage {file .New (* fileTargetURL )},
51+ MaxAllowedPacket : defaultMaxAllowedPacket ,
52+ Compressor : & compression.GzipCompressor {},
53+ DBConn : database.Connection {Host : "abcd" , Port : 3306 , User : "user2" , Pass : "xxxx2" },
54+ }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin }, & core.PruneOptions {Targets : []storage.Storage {file .New (* fileTargetURL )}, Retention : "1h" }},
55+
56+ // timer options
3757 {"once flag" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--once" }, "" , false , core.DumpOptions {
3858 Targets : []storage.Storage {file .New (* fileTargetURL )},
3959 MaxAllowedPacket : defaultMaxAllowedPacket ,
4060 Compressor : & compression.GzipCompressor {},
4161 DBConn : database.Connection {Host : "abc" },
42- }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin , Once : true } },
62+ }, core.TimerOptions {Once : true , Frequency : defaultFrequency , Begin : defaultBegin }, nil },
4363 {"cron flag" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--cron" , "0 0 * * *" }, "" , false , core.DumpOptions {
4464 Targets : []storage.Storage {file .New (* fileTargetURL )},
4565 MaxAllowedPacket : defaultMaxAllowedPacket ,
4666 Compressor : & compression.GzipCompressor {},
4767 DBConn : database.Connection {Host : "abc" },
48- }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin , Cron : "0 0 * * *" }},
68+ }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin , Cron : "0 0 * * *" }, nil },
4969 {"begin flag" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--begin" , "1234" }, "" , false , core.DumpOptions {
5070 Targets : []storage.Storage {file .New (* fileTargetURL )},
5171 MaxAllowedPacket : defaultMaxAllowedPacket ,
5272 Compressor : & compression.GzipCompressor {},
5373 DBConn : database.Connection {Host : "abc" },
54- }, core.TimerOptions {Frequency : defaultFrequency , Begin : "1234" }},
74+ }, core.TimerOptions {Frequency : defaultFrequency , Begin : "1234" }, nil },
5575 {"frequency flag" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--frequency" , "10" }, "" , false , core.DumpOptions {
5676 Targets : []storage.Storage {file .New (* fileTargetURL )},
5777 MaxAllowedPacket : defaultMaxAllowedPacket ,
5878 Compressor : & compression.GzipCompressor {},
5979 DBConn : database.Connection {Host : "abc" },
60- }, core.TimerOptions {Frequency : 10 , Begin : defaultBegin }},
61- {"config file" , []string {"--config-file" , "testdata/config.yml" }, "" , false , core.DumpOptions {
62- Targets : []storage.Storage {file .New (* fileTargetURL )},
63- MaxAllowedPacket : defaultMaxAllowedPacket ,
64- Compressor : & compression.GzipCompressor {},
65- DBConn : database.Connection {Host : "abc" , Port : 3306 , User : "user" , Pass : "xxxx" },
66- }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin }},
67- {"incompatible flags: once/cron" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--once" , "--cron" , "0 0 * * *" }, "" , true , core.DumpOptions {}, core.TimerOptions {}},
68- {"incompatible flags: once/begin" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--once" , "--begin" , "1234" }, "" , true , core.DumpOptions {}, core.TimerOptions {}},
69- {"incompatible flags: once/frequency" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--once" , "--frequency" , "10" }, "" , true , core.DumpOptions {}, core.TimerOptions {}},
70- {"incompatible flags: cron/begin" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--cron" , "0 0 * * *" , "--begin" , "1234" }, "" , true , core.DumpOptions {}, core.TimerOptions {}},
71- {"incompatible flags: cron/frequency" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--cron" , "0 0 * * *" , "--frequency" , "10" }, "" , true , core.DumpOptions {}, core.TimerOptions {}},
80+ }, core.TimerOptions {Frequency : 10 , Begin : defaultBegin }, nil },
81+ {"incompatible flags: once/cron" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--once" , "--cron" , "0 0 * * *" }, "" , true , core.DumpOptions {}, core.TimerOptions {}, nil },
82+ {"incompatible flags: once/begin" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--once" , "--begin" , "1234" }, "" , true , core.DumpOptions {}, core.TimerOptions {}, nil },
83+ {"incompatible flags: once/frequency" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--once" , "--frequency" , "10" }, "" , true , core.DumpOptions {}, core.TimerOptions {}, nil },
84+ {"incompatible flags: cron/begin" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--cron" , "0 0 * * *" , "--begin" , "1234" }, "" , true , core.DumpOptions {}, core.TimerOptions {}, nil },
85+ {"incompatible flags: cron/frequency" , []string {"--server" , "abc" , "--target" , "file:///foo/bar" , "--cron" , "0 0 * * *" , "--frequency" , "10" }, "" , true , core.DumpOptions {
86+ DBConn : database.Connection {Host : "abcd" , Port : 3306 , User : "user2" , Pass : "xxxx2" },
87+ }, core.TimerOptions {Frequency : defaultFrequency , Begin : defaultBegin }, & core.PruneOptions {Targets : []storage.Storage {file .New (* fileTargetURL )}, Retention : "1h" }},
7288 }
7389
7490 for _ , tt := range tests {
7591 t .Run (tt .name , func (t * testing.T ) {
7692 m := newMockExecs ()
77- m .On ("timerDump " , mock .MatchedBy (func (dumpOpts core.DumpOptions ) bool {
93+ m .On ("dump " , mock .MatchedBy (func (dumpOpts core.DumpOptions ) bool {
7894 diff := deep .Equal (dumpOpts , tt .expectedDumpOptions )
7995 if diff == nil {
8096 return true
8197 }
8298 t .Errorf ("dumpOpts compare failed: %v" , diff )
8399 return false
84- }), mock .MatchedBy (func (timerOpts core.TimerOptions ) bool {
100+ })).Return (nil )
101+ m .On ("timer" , mock .MatchedBy (func (timerOpts core.TimerOptions ) bool {
85102 diff := deep .Equal (timerOpts , tt .expectedTimerOptions )
86103 if diff == nil {
87104 return true
88105 }
89106 t .Errorf ("timerOpts compare failed: %v" , diff )
90107 return false
91108 })).Return (nil )
109+ if tt .expectedPruneOptions != nil {
110+ m .On ("prune" , mock .MatchedBy (func (pruneOpts core.PruneOptions ) bool {
111+ diff := deep .Equal (pruneOpts , * tt .expectedPruneOptions )
112+ if diff == nil {
113+ return true
114+ }
115+ t .Errorf ("pruneOpts compare failed: %v" , diff )
116+ return false
117+ })).Return (nil )
118+ }
92119
93120 cmd , err := rootCmd (m )
94121 if err != nil {
0 commit comments