metabase_smoketest/
metabase-smoketest.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use std::time::Duration;

use anyhow::{bail, Context};
use itertools::Itertools;
use tokio::net::TcpStream;
use tokio_postgres::NoTls;
use tracing::debug;

use mz_metabase::{
    DatabaseMetadata, LoginRequest, SetupDatabase, SetupDatabaseDetails, SetupPrefs, SetupRequest,
    SetupUser, Table, TableField,
};
use mz_ore::retry::Retry;
use mz_ore::task;

const DUMMY_EMAIL: &str = "ci@materialize.io";
const DUMMY_PASSWORD: &str = "dummydummy1";

async fn connect_materialized() -> Result<tokio_postgres::Client, anyhow::Error> {
    Retry::default()
        .retry_async(|_| async {
            let res = TcpStream::connect("materialized:6875").await;
            if let Err(e) = &res {
                debug!("error connecting to materialized: {}", e);
            }
            res
        })
        .await?;
    let (client, conn) = tokio_postgres::connect(
        "postgres://materialize@materialized:6875/materialize",
        NoTls,
    )
    .await
    .context("failed connecting to materialized")?;
    task::spawn(|| "metabase_smoketest_mz", async {
        if let Err(e) = conn.await {
            panic!("postgres connection error: {}", e);
        }
    });
    Ok(client)
}

async fn connect_metabase() -> Result<mz_metabase::Client, anyhow::Error> {
    let mut client = mz_metabase::Client::new("http://metabase:3000")
        .context("failed creating metabase client")?;
    let setup_token = Retry::default()
        .max_duration(Duration::from_secs(30))
        .retry_async(|_| async {
            let res = client.session_properties().await;
            if let Err(e) = &res {
                debug!("error connecting to metabase: {}", e);
            }
            res.map(|res| res.setup_token)
        })
        .await?;
    let session_id = match setup_token {
        None => {
            let req = LoginRequest {
                username: DUMMY_EMAIL.into(),
                password: DUMMY_PASSWORD.into(),
            };
            client.login(&req).await?.id
        }
        Some(setup_token) => {
            let req = &SetupRequest {
                allow_tracking: false,
                database: SetupDatabase {
                    engine: "postgres".into(),
                    name: "Materialize".into(),
                    details: SetupDatabaseDetails {
                        host: "materialized".into(),
                        port: 6875,
                        dbname: "materialize".into(),
                        user: "materialize".into(),
                    },
                },
                token: setup_token,
                prefs: SetupPrefs {
                    site_name: "Materialize".into(),
                },
                user: SetupUser {
                    email: DUMMY_EMAIL.into(),
                    first_name: "Materialize".into(),
                    last_name: "CI".into(),
                    password: DUMMY_PASSWORD.into(),
                    site_name: "Materialize".into(),
                },
            };
            client.setup(req).await?.id
        }
    };
    client.set_session_id(session_id);
    Ok(client)
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    mz_ore::test::init_logging();

    let pgclient = connect_materialized().await?;
    pgclient
        .batch_execute(
            "CREATE OR REPLACE MATERIALIZED VIEW orders (id, date, quantity, total) AS
             VALUES (1, '2020-01-03'::date, 6, 10.99), (2, '2020-01-04'::date, 4, 7.48)",
        )
        .await?;

    let metabase_client = connect_metabase().await?;

    let databases = metabase_client.databases().await?;
    debug!("Databases: {:#?}", databases);

    let database_names: Vec<_> = databases.iter().map(|d| &d.name).sorted().collect();
    assert_eq!(database_names, &["Materialize", "Sample Dataset"]);

    let mzdb = databases.iter().find(|d| d.name == "Materialize").unwrap();
    let expected_metadata = DatabaseMetadata {
        tables: vec![Table {
            name: "orders".into(),
            schema: "public".into(),
            fields: vec![
                TableField {
                    name: "date".into(),
                    database_type: "date".into(),
                    base_type: "type/Date".into(),
                    special_type: None,
                },
                TableField {
                    name: "id".into(),
                    database_type: "int4".into(),
                    base_type: "type/Integer".into(),
                    special_type: None,
                },
                TableField {
                    name: "quantity".into(),
                    database_type: "int4".into(),
                    base_type: "type/Integer".into(),
                    special_type: None,
                },
                TableField {
                    name: "total".into(),
                    database_type: "numeric".into(),
                    base_type: "type/Decimal".into(),
                    special_type: None,
                },
            ],
        }],
    };
    // The database sync happens asynchronously and the API doesn't appear to
    // expose when it is complete, so just retry a few times waiting for the
    // metadata we expect.
    Retry::default()
        .retry_async(|_| async {
            let mut metadata = metabase_client.database_metadata(mzdb.id).await?;
            metadata.tables.retain(|t| t.schema == "public");
            metadata.tables.sort_by(|a, b| a.name.cmp(&b.name));
            for t in &mut metadata.tables {
                t.fields.sort_by(|a, b| a.name.cmp(&b.name));
            }
            debug!("Materialize database metadata: {:#?}", metadata);
            if expected_metadata != metadata {
                bail!(
                    "metadata did not match\nexpected:\n{:#?}\nactual:\n{:#?}",
                    expected_metadata,
                    metadata,
                );
            }
            Ok(())
        })
        .await?;

    println!("OK");
    Ok(())
}