Skip to content

Commit ebf45f0

Browse files
authored
Separate workflow (#9)
* seprarating the build process to build_environment * updates * remove db containers after testing; check and remove old containers, and restart new container if old container exists * remove init_sql from mysql/config * extract build-relevant function to build.py; add build command * update readme
1 parent db903c5 commit ebf45f0

9 files changed

Lines changed: 282 additions & 221 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ sqlancer-logs/
44
__pycache__/
55
**/__pycache__/
66

7-
7+
Dockerfile
88
.env

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,37 @@ A suite of automation scripts that streamline the sqlancer testing process.
88
- Docker
99

1010
## Using Auto-sqlancer
11-
### 1. Test all DBMSs
11+
### 1. Build Images
12+
#### Build all DBMS images
13+
```bash
14+
python3 start.py build --dbms all
15+
```
16+
17+
- --dbms all: build all the database images
18+
19+
20+
#### Build a single DBMS
21+
22+
```bash
23+
python3 start.py build --dbms <dbms_name>
24+
25+
```
26+
27+
- <dbms_name>: e.g., mysql, postgres, sqlite, etc.
28+
29+
30+
- Example:
31+
```
32+
python3 start.py build --dbms mysql
33+
```
34+
35+
#### Build sqlancer image
36+
```bash
37+
python3 start.py build --sqlancer
38+
```
1239

40+
### 2. Test
41+
#### Test all DBMSs
1342
```bash
1443
python3 start.py test --dbms all [--cache]
1544
```
@@ -18,7 +47,7 @@ python3 start.py test --dbms all [--cache]
1847

1948
- --cache(optional): skip pulling/building Docker image
2049

21-
### 2. Test a single DBMS
50+
#### Test a single DBMS
2251

2352
```bash
2453
python3 start.py test --dbms <dbms_name> --config <path/to/config.json> [--cache]
@@ -34,7 +63,7 @@ python3 start.py test --dbms <dbms_name> --config <path/to/config.json> [--cache
3463
python3 start.py test --dbms mysql --config mysql/config.json
3564
```
3665

37-
### 3. Test a Custom Dockerfile-Based DBMS Container
66+
#### Test a Custom Dockerfile-Based DBMS Container
3867
```bash
3968
python3 start.py test --dockerfile <path/to/Dockerfile> --config <path/to/config.json> [--cache]
4069
```
@@ -45,5 +74,5 @@ python3 start.py test --dockerfile <path/to/Dockerfile> --config <path/to/config
4574

4675
- Example:
4776
```
48-
python3 start.py test --dockerfile ./Dockerfile --config mysql/config.json
77+
python3 start.py test --dockerfile ./Dockerfile --config ./config.json
4978
```

build.py

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,58 @@
22
import sys
33
import subprocess
44

5-
def build_sqlancer():
6-
subprocess.run(["docker", "build", "-t", "sqlancer:latest", "./sqlancer"], check=True)
5+
def run_command(cmd, **kwargs):
6+
print("[CMD]", " ".join(cmd))
7+
subprocess.run(cmd, check=True, **kwargs)
78

8-
def build_db(dbms, version):
9-
tag = f"{dbms}:{version}"
10-
subprocess.run(["docker", "pull", tag], check=True)
9+
def build_sqlancer_image(force_rebuild=False):
10+
if force_rebuild:
11+
print("[INFO] Rebuilding SQLancer image due to --cache not specified...")
12+
run_command(["docker", "build", "--no-cache", "-t", "sqlancer:latest", "./sqlancer"])
13+
return
1114

12-
if __name__ == "__main__":
13-
cmd = sys.argv[1]
14-
if cmd == "sqlancer":
15-
build_sqlancer()
16-
elif cmd == "db":
17-
dbms = sys.argv[2]
18-
version = sys.argv[4]
19-
build_db(dbms, version)
15+
result = subprocess.run(
16+
["docker", "images", "--format", "{{.Repository}}:{{.Tag}}"],
17+
capture_output=True, text=True
18+
)
19+
images = result.stdout.strip().splitlines()
20+
if "sqlancer:latest" in images:
21+
print("[INFO] SQLancer image already exists: sqlancer:latest")
22+
else:
23+
print("[INFO] SQLancer image not found. Building from ./sqlancer...")
24+
run_command(["docker", "build", "-t", "sqlancer:latest", "./sqlancer"])
25+
26+
def build_network(network_name="sqlancer-net"):
27+
try:
28+
output = subprocess.check_output([
29+
"docker", "network", "ls",
30+
"--filter", f"name={network_name}",
31+
"--format", "{{.Name}}"
32+
])
33+
networks = output.decode().splitlines()
34+
if network_name not in networks:
35+
print(f"[INFO] Creating docker network: {network_name}")
36+
subprocess.run(["docker", "network", "create", network_name], check=True)
37+
else:
38+
print(f"[INFO] Docker network '{network_name}' already exists.")
39+
except Exception as e:
40+
print(f"[ERROR] Failed to check/create Docker network: {e}")
41+
sys.exit(1)
42+
43+
def build_db_image(cfg, use_cache, custom=False, dockerfile_path=""):
44+
if not use_cache and not custom:
45+
image = cfg["image"]
46+
print(f"[INFO] Pulling image {image}")
47+
run_command(["docker", "pull", image])
48+
49+
if custom:
50+
build_cmd = ["docker", "build", "-t", cfg["image"], os.path.dirname(dockerfile_path)]
51+
if not use_cache:
52+
build_cmd.insert(2, "--no-cache")
53+
run_command(build_cmd)
54+
55+
def build_environment(cfg, use_cache, custom=False, dockerfile_path=""):
56+
build_network()
57+
build_sqlancer_image(force_rebuild=not use_cache)
58+
build_db_image(cfg, use_cache, custom, dockerfile_path)
59+

config.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{
22
"dbms_list": ["mysql", "postgres"],
33
"dbms": "mysql",
4+
"container_name": "mysql-custom",
5+
"image": "mysql-custom",
46
"version": "custom",
7+
"env": {
8+
"MYSQL_ROOT_PASSWORD": "12345678"
9+
},
510
"username": "root",
611
"password": "12345678",
12+
"oracle": "FUZZER",
713
"num_threads": 4,
8-
"timeout_seconds": 60,
9-
"oracle": "FUZZER"
14+
"timeout_seconds": 60
1015
}
16+
17+
18+

mysql/config.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@
99
"password": "12345678",
1010
"oracle": "FUZZER",
1111
"num_threads": 4,
12-
"timeout_seconds": 60,
13-
"init_sql": "CREATE DATABASE test;",
14-
"init_sql_command": "docker exec {container_name} mysql -u{username} -p{password} -e \"{sql}\""
12+
"timeout_seconds": 60
1513
}

run_test.py

Lines changed: 0 additions & 193 deletions
This file was deleted.

sqlancer/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ echo "Running: $CMD" | tee -a "$LOG_FILE"
2727
# Run the command and tee output to both console and log file
2828
eval "$CMD" 2>&1 | tee -a "$LOG_FILE"
2929

30-
echo "[INFO] SQLancer finished. Logs saved to $LOG_FILE"
30+
echo "[INFO] SQLancer finished. Logs saved to $LOG_DIR"

0 commit comments

Comments
 (0)