def test_constructs_nothing_if_defaults_is_none(self): with patch_open() as (mock_open, mock_file): hooks.construct_haproxy_config('foo-globals', None, 'foo-monitoring', 'foo-services') self.assertFalse(mock_open.called) self.assertFalse(mock_file.called)
def test_writes_crts(self, create_listen_stanza): create_listen_stanza.return_value = 'some content' content = ("-----BEGIN CERTIFICATE-----\n" "<data>\n" "-----END CERTIFICATE-----\n") services_dict = { 'foo': { 'service_name': 'bar', 'service_host': 'some-host', 'service_port': 'some-port', 'service_options': 'some-options', 'servers': (1, 2), 'crts': [base64.b64encode(content)] }, } with patch.object(os.path, "exists") as exists: exists.return_value = True with patch_open() as (mock_open, mock_file): with patch.object(pwd, "getpwnam") as getpwnam: class DB(object): pw_uid = 9999 getpwnam.return_value = DB() with patch.object(os, "chown") as chown: hooks.write_service_config(services_dict) path = '/var/lib/haproxy/service_bar/0.pem' mock_open.assert_any_call(path, 'w') mock_file.write.assert_any_call(content) chown.assert_called_with(path, 9999, -1) self.assertTrue(create_listen_stanza.called)
def test_writes_errorfiles(self, create_listen_stanza): create_listen_stanza.return_value = 'some content' content = ("HTTP/1.0 403 Forbidden\r\n" "Content-Type: text/html\r\n" "\r\n" "<html></html>") services_dict = { 'foo': { 'service_name': 'bar', 'service_host': 'some-host', 'service_port': 'some-port', 'service_options': 'some-options', 'servers': (1, 2), 'errorfiles': [{ 'http_status': 403, 'content': base64.b64encode(content) }] }, } with patch.object(os.path, "exists") as exists: exists.return_value = True with patch_open() as (mock_open, mock_file): hooks.write_service_config(services_dict) mock_open.assert_any_call( '/var/lib/haproxy/service_bar/403.http', 'w') mock_file.write.assert_any_call(content) self.assertTrue(create_listen_stanza.called)
def test_writes_crts(self, create_listen_stanza): create_listen_stanza.return_value = 'some content' content = ("-----BEGIN CERTIFICATE-----\n" "<data>\n" "-----END CERTIFICATE-----\n") services_dict = { 'foo': { 'service_name': 'bar', 'service_host': 'some-host', 'service_port': 'some-port', 'service_options': 'some-options', 'servers': (1, 2), 'crts': [base64.b64encode(content)] }, } with patch.object(os.path, "exists") as exists: exists.return_value = True with patch_open() as (mock_open, mock_file): with patch.object(pwd, "getpwnam") as getpwnam: class DB(object): pw_uid = 9999 getpwnam.return_value = DB() with patch.object(os, "chown") as chown: hooks.write_service_config(services_dict) path = '/var/lib/haproxy/service_bar/0.pem' mock_open.assert_any_call(path, 'w') mock_file.write.assert_any_call(content) chown.assert_called_with(path, 9999, - 1) self.assertTrue(create_listen_stanza.called)
def test_constructs_haproxy_config_without_optionals(self): with patch_open() as (mock_open, mock_file): hooks.construct_haproxy_config('foo-globals', 'foo-defaults') mock_file.write.assert_called_with('foo-globals\n\n' 'foo-defaults\n\n') mock_open.assert_called_with(hooks.default_haproxy_config, 'w')
def test_constructs_haproxy_config_without_optionals(self): with patch_open() as (mock_open, mock_file): hooks.construct_haproxy_config('foo-globals', 'foo-defaults') mock_file.write.assert_called_with( 'foo-globals\n\n' 'foo-defaults\n\n' ) mock_open.assert_called_with(hooks.default_haproxy_config, 'w')
def test_constructs_haproxy_config(self): with patch_open() as (mock_open, mock_file): hooks.construct_haproxy_config('foo-globals', 'foo-defaults', 'foo-monitoring', 'foo-services') mock_file.write.assert_called_with('foo-globals\n\n' 'foo-defaults\n\n' 'foo-monitoring\n\n' 'foo-services\n\n') mock_open.assert_called_with(hooks.default_haproxy_config, 'w')
def test_loads_services_within_dir_if_no_name_provided(self, glob): with patch_open() as (mock_open, mock_file): mock_file.read.side_effect = ['foo', 'bar'] glob.return_value = ['foo-file', 'bar-file'] result = hooks.load_services() self.assertEqual('foo\n\nbar\n\n', result) mock_open.assert_has_calls([call('foo-file'), call('bar-file')]) mock_file.read.assert_has_calls([call(), call()])
def test_loads_services_by_name(self, path_exists): with patch_open() as (mock_open, mock_file): path_exists.return_value = True mock_file.read.return_value = 'some content' result = hooks.load_services('some-service') self.assertEqual('some content', result) mock_open.assert_called_with( '/var/run/haproxy/some-service.service') mock_file.read.assert_called_with()
def test_constructs_haproxy_config(self): with patch_open() as (mock_open, mock_file): hooks.construct_haproxy_config('foo-globals', 'foo-defaults', 'foo-monitoring', 'foo-services') mock_file.write.assert_called_with( 'foo-globals\n\n' 'foo-defaults\n\n' 'foo-monitoring\n\n' 'foo-services\n\n' ) mock_open.assert_called_with(hooks.default_haproxy_config, 'w')
def test_skip_crts_default(self, create_listen_stanza): create_listen_stanza.return_value = 'some content' services_dict = { 'foo': { 'service_name': 'bar', 'service_host': 'some-host', 'service_port': 'some-port', 'service_options': 'some-options', 'servers': (1, 2), 'crts': ["DEFAULT"] }, } with patch.object(os.path, "exists") as exists: exists.return_value = True with patch.object(os, "makedirs"): with patch_open() as (mock_open, mock_file): hooks.write_service_config(services_dict) self.assertNotEqual( mock_open.call_args, ('/var/lib/haproxy/service_bar/0.pem', 'w')) self.assertTrue(create_listen_stanza.called)
def test_writes_service_config(self, create_listen_stanza): create_listen_stanza.return_value = 'some content' services_dict = { 'foo': { 'service_name': 'bar', 'service_host': 'some-host', 'service_port': 'some-port', 'service_options': 'some-options', 'servers': (1, 2), }, } with patch.object(os.path, "exists") as exists: exists.return_value = True with patch_open() as (mock_open, mock_file): hooks.write_service_config(services_dict) create_listen_stanza.assert_called_with( 'bar', 'some-host', 'some-port', 'some-options', (1, 2), [], [], []) mock_open.assert_called_with( '/var/run/haproxy/bar.service', 'w') mock_file.write.assert_called_with('some content')
def test_writes_service_config(self, create_listen_stanza): create_listen_stanza.return_value = 'some content' services_dict = { 'foo': { 'service_name': 'bar', 'service_host': 'some-host', 'service_port': 'some-port', 'service_options': 'some-options', 'servers': (1, 2), }, } with patch.object(os.path, "exists") as exists: exists.return_value = True with patch_open() as (mock_open, mock_file): hooks.write_service_config(services_dict) create_listen_stanza.assert_called_with( 'bar', 'some-host', 'some-port', 'some-options', (1, 2), []) mock_open.assert_called_with('/var/run/haproxy/bar.service', 'w') mock_file.write.assert_called_with('some content')