wash-cli 0.39.0

wasmCloud Shell (wash) CLI tool
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::{
    fs::{remove_dir_all, File},
    io::prelude::*,
};

use anyhow::{Context, Result};
use serde_json::json;

mod common;

use common::{
    fetch_artifact_digest, get_json_output, init, output_to_string, set_test_file_content,
    test_dir_file, test_dir_with_subfolder, wash,
};

const ECHO_WASM: &str = "ghcr.io/wasmcloud/components/http-hello-world-rust:0.1.0";
const LOGGING_PAR: &str = "wasmcloud.azurecr.io/logging:0.9.1";
const LOCAL_REGISTRY: &str = "localhost:5001";

#[test]
#[cfg_attr(not(can_reach_ghcr_io), ignore = "ghcr.io is not reachable")]
fn integration_reg_pull_basic() {
    const SUBFOLDER: &str = "pull_basic";
    let pull_dir = test_dir_with_subfolder(SUBFOLDER);

    let basic_echo = test_dir_file(SUBFOLDER, "basic_echo.wasm");

    let pull_basic = wash()
        .args([
            "pull",
            ECHO_WASM,
            "--destination",
            basic_echo.to_str().unwrap(),
            "--allow-latest",
        ])
        .output()
        .unwrap_or_else(|_| panic!("failed to pull {ECHO_WASM}"));
    assert!(pull_basic.status.success());
    // Very important
    assert!(output_to_string(pull_basic).unwrap().contains('\u{1F6BF}'));

    remove_dir_all(pull_dir).unwrap();
}

#[test]
#[cfg_attr(not(can_reach_ghcr_io), ignore = "ghcr.io is not reachable")]
fn integration_reg_pull_comprehensive() {
    const SUBFOLDER: &str = "pull_comprehensive";
    let pull_dir = test_dir_with_subfolder(SUBFOLDER);

    let comprehensive_echo = test_dir_file(SUBFOLDER, "comprehensive_echo.wasm");
    let comprehensive_logging = test_dir_file(SUBFOLDER, "comprehensive_logging.par.gz");

    let pull_echo_comprehensive = wash()
        .args([
            "pull",
            ECHO_WASM,
            "--destination",
            comprehensive_echo.to_str().unwrap(),
            "--digest",
            "sha256:079275a324c0fcd0c201878f0c158120c4984472215ec3f64eb91ba9ee139f72",
            "--output",
            "json",
        ])
        .output()
        .unwrap_or_else(|_| panic!("failed to pull {ECHO_WASM}"));

    assert!(pull_echo_comprehensive.status.success());
    let output = get_json_output(pull_echo_comprehensive).unwrap();

    let expected_json = json!({"file": comprehensive_echo.to_str().unwrap(), "success": true});

    assert_eq!(output, expected_json);

    let pull_logging_comprehensive = wash()
        .args([
            "pull",
            LOGGING_PAR,
            "--destination",
            comprehensive_logging.to_str().unwrap(),
            "--digest",
            "sha256:169f2764e529c2b57ad20abb87e0854d67bf6f0912896865e2911dee1bf6af98",
            "--output",
            "json",
        ])
        .output()
        .unwrap_or_else(|_| panic!("failed to pull {ECHO_WASM}"));

    assert!(pull_logging_comprehensive.status.success());
    let output = get_json_output(pull_logging_comprehensive).unwrap();

    let expected_json = json!({"file": comprehensive_logging.to_str().unwrap(), "success": true});

    assert_eq!(output, expected_json);

    remove_dir_all(pull_dir).unwrap();
}

// NOTE: This test will fail without a local docker registry running
#[test]
#[cfg_attr(not(can_reach_ghcr_io), ignore = "ghcr.io is not reachable")]
fn integration_reg_push_basic() {
    const SUBFOLDER: &str = "push_basic";
    let push_dir = test_dir_with_subfolder(SUBFOLDER);

    let pull_echo_wasm = test_dir_file(SUBFOLDER, "echo.wasm");

    // Pull echo.wasm for push tests
    wash()
        .args([
            "pull",
            ECHO_WASM,
            "--destination",
            pull_echo_wasm.to_str().unwrap(),
        ])
        .stderr(std::process::Stdio::inherit())
        .stdout(std::process::Stdio::inherit())
        .output()
        .unwrap_or_else(|_| panic!("failed to pull {ECHO_WASM} for push basic"));

    // Push echo.wasm and pull from local registry
    let echo_push_basic = &format!("{LOCAL_REGISTRY}/echo:pushbasic");
    let localregistry_echo_wasm = test_dir_file(SUBFOLDER, "echo_local.wasm");
    let push_echo = wash()
        .args([
            "push",
            echo_push_basic,
            pull_echo_wasm.to_str().unwrap(),
            "--insecure",
        ])
        .stderr(std::process::Stdio::inherit())
        .stdout(std::process::Stdio::inherit())
        .output()
        .expect("failed to push echo.wasm to local registry");
    assert!(
        push_echo.status.success(),
        "failed to push to local registry"
    );

    let pull_local_registry_echo = wash()
        .args([
            "pull",
            echo_push_basic,
            "--insecure",
            "--destination",
            localregistry_echo_wasm.to_str().unwrap(),
        ])
        .stderr(std::process::Stdio::inherit())
        .stdout(std::process::Stdio::inherit())
        .output()
        .expect("failed to pull echo.wasm from local registry");

    assert!(
        pull_local_registry_echo.status.success(),
        "failed to pull echo.wasm from local registry"
    );

    remove_dir_all(push_dir).unwrap();
}

// NOTE: This test will fail without a local docker registry running
#[tokio::test]
#[cfg_attr(
    not(can_reach_wasmcloud_azurecr_io),
    ignore = "wasmcloud.azurecr.io is not reachable"
)]
async fn integration_reg_push_comprehensive() -> Result<()> {
    const SUBFOLDER: &str = "push_comprehensive";
    let push_dir = test_dir_with_subfolder(SUBFOLDER);

    let pull_echo_wasm = test_dir_file(SUBFOLDER, "echo.wasm");
    let pull_logging_par = test_dir_file(SUBFOLDER, "logging.par.gz");

    // Pull echo.wasm and logging.par.gz for push tests
    wash()
        .args([
            "pull",
            ECHO_WASM,
            "--destination",
            pull_echo_wasm.to_str().unwrap(),
        ])
        .output()
        .unwrap_or_else(|_| panic!("failed to pull {ECHO_WASM} for push basic"));
    wash()
        .args([
            "pull",
            LOGGING_PAR,
            "--destination",
            pull_logging_par.to_str().unwrap(),
        ])
        .output()
        .unwrap_or_else(|_| panic!("failed to pull {LOGGING_PAR} for push basic"));

    let config_json = test_dir_file(SUBFOLDER, "config.json");
    let mut config = File::create(config_json.clone()).unwrap();
    config.write_all(b"{}").unwrap();

    let logging_push_all_options = format!("{LOCAL_REGISTRY}/logging:alloptions");
    let push_all_options = wash()
        .args([
            "push",
            &logging_push_all_options,
            pull_logging_par.to_str().unwrap(),
            "--allow-latest",
            "--insecure",
            "--config",
            config_json.to_str().unwrap(),
            "--output",
            "json",
            "--password",
            "supers3cr3t",
            "--user",
            "localuser",
        ])
        .output()
        .unwrap_or_else(|_| panic!("failed to push {LOGGING_PAR} for push comprehensive"));
    assert!(push_all_options.status.success());

    let output = get_json_output(push_all_options).unwrap();

    let expected_digest = fetch_artifact_digest(&logging_push_all_options).await?;
    let expected_json = json!({"url": logging_push_all_options, "digest": expected_digest, "success": true, "tag": "alloptions"});

    assert_eq!(output, expected_json);

    remove_dir_all(push_dir).unwrap();

    Ok(())
}

// NOTE: This test will fail without a local docker registry running
#[tokio::test]
#[cfg_attr(
    not(can_reach_wasmcloud_azurecr_io),
    ignore = "wasmcloud.azurecr.io is not reachable"
)]
async fn integration_reg_config() -> Result<()> {
    //===== Initial project setup and build component artifact
    let test_setup = init(
        /* component_name= */ "hello",
        /* template_name= */ "hello-world-rust",
    )
    .await?;
    let project_dir = test_setup.project_dir.clone();

    let status = test_setup
        .base_command()
        .args(["build"])
        .kill_on_drop(true)
        .status()
        .await
        .context("Failed to build project")?;

    assert!(status.success());
    let unsigned_file = project_dir.join("build/http_hello_world.wasm");
    assert!(unsigned_file.exists(), "unsigned file not found!");
    let signed_file = project_dir.join("build/http_hello_world_s.wasm");
    assert!(signed_file.exists(), "signed file not found!");

    //===== Setup registry config
    let envs = vec![
        ("WASH_REG_URL", LOCAL_REGISTRY),
        ("WASH_REG_USER", "iambatman"),
        ("WASH_REG_PASSWORD", "iamvengeance"),
    ];
    set_test_file_content(
        &project_dir.join("wasmcloud.toml").clone(),
        r#"
        name = "Hello World"
        language = "rust"
        type = "component"
        
        [component]
        claims = ["wasmcloud:httpserver"]

        [registry]
        url = "localhost:5001"
        credentials = "./registry_creds.json"
        "#,
    )
    .await?;

    set_test_file_content(
        &project_dir.join("registry_creds.json").clone(),
        r#"
        {
            "localhost:5001": {
                "username": "iambatman",
                "password": "iamvengeance"
            }
        }
        "#,
    )
    .await?;

    //===== case: Push (with a full artifact url) to test cli args
    let push_url = format!("{LOCAL_REGISTRY}/hello:0.1.0");
    let cmd = wash()
        .args([
            "push",
            push_url.as_str(),
            signed_file.to_str().unwrap(),
            "--allow-latest",
            "--insecure",
            // Even though we're passing a registry url, it should be ignored since we're also specifying a full artifact url
            "--registry",
            "this-is-ignored",
            "--output",
            "json",
            "--password",
            "iamvengeance",
            "--user",
            "iambatman",
        ])
        .envs(envs.clone().into_iter())
        .output()
        .unwrap_or_else(|e| panic!("failed to push artifact {e}"));

    assert!(cmd.status.success());
    // let output = output_to_string(cmd)?;
    // println!("{}", output);
    let output = get_json_output(cmd).unwrap();
    let expected_digest = fetch_artifact_digest(&push_url).await?;
    let expected_json =
        json!({"url": push_url, "digest": expected_digest, "success": true, "tag": "0.1.0"});
    assert_eq!(output, expected_json);

    //===== case: Push (with a repository url) to test cli args
    let push_url = "hello:0.2.0";
    let cmd = wash()
        .args([
            "push",
            push_url,
            signed_file.to_str().unwrap(),
            "--allow-latest",
            "--registry",
            LOCAL_REGISTRY,
            "--insecure",
            "--output",
            "json",
            "--password",
            "iamvengeance",
            "--user",
            "iambatman",
        ])
        .envs(envs.clone().into_iter())
        .stderr(std::process::Stdio::inherit())
        .output()
        .unwrap_or_else(|e| panic!("failed to push artifact {e}"));
    assert!(cmd.status.success());
    let output = get_json_output(cmd).unwrap();
    let expected_url = format!("{LOCAL_REGISTRY}/{push_url}");
    let expected_digest = fetch_artifact_digest(&expected_url).await?;
    let expected_json = json!({"url": expected_url, "digest": expected_digest, "success": true, "tag": "0.2.0", "url": "localhost:5001/hello:0.2.0"});
    assert_eq!(output, expected_json);

    //===== case: Push (with a repository url) to test env vars
    let push_url = "hello:0.3.0";
    let cmd = wash()
        .args([
            "push",
            push_url,
            signed_file.to_str().unwrap(),
            "--allow-latest",
            "--insecure",
            "--output",
            "json",
        ])
        .envs(envs.into_iter())
        .output()
        .unwrap_or_else(|e| panic!("failed to push artifact {e}"));

    assert!(cmd.status.success());
    let output = get_json_output(cmd).unwrap();
    let expected_url = format!("{LOCAL_REGISTRY}/{push_url}");
    let expected_digest = fetch_artifact_digest(&expected_url).await?;
    let expected_json = json!({"url": expected_url, "digest": expected_digest, "success": true, "tag": "0.3.0", "url": "localhost:5001/hello:0.3.0"});
    assert_eq!(output, expected_json);

    //===== case: Push (with a repository url) to test file configuration
    let push_url = "hello:0.4.0";
    let cmd = test_setup
        .base_command()
        .args([
            "push",
            push_url,
            signed_file.to_str().unwrap(),
            "--allow-latest",
            "--insecure",
            "--output",
            "json",
        ])
        .stderr(std::process::Stdio::inherit())
        .output()
        .await
        .unwrap_or_else(|e| panic!("failed to push artifact {e}"));

    assert!(cmd.status.success());
    let output = get_json_output(cmd).unwrap();
    let expected_url = format!("{LOCAL_REGISTRY}/{push_url}");
    let expected_digest = fetch_artifact_digest(&expected_url).await?;
    let expected_json = json!({"url": expected_url, "digest": expected_digest, "success": true, "tag": "0.4.0", "url": "localhost:5001/hello:0.4.0"});
    assert_eq!(output, expected_json);

    Ok(())
}