-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from admd/original-exception-info
Better error handling in Runner and Wheel calls
- Loading branch information
Showing
8 changed files
with
177 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/test/java/com/suse/salt/netapi/calls/runner/JobsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.suse.salt.netapi.calls.runner; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.any; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.net.HttpURLConnection; | ||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import com.suse.salt.netapi.calls.modules.SaltUtilTest; | ||
import com.suse.salt.netapi.client.SaltClient; | ||
import com.suse.salt.netapi.exception.SaltException; | ||
import com.suse.salt.netapi.results.Result; | ||
import com.suse.salt.netapi.utils.ClientUtils; | ||
|
||
/** | ||
* Tests for the runner Jobs module. | ||
*/ | ||
public class JobsTest { | ||
|
||
private static final int MOCK_HTTP_PORT = 8888; | ||
|
||
static final String JSON_LIST_JOB_RESPONSE = ClientUtils.streamToString( | ||
SaltUtilTest.class.getResourceAsStream("/runner/list_job_response.json")); | ||
static final String JSON_LIST_JOB_UNPARSABLE_DATE_RESPONSE = ClientUtils.streamToString( | ||
SaltUtilTest.class. | ||
getResourceAsStream("/runner/list_job_unparseable_date_response.json")); | ||
static final String JSON_SALT_EXCEPTION_RESPONSE = ClientUtils.streamToString( | ||
SaltUtilTest.class.getResourceAsStream("/runner/salt_exception_response.json")); | ||
|
||
@Rule | ||
public WireMockRule wireMockRule = new WireMockRule(MOCK_HTTP_PORT); | ||
|
||
private SaltClient client; | ||
|
||
@Before | ||
public void init() { | ||
URI uri = URI.create("http://localhost:" + Integer.toString(MOCK_HTTP_PORT)); | ||
client = new SaltClient(uri); | ||
} | ||
|
||
@Test | ||
public void testListJob() throws SaltException { | ||
stubFor(any(urlMatching("/")) | ||
.willReturn(aResponse() | ||
.withStatus(HttpURLConnection.HTTP_OK) | ||
.withHeader("Content-Type", "application/json") | ||
.withBody(JSON_LIST_JOB_RESPONSE))); | ||
|
||
Result<Jobs.Info> result = Jobs.listJob("111").callSync(client); | ||
|
||
assertTrue(result.result().isPresent()); | ||
assertEquals(Optional.empty(), result.error()); | ||
|
||
} | ||
|
||
@Test | ||
public void testListJobInvalidResponse() throws SaltException { | ||
stubFor(any(urlMatching("/")) | ||
.willReturn(aResponse() | ||
.withStatus(HttpURLConnection.HTTP_OK) | ||
.withHeader("Content-Type", "application/json") | ||
.withBody(JSON_LIST_JOB_UNPARSABLE_DATE_RESPONSE))); | ||
|
||
Result<Jobs.Info> result = Jobs.listJob("111").callSync(client); | ||
|
||
assertEquals(Optional.empty(), result.result()); | ||
assertTrue(result.error().isPresent()); | ||
assertThat(result.error().get().toString(), | ||
CoreMatchers.containsString("java.text.ParseException:")); | ||
} | ||
|
||
@Test | ||
public void testResponseWithException() throws SaltException { | ||
stubFor(any(urlMatching("/")) | ||
.willReturn(aResponse() | ||
.withStatus(HttpURLConnection.HTTP_OK) | ||
.withHeader("Content-Type", "application/json") | ||
.withBody(JSON_SALT_EXCEPTION_RESPONSE))); | ||
|
||
Result<Jobs.Info> result = Jobs.listJob("111").callSync(client); | ||
|
||
assertEquals(Optional.empty(), result.result()); | ||
assertTrue(result.error().isPresent()); | ||
assertThat(result.error().get().toString(), | ||
CoreMatchers.containsString("unexpected keyword argument" | ||
+ " 'kwargs'")); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"return": [{ | ||
"Function": "unknown-function", | ||
"jid": "111", | ||
"Target": "unknown-target", | ||
"Target-type": "", | ||
"User": "root", | ||
"Result": {}, | ||
"StartTime": "2017, May 07 17:55:55.169861", | ||
"Arguments": [] | ||
}] | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/resources/runner/list_job_unparseable_date_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"return": [{ | ||
"Function": "unknown-function", | ||
"jid": "111", | ||
"Target": "unknown-target", | ||
"Target-type": "", | ||
"User": "root", | ||
"Result": {}, | ||
"StartTime": "2017", | ||
"Arguments": [] | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"return": ["Exception occurred in runner jobs.list_job: Traceback (most recent call last):\n File \"/usr/lib/python2.7/dist-packages/salt/client/mixins.py\", line 395, in _low\n data['return'] = self.functions[fun](*args, **kwargs)\nTypeError: list_job() got an unexpected keyword argument 'kwargs'\n"] | ||
} |