Skip to content

Commit

Permalink
Dont crash if a pkg is missing a config dir
Browse files Browse the repository at this point in the history
- BLDR-49
  • Loading branch information
Dave Parfitt committed Jan 20, 2016
1 parent ce7c191 commit ae6bcf8
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 14 deletions.
34 changes: 23 additions & 11 deletions src/bldr/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ impl Package {
pub fn from_ident(id: &str) -> BldrResult<Package> {
let items: Vec<&str> = id.split("/").collect();
match items.len() {
4 => Ok(Self::new(items[0].trim().to_string(),
items[1].trim().to_string(),
items[2].trim().to_string(),
items[3].trim().to_string())),
4 => {
Ok(Self::new(items[0].trim().to_string(),
items[1].trim().to_string(),
items[2].trim().to_string(),
items[3].trim().to_string()))
}
_ => Err(bldr_error!(ErrorKind::InvalidPackageIdent(id.to_string()))),
}
}
Expand Down Expand Up @@ -361,7 +363,14 @@ impl Package {
/// helpers above.
pub fn config_files(&self) -> BldrResult<Vec<String>> {
let mut files: Vec<String> = Vec::new();
for config in try!(fs::read_dir(self.join_path("config"))) {
let config_dir = self.join_path("config");
{
let p = Path::new(&config_dir);
if !p.exists() {
return Ok(files);
}
}
for config in try!(fs::read_dir(config_dir)) {
let config = try!(config);
match config.path().file_name() {
Some(filename) => {
Expand Down Expand Up @@ -418,12 +427,15 @@ impl Package {
if let Some(hook) = self.hooks().health_check_hook {
match hook.run(Some(config)) {
Ok(output) => Ok(health_check::CheckResult::ok(output)),
Err(BldrError{err: ErrorKind::HookFailed(_, 1, output), ..}) =>
Ok(health_check::CheckResult::warning(output)),
Err(BldrError{err: ErrorKind::HookFailed(_, 2, output), ..}) =>
Ok(health_check::CheckResult::critical(output)),
Err(BldrError{err: ErrorKind::HookFailed(_, 3, output), ..}) =>
Ok(health_check::CheckResult::unknown(output)),
Err(BldrError{err: ErrorKind::HookFailed(_, 1, output), ..}) => {
Ok(health_check::CheckResult::warning(output))
}
Err(BldrError{err: ErrorKind::HookFailed(_, 2, output), ..}) => {
Ok(health_check::CheckResult::critical(output))
}
Err(BldrError{err: ErrorKind::HookFailed(_, 3, output), ..}) => {
Ok(health_check::CheckResult::unknown(output))
}
Err(BldrError{err: ErrorKind::HookFailed(_, code, output), ..}) => {
Err(bldr_error!(ErrorKind::HealthCheck(format!("hook exited code={}, \
output={}",
Expand Down
15 changes: 15 additions & 0 deletions tests/bldr/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ fn standalone_no_options() {
}
}

#[test]
fn standalone_no_options_without_config() {
setup::gpg_import();
setup::fixture_service("simple_service_without_config");

let d = docker::run("test/simple_service_without_config");
if d.wait_until(r"Shipping out to Boston") {
let output = d.logs();
assert_regex!(&output, r"Starting (.+)");
} else {
// container didn't start in time or pkg doesn't exist
assert!(false);
}
}

#[test]
fn standalone_with_environment_config() {
setup::gpg_import();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

EXIT_NOW=0

set_exit_now() {
EXIT_NOW=1
}

trap "set_exit_now" INT TERM HUP
echo Shipping out to Boston
echo "### Configuration file ###"
cat /opt/bldr/srvc/simple_service/config/simple.conf
echo "### End of configuration file ###"
echo "### Running toml data ###"
cat /opt/bldr/srvc/simple_service/config.toml
echo "### End running toml data ###"

while [ 1 ]; do
if [ $EXIT_NOW = 1 ]; then
echo "Exiting on signal"
exit 0
fi
sleep 1
done

7 changes: 7 additions & 0 deletions tests/fixtures/simple_service_without_config/bldr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

ld_library_path=""
for x in `find /opt -name 'lib' -type d`; do
ld_library_path="$ld_library_path:$x:$x/x86_64-linux-gnu"
done
exec env LD_LIBRARY_PATH=$ld_library_path /bin/bldr-debug $*
1 change: 1 addition & 0 deletions tests/fixtures/simple_service_without_config/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setting = "true"
33 changes: 33 additions & 0 deletions tests/fixtures/simple_service_without_config/plan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pkg_name=simple_service_without_config
pkg_derivation=test
pkg_version=0.0.1
pkg_license=('Apache2')
pkg_maintainer="Adam Jacob <[email protected]>"
pkg_source=http://example.com/releases/${pkg_name}-${pkg_version}.tar.bz2
pkg_filename=${pkg_name}-${pkg_version}.tar.bz2
pkg_shasum=0e21be5d7c5e6ab6adcbed257619897db59be9e1ded7ef6fd1582d0cdb5e5bb7
pkg_gpg_key=3853DA6B
pkg_binary_path=(bin)
pkg_deps=(chef/gpgme chef/libassuan chef/libgpg-error)
pkg_service_run="bin/simple_service_without_config"
pkg_docker_build="auto"

do_begin() {
tar -cjvf $BLDR_SRC_CACHE/${pkg_name}-${pkg_version}.tar.bz2 --exclude 'plans' --exclude '.git' --exclude '.gitignore' --exclude 'target' --transform "s,^\.,simple_service_without_config-0.0.1," .
pkg_shasum=$(trim $(sha256sum /opt/bldr/cache/src/simple_service_without_config-0.0.1.tar.bz2 | cut -d " " -f 1))
}

do_download() {
return 0
}

do_build() {
return 0
}

do_install() {
cp -r $BLDR_SRC_CACHE/$pkg_dirname/bin $pkg_prefix
chmod 755 $pkg_path/bin
chmod 755 $pkg_path/bin/*
return 0
}
32 changes: 29 additions & 3 deletions tests/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ mod setup {
.unwrap();
copy_cmd.wait().unwrap();

let mut simple_service = match util::command::bldr_build(tempdir.path()
.join("simple_serv\
ice")) {
let mut simple_service =
match util::command::bldr_build(tempdir.path().join("simple_service")) {
Ok(cmd) => cmd,
Err(e) => panic!("{:?}", e),
};
Expand All @@ -61,6 +60,33 @@ mod setup {
});
}

pub fn fixture_service(pkg: &str) {
static ONCE: Once = ONCE_INIT;
ONCE.call_once(|| {
let tempdir = TempDir::new(pkg).unwrap();
let mut copy_cmd = Command::new("cp")
.arg("-r")
.arg(util::path::fixture(pkg))
.arg(tempdir.path().to_str().unwrap())
.spawn()
.unwrap();
copy_cmd.wait().unwrap();

let mut simple_service =
match util::command::bldr_build(tempdir.path().join(pkg)) {
Ok(cmd) => cmd,
Err(e) => panic!("{:?}", e),
};
simple_service.wait_with_output();
if !simple_service.status.unwrap().success() {
panic!("Failed to build {}: stdout: {:?}\nstderr: {:?}",
pkg,
simple_service.stdout.unwrap(),
simple_service.stderr.unwrap())
}
});
}

pub fn key_install() {
static ONCE: Once = ONCE_INIT;
ONCE.call_once(|| {
Expand Down

1 comment on commit ae6bcf8

@chef-delivery
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivery Status:

Verify
Unit
Lint
Syntax

Please sign in to comment.