Skip to content

Commit

Permalink
fix: not allow duplicated connectors (#4288)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraidev authored Dec 9, 2024
1 parent 56af9f9 commit aaac78a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
57 changes: 52 additions & 5 deletions crates/cdk/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,17 @@ mod local_index {
Ok(index)
}

fn insert(&mut self, entry: Entry) {
self.entries.push(entry)
fn insert(&mut self, entry: Entry) -> Result<()> {
match entry {
Entry::Local { ref name, .. } => {
if let Some((_, _)) = self.find_by_name(name) {
return Err(anyhow!("Connector with name {} already exists", name));
}
}
}

self.entries.push(entry);
Ok(())
}

fn remove(&mut self, index: usize) -> Result<()> {
Expand Down Expand Up @@ -560,7 +569,7 @@ mod local_index {

pub(super) fn store(deployment: DeploymentResult) -> Result<()> {
let mut index = load()?;
index.insert(deployment.into());
index.insert(deployment.into())?;
index.flush()
}

Expand Down Expand Up @@ -682,7 +691,7 @@ mod local_index {
name: "test_connector".to_owned(),
log_file: None,
tmp_dir: None,
});
})?;
index.flush()?;

//then
Expand Down Expand Up @@ -721,7 +730,7 @@ mod local_index {
name: "test_connector2".to_owned(),
log_file: None,
tmp_dir: None,
});
})?;
index.flush()?;

//then
Expand All @@ -742,6 +751,44 @@ mod local_index {
Ok(())
}

#[test]
fn test_do_not_allow_duplicated_insert() -> Result<()> {
//given
let file_path = TestFile::new();
std::fs::write(
&file_path,
b"[[entries]]\ntype = \"local\"\nprocess_id = 1\nname = \"test_connector\"\n",
)?;

//when
let mut index: LocalIndex<NoopOperator> = LocalIndex::load(&file_path)?;
assert!(index.find_by_name("test_connector").is_some());

let result = index.insert(Entry::Local {
process_id: 1,
name: "test_connector".to_owned(),
log_file: None,
tmp_dir: None,
});

//then
assert!(result.is_err());

let mut output = Cursor::new(Vec::new());
index.print_table(&mut output)?;
let output = String::from_utf8_lossy(output.get_ref());
assert_eq!(
output,
" NAME STATUS \n test_connector Running \n"
);
assert_eq!(
std::fs::read_to_string(file_path)?,
"[[entries]]\ntype = \"local\"\nprocess_id = 1\nname = \"test_connector\"\n"
);

Ok(())
}

#[test]
fn test_remove() -> Result<()> {
//given
Expand Down
18 changes: 18 additions & 0 deletions tests/cli/cdk_smoke_tests/cdk-multi-deploy.bats
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ setup_file() {
assert_success
}

@test "Deploy duplicated connectors should fail" {
# Deploy
cd $CONNECTOR_DIR
run $CDK_BIN deploy --target x86_64-unknown-linux-gnu start \
$CONFIG_FILE_FLAG
assert_success
assert_output --partial "Connector runs with process id"

run $CDK_BIN deploy --target x86_64-unknown-linux-gnu start \
$CONFIG_FILE_FLAG
assert_failure
assert_output --partial "Connector with name my-json-test-connector already exists"


run $CDK_BIN deploy shutdown --name my-json-test-connector
assert_success
}

@test "Run multiple connectors with --ipkg" {
# create package meta doesn't exist
cd $CONNECTOR_DIR
Expand Down

0 comments on commit aaac78a

Please sign in to comment.