| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314 | import loggingimport pytestfrom flexmock import flexmockfrom borgmatic.hooks.data_source import mariadb as moduledef test_parse_extra_options_passes_through_empty_options():    assert module.parse_extra_options('') == ((), None)def test_parse_extra_options_with_defaults_extra_file_removes_and_and_parses_out_filename():    assert module.parse_extra_options('--defaults-extra-file=extra.cnf --skip-ssl') == (        ('--skip-ssl',),        'extra.cnf',    )def test_parse_extra_options_without_defaults_extra_file_passes_through_options():    assert module.parse_extra_options('--skip-ssl --and=stuff') == (        ('--skip-ssl', '--and=stuff'),        None,    )def test_make_defaults_file_pipe_without_username_or_password_bails():    flexmock(module.os).should_receive('pipe').never()    assert module.make_defaults_file_options(username=None, password=None) == ()def test_make_defaults_file_option_with_username_and_password_writes_them_to_file_descriptor():    read_descriptor = 99    write_descriptor = flexmock()    flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)    flexmock(module.os).should_receive('write').with_args(        write_descriptor, b'[client]\nuser=root\npassword="trustsome1"'    ).once()    flexmock(module.os).should_receive('close')    flexmock(module.os).should_receive('set_inheritable')    assert module.make_defaults_file_options(username='root', password='trustsome1') == (        '--defaults-extra-file=/dev/fd/99',    )def test_make_defaults_file_escapes_password_containing_backslash():    read_descriptor = 99    write_descriptor = flexmock()    flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)    flexmock(module.os).should_receive('write').with_args(        write_descriptor, b'[client]\nuser=root\n' + br'password="trust\\nsome1"'    ).once()    flexmock(module.os).should_receive('close')    flexmock(module.os).should_receive('set_inheritable')    assert module.make_defaults_file_options(username='root', password=r'trust\nsome1') == (        '--defaults-extra-file=/dev/fd/99',    )def test_make_defaults_file_pipe_with_only_username_writes_it_to_file_descriptor():    read_descriptor = 99    write_descriptor = flexmock()    flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)    flexmock(module.os).should_receive('write').with_args(        write_descriptor, b'[client]\nuser=root'    ).once()    flexmock(module.os).should_receive('close')    flexmock(module.os).should_receive('set_inheritable')    assert module.make_defaults_file_options(username='root', password=None) == (        '--defaults-extra-file=/dev/fd/99',    )def test_make_defaults_file_pipe_with_only_password_writes_it_to_file_descriptor():    read_descriptor = 99    write_descriptor = flexmock()    flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)    flexmock(module.os).should_receive('write').with_args(        write_descriptor, b'[client]\npassword="trustsome1"'    ).once()    flexmock(module.os).should_receive('close')    flexmock(module.os).should_receive('set_inheritable')    assert module.make_defaults_file_options(username=None, password='trustsome1') == (        '--defaults-extra-file=/dev/fd/99',    )def test_make_defaults_file_option_with_defaults_extra_filename_includes_it_in_file_descriptor():    read_descriptor = 99    write_descriptor = flexmock()    flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)    flexmock(module.os).should_receive('write').with_args(        write_descriptor, b'!include extra.cnf\n[client]\nuser=root\npassword="trustsome1"'    ).once()    flexmock(module.os).should_receive('close')    flexmock(module.os).should_receive('set_inheritable')    assert module.make_defaults_file_options(        username='root', password='trustsome1', defaults_extra_filename='extra.cnf'    ) == ('--defaults-extra-file=/dev/fd/99',)def test_make_defaults_file_option_with_only_defaults_extra_filename_uses_it_instead_of_file_descriptor():    flexmock(module.os).should_receive('pipe').never()    assert module.make_defaults_file_options(        username=None, password=None, defaults_extra_filename='extra.cnf'    ) == ('--defaults-extra-file=extra.cnf',)def test_database_names_to_dump_passes_through_name():    environment = flexmock()    names = module.database_names_to_dump(        {'name': 'foo'}, {}, 'root', 'trustsome1', environment, dry_run=False    )    assert names == ('foo',)def test_database_names_to_dump_bails_for_dry_run():    environment = flexmock()    flexmock(module).should_receive('execute_command_and_capture_output').never()    names = module.database_names_to_dump(        {'name': 'all'}, {}, 'root', 'trustsome1', environment, dry_run=True    )    assert names == ()def test_database_names_to_dump_queries_mariadb_for_database_names():    environment = flexmock()    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module).should_receive('execute_command_and_capture_output').with_args(        (            'mariadb',            '--defaults-extra-file=/dev/fd/99',            '--skip-column-names',            '--batch',            '--execute',            'show schemas',        ),        environment=environment,    ).and_return('foo\nbar\nmysql\n').once()    names = module.database_names_to_dump(        {'name': 'all'}, {}, 'root', 'trustsome1', environment, dry_run=False    )    assert names == ('foo', 'bar')def test_database_names_to_dump_runs_mariadb_with_tls():    environment = flexmock()    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module).should_receive('execute_command_and_capture_output').with_args(        (            'mariadb',            '--defaults-extra-file=/dev/fd/99',            '--ssl',            '--skip-column-names',            '--batch',            '--execute',            'show schemas',        ),        environment=environment,    ).and_return('foo\nbar\nmysql\n').once()    names = module.database_names_to_dump(        {'name': 'all', 'tls': True}, {}, 'root', 'trustsome1', environment, dry_run=False    )    assert names == ('foo', 'bar')def test_database_names_to_dump_runs_mariadb_without_tls():    environment = flexmock()    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module).should_receive('execute_command_and_capture_output').with_args(        (            'mariadb',            '--defaults-extra-file=/dev/fd/99',            '--skip-ssl',            '--skip-column-names',            '--batch',            '--execute',            'show schemas',        ),        environment=environment,    ).and_return('foo\nbar\nmysql\n').once()    names = module.database_names_to_dump(        {'name': 'all', 'tls': False}, {}, 'root', 'trustsome1', environment, dry_run=False    )    assert names == ('foo', 'bar')def test_use_streaming_true_for_any_databases():    assert module.use_streaming(        databases=[flexmock(), flexmock()],        config=flexmock(),    )def test_use_streaming_false_for_no_databases():    assert not module.use_streaming(databases=[], config=flexmock())def test_dump_data_sources_dumps_each_database():    databases = [{'name': 'foo'}, {'name': 'bar'}]    processes = [flexmock(), flexmock()]    flexmock(module).should_receive('make_dump_path').and_return('')    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).and_return(None)    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(        ('bar',)    )    for name, process in zip(('foo', 'bar'), processes):        flexmock(module).should_receive('execute_dump_command').with_args(            database={'name': name},            config={},            username=None,            password=None,            dump_path=object,            database_names=(name,),            environment={'USER': 'root'},            dry_run=object,            dry_run_label=object,        ).and_return(process).once()    assert (        module.dump_data_sources(            databases,            {},            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=[],            dry_run=False,        )        == processes    )def test_dump_data_sources_dumps_with_password():    database = {'name': 'foo', 'username': 'root', 'password': 'trustsome1'}    process = flexmock()    flexmock(module).should_receive('make_dump_path').and_return('')    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(        ('bar',)    )    flexmock(module).should_receive('execute_dump_command').with_args(        database=database,        config={},        username='root',        password='trustsome1',        dump_path=object,        database_names=('foo',),        environment={'USER': 'root'},        dry_run=object,        dry_run_label=object,    ).and_return(process).once()    assert module.dump_data_sources(        [database],        {},        config_paths=('test.yaml',),        borgmatic_runtime_directory='/run/borgmatic',        patterns=[],        dry_run=False,    ) == [process]def test_dump_data_sources_dumps_all_databases_at_once():    databases = [{'name': 'all'}]    process = flexmock()    flexmock(module).should_receive('make_dump_path').and_return('')    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))    flexmock(module).should_receive('execute_dump_command').with_args(        database={'name': 'all'},        config={},        username=None,        password=None,        dump_path=object,        database_names=('foo', 'bar'),        environment={'USER': 'root'},        dry_run=object,        dry_run_label=object,    ).and_return(process).once()    assert module.dump_data_sources(        databases,        {},        config_paths=('test.yaml',),        borgmatic_runtime_directory='/run/borgmatic',        patterns=[],        dry_run=False,    ) == [process]def test_dump_data_sources_dumps_all_databases_separately_when_format_configured():    databases = [{'name': 'all', 'format': 'sql'}]    processes = [flexmock(), flexmock()]    flexmock(module).should_receive('make_dump_path').and_return('')    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).and_return(None)    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))    for name, process in zip(('foo', 'bar'), processes):        flexmock(module).should_receive('execute_dump_command').with_args(            database={'name': name, 'format': 'sql'},            config={},            username=None,            password=None,            dump_path=object,            database_names=(name,),            environment={'USER': 'root'},            dry_run=object,            dry_run_label=object,        ).and_return(process).once()    assert (        module.dump_data_sources(            databases,            {},            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=[],            dry_run=False,        )        == processes    )def test_database_names_to_dump_runs_mariadb_with_list_options():    database = {'name': 'all', 'list_options': '--defaults-extra-file=mariadb.cnf --skip-ssl'}    flexmock(module).should_receive('parse_extra_options').and_return(        ('--skip-ssl',), 'mariadb.cnf'    )    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', 'mariadb.cnf'    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module).should_receive('execute_command_and_capture_output').with_args(        (            'mariadb',            '--defaults-extra-file=/dev/fd/99',            '--skip-ssl',            '--skip-column-names',            '--batch',            '--execute',            'show schemas',        ),        environment=None,    ).and_return(('foo\nbar')).once()    assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (        'foo',        'bar',    )def test_database_names_to_dump_runs_non_default_mariadb_with_list_options():    database = {        'name': 'all',        'list_options': '--defaults-extra-file=mariadb.cnf --skip-ssl',        'mariadb_command': 'custom_mariadb',    }    flexmock(module).should_receive('parse_extra_options').and_return(        ('--skip-ssl',), 'mariadb.cnf'    )    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', 'mariadb.cnf'    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module).should_receive('execute_command_and_capture_output').with_args(        environment=None,        full_command=(            'custom_mariadb',  # Custom MariaDB command            '--defaults-extra-file=/dev/fd/99',            '--skip-ssl',            '--skip-column-names',            '--batch',            '--execute',            'show schemas',        ),    ).and_return(('foo\nbar')).once()    assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (        'foo',        'bar',    )def test_execute_dump_command_runs_mariadb_dump():    process = flexmock()    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').with_args(        (            'mariadb-dump',            '--defaults-extra-file=/dev/fd/99',            '--add-drop-database',            '--databases',            'foo',            '--result-file',            'dump',        ),        environment=None,        run_to_completion=False,    ).and_return(process).once()    assert (        module.execute_dump_command(            database={'name': 'foo'},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=False,            dry_run_label='',        )        == process    )def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database():    process = flexmock()    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').with_args(        (            'mariadb-dump',            '--defaults-extra-file=/dev/fd/99',            '--databases',            'foo',            '--result-file',            'dump',        ),        environment=None,        run_to_completion=False,    ).and_return(process).once()    assert (        module.execute_dump_command(            database={'name': 'foo', 'add_drop_database': False},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=False,            dry_run_label='',        )        == process    )def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port():    process = flexmock()    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').with_args(        (            'mariadb-dump',            '--defaults-extra-file=/dev/fd/99',            '--add-drop-database',            '--host',            'database.example.org',            '--port',            '5433',            '--protocol',            'tcp',            '--databases',            'foo',            '--result-file',            'dump',        ),        environment=None,        run_to_completion=False,    ).and_return(process).once()    assert (        module.execute_dump_command(            database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=False,            dry_run_label='',        )        == process    )def test_execute_dump_command_runs_mariadb_dump_with_tls():    process = flexmock()    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').with_args(        (            'mariadb-dump',            '--defaults-extra-file=/dev/fd/99',            '--add-drop-database',            '--ssl',            '--databases',            'foo',            '--result-file',            'dump',        ),        environment=None,        run_to_completion=False,    ).and_return(process).once()    assert (        module.execute_dump_command(            database={'name': 'foo', 'tls': True},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=False,            dry_run_label='',        )        == process    )def test_execute_dump_command_runs_mariadb_dump_without_tls():    process = flexmock()    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').with_args(        (            'mariadb-dump',            '--defaults-extra-file=/dev/fd/99',            '--add-drop-database',            '--skip-ssl',            '--databases',            'foo',            '--result-file',            'dump',        ),        environment=None,        run_to_completion=False,    ).and_return(process).once()    assert (        module.execute_dump_command(            database={'name': 'foo', 'tls': False},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=False,            dry_run_label='',        )        == process    )def test_execute_dump_command_runs_mariadb_dump_with_username_and_password():    process = flexmock()    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').with_args(        (            'mariadb-dump',            '--defaults-extra-file=/dev/fd/99',            '--add-drop-database',            '--databases',            'foo',            '--result-file',            'dump',        ),        environment={},        run_to_completion=False,    ).and_return(process).once()    assert (        module.execute_dump_command(            database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment={},            dry_run=False,            dry_run_label='',        )        == process    )def test_execute_dump_command_runs_mariadb_dump_with_options():    process = flexmock()    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return(('--stuff=such',), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').with_args(        (            'mariadb-dump',            '--defaults-extra-file=/dev/fd/99',            '--stuff=such',            '--add-drop-database',            '--databases',            'foo',            '--result-file',            'dump',        ),        environment=None,        run_to_completion=False,    ).and_return(process).once()    assert (        module.execute_dump_command(            database={'name': 'foo', 'options': '--stuff=such'},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=False,            dry_run_label='',        )        == process    )def test_execute_dump_command_runs_non_default_mariadb_dump_with_options():    process = flexmock()    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return(('--stuff=such',), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').with_args(        (            'custom_mariadb_dump',  # Custom MariaDB dump command            '--defaults-extra-file=/dev/fd/99',            '--stuff=such',            '--add-drop-database',            '--databases',            'foo',            '--result-file',            'dump',        ),        environment=None,        run_to_completion=False,    ).and_return(process).once()    assert (        module.execute_dump_command(            database={                'name': 'foo',                'mariadb_dump_command': 'custom_mariadb_dump',                'options': '--stuff=such',            },  # Custom MariaDB dump command specified            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=False,            dry_run_label='',        )        == process    )def test_execute_dump_command_with_duplicate_dump_skips_mariadb_dump():    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(True)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()    flexmock(module).should_receive('execute_command').never()    assert (        module.execute_dump_command(            database={'name': 'foo'},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=True,            dry_run_label='SO DRY',        )        is None    )def test_execute_dump_command_with_dry_run_skips_mariadb_dump():    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')    flexmock(module.os.path).should_receive('exists').and_return(False)    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.dump).should_receive('create_named_pipe_for_dump')    flexmock(module).should_receive('execute_command').never()    assert (        module.execute_dump_command(            database={'name': 'foo'},            config={},            username='root',            password='trustsome1',            dump_path=flexmock(),            database_names=('foo',),            environment=None,            dry_run=True,            dry_run_label='SO DRY',        )        is None    )def test_dump_data_sources_errors_for_missing_all_databases():    databases = [{'name': 'all'}]    flexmock(module).should_receive('make_dump_path').and_return('')    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(        'databases/localhost/all'    )    flexmock(module).should_receive('database_names_to_dump').and_return(())    with pytest.raises(ValueError):        assert module.dump_data_sources(            databases,            {},            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=[],            dry_run=False,        )def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():    databases = [{'name': 'all'}]    flexmock(module).should_receive('make_dump_path').and_return('')    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(        'databases/localhost/all'    )    flexmock(module).should_receive('database_names_to_dump').and_return(())    assert (        module.dump_data_sources(            databases,            {},            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=[],            dry_run=True,        )        == []    )def test_restore_data_source_dump_runs_mariadb_to_restore():    hook_config = [{'name': 'foo'}, {'name': 'bar'}]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        None, None, None    ).and_return(())    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        ('mariadb', '--batch'),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source={'name': 'foo'},        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_runs_mariadb_with_options():    hook_config = [{'name': 'foo', 'restore_options': '--harder'}]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return(('--harder',), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        None, None, None    ).and_return(())    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        ('mariadb', '--harder', '--batch'),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source=hook_config[0],        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_runs_non_default_mariadb_with_options():    hook_config = [        {'name': 'foo', 'restore_options': '--harder', 'mariadb_command': 'custom_mariadb'}    ]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return(('--harder',), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        None, None, None    ).and_return(())    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        ('custom_mariadb', '--harder', '--batch'),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source=hook_config[0],        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_runs_mariadb_with_hostname_and_port():    hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        None, None, None    ).and_return(())    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        (            'mariadb',            '--batch',            '--host',            'database.example.org',            '--port',            '5433',            '--protocol',            'tcp',        ),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source=hook_config[0],        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_runs_mariadb_with_tls():    hook_config = [{'name': 'foo', 'tls': True}]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        None, None, None    ).and_return(())    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        (            'mariadb',            '--batch',            '--ssl',        ),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source=hook_config[0],        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_runs_mariadb_without_tls():    hook_config = [{'name': 'foo', 'tls': False}]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        None, None, None    ).and_return(())    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        (            'mariadb',            '--batch',            '--skip-ssl',        ),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source=hook_config[0],        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_runs_mariadb_with_username_and_password():    hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'root', 'trustsome1', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        ('mariadb', '--defaults-extra-file=/dev/fd/99', '--batch'),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source=hook_config[0],        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():    hook_config = [        {            'name': 'foo',            'username': 'root',            'password': 'trustsome1',            'restore_hostname': 'restorehost',            'restore_port': 'restoreport',            'restore_username': 'restoreusername',            'restore_password': 'restorepassword',        }    ]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'cliusername', 'clipassword', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        (            'mariadb',            '--defaults-extra-file=/dev/fd/99',            '--batch',            '--host',            'clihost',            '--port',            'cliport',            '--protocol',            'tcp',        ),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source=hook_config[0],        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': 'clihost',            'port': 'cliport',            'username': 'cliusername',            'password': 'clipassword',        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():    hook_config = [        {            'name': 'foo',            'username': 'root',            'password': 'trustsome1',            'hostname': 'dbhost',            'port': 'dbport',            'tls': True,            'restore_username': 'restoreuser',            'restore_password': 'restorepass',            'restore_hostname': 'restorehost',            'restore_port': 'restoreport',            'restore_tls': False,        }    ]    extract_process = flexmock(stdout=flexmock())    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        'restoreuser', 'restorepass', None    ).and_return(('--defaults-extra-file=/dev/fd/99',))    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').with_args(        (            'mariadb',            '--defaults-extra-file=/dev/fd/99',            '--batch',            '--host',            'restorehost',            '--port',            'restoreport',            '--protocol',            'tcp',            '--skip-ssl',        ),        processes=[extract_process],        output_log_level=logging.DEBUG,        input_file=extract_process.stdout,        environment={'USER': 'root'},    ).once()    module.restore_data_source_dump(        hook_config,        {},        data_source=hook_config[0],        dry_run=False,        extract_process=extract_process,        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )def test_restore_data_source_dump_with_dry_run_skips_restore():    hook_config = [{'name': 'foo'}]    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module).should_receive('parse_extra_options').and_return((), None)    flexmock(module).should_receive('make_defaults_file_options').with_args(        None, None, None    ).and_return(())    flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})    flexmock(module).should_receive('execute_command_with_processes').never()    module.restore_data_source_dump(        hook_config,        {},        data_source={'name': 'foo'},        dry_run=True,        extract_process=flexmock(),        connection_params={            'hostname': None,            'port': None,            'username': None,            'password': None,        },        borgmatic_runtime_directory='/run/borgmatic',    )
 |