"""Offline Linux MongoDB deployment, PATH, daemon flags, log parser & shutdown verifier (Ch05 linux-install)."""
import json
import posixpath
import re
VERSION = "CPython 3.12.14 offline MongoDB document/query verifier (mongod absent)"
def case_linux_distro_dependency_and_tarball_layout():
deps = {
"rhel_centos": ["libcurl", "openssl"],
"ubuntu_18_debian_10": ["libcurl4", "openssl"],
"ubuntu_16_debian_9": ["libcurl3", "openssl"],
}
install_dir = "/usr/local/mongodb"
bin_dir = posixpath.join(install_dir, "bin")
path_env = f"{bin_dir}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
actual = {
"distro_profiles": len(deps),
"all_require_openssl": all("openssl" in v for v in deps.values()),
"bin_first_in_path": path_env.split(":")[0] == "/usr/local/mongodb/bin",
"shell_rc_files": ["~/.bashrc", "~/.profile"],
}
expected = {
"distro_profiles": 3,
"all_require_openssl": True,
"bin_first_in_path": True,
"shell_rc_files": ["~/.bashrc", "~/.profile"],
}
return {
"name": "linux_distro_dependency_and_tarball_layout",
"summary": "验证主流 Linux 发行版依赖包组合、tgz 解压目录布局与 PATH 环境变量配置。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_linux_data_log_dirs_and_mongod_flags():
cmd = "mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongod.log --fork"
tokens = cmd.split()
dbpath = tokens[tokens.index("--dbpath") + 1]
logpath = tokens[tokens.index("--logpath") + 1]
has_fork = "--fork" in tokens
logpath_is_file = posixpath.basename(logpath).endswith(".log")
actual = {
"dbpath": dbpath,
"logpath": logpath,
"logpath_is_file": logpath_is_file,
"daemon_fork": has_fork,
}
expected = {
"dbpath": "/var/lib/mongodb",
"logpath": "/var/log/mongodb/mongod.log",
"logpath_is_file": True,
"daemon_fork": True,
}
return {
"name": "linux_data_log_dirs_and_mongod_flags",
"summary": "校验 --dbpath 数据目录、--logpath 必须指向具体日志文件以及 --fork 后台守护参数。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_mongod_log_listener_parser():
log_sample = (
"2020-07-09T12:20:17.391+0800 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock\n"
"2020-07-09T12:20:17.392+0800 I NETWORK [listener] Listening on 127.0.0.1\n"
"2020-07-09T12:20:17.392+0800 I NETWORK [listener] waiting for connections on port 27017\n"
)
sock = re.search(r"Listening on (/tmp/mongodb-\d+\.sock)", log_sample).group(1)
host = re.search(r"Listening on (\d+\.\d+\.\d+\.\d+)", log_sample).group(1)
port = int(re.search(r"waiting for connections on port (\d+)", log_sample).group(1))
actual = {"socket": sock, "bind_ip": host, "port": port}
expected = {"socket": "/tmp/mongodb-27017.sock", "bind_ip": "127.0.0.1", "port": 27017}
return {
"name": "mongod_log_listener_parser",
"summary": "解析 mongod.log 网络监听日志,确认 Unix Socket、127.0.0.1 绑定与 27017 端口就绪。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_shell_crud_and_graceful_shutdown():
state = {"running": True, "current_db": "test", "docs": []}
state["docs"].append({"_id": "5f069bdb4e02f8baf90f1184", "x": 10})
# Shutdown requires admin database in shell
denied_in_test = state["current_db"] != "admin"
state["current_db"] = "admin"
if state["current_db"] == "admin":
state["running"] = False
actual = {
"arith_checks": [2 + 2, 3 + 6],
"inserted_x": state["docs"][0]["x"],
"shutdown_requires_admin": denied_in_test,
"stopped_cleanly": not state["running"],
}
expected = {
"arith_checks": [4, 9],
"inserted_x": 10,
"shutdown_requires_admin": True,
"stopped_cleanly": True,
}
return {
"name": "shell_crud_and_graceful_shutdown",
"summary": "验证 Shell 写入/检索以及在 admin 库调用 db.shutdownServer() 优雅关闭流程。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_linux_distro_dependency_and_tarball_layout(),
case_linux_data_log_dirs_and_mongod_flags(),
case_mongod_log_listener_parser(),
case_shell_crud_and_graceful_shutdown(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/mongodb-linux-install-lab/explore-linux-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-linux-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1751.zip
课程关联工程包(登记关联49章) · 含当前章节或实例 · mbskycom202612a1732.zip