moonharness

    Dependency-aware integration test orchestration in MoonBit

    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    6 hours ago
    Downloads
    2

    #MoonHarness

    MoonHarness 是用 MoonBit 编写的多进程集成测试编排引擎。配置先构建服务依赖图,再按就绪状态启动下游。运行结束时按逆拓扑顺序回收已启动的进程,并输出可定位到根因服务的 JSON 报告。

    #快速体验

    安装 MoonBit 工具链 和 Native C 编译环境后,在仓库根目录执行。示例会占用本机 TCP 端口 18080–18082。

    moon update moon build --target native cmd/moonharness examples/fixture moon run --target native cmd/moonharness -- validate --config examples/multi-service/config.json moon run --target native cmd/moonharness -- plan --config examples/multi-service/config.json moon run --target native cmd/moonharness -- run --config examples/multi-service/config.json -- _build/native/debug/build/examples/fixture/fixture.exe smoke moon run --target native cmd/moonharness -- report --latest

    MoonBit Native 构建产物使用 .exe 后缀,包括 Linux。示例启动 Auth、Storage 和 API 三个进程。Auth 和 Storage 可同时启动,API 等待两者就绪。Smoke 进程请求 API 的 /summary 并验证它确实组合了两个上游响应。启动计划是 [["auth","storage"],["api"]],服务回收顺序是 API、Storage、Auth。

    运行后,CLI 打印报告路径。.moonharness/<run-id>/ 包含 report.json、events.jsonl、services.json 和每个进程的两路日志。服务和场景可以通过 MOONHARNESS_FIXTURE_DIR 共享临时数据,回收后目录自动移除。退出状态为 0 表示成功,1 表示会话失败,2 表示调用、输入或报告写入错误。省略外部命令会在服务全部就绪之后立即结束会话。

    #配置与实现

    Linux 需要 PATH 中的 util-linux setsid。超时、服务崩溃或收到中断时,运行器回收服务和场景进程组内的后代。仅运行受信任的本地配置和程序。参见 安全说明。

    ConfigError

    pub suberror ConfigError {
    InvalidConfig(String, String)
    }

    A configuration diagnostic with a JSON field path.
    impl Show for ConfigError

    ConfigError::output

    fn ConfigError::output(self : ConfigError, logger : &Logger) -> Unit

    ConfigError::to_string

    fn ConfigError::to_string(self : ConfigError) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    Config

    pub(all) struct Config {
    limits : Limits
    services : Array[Service]
    }

    Graph

    pub struct Graph {
    nodes : Array[String]
    indexes : Map[String, Int]
    incoming : Array[Array[Int]]
    outgoing : Array[Array[Int]]
    sorted : Array[Int]
    tiers : Array[Array[Int]]
    }

    Stable dependency order with indexed incoming and outgoing edges.

    Graph::affected

    fn Graph::affected(self : Graph, name : String) -> Array[String]?

    Return transitive dependents in stable topological order.

    Graph::build

    fn Graph::build(services : Array[Service]) -> Graph raise ConfigError

    Build a graph using declaration order to break ties within each layer.

    Graph::dependencies

    fn Graph::dependencies(self : Graph, name : String) -> Array[String]?

    Graph::layers

    fn Graph::layers(self : Graph) -> Array[Array[String]]

    Graph::schedule

    fn Graph::schedule(self : Graph, max_parallel : Int) -> Schedule

    Graph::shutdown_order

    fn Graph::shutdown_order(self : Graph) -> Array[String]

    Graph::startup_order

    fn Graph::startup_order(self : Graph) -> Array[String]

    Limits

    pub(all) struct Limits {
    max_parallel : Int
    startup_timeout_ms : Int
    shutdown_grace_ms : Int
    scenario_timeout_ms : Int
    }

    Timing and resource bounds for one session, in milliseconds.

    Probe

    pub(all) enum Probe {
    Alive
    Tcp(String, Int, Int)
    Http(String, Int, String, Int, Int)
    }

    Readiness is either process liveness, TCP connectivity, or a local HTTP status.

    Schedule

    pub struct Schedule {
    graph : Graph
    max_parallel : Int
    // private fields
    }

    State used to gate service dispatch independently of the process backend.

    Schedule::complete

    fn Schedule::complete(self : Schedule) -> Bool

    Schedule::dispatch

    fn Schedule::dispatch(self : Schedule) -> Array[Int]

    Reserve currently eligible services in deterministic order. Ready services release startup slots even though their processes continue running.

    Schedule::has_dispatched

    fn Schedule::has_dispatched(self : Schedule, index : Int) -> Bool

    Schedule::ready

    fn Schedule::ready(self : Schedule, index : Int) -> Unit

    Service

    pub(all) struct Service {
    name : String
    program : String
    args : Array[String]
    cwd : String?
    depends_on : Array[String]
    ready : Probe
    }

    An executable and argument vector with its dependency and readiness contract.

    parse_config

    fn parse_config(input : String) -> Config raise ConfigError

    Parse a version 1 JSON config, rejecting unknown fields and unsafe names.