예제 #1
0
    def Run(self, args):
        if not args.host_port:
            args.host_port = arg_parsers.HostPort.Parse(
                datastore_util.GetHostPort(), ipv6_enabled=socket.has_ipv6)
        args.host_port.host = args.host_port.host or 'localhost'

        datastore_util.PrepareGCDDataDir(args)
        with datastore_util.StartGCDEmulator(args) as proc:
            datastore_util.WriteGCDEnvYaml(args)
            util.PrefixOutput(proc, 'datastore')
예제 #2
0
  def _DoTestPrepareGCDDataDir(self):
    gcd_root_mock = self.StartObjectPatch(datastore_util, 'GetGCDRoot')
    gcd_root_mock.return_value = 'pathtogcdroot'
    exec_mock = self.StartObjectPatch(util, 'Exec')
    process = mock.Mock()
    process.poll.return_value = 0
    exec_mock.return_value.__enter__.return_value = process
    prefix_mock = self.StartObjectPatch(util, 'PrefixOutput')

    data_dir = self.CreateTempDir()
    args = type(str('args_mock'),
                (object,),
                dict(data_dir=data_dir))

    # Nothing should be done if data-dir is non-empty
    tmp_file = self.Touch(directory=data_dir)
    datastore_util.PrepareGCDDataDir(args)
    self.assertFalse(exec_mock.called)

    # gcd create should be called if data-dr is empty
    exec_mock.reset_mock()
    prefix_mock.reset_mock()
    os.remove(tmp_file)
    datastore_util.PrepareGCDDataDir(args)
    create_args = ['create',
                   '--project_id={0}'.format(self.Project()),
                   data_dir,]
    exec_args = datastore_util.ArgsForGCDEmulator(create_args)
    exec_mock.assert_called_once_with(exec_args)
    prefix_mock.assert_called_once_with(process, 'datastore')

    # gcd create should be called if data-dir does not exist
    exec_mock.reset_mock()
    prefix_mock.reset_mock()
    os.rmdir(data_dir)
    datastore_util.PrepareGCDDataDir(args)
    exec_mock.assert_called_once_with(exec_args)
    prefix_mock.assert_called_once_with(process, 'datastore')

    # Should throw exception if PrepareGCDDataDir fails
    process.poll.return_value = 1
    with self.assertRaises(datastore_util.UnableToPrepareDataDir):
      datastore_util.PrepareGCDDataDir(args)