chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
import pytest
|
||||
|
||||
from e2b import AsyncTemplate
|
||||
from e2b.template.types import InstructionType
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_from_dockerfile():
|
||||
dockerfile = """FROM node:24
|
||||
WORKDIR /app
|
||||
COPY package.json .
|
||||
RUN npm install
|
||||
ENTRYPOINT ["sleep", "20"]"""
|
||||
|
||||
template = AsyncTemplate().from_dockerfile(dockerfile)
|
||||
|
||||
# base image
|
||||
assert template._template._base_image == "node:24"
|
||||
|
||||
instructions = template._template._instructions
|
||||
|
||||
# Docker defaults
|
||||
assert instructions[1]["type"] == InstructionType.WORKDIR
|
||||
assert instructions[1]["args"][0] == "/"
|
||||
|
||||
# Instructions from Dockerfile
|
||||
assert instructions[2]["type"] == InstructionType.WORKDIR
|
||||
assert instructions[2]["args"][0] == "/app"
|
||||
|
||||
assert instructions[3]["type"] == InstructionType.COPY
|
||||
assert instructions[3]["args"][0] == "package.json"
|
||||
assert instructions[3]["args"][1] == "."
|
||||
|
||||
assert instructions[4]["type"] == InstructionType.RUN
|
||||
assert instructions[4]["args"][0] == "npm install"
|
||||
|
||||
# E2B defaults appended
|
||||
assert instructions[5]["type"] == InstructionType.USER
|
||||
assert instructions[5]["args"][0] == "user"
|
||||
|
||||
# Start command
|
||||
assert template._template._start_cmd == "sleep 20"
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_from_dockerfile_with_default_user_and_workdir():
|
||||
dockerfile = "FROM node:24"
|
||||
|
||||
template = AsyncTemplate().from_dockerfile(dockerfile)
|
||||
|
||||
assert template._template._instructions[-2]["type"] == InstructionType.USER
|
||||
assert template._template._instructions[-2]["args"][0] == "user"
|
||||
assert template._template._instructions[-1]["type"] == InstructionType.WORKDIR
|
||||
assert template._template._instructions[-1]["args"][0] == "/home/user"
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_from_dockerfile_with_custom_user_and_workdir():
|
||||
dockerfile = "FROM node:24\nUSER mish\nWORKDIR /home/mish"
|
||||
|
||||
template = AsyncTemplate().from_dockerfile(dockerfile)
|
||||
|
||||
assert template._template._instructions[-2]["type"] == InstructionType.USER
|
||||
assert template._template._instructions[-2]["args"][0] == "mish"
|
||||
assert template._template._instructions[-1]["type"] == InstructionType.WORKDIR
|
||||
assert template._template._instructions[-1]["args"][0] == "/home/mish"
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_from_dockerfile_with_multi_source_copy():
|
||||
dockerfile = """FROM node:24
|
||||
COPY file1.txt file2.txt file3.txt /dest/"""
|
||||
|
||||
template = AsyncTemplate().from_dockerfile(dockerfile)
|
||||
|
||||
instructions = template._template._instructions
|
||||
|
||||
copy_instructions = [i for i in instructions if i["type"] == InstructionType.COPY]
|
||||
|
||||
assert len(copy_instructions) == 3
|
||||
assert copy_instructions[0]["args"][0] == "file1.txt"
|
||||
assert copy_instructions[0]["args"][1] == "/dest/"
|
||||
assert copy_instructions[1]["args"][0] == "file2.txt"
|
||||
assert copy_instructions[1]["args"][1] == "/dest/"
|
||||
assert copy_instructions[2]["args"][0] == "file3.txt"
|
||||
assert copy_instructions[2]["args"][1] == "/dest/"
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_from_dockerfile_with_multi_source_copy_chown():
|
||||
dockerfile = """FROM node:24
|
||||
COPY --chown=myuser:mygroup pkg.json pkg-lock.json /app/"""
|
||||
|
||||
template = AsyncTemplate().from_dockerfile(dockerfile)
|
||||
|
||||
instructions = template._template._instructions
|
||||
|
||||
copy_instructions = [i for i in instructions if i["type"] == InstructionType.COPY]
|
||||
|
||||
assert len(copy_instructions) == 2
|
||||
assert copy_instructions[0]["args"][0] == "pkg.json"
|
||||
assert copy_instructions[0]["args"][1] == "/app/"
|
||||
assert copy_instructions[0]["args"][2] == "myuser:mygroup"
|
||||
assert copy_instructions[1]["args"][0] == "pkg-lock.json"
|
||||
assert copy_instructions[1]["args"][1] == "/app/"
|
||||
assert copy_instructions[1]["args"][2] == "myuser:mygroup"
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_from_dockerfile_with_copy_chown():
|
||||
dockerfile = """FROM node:24
|
||||
COPY --chown=myuser:mygroup app.js /app/
|
||||
COPY --chown=anotheruser config.json /config/"""
|
||||
|
||||
template = AsyncTemplate().from_dockerfile(dockerfile)
|
||||
|
||||
instructions = template._template._instructions
|
||||
|
||||
# First COPY instruction (after initial USER root and WORKDIR /)
|
||||
copy_instruction1 = instructions[2]
|
||||
assert copy_instruction1["type"] == InstructionType.COPY
|
||||
assert copy_instruction1["args"][0] == "app.js"
|
||||
assert copy_instruction1["args"][1] == "/app/"
|
||||
assert copy_instruction1["args"][2] == "myuser:mygroup" # user from --chown
|
||||
|
||||
# Second COPY instruction
|
||||
copy_instruction2 = instructions[3]
|
||||
assert copy_instruction2["type"] == InstructionType.COPY
|
||||
assert copy_instruction2["args"][0] == "config.json"
|
||||
assert copy_instruction2["args"][1] == "/config/"
|
||||
assert (
|
||||
copy_instruction2["args"][2] == "anotheruser"
|
||||
) # user from --chown (without group)
|
||||
@@ -0,0 +1,32 @@
|
||||
import pytest
|
||||
|
||||
from e2b import AsyncTemplate
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_make_symlink(async_build):
|
||||
template = (
|
||||
AsyncTemplate()
|
||||
.from_image("ubuntu:22.04")
|
||||
.skip_cache()
|
||||
.make_symlink(".bashrc", ".bashrc.local")
|
||||
.run_cmd('test "$(readlink .bashrc.local)" = ".bashrc"')
|
||||
)
|
||||
|
||||
await async_build(template)
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_make_symlink_force(async_build):
|
||||
template = (
|
||||
AsyncTemplate()
|
||||
.from_image("ubuntu:22.04")
|
||||
.make_symlink(".bashrc", ".bashrc.local")
|
||||
.skip_cache()
|
||||
.make_symlink(
|
||||
".bashrc", ".bashrc.local", force=True
|
||||
) # Overwrite existing symlink
|
||||
.run_cmd('test "$(readlink .bashrc.local)" = ".bashrc"')
|
||||
)
|
||||
|
||||
await async_build(template)
|
||||
@@ -0,0 +1,40 @@
|
||||
import pytest
|
||||
|
||||
from e2b import AsyncTemplate
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_run_command(async_build):
|
||||
template = AsyncTemplate().from_image("ubuntu:22.04").skip_cache().run_cmd("ls -l")
|
||||
|
||||
await async_build(template)
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_run_command_as_different_user(async_build):
|
||||
template = (
|
||||
AsyncTemplate()
|
||||
.from_image("ubuntu:22.04")
|
||||
.skip_cache()
|
||||
.run_cmd('test "$(whoami)" = "root"', user="root")
|
||||
)
|
||||
|
||||
await async_build(template)
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_run_command_as_user_that_does_not_exist(async_build):
|
||||
template = (
|
||||
AsyncTemplate()
|
||||
.from_image("ubuntu:22.04")
|
||||
.skip_cache()
|
||||
.run_cmd("whoami", user="root123")
|
||||
)
|
||||
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
await async_build(template)
|
||||
|
||||
assert (
|
||||
"failed to run command 'whoami': command failed: unauthenticated: invalid username: 'root123'"
|
||||
in str(exc_info.value)
|
||||
)
|
||||
@@ -0,0 +1,57 @@
|
||||
import pytest
|
||||
|
||||
from e2b import AsyncTemplate
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_to_dockerfile():
|
||||
template = (
|
||||
AsyncTemplate()
|
||||
.from_ubuntu_image("24.04")
|
||||
.copy("README.md", "/app/README.md")
|
||||
.run_cmd('echo "Hello, World!"')
|
||||
)
|
||||
|
||||
dockerfile = AsyncTemplate.to_dockerfile(template)
|
||||
|
||||
expected_dockerfile = """FROM ubuntu:24.04
|
||||
COPY README.md /app/README.md
|
||||
RUN echo "Hello, World!"
|
||||
"""
|
||||
assert dockerfile == expected_dockerfile
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_to_dockerfile_with_options():
|
||||
template = (
|
||||
AsyncTemplate()
|
||||
.from_ubuntu_image("24.04")
|
||||
.copy("README.md", "/app/README.md", user="root")
|
||||
.run_cmd('echo "Hello, World!"', user="root")
|
||||
)
|
||||
|
||||
dockerfile = AsyncTemplate.to_dockerfile(template)
|
||||
|
||||
expected_dockerfile = """FROM ubuntu:24.04
|
||||
COPY README.md /app/README.md
|
||||
RUN echo "Hello, World!"
|
||||
"""
|
||||
assert dockerfile == expected_dockerfile
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
async def test_to_dockerfile_with_env_instructions():
|
||||
template = (
|
||||
AsyncTemplate()
|
||||
.from_ubuntu_image("24.04")
|
||||
.set_envs({"NODE_ENV": "production", "PORT": "8080"})
|
||||
.set_envs({"DEBUG": "false"})
|
||||
)
|
||||
|
||||
dockerfile = AsyncTemplate.to_dockerfile(template)
|
||||
|
||||
expected_dockerfile = """FROM ubuntu:24.04
|
||||
ENV NODE_ENV=production PORT=8080
|
||||
ENV DEBUG=false
|
||||
"""
|
||||
assert dockerfile == expected_dockerfile
|
||||
Reference in New Issue
Block a user