-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #5257: Add ErrorStreamMessage and StatusStreamMessage to ease moc…
…king of pods/exec requests (#5258) * Add ErrorStreamMessage and StatusStreamMessage to ease mocking of pods/exec requests * Fix warnings in PodTest
- Loading branch information
1 parent
afe0838
commit f58cdde
Showing
8 changed files
with
322 additions
and
55 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
30 changes: 30 additions & 0 deletions
30
...erver-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessage.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,30 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client.server.mock; | ||
|
||
import io.fabric8.mockwebserver.internal.WebSocketMessage; | ||
|
||
import static io.fabric8.kubernetes.client.server.mock.OutputStreamMessage.getBodyBytes; | ||
|
||
public class ErrorStreamMessage extends WebSocketMessage { | ||
|
||
static final byte ERR_STREAM_ID = 2; | ||
|
||
public ErrorStreamMessage(String body) { | ||
super(0L, getBodyBytes(ERR_STREAM_ID, body), true, true); | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
...rver-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessage.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,47 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client.server.mock; | ||
|
||
import io.fabric8.kubernetes.api.model.Status; | ||
import io.fabric8.kubernetes.api.model.StatusBuilder; | ||
import io.fabric8.kubernetes.client.utils.Serialization; | ||
import io.fabric8.mockwebserver.internal.WebSocketMessage; | ||
|
||
import static io.fabric8.kubernetes.client.server.mock.OutputStreamMessage.getBodyBytes; | ||
|
||
public class StatusStreamMessage extends WebSocketMessage { | ||
|
||
static final byte ERROR_CHANNEL_STREAM_ID = 3; | ||
|
||
public StatusStreamMessage(final int exitCode) { | ||
super(0L, getBodyBytes(ERROR_CHANNEL_STREAM_ID, getStatusBody(exitCode)), true, true); | ||
} | ||
|
||
private static String getStatusBody(int exitCode) { | ||
final Status status = new StatusBuilder() // | ||
.withStatus(exitCode == 0 ? "Success" : "Failure") | ||
.withReason(exitCode == 0 ? "ExitCode" : "NonZeroExitCode") | ||
.withNewDetails() | ||
.addNewCause() | ||
.withReason("ExitCode") | ||
.withMessage(String.valueOf(exitCode)) | ||
.endCause() | ||
.endDetails() | ||
.build(); | ||
return Serialization.asJson(status); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...r-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessageTest.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,33 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client.server.mock; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ErrorStreamMessageTest { | ||
|
||
@Test | ||
void testMessageEncoding() { | ||
final ErrorStreamMessage message = new ErrorStreamMessage("foobar"); | ||
assertThat(message.isBinary()).isTrue(); | ||
assertThat(message.isToBeRemoved()).isTrue(); | ||
assertThat(message.getBytes()).startsWith(ErrorStreamMessage.ERR_STREAM_ID); | ||
assertThat(message.getBody().substring(1)).isEqualTo("foobar"); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessageTest.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,33 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client.server.mock; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class OutputStreamMessageTest { | ||
|
||
@Test | ||
void testMessageEncoding() { | ||
final OutputStreamMessage message = new OutputStreamMessage("foobar"); | ||
assertThat(message.isBinary()).isTrue(); | ||
assertThat(message.isToBeRemoved()).isTrue(); | ||
assertThat(message.getBytes()).startsWith(OutputStreamMessage.OUT_STREAM_ID); | ||
assertThat(message.getBody().substring(1)).isEqualTo("foobar"); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessageTest.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,67 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.kubernetes.client.server.mock; | ||
|
||
import io.fabric8.kubernetes.api.model.Status; | ||
import io.fabric8.kubernetes.api.model.StatusBuilder; | ||
import io.fabric8.kubernetes.client.utils.Serialization; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class StatusStreamMessageTest { | ||
|
||
@Test | ||
void testMessageEncoding_withExitCode0() { | ||
final StatusStreamMessage message = new StatusStreamMessage(0); | ||
assertThat(message.isBinary()).isTrue(); | ||
assertThat(message.isToBeRemoved()).isTrue(); | ||
assertThat(message.getBytes()).startsWith(StatusStreamMessage.ERROR_CHANNEL_STREAM_ID); | ||
|
||
final Status status = new StatusBuilder() // | ||
.withStatus("Success") | ||
.withReason("ExitCode") | ||
.withNewDetails() | ||
.addNewCause() | ||
.withReason("ExitCode") | ||
.withMessage(String.valueOf(0)) | ||
.endCause() | ||
.endDetails() | ||
.build(); | ||
assertThat(message.getBody().substring(1)).isEqualTo(Serialization.asJson(status)); | ||
} | ||
|
||
@Test | ||
void testMessageEncoding_withExitCode1() { | ||
final StatusStreamMessage message = new StatusStreamMessage(1); | ||
assertThat(message.isBinary()).isTrue(); | ||
assertThat(message.isToBeRemoved()).isTrue(); | ||
assertThat(message.getBytes()).startsWith(StatusStreamMessage.ERROR_CHANNEL_STREAM_ID); | ||
|
||
final Status status = new StatusBuilder() // | ||
.withStatus("Failure") | ||
.withReason("NonZeroExitCode") | ||
.withNewDetails() | ||
.addNewCause() | ||
.withReason("ExitCode") | ||
.withMessage(String.valueOf(1)) | ||
.endCause() | ||
.endDetails() | ||
.build(); | ||
assertThat(message.getBody().substring(1)).isEqualTo(Serialization.asJson(status)); | ||
} | ||
} |
Oops, something went wrong.