verifyBdStoreIdentity function

Future<BdStoreIdentity> verifyBdStoreIdentity({
  1. required String bdVersion,
  2. required String storePath,
  3. required String configPath,
  4. required String rootPath,
  5. required ProxiedBdPorts reserved,
})

Reads bd's proxy artifacts and refuses unless they, and the live processes they name, all describe the store the caller just built.

bd 1.1.0 publishes no client-info record, so the live db-proxy-child and dolt sql-server argv ARE the account of what a store is bound to. Each check is separately loud: a store that quietly answers out of the wrong server is the defect, and a verification that only reports "something is wrong" would have cost the same round the silent binding did.

Implementation

Future<BdStoreIdentity> verifyBdStoreIdentity({
  required String bdVersion,
  required String storePath,
  required String configPath,
  required String rootPath,
  required ProxiedBdPorts reserved,
}) async {
  final proxyPath = p.join(rootPath, 'proxy.pid');
  final doltPath = p.join(rootPath, 'proxy-child.pid');
  final proxy = proxiedBdPidArtifact(proxyPath);
  final dolt = proxiedBdPidArtifact(doltPath);
  final table = await processTable();

  Never refuse(String what) => throw StateError(
    'the fixture bd store at $storePath did not bind the server it started: '
    '$what; bd_version=$bdVersion config_path=$configPath '
    'root_path=$rootPath reserved_proxy_port=${reserved.proxyPort} '
    'reserved_dolt_port=${reserved.doltPort}; '
    'proxy.pid=${_artifactText(proxyPath)} '
    'proxy-child.pid=${_artifactText(doltPath)}; '
    'store processes '
    '${[for (final row in storeProcessesIn(table, storePath)) '${row.pid} ${row.command}'].join('; ')}',
  );

  if (proxy == null) refuse('$proxyPath is absent');
  if (dolt == null) refuse('$doltPath is absent');
  if (proxy.port == null) refuse('$proxyPath records no port');
  if (dolt.port == null) refuse('$doltPath records no port');
  if (proxy.port != reserved.proxyPort) {
    refuse(
      'the proxy answers on ${proxy.port}, not the reserved '
      '${reserved.proxyPort}',
    );
  }
  if (dolt.port != reserved.doltPort) {
    refuse(
      'the Dolt server answers on ${dolt.port}, not the reserved '
      '${reserved.doltPort}',
    );
  }

  final proxyCommand = table[proxy.pid];
  if (proxyCommand == null) refuse('proxy ${proxy.pid} is not running');
  if (!proxyCommand.contains('db-proxy-child')) {
    refuse('${proxy.pid} is not a db-proxy-child: $proxyCommand');
  }
  if (commandArgument(proxyCommand, 'root') != rootPath) {
    refuse('proxy ${proxy.pid} serves another root: $proxyCommand');
  }
  if (commandArgument(proxyCommand, 'config') != configPath) {
    refuse('proxy ${proxy.pid} serves another config: $proxyCommand');
  }
  if (commandArgument(proxyCommand, 'port') != '${reserved.proxyPort}') {
    refuse('proxy ${proxy.pid} serves another port: $proxyCommand');
  }

  final doltCommand = table[dolt.pid];
  if (doltCommand == null) refuse('Dolt server ${dolt.pid} is not running');
  if (!doltCommand.contains('dolt sql-server')) {
    refuse('${dolt.pid} is not a dolt sql-server: $doltCommand');
  }
  if (commandArgument(doltCommand, 'config') != configPath) {
    refuse('Dolt server ${dolt.pid} serves another config: $doltCommand');
  }

  return BdStoreIdentity(
    bdVersion: bdVersion,
    storePath: storePath,
    configPath: configPath,
    rootPath: rootPath,
    proxyPid: proxy.pid,
    proxyPort: proxy.port!,
    doltPid: dolt.pid,
    doltPort: dolt.port!,
  );
}