import os

import pytest

from .common import INSTANCE, HOST
from datadog_checks.dev import docker_run, get_here, run_command
from datadog_checks.dev.conditions import CheckCommandOutput


@pytest.fixture(scope='session')
def dd_environment():
    compose_file = os.path.join(get_here(), 'compose', 'docker-compose.yaml')

    # Build the topology jar to use in the environment
    with docker_run(compose_file, build=True, service_name='topology-maker', sleep=15):
        run_command(
            ['docker', 'cp', 'topology-build:/topology.jar', os.path.join(get_here(), 'compose')]
        )
    nimbus_condition = CheckCommandOutput(['nc', '-zv', HOST, '6627'], 'succeeded')
    with docker_run(compose_file, service_name='storm-nimbus', conditions=[nimbus_condition]):
        with docker_run(compose_file, service_name='storm-ui',
                        log_patterns=[r'org.apache.storm.ui.core']):
            with docker_run(
                compose_file, service_name='topology',
                log_patterns=['Finished submitting topology: topology']
            ):
                yield INSTANCE

import os
import socket

import pytest

from .common import INSTANCE, HOST
from datadog_checks.dev import docker_run, get_here, run_command
from datadog_checks.dev.conditions import WaitFor


def wait_for_thrift():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((HOST, 6627))
    sock.close()


@pytest.fixture(scope='session')
def dd_environment():
    compose_file = os.path.join(get_here(), 'compose', 'docker-compose.yaml')

    # Build the topology jar to use in the environment
    with docker_run(compose_file, build=True, service_name='topology-maker', sleep=15):
        run_command(
            ['docker', 'cp', 'topology-build:/topology.jar', os.path.join(get_here(), 'compose')]
        )
    nimbus_condition = WaitFor(wait_for_thrift)
    with docker_run(compose_file, service_name='storm-nimbus', conditions=[nimbus_condition]):
        with docker_run(compose_file, service_name='storm-ui',
                        log_patterns=[r'org.apache.storm.ui.core']):
            with docker_run(
                compose_file, service_name='topology',
                log_patterns=['Finished submitting topology: topology']
            ):
                yield INSTANCE

Use socket instead of nc
Use socket instead of nc

  import os
+ import socket
  
  import pytest
  
  from .common import INSTANCE, HOST
  from datadog_checks.dev import docker_run, get_here, run_command
- from datadog_checks.dev.conditions import CheckCommandOutput
+ from datadog_checks.dev.conditions import WaitFor
+ 
+ 
+ def wait_for_thrift():
+     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+     sock.connect((HOST, 6627))
+     sock.close()
  
  
  @pytest.fixture(scope='session')
  def dd_environment():
      compose_file = os.path.join(get_here(), 'compose', 'docker-compose.yaml')
  
      # Build the topology jar to use in the environment
      with docker_run(compose_file, build=True, service_name='topology-maker', sleep=15):
          run_command(
              ['docker', 'cp', 'topology-build:/topology.jar', os.path.join(get_here(), 'compose')]
          )
-     nimbus_condition = CheckCommandOutput(['nc', '-zv', HOST, '6627'], 'succeeded')
+     nimbus_condition = WaitFor(wait_for_thrift)
      with docker_run(compose_file, service_name='storm-nimbus', conditions=[nimbus_condition]):
          with docker_run(compose_file, service_name='storm-ui',
                          log_patterns=[r'org.apache.storm.ui.core']):
              with docker_run(
                  compose_file, service_name='topology',
                  log_patterns=['Finished submitting topology: topology']
              ):
                  yield INSTANCE
  
Use socket instead of nc
## Code Before:
import os

import pytest

from .common import INSTANCE, HOST
from datadog_checks.dev import docker_run, get_here, run_command
from datadog_checks.dev.conditions import CheckCommandOutput


@pytest.fixture(scope='session')
def dd_environment():
    compose_file = os.path.join(get_here(), 'compose', 'docker-compose.yaml')

    # Build the topology jar to use in the environment
    with docker_run(compose_file, build=True, service_name='topology-maker', sleep=15):
        run_command(
            ['docker', 'cp', 'topology-build:/topology.jar', os.path.join(get_here(), 'compose')]
        )
    nimbus_condition = CheckCommandOutput(['nc', '-zv', HOST, '6627'], 'succeeded')
    with docker_run(compose_file, service_name='storm-nimbus', conditions=[nimbus_condition]):
        with docker_run(compose_file, service_name='storm-ui',
                        log_patterns=[r'org.apache.storm.ui.core']):
            with docker_run(
                compose_file, service_name='topology',
                log_patterns=['Finished submitting topology: topology']
            ):
                yield INSTANCE

## Instruction:
Use socket instead of nc
## Code After:
import os
import socket

import pytest

from .common import INSTANCE, HOST
from datadog_checks.dev import docker_run, get_here, run_command
from datadog_checks.dev.conditions import WaitFor


def wait_for_thrift():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((HOST, 6627))
    sock.close()


@pytest.fixture(scope='session')
def dd_environment():
    compose_file = os.path.join(get_here(), 'compose', 'docker-compose.yaml')

    # Build the topology jar to use in the environment
    with docker_run(compose_file, build=True, service_name='topology-maker', sleep=15):
        run_command(
            ['docker', 'cp', 'topology-build:/topology.jar', os.path.join(get_here(), 'compose')]
        )
    nimbus_condition = WaitFor(wait_for_thrift)
    with docker_run(compose_file, service_name='storm-nimbus', conditions=[nimbus_condition]):
        with docker_run(compose_file, service_name='storm-ui',
                        log_patterns=[r'org.apache.storm.ui.core']):
            with docker_run(
                compose_file, service_name='topology',
                log_patterns=['Finished submitting topology: topology']
            ):
                yield INSTANCE
