Skip to content

Commit

Permalink
Small fixes to initial commit
Browse files Browse the repository at this point in the history
- Move FNF handling to before IOE (FNF is subclass of IOE)
- Update tests to assert exact message strings
- add test for bad gateway (502) from /metadata
  • Loading branch information
hhughes committed Jul 27, 2023
1 parent 680c7db commit fc66bca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ protected BufferedReader fetchProxyMetadata(
try {
return new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
} catch (FileNotFoundException e) {
throw new IllegalStateException(
"Metadata service request path not found. Please make sure the metadata service url is correct",
e);
} catch (IOException ioe) {
throw new IllegalStateException(
"Unable to read data from cloud metadata service. Please make sure your cluster is not parked or terminated",
Expand All @@ -239,10 +243,6 @@ protected BufferedReader fetchProxyMetadata(
throw new IllegalStateException(
"Unable to resolve host for cloud metadata service. Please make sure your cluster is not terminated",
e);
} catch (FileNotFoundException e) {
throw new IllegalStateException(
"Metadata service request path not found. Please make sure the metadata service url is correct",
e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void should_throw_when_bundle_not_readable() throws Exception {
Throwable t = catchThrowable(() -> cloudConfigFactory.createCloudConfig(configFile));
assertThat(t)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Invalid bundle: missing file config.json");
.hasMessage("Invalid bundle: missing file config.json");
}

@Test
Expand All @@ -133,19 +133,40 @@ public void should_throw_when_metadata_not_found() throws Exception {
URL configFile = new URL("http", "localhost", wireMockRule.port(), BUNDLE_PATH);
CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
Throwable t = catchThrowable(() -> cloudConfigFactory.createCloudConfig(configFile));
assertThat(t).isInstanceOf(IllegalStateException.class).hasMessageContaining("metadata");
assertThat(t)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Metadata service request path not found. Please make sure the metadata service url is correct");
}

@Test
public void should_throw_when_stream_io_error_fetch_metadata() throws Exception {
public void should_throw_when_metadata_unauthorized() throws Exception {
// given
mockHttpSecureBundle(secureBundle());
stubFor(any(urlPathEqualTo("/metadata")).willReturn(aResponse().withStatus(401)));
// when
URL configFile = new URL("http", "localhost", wireMockRule.port(), BUNDLE_PATH);
CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
Throwable t = catchThrowable(() -> cloudConfigFactory.createCloudConfig(configFile));
assertThat(t).isInstanceOf(IllegalStateException.class).hasMessageContaining("metadata");
assertThat(t)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Unable to read data from cloud metadata service. Please make sure your cluster is not parked or terminated");
}

@Test
public void should_throw_when_metadata_bad_gateway() throws Exception {
// given
mockHttpSecureBundle(secureBundle());
stubFor(any(urlPathEqualTo("/metadata")).willReturn(aResponse().withStatus(502)));
// when
URL configFile = new URL("http", "localhost", wireMockRule.port(), BUNDLE_PATH);
CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
Throwable t = catchThrowable(() -> cloudConfigFactory.createCloudConfig(configFile));
assertThat(t)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Unable to read data from cloud metadata service. Please make sure your cluster is not parked or terminated");
}

@Test
Expand Down

0 comments on commit fc66bca

Please sign in to comment.