Add CurlCommand option to connect to canary (#2060)

Add a --canary option (default to false) to the CurlCommand that allows
connection to the canary endpoints.

During canary analysis, only the DEFAULT-canary receives traffic. This
new flag allows use to test other canary services manually using the
curl command.
This commit is contained in:
Weimin Yu
2023-06-22 11:20:41 -04:00
committed by GitHub
parent 86b62ebe76
commit a839ec434e
4 changed files with 93 additions and 14 deletions
@@ -22,6 +22,7 @@ import static google.registry.request.Action.Service.TOOLS;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@@ -29,6 +30,7 @@ import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.request.Action.Service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
@@ -46,7 +48,7 @@ class CurlCommandTest extends CommandTestCase<CurlCommand> {
@BeforeEach
void beforeEach() {
command.setConnection(connection);
when(connection.withService(any())).thenReturn(connectionForService);
when(connection.withService(any(Service.class), anyBoolean())).thenReturn(connectionForService);
}
@Captor ArgumentCaptor<ImmutableMap<String, String>> urlParamCaptor;
@@ -54,7 +56,7 @@ class CurlCommandTest extends CommandTestCase<CurlCommand> {
@Test
void testGetInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--service=TOOLS");
verify(connection).withService(TOOLS);
verify(connection).withService(eq(TOOLS), eq(false));
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendGetRequest(eq("/foo/bar?a=1&b=2"), eq(ImmutableMap.<String, String>of()));
@@ -63,7 +65,7 @@ class CurlCommandTest extends CommandTestCase<CurlCommand> {
@Test
void testExplicitGetInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--request=GET", "--service=BACKEND");
verify(connection).withService(BACKEND);
verify(connection).withService(eq(BACKEND), eq(false));
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendGetRequest(eq("/foo/bar?a=1&b=2"), eq(ImmutableMap.<String, String>of()));
@@ -72,7 +74,7 @@ class CurlCommandTest extends CommandTestCase<CurlCommand> {
@Test
void testPostInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--data=some data", "--service=DEFAULT");
verify(connection).withService(DEFAULT);
verify(connection).withService(eq(DEFAULT), eq(false));
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
@@ -89,7 +91,7 @@ class CurlCommandTest extends CommandTestCase<CurlCommand> {
"--data=some data",
"--service=DEFAULT",
"--content-type=application/json");
verify(connection).withService(DEFAULT);
verify(connection).withService(eq(DEFAULT), eq(false));
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
@@ -118,7 +120,7 @@ class CurlCommandTest extends CommandTestCase<CurlCommand> {
void testMultiDataPost() throws Exception {
runCommand(
"--path=/foo/bar?a=1&b=2", "--data=first=100", "-d", "second=200", "--service=PUBAPI");
verify(connection).withService(PUBAPI);
verify(connection).withService(eq(PUBAPI), eq(false));
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
@@ -132,7 +134,7 @@ class CurlCommandTest extends CommandTestCase<CurlCommand> {
void testDataDoesntSplit() throws Exception {
runCommand(
"--path=/foo/bar?a=1&b=2", "--data=one,two", "--service=PUBAPI");
verify(connection).withService(PUBAPI);
verify(connection).withService(eq(PUBAPI), eq(false));
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
@@ -145,7 +147,20 @@ class CurlCommandTest extends CommandTestCase<CurlCommand> {
@Test
void testExplicitPostInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--request=POST", "--service=TOOLS");
verify(connection).withService(TOOLS);
verify(connection).withService(eq(TOOLS), eq(false));
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
eq("/foo/bar?a=1&b=2"),
eq(ImmutableMap.<String, String>of()),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq("".getBytes(UTF_8)));
}
@Test
void testCanaryInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--request=POST", "--service=TOOLS", "--canary");
verify(connection).withService(eq(TOOLS), eq(true));
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
@@ -0,0 +1,38 @@
// Copyright 2023 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.request.Action.Service.DEFAULT;
import org.junit.jupiter.api.Test;
/** Unit tests for {@link google.registry.tools.ServiceConnection}. */
public class ServiceConnectionTest {
@Test
void testServerUrl_notCanary() {
ServiceConnection connection = new ServiceConnection().withService(DEFAULT, false);
String serverUrl = connection.getServer().toString();
assertThat(serverUrl).isEqualTo("https://localhost"); // See default-config.yaml
}
@Test
void testServerUrl_canary() {
ServiceConnection connection = new ServiceConnection().withService(DEFAULT, true);
String serverUrl = connection.getServer().toString();
assertThat(serverUrl).isEqualTo("https://nomulus-dot-localhost");
}
}