Best Python code snippet using autotest_python
test_connections.py
Source:test_connections.py  
...12    def test_hosts_no_containers(self, output_mock):13        """ When there are no containers or process response, well, no hosts!  """14        with pytest.raises(ValueError):15            connections.DockerConnection.hosts(MagicMock())16    def test_no_hosts(self, output_mock):17        """ When no args.hosts are passed, return all running containers """18        self._setup_sife_effect(output_mock)19        hosts = connections.DockerConnection.hosts(MagicMock())20        assert hosts == ['containerid1', 'containerid2']21    def test_hosts(self, output_mock):22        args = MagicMock()23        args.hosts = ['ahost']24        self._setup_sife_effect(output_mock)25        # if the hostname doesn't match anything, nothing is returned26        with pytest.raises(ValueError):27            connections.DockerConnection.hosts(args)28        # If the hostname does match one, return it29        args.hosts = ['one']30        assert ['containerid1'] == connections.DockerConnection.hosts(args)31        # If the hostname matches both, return both32        args.hosts = ['two', 'one']33        assert ['containerid1', 'containerid2'] == connections.DockerConnection.hosts(args)34        # Matches on IDs as well35        args.hosts = ['containerid1']36        assert ['containerid1'] == connections.DockerConnection.hosts(args)37    def test_hosts_approximate(self, output_mock):38        args = MagicMock()39        args.hosts = ['wo']40        args.approximate = True41        self._setup_sife_effect(output_mock)42        assert ['containerid2'] == connections.DockerConnection.hosts(args)43        args.hosts = ['o']44        assert ['containerid1', 'containerid2'] == connections.DockerConnection.hosts(args)45        args.hosts = ['blah']46        with pytest.raises(ValueError):47            connections.DockerConnection.hosts(args)48        args.hosts = ['wo', 'blah']49        assert ['containerid2'] == connections.DockerConnection.hosts(args)50    def test_connect(self, output_mock):51        args = MagicMock()52        args.hosts = ['containerid1']53        args.approximate = False54        args.docker_command = ''55        self._setup_sife_effect(output_mock)56        assert 'docker exec -it containerid1 bash' == \57            connections.DockerConnection.connect('containerid1', args)58    def test_command(self, output_mock):59        args = MagicMock()60        args.hosts = ['host1']61        args.approximate = False62        args.docker_command = ''63        args.command = 'pwd'64        self._setup_sife_effect(output_mock)65        assert 'docker exec -it containerid1 pwd && docker exec -it containerid1 bash' == \66            connections.DockerConnection.command('containerid1', args)67    def test_copy(self, output_mock):68        args = MagicMock()69        args.hosts = ['host1']70        args.approximate = False71        args.docker_command = ''72        args.script = 'test.sh'73        self._setup_sife_effect(output_mock)74        assert ('docker cp test.sh containerid1:/tmp && '75                'docker exec -it containerid1 chmod u+x /tmp/test.sh && '76                'docker exec -it containerid1 /tmp/test.sh && '77                'docker exec -it containerid1 bash') == \78            connections.DockerConnection.copy('containerid1', args)79@patch('scripts.connections.check_output_as_list')80class TestDockerComposeConnection:81    compose_containers = ['one', 'two']82    def _setup_sife_effect(self, output_mock):83        def side_effect(*args, **kwargs):84            if args[0] == 'docker-compose ps --filter="status=running" --services':85                return self.compose_containers86            if args[0] == 'docker-compose ps -q one':87                return ['containerid1']88            if args[0] == 'docker-compose ps -q two':89                return ['containerid2']90        output_mock.side_effect = side_effect91    def test_hosts_approximate(self, output_mock):92        args = MagicMock()93        args.hosts = ['ne']94        args.approximate = True95        self._setup_sife_effect(output_mock)96        assert ['containerid1'] == connections.DockerComposeConnection.hosts(args)97        args.hosts = ['o']98        assert ['containerid1', 'containerid2'] == connections.DockerComposeConnection.hosts(args)99        args.hosts = ['blah']100        with pytest.raises(ValueError):101            connections.DockerComposeConnection.hosts(args)102        args.hosts = ['ne', 'blah']103        assert ['containerid1'] == connections.DockerComposeConnection.hosts(args)104@patch('scripts.connections.check_output_as_list')105class TestSSHDockerConnection:106    host1_docker_containers = ['one,containerid1', 'two,containerid2']107    host2_docker_containers = ['one2,containerid12', 'two2,containerid22']108    def _setup_sife_effect(self, output_mock):109        def side_effect(*args, **kwargs):110            if args[0] == "ssh host1 docker ps --format '{{.Names}},{{.ID}}'":111                return self.host1_docker_containers112            if args[0] == "ssh host2 docker ps --format '{{.Names}},{{.ID}}'":113                return self.host2_docker_containers114        output_mock.side_effect = side_effect115    def test_no_hosts(self, output_mock):116        """ When there are no hosts, there is an error """117        with pytest.raises(ValueError):118            connections.SSHDockerConnection.hosts(MagicMock())119    def test_no_docker_containers(self, output_mock):120        """ When no args.docker_containers are passed, return all running containers """121        args = MagicMock()122        args.hosts = ['host1']123        self._setup_sife_effect(output_mock)124        hosts = connections.SSHDockerConnection.hosts(args)125        assert hosts == ['host1,containerid1', 'host1,containerid2']126        # Whene there are multiple hosts, all the docker containers are127        # returned:128        args.hosts = ['host1', 'host2']129        hosts = connections.SSHDockerConnection.hosts(args)...test_config.py
Source:test_config.py  
...14    def test_empty_config(self):15        self.assertRaises(ConfigError, Config, "---")16    def test_broken_config(self):17        self.assertRaises(ConfigError, Config, "[")18    def test_no_hosts(self):19        self.assertRaises(ConfigError, Config, "defaults:")20    def test_get_hosts(self):21        c = Config("""22---23hosts:24  - address: 10.0.0.125    secret:  test126    name:    nas127  - address: 10.0.0.228    name:    nas229    secret:  test130  - address: 10.0.0.331    secret:  test132    name:    nas3...test_host.py
Source:test_host.py  
...17        config = {"ALLOWED_HOSTS": []}  # No hosts allowed18        self._setup_with_config(config)19        resp = self.client.get("/foo")20        self.assertEqual(resp.status_code, BadHost.code)21    def test_no_hosts(self):22        config = {"ALLOWED_HOSTS": None}  # All hosts allowed23        with self.assertLogs(logger="doku", level=logging.WARNING) as logs:24            self._setup_with_config(config)25        message = f"WARNING:doku:{host_middleware.LOG_MESSAGE}"26        self.assertIn(message, logs.output)27        resp = self.client.get("/foo")28        self.assertEqual(resp.status_code, 200)29    def test_localhost(self):30        config = {"ALLOWED_HOSTS": "localhost"}31        self._setup_with_config(config)32        resp = self.client.get("/foo")33        self.assertEqual(resp.status_code, 200)34    def test_other_host(self):35        config = {"ALLOWED_HOSTS": "example.org"}...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
