Skip to content

Commit

Permalink
e2e test for clean --stale
Browse files Browse the repository at this point in the history
Summary: Using `--keep-since-time` instead of `--stale` to use a fixed timestamp for testing instead of a duration.

Reviewed By: krallin

Differential Revision: D41225823

fbshipit-source-id: 83b6caa6bbcf4fde293676d9ee73423aded6437f
  • Loading branch information
christolliday authored and facebook-github-bot committed Dec 5, 2022
1 parent 85c8e06 commit b2b9404
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
9 changes: 7 additions & 2 deletions buck2_client/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use humantime;
use threadpool::ThreadPool;
use walkdir::WalkDir;

use crate::commands::clean_stale::parse_clean_stale_args;
use crate::commands::clean_stale::CleanStaleCommand;

#[derive(Debug, clap::Parser)]
Expand Down Expand Up @@ -56,16 +57,20 @@ the specified duration, without killing the daemon",
value_name = "DURATION"
)]
stale: Option<Option<humantime::Duration>>,

// Like stale but since a specific timestamp, for testing
#[clap(long = "keep-since-time", conflicts_with = "stale", hidden = true)]
keep_since_time: Option<i64>,
}

impl CleanCommand {
pub fn exec(self, matches: &clap::ArgMatches, ctx: ClientCommandContext) -> anyhow::Result<()> {
if self.stale.is_some() {
if let Some(keep_since_arg) = parse_clean_stale_args(self.stale, self.keep_since_time)? {
let cmd = CleanStaleCommand {
console_opts: self.console_opts,
config_opts: self.config_opts,
event_log_opts: self.event_log_opts,
stale: self.stale.unwrap(),
keep_since_arg,
dry_run: self.dry_run,
};
cmd.exec(matches, ctx);
Expand Down
55 changes: 43 additions & 12 deletions buck2_client/src/commands/clean_stale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use buck2_client_ctx::exit_result::ExitResult;
use buck2_client_ctx::streaming::StreamingCommand;
use chrono::DateTime;
use chrono::Duration;
use chrono::TimeZone;
use chrono::Utc;
use cli_proto::CleanStaleRequest;
use cli_proto::CleanStaleResponse;
Expand All @@ -32,10 +33,33 @@ pub struct CleanStaleCommand {
pub console_opts: CommonConsoleOptions,
pub config_opts: CommonBuildConfigurationOptions,
pub event_log_opts: CommonDaemonCommandOptions,
pub stale: Option<humantime::Duration>,
pub keep_since_arg: KeepSinceArg,
pub dry_run: bool,
}

/// Specifies the maximum age of artifacts to keep
pub enum KeepSinceArg {
Duration(Duration),
Time(i64),
}

pub fn parse_clean_stale_args(
stale: Option<Option<humantime::Duration>>,
keep_since_time: Option<i64>,
) -> anyhow::Result<Option<KeepSinceArg>> {
let arg = match (stale, keep_since_time) {
(Some(Some(human_duration)), None) => {
let duration = chrono::Duration::from_std(human_duration.into())?;
Some(KeepSinceArg::Duration(duration))
}
(Some(None), None) => Some(KeepSinceArg::Duration(chrono::Duration::weeks(7))),
(None, Some(time)) => Some(KeepSinceArg::Time(time)),
(Some(_), Some(_)) => unreachable!("keep-since-time conflicts_with stale"),
(None, None) => None,
};
Ok(arg)
}

#[async_trait]
impl StreamingCommand for CleanStaleCommand {
const COMMAND_NAME: &'static str = "clean";
Expand All @@ -46,18 +70,25 @@ impl StreamingCommand for CleanStaleCommand {
matches: &clap::ArgMatches,
mut ctx: ClientCommandContext,
) -> ExitResult {
let duration: Duration = self
.stale
.map_or(Ok(Duration::weeks(7)), |dur| Duration::from_std(dur.into()))
.context("Error converting duration")?;
let keep_since_time = match self.keep_since_arg {
KeepSinceArg::Duration(duration) => {
let keep_since_time: DateTime<Utc> = Utc::now()
.checked_sub_signed(duration)
.context("Duration underflow")?;

let keep_since_time: DateTime<Utc> = Utc::now()
.checked_sub_signed(duration)
.context("Duration underflow")?;
buck2_client_ctx::eprintln!(
"Cleaning artifacts more than {} old",
humantime::format_duration(duration.to_std().context("Error converting duration")?),
)?;
buck2_client_ctx::eprintln!(
"Cleaning artifacts more than {} old",
humantime::format_duration(
duration.to_std().context("Error converting duration")?
),
)?;
keep_since_time
}
KeepSinceArg::Time(timestamp) => Utc
.timestamp_opt(timestamp, 0)
.single()
.context("Invalid timestamp")?,
};

let context = ctx.client_context(&self.config_opts, matches, self.sanitized_argv())?;
let response: CleanStaleResponse = buckd
Expand Down

0 comments on commit b2b9404

Please sign in to comment.