Skip to content

Commit

Permalink
bunch of stuff
Browse files Browse the repository at this point in the history
Signed-off-by: mwrock <matt@mattwrock.com>
  • Loading branch information
mwrock committed Dec 21, 2018
1 parent 4b8941a commit fb2cecc
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions components/common/src/command/package/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,7 @@ impl<'a> InstallTask<'a> {
Some(package_install) => {
// The installed package was found on disk
ui.status(Status::Using, &target_ident)?;
if self.install_hook_mode == &InstallHookMode::Always {
self.check_install_hooks(ui, &target_ident)?;
}
self.check_install_hooks(ui, &package_install)?;
ui.end(format!(
"Install of {} complete with {} new packages installed.",
&target_ident, 0
Expand Down Expand Up @@ -559,9 +557,7 @@ impl<'a> InstallTask<'a> {
Some(package_install) => {
// The installed package was found on disk
ui.status(Status::Using, &target_ident)?;
if self.install_hook_mode == &InstallHookMode::Always {
self.check_install_hooks(ui, &target_ident)?;
}
self.check_install_hooks(ui, &package_install)?;
ui.end(format!(
"Install of {} complete with {} new packages installed.",
&target_ident, 0
Expand Down Expand Up @@ -849,12 +845,10 @@ impl<'a> InstallTask<'a> {
}
}

fn check_install_hooks<T>(&self, ui: &mut T, ident: &FullyQualifiedPackageIdent) -> Result<()>
fn check_install_hooks<T>(&self, ui: &mut T, package: &PackageInstall) -> Result<()>
where
T: UIWriter,
{
let package = PackageInstall::load(ident.as_ref(), Some(self.fs_root_path))?;

for dependency in package.tdeps()?.iter() {
self.check_install_hook(
ui,
Expand Down
32 changes: 32 additions & 0 deletions components/sup/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ use butterfly;
use butterfly::member::Member;
use butterfly::server::{timing::Timing, ServerProxy, Suitability};
use butterfly::trace::Trace;
use common;
pub use common::templating::package::Pkg;
use futures::prelude::*;
use futures::sync::mpsc;
use hcore::crypto::SymKey;
use hcore::env;
use hcore::fs::FS_ROOT_PATH;
use hcore::os::process::{self, Pid, Signal};
use hcore::os::signals::{self, SignalEvent};
use hcore::package::{Identifiable, PackageIdent, PackageInstall};
Expand Down Expand Up @@ -464,6 +466,36 @@ impl Manager {
}
};

let package =
PackageInstall::load(&service.pkg.ident, Some(Path::new(&*FS_ROOT_PATH))).unwrap();
let ui = &mut common::ui::UI::with_sinks();
if let Ok(tdeps) = package.tdeps() {
for dependency in tdeps.iter() {
match PackageInstall::load(dependency, Some(Path::new(&*FS_ROOT_PATH))) {
Ok(pkg) => {
if let Err(err) =
common::command::package::install::run_install_hook_when_failed(
ui, &pkg,
) {
outputln!("Failed to run install hook for {}, {}", &pkg.ident(), err);
return;
}
}
Err(err) => {
outputln!("Failed to load package {}, {}", dependency, err);
return;
}
}
}
}

if let Err(err) =
common::command::package::install::run_install_hook_when_failed(ui, &package)
{
outputln!("Failed to run install hook for {}, {}", &spec.ident, err);
return;
}

if let Err(e) = service.create_svc_path() {
outputln!(
"Can't create directory {}: {}",
Expand Down
8 changes: 3 additions & 5 deletions components/sup/src/util/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ where
&InstallMode::default(),
// TODO (CM): pass through and enable ignore-local mode
&LocalPackageUsage::default(),
&InstallHookMode::default(),
// Install hooks are run when the supervisor loads the package
&InstallHookMode::Never,
).map_err(SupError::from)
}

Expand All @@ -83,10 +84,7 @@ where
T: UIWriter,
{
match installed(install_source) {
Some(package) => {
common::command::package::install::run_install_hook_when_failed(ui, &package)?;
Ok(package)
}
Some(package) => Ok(package),
None => install(ui, bldr_url, install_source, channel),
}
}
Expand Down
83 changes: 83 additions & 0 deletions components/sup/tests/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,89 @@ hook_value = "default"
assert_ne!(pid_before_apply, pid_after_apply);
}

#[test]
fn install_success_sets_zero_status() {
let hab_root = utils::HabRoot::new("install_success_sets_zero_status");

let origin_name = "sup-integration-test";
let package_name = "install-hook-succeeds";
let service_group = "default";

utils::setup_package_files(
&origin_name,
&package_name,
&service_group,
&FIXTURE_ROOT,
&hab_root,
);

let mut test_sup = utils::TestSup::new_with_random_ports(
&hab_root,
&origin_name,
&package_name,
&service_group,
);

test_sup.start();
utils::sleep_seconds(3);

let status_created_before = hab_root.install_status_created(origin_name, package_name);

assert_eq!(hab_root.install_status_of(origin_name, package_name), 0);
assert!(hab_root.pid_of(package_name) > 0);

test_sup.stop();
utils::sleep_seconds(3);
test_sup.start();
utils::sleep_seconds(3);

let status_created_after = hab_root.install_status_created(origin_name, package_name);

assert_eq!(status_created_before, status_created_after);
}

#[test]
fn install_fails_sets_one_status() {
let hab_root = utils::HabRoot::new("install_fails_sets_one_status");

let origin_name = "sup-integration-test";
let package_name = "install-hook-fails";
let service_group = "default";

utils::setup_package_files(
&origin_name,
&package_name,
&service_group,
&FIXTURE_ROOT,
&hab_root,
);

let mut test_sup = utils::TestSup::new_with_random_ports(
&hab_root,
&origin_name,
&package_name,
&service_group,
);

test_sup.start();
utils::sleep_seconds(3);

let status_created_before = hab_root.install_status_created(origin_name, package_name);
let result = std::panic::catch_unwind(|| hab_root.pid_of(package_name));

assert_eq!(hab_root.install_status_of(origin_name, package_name), 1);
assert!(result.is_err());

test_sup.stop();
utils::sleep_seconds(3);
test_sup.start();
utils::sleep_seconds(3);

let status_created_after = hab_root.install_status_created(origin_name, package_name);

assert_ne!(status_created_before, status_created_after);
}

#[test]
fn hooks_change_but_config_files_do_not_still_restarts() {
let hab_root = utils::HabRoot::new("hooks_change_but_config_files_do_not_still_restarts");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ident = "sup-integration-test/install-hook-fails"
group = "default"
bldr_url = "http://hab.sup.test"
channel = "unstable"
topology = "standalone"
update_strategy = "at-once"
binds = []
desired_state = "up"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x86_64-linux
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

echo "I am a failure!"
exit 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

while true;
do
echo "Running: $0"
sleep 1
done
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x86_64-linux
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sup-integration-test/install-hook-succeeds/1.0.0/20170721000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

while true;
do
echo "Running: $0"
sleep 1
done
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ident = "sup-integration-test/install-hook-succeeds"
group = "default"
bldr_url = "http://hab.sup.test"
channel = "unstable"
topology = "standalone"
update_strategy = "at-once"
binds = []
desired_state = "up"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x86_64-linux
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "I am a success!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

while true;
do
echo "Running: $0"
sleep 1
done
56 changes: 52 additions & 4 deletions components/sup/tests/utils/hab_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::string::ToString;
use std::time::SystemTime;

use hcore::fs::PKG_PATH;
use hcore::package::metadata::MetaFile;
use hcore::package::PackageIdent;
use tempfile::Builder;
use tempfile::TempDir;

Expand Down Expand Up @@ -59,13 +61,32 @@ impl HabRoot {
S: AsRef<Path>,
T: AsRef<Path>,
{
let ident = self.pkg_ident(origin, pkg_name);

self.0
.path()
.join(PKG_PATH)
.join(origin.as_ref())
.join(pkg_name.as_ref())
.join("1.0.0")
.join("20170721000000")
.join(ident.origin)
.join(ident.name)
.join(ident.version.as_ref().unwrap())
.join(ident.release.as_ref().unwrap())
}

/// Directory to which "expanded package" files should be placed.
///
/// We assign a hard-coded version and release, because
/// they aren't important for the things we're currently testing
pub fn pkg_ident<S, T>(&self, origin: S, pkg_name: T) -> PackageIdent
where
S: AsRef<Path>,
T: AsRef<Path>,
{
PackageIdent::new(
origin.as_ref().to_str().unwrap(),
pkg_name.as_ref().to_str().unwrap(),
Some("1.0.0"),
Some("20170721000000"),
)
}

/// Returns the path to the service user metafile for a given package.
Expand Down Expand Up @@ -145,6 +166,33 @@ impl HabRoot {
.expect("Couldn't parse PID file content as u32!")
}

/// Read the INSTALL_HOOK_STATUS file for a package and return the status value
///
/// Use this to determine if an install hook was run and determine its success
pub fn install_status_of<S, T>(&self, origin: S, pkg_name: T) -> u32
where
S: AsRef<Path>,
T: AsRef<Path>,
{
let path = self.pkg_path(origin, pkg_name).join("INSTALL_HOOK_STATUS");
Self::file_content(path)
.parse::<u32>()
.expect("Couldn't parse status file content as u32!")
}

/// Retrieve the last modification time of the INSTALL_HOOK_STATUS file for a package
///
/// Use this to determine if an install hook was run a subsequent time
pub fn install_status_created<S, T>(&self, origin: S, pkg_name: T) -> SystemTime
where
S: AsRef<Path>,
T: AsRef<Path>,
{
let path = self.pkg_path(origin, pkg_name).join("INSTALL_HOOK_STATUS");
let f = File::open(&path).expect(format!("Couldn't open file {:?}", path).as_str());
f.metadata().unwrap().modified().unwrap()
}

/// Path to the service directory for a package
fn svc_path<P>(&self, pkg_name: P) -> PathBuf
where
Expand Down
13 changes: 13 additions & 0 deletions components/sup/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::thread;
use std::time::Duration;

use hcore::os::users;
use hcore::package::PackageInstall;

pub mod fixture_root;
pub mod hab_root;
Expand Down Expand Up @@ -74,6 +75,18 @@ pub fn setup_package_files<O, P, S>(
let hab_pkg_path = hab_root.pkg_path(&origin_name, &package_name);
copy_dir(&expanded_fixture_dir, &hab_pkg_path);
write_default_svc_user_and_group_metafiles(&hab_root, &origin_name, &package_name);

let install = PackageInstall::load(
&hab_root.pkg_ident(&origin_name, &package_name),
Some(hab_root.as_ref()),
).expect(format!("Could not load package {:?}/{:?}", &origin_name, &package_name).as_str());
if let Ok(tdeps) = install.tdeps() {
for dependency in tdeps.iter() {
let fixture_dir = fixture_root.expanded_package_dir(&dependency.name);
let pkg_path = hab_root.pkg_path(&dependency.origin, &dependency.name);
copy_dir(&fixture_dir, &pkg_path);
}
}
}

/// Recursively copy the contents of `source_dir` into `dest_dir`
Expand Down
Loading

0 comments on commit fb2cecc

Please sign in to comment.