Skip to content

Commit

Permalink
Fix #5257: Add ErrorStreamMessage and StatusStreamMessage to ease moc…
Browse files Browse the repository at this point in the history
…king of pods/exec requests (#5258)

* Add ErrorStreamMessage and StatusStreamMessage to ease mocking of pods/exec requests

* Fix warnings in PodTest
  • Loading branch information
Donnerbart authored Sep 6, 2023
1 parent afe0838 commit f58cdde
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Fix #5368: added support for additional ListOptions fields
* Fix #5377: added a createOr and unlock function to provide a straight-forward replacement for createOrReplace.
* Fix #5388: [crd-generator] Generate deterministic CRDs
* Fix #5257: Add ErrorStreamMessage and StatusStreamMessage to ease mocking of pods/exec requests

#### Dependency Upgrade
* Fix #5373: Gradle base API based on v8.2.1
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@

public class OutputStreamMessage extends WebSocketMessage {

private static final byte OUT_STREAM_ID = 1;
static final byte OUT_STREAM_ID = 1;

public OutputStreamMessage(String body) {
super(0L, getBodyBytes(OUT_STREAM_ID, body), true, true);
}

private static byte[] getBodyBytes(byte prefix, String body) {
static byte[] getBodyBytes(byte prefix, String body) {
byte[] original = body.getBytes(StandardCharsets.UTF_8);
byte[] prefixed = new byte[original.length + 1];
prefixed[0] = prefix;
System.arraycopy(original, 0, prefixed, 1, original.length);
return prefixed;
}

}
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);
}
}
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");
}
}
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");
}
}
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));
}
}
Loading

0 comments on commit f58cdde

Please sign in to comment.