webserver.wait_for_unit("nginx") webserver.wait_for_open_port(80) # Preparations webserverRoot = "/var/lib/nginx-localhost/root" indexContent = "It works!" webserver.succeed(f"mkdir {webserverRoot}") webserver.succeed(f"echo '{indexContent}' > {webserverRoot}/index.html") webserver.succeed(f"chown -R nginx-localhost\: {webserverRoot}") # Helpers def expect_http_code(node, code, url): http_code = node.succeed( f"curl -s --no-location -o /dev/null -w '%{{http_code}}' '{url}'") assert http_code == code, \ f"expected {code} but got following response:\n{http_code}" def expect_http_location(node, location, url): redirect_url = node.succeed( f"curl -s --no-location -o /dev/null -w '%{{redirect_url}}' '{url}'") assert redirect_url == location, \ f"expected redirect to {location} but got:\n{redirect_url}" def expect_http_content(node, expectedContent, url): content = node.succeed(f"curl --no-location --silent '{url}'") assert content.strip() == expectedContent.strip(), \ f"expected:\n{expectedContent}\n at { url} but got following content:\n'{content}'" # Tests with subtest("website is successfully served on localhost"): expect_http_code(webserver, "200", "http://localhost/index.html") expect_http_content(webserver, indexContent, "http://localhost/index.html") with subtest("example.com is a hosts alias and redirects to localhost"): webserver.succeed("grep example.com /etc/hosts") url = "http://example.com/index.html" expect_http_code(webserver, "301", url) expect_http_location( webserver, "http://localhost/index.html", url)