Skip to content

Commit

Permalink
#391: resolve nginx test flakiness
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderankin committed Nov 21, 2023
1 parent 928af5a commit e30a1c5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions nginx/testcontainers/nginx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import urllib.error
import urllib.parse
import urllib.request

from testcontainers.core.container import DockerContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.waiting_utils import wait_container_is_ready


class NginxContainer(DockerContainer):
Expand All @@ -20,3 +25,17 @@ def __init__(self, image: str = "nginx:latest", port: int = 80, **kwargs) -> Non
super(NginxContainer, self).__init__(image, **kwargs)
self.port = port
self.with_exposed_ports(self.port)

def start(self) -> 'NginxContainer':
super().start()

host = self.get_container_host_ip()
port = str(self.get_exposed_port(self.port))
self._connect(host, port)

return self

@wait_container_is_ready(urllib.error.URLError)
def _connect(self, host: str, port: str) -> None:
url = urllib.parse.urlunsplit(('http', f'{host}:{port}', '', '', ''))
urllib.request.urlopen(url, timeout=1)

0 comments on commit e30a1c5

Please sign in to comment.