蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

校验 Ubuntu APT 安装指令、postgres 会话提示符、服务启停命令及本地 SQL 冒烟

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-linux-install-postgresql.py
explore-linux-install-postgresql.py117 行
"""Linux 上安装 PostgreSQL 离线实验:校验 Ubuntu APT 安装指令、postgres 超级用户会话、服务控制与本地 SQL 冒烟测试。"""
import json
import platform
import shlex
import sqlite3


def run() -> dict:
    cases = []

    # Case 1: Ubuntu apt-get installation plan
    install_cmds = [
        "sudo apt-get update",
        "sudo apt-get install postgresql postgresql-client",
    ]
    parsed = [shlex.split(c) for c in install_cmds]
    packages = parsed[1][3:]
    exp1 = {
        "has_update": True,
        "installer": "apt-get",
        "packages": ["postgresql", "postgresql-client"],
    }
    act1 = {
        "has_update": parsed[0][1:3] == ["apt-get", "update"],
        "installer": parsed[1][1],
        "packages": packages,
    }
    cases.append(
        {
            "name": "ubuntu_apt_install_plan",
            "summary": "校验 Ubuntu 下通过 apt-get 更新软件源并安装服务端与客户端工具包的命令结构",
            "expected": exp1,
            "actual": act1,
            "passed": act1 == exp1,
        }
    )

    # Case 2: postgres superuser switch, psql prompt & \q exit
    switch_tokens = shlex.split("sudo -i -u postgres")
    prompt_superuser = "postgres=#"
    prompt_regular = "postgres=>"
    exit_cmd = "\\q"
    exp2 = {
        "target_user": "postgres",
        "superuser_prompt_suffix": "=#",
        "regular_prompt_suffix": "=>",
        "quit_metacommand": "\\q",
    }
    act2 = {
        "target_user": switch_tokens[switch_tokens.index("-u") + 1],
        "superuser_prompt_suffix": prompt_superuser[-2:],
        "regular_prompt_suffix": prompt_regular[-2:],
        "quit_metacommand": exit_cmd,
    }
    cases.append(
        {
            "name": "postgres_superuser_psql_session",
            "summary": "校验切换 postgres 系统用户、psql 超级用户提示符(=#)及退出元命令 \\q",
            "expected": exp2,
            "actual": act2,
            "passed": act2 == exp2,
        }
    )

    # Case 3: Service control actions (SysV init & systemd mapping)
    sysv_cmds = [
        "sudo /etc/init.d/postgresql start",
        "sudo /etc/init.d/postgresql stop",
        "sudo /etc/init.d/postgresql restart",
    ]
    actions = [shlex.split(c)[-1] for c in sysv_cmds]
    systemd_equiv = {act: f"sudo systemctl {act} postgresql" for act in actions}
    exp3 = {
        "actions": ["start", "stop", "restart"],
        "systemd_restart": "sudo systemctl restart postgresql",
    }
    act3 = {
        "actions": actions,
        "systemd_restart": systemd_equiv["restart"],
    }
    cases.append(
        {
            "name": "service_lifecycle_commands",
            "summary": "核对 /etc/init.d/postgresql 启停重启命令及其现代 systemctl 对应写法",
            "expected": exp3,
            "actual": act3,
            "passed": act3 == exp3,
        }
    )

    # Case 4: Post-install SQL smoke test in memory SQLite
    conn = sqlite3.connect(":memory:")
    conn.execute("CREATE TABLE install_check (id INTEGER PRIMARY KEY, platform TEXT NOT NULL);")
    conn.execute("INSERT INTO install_check VALUES (1, 'Ubuntu Linux');")
    row = conn.execute("SELECT id, platform FROM install_check;").fetchone()
    conn.close()
    exp4 = [1, "Ubuntu Linux"]
    act4 = list(row)
    cases.append(
        {
            "name": "post_install_sql_smoke_test",
            "summary": "通过内存 SQLite 执行建表、写入与查询验证安装后的基础 SQL 冒烟流程",
            "expected": exp4,
            "actual": act4,
            "passed": act4 == exp4,
        }
    )

    return {
        "version": f"CPython {platform.python_version()} / SQLite {sqlite3.sqlite_version} (offline PostgreSQL verifier)",
        "cases": cases,
    }


if __name__ == "__main__":
    print(json.dumps(run(), ensure_ascii=False, indent=2))
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/postgresql-linux-install-postgresql-lab/explore-linux-install-postgresql.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-linux-install-postgresql.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PostgreSQL

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1653.zip

下载课程关联工程包:PostgreSQL

课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a1633.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习