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
- remove wildcard exceptions
  • Loading branch information
hhughes committed Jul 27, 2023
1 parent 680c7db commit f292c60
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.*;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -226,6 +232,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 +249,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 @@ -35,7 +35,8 @@
import com.github.tomakehurst.wiremock.jetty9.JettyHttpServer;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.common.base.Joiner;
import java.io.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -121,7 +122,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 +134,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 f292c60

Please sign in to comment.