def test_set_recipe():
    d = m.DockerClass()
    d.set_recipe("recipe")
    assert d.recipe == "recipe"
    with pytest.raises(ValueError):
        d.set_recipe("")
    with pytest.raises(TypeError):
        d.set_recipe(3)
def test_get_build_command():
    d = m.DockerClass()
    with pytest.raises(ValueError):
        d.get_build_command()
    d.set_recipe_name("recipe_name")
    with pytest.raises(ValueError):
        d.get_build_command()
    d.set_image_name("image_name")
    assert d.get_build_command(
    ) == "docker build -t image_name -f recipe_name ."
def test_write_recipe():
    d = m.DockerClass()
    with pytest.raises(ValueError):
        d.write_recipe()
    d.set_recipe_name("recipe_name")
    with pytest.raises(ValueError):
        d.write_recipe()
    d.set_recipe("recipe")
    d.write_recipe()
    with open("recipe_name", "r") as file:
        recipe = file.read()
        assert recipe == d.recipe
        # Clean up
        os.remove("recipe_name")
def test_build_image():
    image_name = "docker_image_test"
    recipe_name = "Dockerfile_test"
    recipe = "FROM alpine:3.7\n"
    d = m.DockerClass()
    d.set_recipe_name(recipe_name)
    d.set_image_name(image_name)
    d.set_recipe(recipe)
    d.write_recipe()
    d.build_image()
    # Check whether the image is actually created
    assert subprocess.check_output(["docker", "images", "-q", image_name],
                                   text=True) != ""
    # Clean up
    subprocess.run(["docker", "rmi", image_name])
    # Note that the base image is not removed, just in case it's being used by others
    os.remove(recipe_name)
def test_set_image_name():
    d = m.DockerClass()
    d.set_image_name("good_name")
    assert d.image_name == "good_name"
    with pytest.raises(ValueError):
        d.set_image_name("bad name")