mirror of
https://github.com/google/nomulus.git
synced 2025-07-08 12:13:19 +02:00
Add option --non_live_versions to set_num_instances command
This commit introduced a new flag to enable SetNumInstancesCommand to be able to set the number of instances for all non-live versions for a given service or for all deployed services. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=222826003
This commit is contained in:
parent
19b7a7b3ec
commit
d20b83c820
16 changed files with 582 additions and 85 deletions
146
javatests/google/registry/testing/AppEngineAdminApiHelper.java
Normal file
146
javatests/google/registry/testing/AppEngineAdminApiHelper.java
Normal file
|
@ -0,0 +1,146 @@
|
|||
// Copyright 2018 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.testing;
|
||||
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.api.services.appengine.v1.Appengine;
|
||||
import com.google.api.services.appengine.v1.model.ListServicesResponse;
|
||||
import com.google.api.services.appengine.v1.model.ListVersionsResponse;
|
||||
import com.google.api.services.appengine.v1.model.ManualScaling;
|
||||
import com.google.api.services.appengine.v1.model.Service;
|
||||
import com.google.api.services.appengine.v1.model.TrafficSplit;
|
||||
import com.google.api.services.appengine.v1.model.Version;
|
||||
import com.google.common.collect.ImmutableMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import google.registry.model.Buildable;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** Helper class to provide a builder to construct {@link Appengine} object for testing. */
|
||||
public class AppEngineAdminApiHelper extends ImmutableObject implements Buildable {
|
||||
|
||||
private Appengine appengine;
|
||||
private String appId = "domain-registry-test";
|
||||
private Multimap<String, String> liveVersionsMap = ImmutableMultimap.of();
|
||||
private Multimap<String, String> manualScalingVersionsMap = ImmutableMultimap.of();
|
||||
|
||||
/** Returns the {@link Appengine} object. */
|
||||
public Appengine getAppengine() {
|
||||
return appengine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder asBuilder() {
|
||||
return new Builder(clone(this));
|
||||
}
|
||||
|
||||
/** A builder for constructing {@link Appengine} object, since it is immutable. */
|
||||
public static class Builder extends Buildable.Builder<AppEngineAdminApiHelper> {
|
||||
public Builder() {}
|
||||
|
||||
private Builder(AppEngineAdminApiHelper instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
public Builder setAppId(String appId) {
|
||||
getInstance().appId = appId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setLiveVersionsMap(Multimap<String, String> liveVersionsMap) {
|
||||
getInstance().liveVersionsMap = liveVersionsMap;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setManualScalingVersionsMap(Multimap<String, String> manualScalingVersionsMap) {
|
||||
getInstance().manualScalingVersionsMap = manualScalingVersionsMap;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppEngineAdminApiHelper build() {
|
||||
getInstance().appengine = mock(Appengine.class, RETURNS_DEEP_STUBS);
|
||||
|
||||
// Mockito cannot mock ListServicesResponse as it is a final class
|
||||
ListServicesResponse listServicesResponse = new ListServicesResponse();
|
||||
try {
|
||||
when((Object) getInstance().appengine.apps().services().list(getInstance().appId).execute())
|
||||
.thenReturn(listServicesResponse);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// Add all given live versions to mocked Appengine object
|
||||
java.util.List<Service> serviceList = new ArrayList<>();
|
||||
getInstance()
|
||||
.liveVersionsMap
|
||||
.asMap()
|
||||
.forEach(
|
||||
(serviceId, versionList) -> {
|
||||
Service service = new Service();
|
||||
TrafficSplit trafficSplit = new TrafficSplit();
|
||||
trafficSplit.setAllocations(
|
||||
versionList.stream()
|
||||
.collect(Collectors.toMap(version -> version, version -> 1.0)));
|
||||
|
||||
service.setId(serviceId);
|
||||
service.setSplit(trafficSplit);
|
||||
serviceList.add(service);
|
||||
});
|
||||
listServicesResponse.setServices(serviceList);
|
||||
|
||||
// Add all given manual scaling versions to mocked Appengine object
|
||||
getInstance()
|
||||
.manualScalingVersionsMap
|
||||
.asMap()
|
||||
.forEach(
|
||||
(service, versionList) -> {
|
||||
// Mockito cannot mock ListVersionsResponse as it is a final class
|
||||
ListVersionsResponse listVersionsResponse = new ListVersionsResponse();
|
||||
try {
|
||||
when((Object)
|
||||
getInstance()
|
||||
.appengine
|
||||
.apps()
|
||||
.services()
|
||||
.versions()
|
||||
.list(getInstance().appId, service)
|
||||
.execute())
|
||||
.thenReturn(listVersionsResponse);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
listVersionsResponse.setVersions(
|
||||
versionList.stream()
|
||||
.map(
|
||||
versionId -> {
|
||||
Version version = new Version();
|
||||
ManualScaling manualScaling = new ManualScaling();
|
||||
version.setManualScaling(manualScaling);
|
||||
version.setId(versionId);
|
||||
return version;
|
||||
})
|
||||
.collect(Collectors.toList()));
|
||||
});
|
||||
|
||||
return getInstance();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -31,6 +31,7 @@ java_library(
|
|||
"//java/google/registry/util",
|
||||
"//java/google/registry/xml",
|
||||
"//third_party/objectify:objectify-v4_1",
|
||||
"@com_google_apis_google_api_services_appengine",
|
||||
"@com_google_appengine_api_1_0_sdk",
|
||||
"@com_google_appengine_api_stubs",
|
||||
"@com_google_appengine_testing",
|
||||
|
|
|
@ -39,6 +39,7 @@ java_library(
|
|||
"//third_party/objectify:objectify-v4_1",
|
||||
"@com_beust_jcommander",
|
||||
"@com_google_api_client",
|
||||
"@com_google_apis_google_api_services_appengine",
|
||||
"@com_google_apis_google_api_services_dns",
|
||||
"@com_google_appengine_api_1_0_sdk",
|
||||
"@com_google_appengine_remote_api",
|
||||
|
|
|
@ -20,6 +20,8 @@ import static org.mockito.Mockito.times;
|
|||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import com.beust.jcommander.ParameterException;
|
||||
import com.google.common.collect.ImmutableMultimap;
|
||||
import google.registry.testing.AppEngineAdminApiHelper;
|
||||
import google.registry.testing.InjectRule;
|
||||
import google.registry.util.AppEngineServiceUtils;
|
||||
import org.junit.Before;
|
||||
|
@ -34,18 +36,22 @@ public class SetNumInstancesCommandTest extends CommandTestCase<SetNumInstancesC
|
|||
|
||||
@Mock AppEngineServiceUtils appEngineServiceUtils;
|
||||
|
||||
private final String projectId = "domain-registry-test";
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
command = new SetNumInstancesCommand();
|
||||
command.appEngineServiceUtils = appEngineServiceUtils;
|
||||
command.projectId = projectId;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_missingService_throwsException() {
|
||||
ParameterException thrown =
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
ParameterException.class, () -> runCommand("--version=version", "--numInstances=5"));
|
||||
assertThat(thrown).hasMessageThat().contains("The following option is required: --service");
|
||||
IllegalArgumentException.class,
|
||||
() -> runCommand("--versions=version", "--num_instances=5"));
|
||||
assertThat(thrown).hasMessageThat().contains("Service must be specified");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -53,35 +59,47 @@ public class SetNumInstancesCommandTest extends CommandTestCase<SetNumInstancesC
|
|||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> runCommand("--service=", "--version=version", "--numInstances=5"));
|
||||
assertThat(thrown).hasMessageThat().contains("Service must be specified");
|
||||
() -> runCommand("--services=", "--versions=version", "--num_instances=5"));
|
||||
assertThat(thrown).hasMessageThat().contains("Invalid service(s): []");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_invalidService_throwsException() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
runCommand(
|
||||
"--services=INVALID,DEFAULT", "--versions=version", "--num_instances=5"));
|
||||
assertThat(thrown).hasMessageThat().contains("Invalid service(s): [INVALID]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_missingVersion_throwsException() {
|
||||
ParameterException thrown =
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
ParameterException.class, () -> runCommand("--service=service", "--numInstances=5"));
|
||||
assertThat(thrown).hasMessageThat().contains("The following option is required: --version");
|
||||
IllegalArgumentException.class,
|
||||
() -> runCommand("--services=DEFAULT", "--num_instances=5"));
|
||||
assertThat(thrown).hasMessageThat().contains("Version must be specified");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_emptyVersion_throwsException() {
|
||||
IllegalArgumentException thrown =
|
||||
ParameterException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> runCommand("--service=service", "--version=", "--numInstances=5"));
|
||||
assertThat(thrown).hasMessageThat().contains("Version must be specified");
|
||||
ParameterException.class,
|
||||
() -> runCommand("--services=DEFAULT", "--num_instances=5", "--versions"));
|
||||
assertThat(thrown).hasMessageThat().contains("Expected a value after parameter --versions");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_missingNumInstances_throwsException() {
|
||||
ParameterException thrown =
|
||||
assertThrows(
|
||||
ParameterException.class, () -> runCommand("--service=service", "--version=version"));
|
||||
ParameterException.class, () -> runCommand("--services=DEFAULT", "--versions=version"));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains("The following option is required: --numInstances");
|
||||
.contains("The following option is required: --num_instances");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -89,13 +107,111 @@ public class SetNumInstancesCommandTest extends CommandTestCase<SetNumInstancesC
|
|||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> runCommand("--service=service", "--version=version", "--numInstances=-5"));
|
||||
() -> runCommand("--services=DEFAULT", "--versions=version", "--num_instances=-5"));
|
||||
assertThat(thrown).hasMessageThat().contains("Number of instances must be greater than zero");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_versionNotNullWhenSettingAllNonLiveVersions_throwsException() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> runCommand("--services=DEFAULT", "--versions=version", "--num_instances=-5"));
|
||||
assertThat(thrown).hasMessageThat().contains("Number of instances must be greater than zero");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_settingNonManualScalingVersions_throwsException() {
|
||||
command.appengine =
|
||||
new AppEngineAdminApiHelper.Builder()
|
||||
.setAppId(projectId)
|
||||
.setManualScalingVersionsMap(ImmutableMultimap.of("default", "version1"))
|
||||
.build()
|
||||
.getAppengine();
|
||||
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
runCommand(
|
||||
"--non_live_versions=true",
|
||||
"--services=DEFAULT",
|
||||
"--versions=version",
|
||||
"--num_instances=10"));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains("--versions cannot be set if --non_live_versions is set");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_validParameters_succeeds() throws Exception {
|
||||
runCommand("--service=service", "--version=version", "--numInstances=10");
|
||||
verify(appEngineServiceUtils, times(1)).setNumInstances("service", "version", 10L);
|
||||
command.appengine =
|
||||
new AppEngineAdminApiHelper.Builder()
|
||||
.setAppId(projectId)
|
||||
.setManualScalingVersionsMap(ImmutableMultimap.of("default", "version"))
|
||||
.build()
|
||||
.getAppengine();
|
||||
|
||||
runCommand("--services=DEFAULT", "--versions=version", "--num_instances=10");
|
||||
verify(appEngineServiceUtils, times(1)).setNumInstances("default", "version", 10L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_settingMultipleServicesAndVersions_succeeds() throws Exception {
|
||||
command.appengine =
|
||||
new AppEngineAdminApiHelper.Builder()
|
||||
.setAppId(projectId)
|
||||
.setManualScalingVersionsMap(
|
||||
ImmutableMultimap.of(
|
||||
"default", "version1",
|
||||
"default", "version2",
|
||||
"backend", "version1",
|
||||
"backend", "version2"))
|
||||
.build()
|
||||
.getAppengine();
|
||||
|
||||
runCommand("--services=DEFAULT,BACKEND", "--versions=version1,version2", "--num_instances=10");
|
||||
verify(appEngineServiceUtils, times(1)).setNumInstances("default", "version1", 10L);
|
||||
verify(appEngineServiceUtils, times(1)).setNumInstances("default", "version2", 10L);
|
||||
verify(appEngineServiceUtils, times(1)).setNumInstances("backend", "version1", 10L);
|
||||
verify(appEngineServiceUtils, times(1)).setNumInstances("backend", "version2", 10L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_settingAllNonLiveVersions_succeeds() throws Exception {
|
||||
command.appengine =
|
||||
new AppEngineAdminApiHelper.Builder()
|
||||
.setAppId(projectId)
|
||||
.setManualScalingVersionsMap(
|
||||
ImmutableMultimap.of(
|
||||
"default", "version1", "default", "version2", "default", "version3"))
|
||||
.setLiveVersionsMap(ImmutableMultimap.of("default", "version2"))
|
||||
.build()
|
||||
.getAppengine();
|
||||
|
||||
runCommand("--non_live_versions=true", "--services=DEFAULT", "--num_instances=10");
|
||||
verify(appEngineServiceUtils, times(1)).setNumInstances("default", "version1", 10L);
|
||||
verify(appEngineServiceUtils, times(0)).setNumInstances("default", "version2", 10L);
|
||||
verify(appEngineServiceUtils, times(1)).setNumInstances("default", "version3", 10L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_noNonLiveVersions_succeeds() throws Exception {
|
||||
command.appengine =
|
||||
new AppEngineAdminApiHelper.Builder()
|
||||
.setAppId(projectId)
|
||||
.setManualScalingVersionsMap(
|
||||
ImmutableMultimap.of(
|
||||
"default", "version1", "default", "version2", "default", "version3"))
|
||||
.setLiveVersionsMap(
|
||||
ImmutableMultimap.of(
|
||||
"default", "version1", "default", "version2", "default", "version3"))
|
||||
.build()
|
||||
.getAppengine();
|
||||
|
||||
runCommand("--non_live_versions=true", "--services=DEFAULT", "--num_instances=10");
|
||||
verify(appEngineServiceUtils, times(0)).setNumInstances("default", "version1", 10L);
|
||||
verify(appEngineServiceUtils, times(0)).setNumInstances("default", "version2", 10L);
|
||||
verify(appEngineServiceUtils, times(0)).setNumInstances("default", "version3", 10L);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue