wasmCloud / wadm

wasmCloud Application Deployment Manager (wadm) is a Wasm-native orchestrator for managing and scaling declarative wasmCloud applications.
https://wasmcloud.com
Apache License 2.0
108 stars 28 forks source link

[CHORE] Simplify e2e test setup with shared logic #456

Open brooksmtownsend opened 1 month ago

brooksmtownsend commented 1 month ago

All of the e2e tests currently start with some variation of this code in order to setup the proper lattices & expected number of hosts:

    let mut client_info = ClientInfo::new(manifest_dir, compose_file).await;
    client_info
        .add_ctl_client(SHARED_COMPONENTS_LATTICE, None)
        .await;
    client_info.add_wadm_client(SHARED_COMPONENTS_LATTICE).await;
    client_info
        .add_ctl_client(SHARED_PROVIDERS_LATTICE, None)
        .await;
    client_info.add_wadm_client(SHARED_PROVIDERS_LATTICE).await;
    client_info.add_ctl_client(INVALID_TEST_LATTICE, None).await;
    client_info.add_wadm_client(INVALID_TEST_LATTICE).await;
    client_info.launch_wadm().await;

    // Wait for the first event on the lattice prefix before we start deploying and checking
    // statuses. Wadm can absolutely handle hosts starting before you start the wadm process, but the first event
    // on the lattice will initialize the lattice monitor and for the following test we quickly assert things.
    let mut sub = client_info
        .client
        .subscribe("wasmbus.evt.*.>".to_string())
        .await
        .expect("Should be able to subscribe to default events");
    // Host heartbeats happen every 30 seconds, if we don't get a heartbeat in 2 minutes, bail.
    let _ = tokio::time::timeout(std::time::Duration::from_secs(120), sub.next())
        .await
        .expect("should have received a host heartbeat event before timeout");

    // Wait for hosts to start
    let mut did_start = false;
    for _ in 0..10 {
        match (
            client_info
                .ctl_client(SHARED_COMPONENTS_LATTICE)
                .get_hosts()
                .await,
            client_info
                .ctl_client(SHARED_PROVIDERS_LATTICE)
                .get_hosts()
                .await,
        ) {
            (Ok(hosts_one), Ok(hosts_two)) if hosts_one.len() == 2 && hosts_two.len() == 2 => {
                eprintln!(
                    "Hosts {}/2, {}/2 currently available",
                    hosts_one.len(),
                    hosts_two.len()
                );
                did_start = true;
                break;
            }
            (Ok(hosts_one), Ok(hosts_two)) => {
                eprintln!(
                    "Waiting for all hosts to be available, {}/2, {}/2 currently available",
                    hosts_one.len(),
                    hosts_two.len()
                );
            }
            (Err(e), _) | (_, Err(e)) => {
                eprintln!("Error when fetching hosts: {e}",)
            }
        }
        tokio::time::sleep(Duration::from_secs(1)).await;
    }

    if !did_start {
        panic!("Hosts didn't start")
    }

What would be really nice is for the common e2e logic to have a helper function that takes a list of lattices & expected hosts in that lattice to simplify the setup of e2e tests.

This is a good first issue for someone looking to make an impact on test reusability 😄