Skip to content

Commit 0f7b4d0

Browse files
committed
Move plugin tests to using testing framework
Signed-off-by: Ryan Levick <[email protected]>
1 parent c4f7e6f commit 0f7b4d0

File tree

3 files changed

+254
-253
lines changed

3 files changed

+254
-253
lines changed

tests/integration.rs

Lines changed: 62 additions & 252 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ mod integration_tests {
1919
sync::{Arc, Mutex},
2020
time::Duration,
2121
};
22-
use tempfile::tempdir;
2322
use tokio::{net::TcpStream, task, time::sleep};
2423
use tracing::log;
2524

@@ -235,257 +234,6 @@ mod integration_tests {
235234
Ok(())
236235
}
237236

238-
/// Builds app in `dir` and verifies the build succeeded. Expects manifest
239-
/// in `spin.toml` inside `dir`.
240-
async fn do_test_build_command(dir: impl AsRef<Path>) -> Result<()> {
241-
let dir = dir.as_ref();
242-
let manifest_file = dir.join("spin.toml");
243-
let manifest = spin_manifest::manifest_from_file(&manifest_file)?;
244-
245-
let sources = manifest
246-
.components
247-
.iter()
248-
.map(|(id, component)| {
249-
let v2::ComponentSource::Local(file) = &component.source else {
250-
panic!(
251-
"{}.{}: source is not a file reference",
252-
manifest.application.name, id
253-
)
254-
};
255-
(id, dir.join(file))
256-
})
257-
.collect::<HashMap<_, _>>();
258-
259-
// Delete build output so that later it can be assumed: if the output
260-
// exists, it is because `spin build` succeeded.
261-
for source in sources.values() {
262-
if source.exists() {
263-
std::fs::remove_file(source)?
264-
}
265-
}
266-
267-
run(
268-
vec![
269-
spin_binary().as_str(),
270-
"build",
271-
"--file",
272-
manifest_file.to_str().unwrap(),
273-
],
274-
None,
275-
None,
276-
)?;
277-
278-
let mut missing_sources_count = 0;
279-
for (component_id, source) in sources.iter() {
280-
if source.exists() {
281-
std::fs::remove_file(source)?;
282-
} else {
283-
missing_sources_count += 1;
284-
println!(
285-
"{}.{} source file was not generated by build",
286-
manifest.application.name, component_id
287-
);
288-
}
289-
}
290-
assert_eq!(missing_sources_count, 0);
291-
292-
Ok(())
293-
}
294-
295-
#[test]
296-
fn spin_up_gives_help_on_new_app() -> Result<()> {
297-
let temp_dir = tempdir()?;
298-
let dir = temp_dir.path();
299-
let manifest_file = dir.join("spin.toml");
300-
301-
// We still don't see full help if there are no components.
302-
let toml_text = r#"spin_version = "1"
303-
name = "unbuilt"
304-
trigger = { type = "http" }
305-
version = "0.1.0"
306-
[[component]]
307-
id = "unbuilt"
308-
source = "fake.wasm"
309-
[component.trigger]
310-
route = "/..."
311-
"#;
312-
313-
std::fs::write(&manifest_file, toml_text)?;
314-
std::fs::write(dir.join("fake.wasm"), "")?;
315-
316-
let binary = spin_binary();
317-
let up_help_args = vec![
318-
&binary,
319-
"up",
320-
"--file",
321-
manifest_file.to_str().unwrap(),
322-
"--help",
323-
];
324-
325-
let output = run(up_help_args, None, None)?;
326-
327-
let stdout = String::from_utf8_lossy(&output.stdout);
328-
assert!(stdout.contains("--quiet"));
329-
assert!(stdout.contains("--listen"));
330-
331-
Ok(())
332-
}
333-
334-
// TODO: Test on Windows
335-
#[cfg(not(target_os = "windows"))]
336-
#[test]
337-
fn test_spin_plugin_install_command() -> Result<()> {
338-
// Create a temporary directory for plugin source and manifests
339-
let temp_dir = tempdir()?;
340-
let dir = temp_dir.path();
341-
let installed_plugins_dir = dir.join("tmp");
342-
343-
// Ensure that spin installs the plugins into the temporary directory
344-
let mut env_map: HashMap<&str, &str> = HashMap::new();
345-
env_map.insert(
346-
"TEST_PLUGINS_DIRECTORY",
347-
installed_plugins_dir.to_str().unwrap(),
348-
);
349-
350-
let path_to_test_dir = std::env::current_dir()?;
351-
let file_url = format!(
352-
"file:{}/tests/plugin/example.tar.gz",
353-
path_to_test_dir.to_str().unwrap()
354-
);
355-
let mut plugin_manifest_json = serde_json::json!(
356-
{
357-
"name": "example",
358-
"description": "A description of the plugin.",
359-
"homepage": "www.example.com",
360-
"version": "0.2.0",
361-
"spinCompatibility": ">=0.5",
362-
"license": "MIT",
363-
"packages": [
364-
{
365-
"os": "linux",
366-
"arch": "amd64",
367-
"url": file_url,
368-
"sha256": "f7a5a8c16a94fe934007f777a1bf532ef7e42b02133e31abf7523177b220a1ce"
369-
},
370-
{
371-
"os": "macos",
372-
"arch": "aarch64",
373-
"url": file_url,
374-
"sha256": "f7a5a8c16a94fe934007f777a1bf532ef7e42b02133e31abf7523177b220a1ce"
375-
},
376-
{
377-
"os": "macos",
378-
"arch": "amd64",
379-
"url": file_url,
380-
"sha256": "f7a5a8c16a94fe934007f777a1bf532ef7e42b02133e31abf7523177b220a1ce"
381-
}
382-
]
383-
});
384-
let manifest_file_path = dir.join("example-plugin-manifest.json");
385-
std::fs::write(
386-
&manifest_file_path,
387-
serde_json::to_string(&plugin_manifest_json).unwrap(),
388-
)?;
389-
390-
// Install plugin
391-
let binary = spin_binary();
392-
let install_args = vec![
393-
&binary,
394-
"plugins",
395-
"install",
396-
"--file",
397-
manifest_file_path.to_str().unwrap(),
398-
"--yes",
399-
];
400-
run(install_args, None, Some(env_map.clone()))?;
401-
402-
// Execute example plugin which writes "This is an example Spin plugin!" to a specified file
403-
let binary = spin_binary();
404-
let execute_args = vec![&binary, "example"];
405-
let output = run(execute_args, None, Some(env_map.clone()))?;
406-
407-
// Verify plugin successfully wrote to output file
408-
assert!(std::str::from_utf8(&output.stdout)?
409-
.trim()
410-
.contains("This is an example Spin plugin!"));
411-
412-
// Upgrade plugin to newer version
413-
*plugin_manifest_json.get_mut("version").unwrap() = serde_json::json!("0.2.1");
414-
std::fs::write(
415-
dir.join("example-plugin-manifest.json"),
416-
serde_json::to_string(&plugin_manifest_json).unwrap(),
417-
)?;
418-
let binary = spin_binary();
419-
let upgrade_args = vec![
420-
&binary,
421-
"plugins",
422-
"upgrade",
423-
"example",
424-
"--file",
425-
manifest_file_path
426-
.to_str()
427-
.ok_or_else(|| anyhow::anyhow!("Cannot convert PathBuf to str"))?,
428-
"--yes",
429-
];
430-
run(upgrade_args, None, Some(env_map))?;
431-
432-
// Check plugin version
433-
let installed_manifest = installed_plugins_dir
434-
.join("spin")
435-
.join("plugins")
436-
.join("manifests")
437-
.join("example.json");
438-
let manifest = std::fs::read_to_string(installed_manifest)?;
439-
assert!(manifest.contains("0.2.1"));
440-
441-
// Uninstall plugin
442-
let binary = spin_binary();
443-
let uninstall_args = vec![&binary, "plugins", "uninstall", "example"];
444-
run(uninstall_args, None, None)?;
445-
Ok(())
446-
}
447-
448-
// TODO: Test on Windows
449-
#[cfg(not(target_os = "windows"))]
450-
#[test]
451-
fn test_cloud_plugin_install() -> Result<()> {
452-
// Create a temporary directory for plugin source and manifests
453-
let temp_dir = tempdir()?;
454-
let dir = temp_dir.path();
455-
let installed_plugins_dir = dir.join("tmp");
456-
457-
// Ensure that spin installs the plugins into the temporary directory
458-
let mut env_map: HashMap<&str, &str> = HashMap::new();
459-
env_map.insert(
460-
"TEST_PLUGINS_DIRECTORY",
461-
installed_plugins_dir.to_str().unwrap(),
462-
);
463-
464-
// `spin login --help` should cause the `cloud` plugin to be installed
465-
let spin_binary = spin_binary();
466-
let args = vec![&spin_binary, "login", "--help"];
467-
468-
// Execute example plugin which writes "This is an example Spin plugin!" to a specified file
469-
let output = run(args, None, Some(env_map.clone()))?;
470-
471-
// Ensure plugin is installed
472-
assert!(std::str::from_utf8(&output.stdout)?
473-
.trim()
474-
.contains("The `cloud` plugin is required. Installing now."));
475-
476-
// Ensure login help info is displayed
477-
assert!(std::str::from_utf8(&output.stdout)?
478-
.trim()
479-
.contains("Log into Fermyon Cloud"));
480-
481-
Ok(())
482-
}
483-
484-
#[tokio::test]
485-
async fn test_build_command() -> Result<()> {
486-
do_test_build_command("tests/build/simple").await
487-
}
488-
489237
#[tokio::test]
490238
async fn test_outbound_post() -> Result<()> {
491239
let listener = tokio::net::TcpListener::bind((Ipv4Addr::new(127, 0, 0, 1), 0)).await?;
@@ -739,6 +487,11 @@ route = "/..."
739487
Ok(())
740488
}
741489

490+
#[tokio::test]
491+
async fn test_build_command() -> Result<()> {
492+
do_test_build_command("tests/build/simple").await
493+
}
494+
742495
/// Build an app whose component `workdir` is a subdirectory.
743496
#[tokio::test]
744497
#[cfg(not(tarpaulin))]
@@ -752,4 +505,61 @@ route = "/..."
752505
async fn test_build_command_sibling_workdir() -> Result<()> {
753506
do_test_build_command("tests/build/sibling").await
754507
}
508+
509+
/// Builds app in `dir` and verifies the build succeeded. Expects manifest
510+
/// in `spin.toml` inside `dir`.
511+
async fn do_test_build_command(dir: impl AsRef<Path>) -> Result<()> {
512+
let dir = dir.as_ref();
513+
let manifest_file = dir.join("spin.toml");
514+
let manifest = spin_manifest::manifest_from_file(&manifest_file)?;
515+
516+
let sources = manifest
517+
.components
518+
.iter()
519+
.map(|(id, component)| {
520+
let v2::ComponentSource::Local(file) = &component.source else {
521+
panic!(
522+
"{}.{}: source is not a file reference",
523+
manifest.application.name, id
524+
)
525+
};
526+
(id, dir.join(file))
527+
})
528+
.collect::<HashMap<_, _>>();
529+
530+
// Delete build output so that later it can be assumed: if the output
531+
// exists, it is because `spin build` succeeded.
532+
for source in sources.values() {
533+
if source.exists() {
534+
std::fs::remove_file(source)?
535+
}
536+
}
537+
538+
run(
539+
vec![
540+
spin_binary().as_str(),
541+
"build",
542+
"--file",
543+
manifest_file.to_str().unwrap(),
544+
],
545+
None,
546+
None,
547+
)?;
548+
549+
let mut missing_sources_count = 0;
550+
for (component_id, source) in sources.iter() {
551+
if source.exists() {
552+
std::fs::remove_file(source)?;
553+
} else {
554+
missing_sources_count += 1;
555+
println!(
556+
"{}.{} source file was not generated by build",
557+
manifest.application.name, component_id
558+
);
559+
}
560+
}
561+
assert_eq!(missing_sources_count, 0);
562+
563+
Ok(())
564+
}
755565
}

0 commit comments

Comments
 (0)