1import elasticsearch 2import curator 3import os 4import json 5import string, random, tempfile 6from click import testing as clicktest 7from mock import patch, Mock 8from datetime import datetime, timedelta 9 10from . import CuratorTestCase 11from . import testvars as testvars 12 13import logging 14logger = logging.getLogger(__name__) 15 16host, port = os.environ.get('TEST_ES_SERVER', 'localhost:9200').split(':') 17port = int(port) if port else 9200 18 19class TestActionFileSnapshot(CuratorTestCase): 20 def test_snapshot(self): 21 self.create_indices(5) 22 self.create_repository() 23 snap_name = 'snapshot1' 24 self.write_config( 25 self.args['configfile'], testvars.client_config.format(host, port)) 26 self.write_config(self.args['actionfile'], 27 testvars.snapshot_test.format(self.args['repository'], snap_name, 1, 30)) 28 test = clicktest.CliRunner() 29 _ = test.invoke( 30 curator.cli, 31 [ 32 '--config', self.args['configfile'], 33 self.args['actionfile'] 34 ], 35 ) 36 snapshot = curator.get_snapshot( 37 self.client, self.args['repository'], '_all' 38 ) 39 self.assertEqual(1, len(snapshot['snapshots'])) 40 self.assertEqual(snap_name, snapshot['snapshots'][0]['snapshot']) 41 def test_snapshot_datemath(self): 42 self.create_indices(5) 43 self.create_repository() 44 snap_name = '<snapshot-{now-1d/d}>' 45 snap_name_parsed = u'snapshot-{0}'.format((datetime.utcnow()-timedelta(days=1)).strftime('%Y.%m.%d')) 46 self.write_config( 47 self.args['configfile'], testvars.client_config.format(host, port)) 48 self.write_config(self.args['actionfile'], 49 testvars.snapshot_test.format(self.args['repository'], snap_name, 1, 30)) 50 test = clicktest.CliRunner() 51 _ = test.invoke( 52 curator.cli, 53 [ 54 '--config', self.args['configfile'], 55 self.args['actionfile'] 56 ], 57 ) 58 snapshot = curator.get_snapshot( 59 self.client, self.args['repository'], '_all' 60 ) 61 self.assertEqual(1, len(snapshot['snapshots'])) 62 self.assertEqual(snap_name_parsed, snapshot['snapshots'][0]['snapshot']) 63 def test_snapshot_ignore_empty_list(self): 64 self.create_indices(5) 65 self.create_repository() 66 snap_name = 'snapshot1' 67 self.write_config( 68 self.args['configfile'], testvars.client_config.format(host, port)) 69 self.write_config(self.args['actionfile'], 70 testvars.test_682.format(self.args['repository'], snap_name, True, 1, 30)) 71 test = clicktest.CliRunner() 72 _ = test.invoke( 73 curator.cli, 74 [ 75 '--config', self.args['configfile'], 76 self.args['actionfile'] 77 ], 78 ) 79 snapshot = curator.get_snapshot( 80 self.client, self.args['repository'], '_all' 81 ) 82 self.assertEqual(0, len(snapshot['snapshots'])) 83 self.assertEquals(0, len(curator.get_indices(self.client))) 84 def test_snapshot_do_not_ignore_empty_list(self): 85 self.create_indices(5) 86 self.create_repository() 87 snap_name = 'snapshot1' 88 self.write_config( 89 self.args['configfile'], testvars.client_config.format(host, port)) 90 self.write_config(self.args['actionfile'], 91 testvars.test_682.format(self.args['repository'], snap_name, False, 1, 30)) 92 test = clicktest.CliRunner() 93 _ = test.invoke( 94 curator.cli, 95 [ 96 '--config', self.args['configfile'], 97 self.args['actionfile'] 98 ], 99 ) 100 snapshot = curator.get_snapshot( 101 self.client, self.args['repository'], '_all' 102 ) 103 self.assertEqual(0, len(snapshot['snapshots'])) 104 self.assertEquals(5, len(curator.get_indices(self.client))) 105 def test_no_repository(self): 106 self.create_indices(5) 107 self.write_config( 108 self.args['configfile'], testvars.client_config.format(host, port)) 109 self.write_config(self.args['actionfile'], 110 testvars.snapshot_test.format(' ', 'snap_name', 1, 30)) 111 test = clicktest.CliRunner() 112 _ = test.invoke( 113 curator.cli, 114 [ 115 '--config', self.args['configfile'], 116 self.args['actionfile'] 117 ], 118 ) 119 self.assertEqual(1, _.exit_code) 120 def test_extra_option(self): 121 self.create_indices(5) 122 self.write_config( 123 self.args['configfile'], testvars.client_config.format(host, port)) 124 self.write_config(self.args['actionfile'], 125 testvars.bad_option_proto_test.format('snapshot')) 126 test = clicktest.CliRunner() 127 _ = test.invoke( 128 curator.cli, 129 [ 130 '--config', self.args['configfile'], 131 self.args['actionfile'] 132 ], 133 ) 134 self.assertEqual(1, _.exit_code) 135 136class TestCLISnapshot(CuratorTestCase): 137 def test_snapshot(self): 138 self.create_indices(5) 139 self.create_repository() 140 snap_name = 'snapshot1' 141 args = self.get_runner_args() 142 args += [ 143 '--config', self.args['configfile'], 144 'snapshot', 145 '--repository', self.args['repository'], 146 '--name', snap_name, 147 '--wait_interval', '1', 148 '--max_wait', '30', 149 '--filter_list', '{"filtertype":"none"}', 150 ] 151 self.assertEqual(0, self.run_subprocess(args, logname='TestCLISnapshot.test_snapshot')) 152 snapshot = curator.get_snapshot(self.client, self.args['repository'], '_all') 153 self.assertEqual(1, len(snapshot['snapshots'])) 154 self.assertEqual(snap_name, snapshot['snapshots'][0]['snapshot'])