Skip to content

Commit f6bec80

Browse files
daTokenizerSergeyPirogov
authored andcommitted
add redis and general container (#24)
* add elasticsearch container * add the Redis and General container types * fix name * remove unneeded urllib test
1 parent 5eb9e60 commit f6bec80

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

testcontainers/elasticsearch.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
from testcontainers.core.container import DockerContainer
14+
from testcontainers.core.waiting_utils import wait_container_is_ready
15+
import urllib
16+
17+
18+
class ElasticsearchContainer(DockerContainer):
19+
def __init__(self, image="elasticsearch:latest", port_to_expose=9200):
20+
super(ElasticsearchContainer, self).__init__(image)
21+
self.port_to_expose = port_to_expose
22+
self.with_exposed_ports(self.port_to_expose)
23+
cmd_dict = {
24+
'transport.host': '127.0.0.1',
25+
'http.host': '0.0.0.0',
26+
'discovery.zen.minimum_master_nodes': '1'
27+
}
28+
command = ' '.join(['-E{0}={1}'.format(k, v) for k, v in cmd_dict.items()])
29+
self.with_command(command)
30+
31+
@wait_container_is_ready()
32+
def _connect(self):
33+
port = self.get_exposed_port(self.port_to_expose)
34+
res = urllib.request.urlopen('http://127.0.0.1:{}'.format(port))
35+
if res.status != 200:
36+
raise Exception()
37+
38+
def start(self):
39+
super().start()
40+
self._connect()
41+
return self

testcontainers/general.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
from testcontainers.core.container import DockerContainer
14+
from testcontainers.core.waiting_utils import wait_container_is_ready
15+
16+
17+
class TestContainer(DockerContainer):
18+
def __init__(self, image, port_to_expose=None):
19+
super(RedisContainer, self).__init__(image)
20+
if port_to_expose:
21+
self.port_to_expose = port_to_expose
22+
self.with_exposed_ports(self.port_to_expose)

testcontainers/redis.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
from testcontainers.core.container import DockerContainer
14+
from testcontainers.core.waiting_utils import wait_container_is_ready
15+
16+
class RedisContainer(DockerContainer):
17+
def __init__(self, image="redis:latest", port_to_expose=6379):
18+
super(RedisContainer, self).__init__(image)
19+
self.port_to_expose = port_to_expose
20+
self.with_exposed_ports(self.port_to_expose)
21+

0 commit comments

Comments
 (0)