File size: 2,068 Bytes
85b7ac8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from server.models import FileContent, FileType
from server.simulators.docker_simulator import DockerSimulator
from server.simulators.workflow_simulator import WorkflowSimulator


def _fc(path: str, content: str, file_type: FileType = FileType.OTHER) -> FileContent:
    return FileContent(path=path, content=content, file_type=file_type, line_count=content.count("\n") + 1)


def test_docker_simulator_catches_missing_copy_source():
    sim = DockerSimulator()
    dockerfile = _fc("Dockerfile", "FROM python:3.11-slim\nCOPY missing.txt .", FileType.DOCKERFILE)
    result = sim.validate(dockerfile, {"Dockerfile": dockerfile})
    assert result["build_success"] is False
    assert "missing.txt" in result.get("error", "")


def test_docker_simulator_detects_runtime_workdir_issue():
    sim = DockerSimulator()
    dockerfile = _fc(
        "Dockerfile",
        "FROM node:18-alpine\nCOPY package*.json ./\nRUN npm ci\nCOPY . .\nCMD [\"npm\", \"start\"]",
        FileType.DOCKERFILE,
    )
    context = {
        "Dockerfile": dockerfile,
        "package.json": _fc("package.json", '{"name":"app"}'),
    }
    result = sim.validate(dockerfile, context)
    assert result["build_success"] is True
    assert result["run_success"] is False


def test_workflow_simulator_catches_missing_secrets():
    sim = WorkflowSimulator()
    wf = _fc(
        ".github/workflows/build.yml",
        "name: Build\non: push\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - run: echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin",
        FileType.WORKFLOW,
    )
    result = sim.validate(wf, {".github/workflows/build.yml": wf})
    assert result["parse_success"] is True
    assert result["execution_success"] is False


def test_workflow_simulator_catches_yaml_errors():
    sim = WorkflowSimulator()
    wf = _fc(".github/workflows/build.yml", "jobs:\n  build: [", FileType.WORKFLOW)
    result = sim.validate(wf, {".github/workflows/build.yml": wf})
    assert result["parse_success"] is False