def test_run_ticket_failure(self): """Tests appmgr.run sequence, which will result in supervisor exec. """ # access protected module _allocate_network_ports # pylint: disable=w0212 manifest = { 'shared_network': False, 'disk': '100G', 'name': 'proid.myapp#0', 'memory': '100M', 'environment': 'dev', 'uniqueid': 'ID1234', 'proid': 'foo', 'services': [ { 'command': '/bin/true', 'restart_count': 3, 'name': 'web_server' } ], 'endpoints': [ { 'name': 'http', 'port': 8000 }, { 'name': 'port0', 'port': 0 } ], 'cpu': '100%' } treadmill.appmgr.manifest.read.return_value = manifest app_dir = os.path.join(self.root, 'apps', 'proid.myapp#0') os.makedirs(app_dir) mock_nwrk_client = self.app_env.svc_network.make_client.return_value network = { 'vip': '2.2.2.2', 'gateway': '1.1.1.1', 'veth': 'testveth.0', } mock_nwrk_client.wait.return_value = network def _fake_allocate_network_ports(_ip, manifest): """Mimick inplace manifest modification in _allocate_network_ports. """ manifest['ephemeral_ports'] = [] return mock.DEFAULT treadmill.appmgr.run._allocate_network_ports.side_effect = \ _fake_allocate_network_ports # Make sure that despite ticket absence there is no throw. mock_watchdog = mock.Mock() app_run.run( self.app_env, app_dir, mock_watchdog, terminated=() ) self.assertTrue(mock_watchdog.remove.called)
def test_run_no_ephemeral(self): """Tests appmgr.run without ephemeral ports in manifest.""" # Modify app manifest so that it does not contain ephemeral ports, # make sure that .etc/ld.so.preload is not created. # access protected module _allocate_network_ports # pylint: disable=w0212 manifest = { 'shared_network': False, 'disk': '100G', 'name': 'proid.myapp#0', 'passthrough': [ 'xxx', 'yyy', 'zzz' ], 'memory': '100M', 'host_ip': '172.31.81.67', 'system_services': [], 'environment': 'dev', 'uniqueid': 'ID1234', 'proid': 'foo', 'services': [ { 'command': '/bin/true', 'restart_count': 3, 'name': 'web_server' } ], 'endpoints': [ { 'name': 'http', 'port': 8000 }, { 'name': 'port0', 'port': 0 }, { 'type': 'infra', 'name': 'ssh', 'port': 0 } ], 'cpu': '100%' } treadmill.appmgr.manifest.read.return_value = manifest app_dir = os.path.join(self.root, 'apps', 'proid.myapp#0') os.makedirs(app_dir) mock_nwrk_client = self.app_env.svc_network.make_client.return_value network = { 'vip': '2.2.2.2', 'gateway': '1.1.1.1', 'veth': 'testveth.0', } mock_nwrk_client.wait.return_value = network rootdir = os.path.join(app_dir, 'root') def _fake_allocate_network_ports(_ip, manifest): """Mimick inplace manifest modification in _allocate_network_ports. """ manifest['ephemeral_ports'] = [] return mock.DEFAULT treadmill.appmgr.run._allocate_network_ports.side_effect = \ _fake_allocate_network_ports mock_watchdog = mock.Mock() app_run.run( self.app_env, app_dir, mock_watchdog, terminated=() ) self.assertFalse( os.path.exists( os.path.join(rootdir, '.etc/ld.so.preload') ) ) self.assertTrue(mock_watchdog.remove.called)
def test_run(self): """Tests appmgr.run sequence, which will result in supervisor exec. """ # access protected module _allocate_network_ports # pylint: disable=w0212 manifest = { 'shared_network': False, 'ephemeral_ports': 3, 'passthrough': [ 'xxx', 'yyy', 'zzz' ], 'memory': '100M', 'host_ip': '172.31.81.67', 'uniqueid': 'ID1234', 'services': [ { 'command': '/bin/true', 'restart_count': 3, 'name': 'web_server' } ], 'disk': '100G', 'tickets': True, 'name': 'proid.myapp#0', 'system_services': [], 'environment': 'dev', 'proid': 'foo', 'endpoints': [ { 'name': 'http', 'port': 8000 }, { 'name': 'port0', 'port': 0 }, { 'type': 'infra', 'name': 'ssh', 'port': 0 } ], 'cpu': '100%' } treadmill.appmgr.manifest.read.return_value = manifest app_unique_name = 'proid.myapp-0-0000000ID1234' app_dir = os.path.join(self.root, 'apps', app_unique_name) os.makedirs(app_dir) mock_nwrk_client = self.app_env.svc_network.make_client.return_value network = { 'vip': '2.2.2.2', 'gateway': '1.1.1.1', 'veth': 'testveth.0', } mock_nwrk_client.wait.return_value = network def _fake_allocate_network_ports(_ip, manifest): """Mimick inplace manifest modification in _allocate_network_ports. """ manifest['ephemeral_ports'] = ['1', '2', '3'] return mock.DEFAULT treadmill.appmgr.run._allocate_network_ports.side_effect = \ _fake_allocate_network_ports mock_watchdog = mock.Mock() treadmill.subproc.BINARIES['treadmill_bind_preload.so'] = ( '/some/$LIB/treadmill_bind_preload.so') app_run.run( self.app_env, app_dir, mock_watchdog, terminated=() ) # Check that port allocation is correctly called. # XXX(boysson): potential mock bug: assert_call expects the vip since # manifest is modified in place even though the vip are # allocated *after*. manifest['vip'] = { 'ip0': '1.1.1.1', 'ip1': '2.2.2.2', } manifest['network'] = network manifest['ephemeral_ports'] = ['1', '2', '3'] treadmill.appmgr.run._allocate_network_ports.assert_called_with( '172.31.81.67', manifest, ) # Make sure, post modification, that the manifest is readable by other. st = os.stat(os.path.join(app_dir, 'state.yml')) self.assertTrue(st.st_mode & stat.S_IRUSR) self.assertTrue(st.st_mode & stat.S_IRGRP) self.assertTrue(st.st_mode & stat.S_IROTH) self.assertTrue(st.st_mode & stat.S_IWUSR) self.assertFalse(st.st_mode & stat.S_IWGRP) self.assertFalse(st.st_mode & stat.S_IWOTH) # State yml is what is copied in the container shutil.copy.assert_called_with( os.path.join(app_dir, 'state.yml'), os.path.join(app_dir, 'root', 'app.yml'), ) # Network unshare app = utils.to_obj(manifest) treadmill.appmgr.run._unshare_network.assert_called_with( self.app_env, app ) # Create root dir treadmill.appmgr.run._create_root_dir.assert_called_with( self.app_env, app_dir, os.path.join(app_dir, 'root'), app, ) # XXX(boysson): Missing environ_dir/manifest_dir tests # Create supervision tree treadmill.appmgr.run._create_supervision_tree.assert_called_with( app_dir, self.app_env.app_events_dir, app ) treadmill.appmgr.run._share_cgroup_info.assert_called_with( app, os.path.join(app_dir, 'root'), ) # Ephemeral LDPRELOAD treadmill.appmgr.run._prepare_ldpreload.assert_called_with( os.path.join(app_dir, 'root'), ['/some/$LIB/treadmill_bind_preload.so'] ) # Misc bind mounts treadmill.fs.mount_bind.assert_has_calls([ mock.call( os.path.join(app_dir, 'root'), '/etc/resolv.conf', bind_opt='--bind', target=os.path.join(app_dir, 'root/.etc/resolv.conf') ), mock.call( os.path.join(app_dir, 'root'), '/etc/ld.so.preload', bind_opt='--bind', target=os.path.join(app_dir, 'root/.etc/ld.so.preload') ), mock.call( os.path.join(app_dir, 'root'), '/etc/pam.d/sshd', bind_opt='--bind', target=os.path.join(app_dir, 'root/treadmill/etc/pam.d/sshd') ), ]) self.assertTrue(mock_watchdog.remove.called)