Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: restore ServicesMainTest.java #17262

Merged
merged 44 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1c41076
feat: node starts without using Netwoprk.isLocal
edward-swirldslabs Mar 13, 2024
675ac38
missing newline
edward-swirldslabs Mar 13, 2024
a2a49cc
refactored use of nodesToStart
edward-swirldslabs Mar 14, 2024
13a4ae1
Merge branch 'develop' into 11751-determine-node-ids-to-start-without…
edward-swirldslabs Dec 17, 2024
8944f2e
Cleanup Post Merge Of Develop
edward-swirldslabs Dec 17, 2024
4692667
code review.
edward-swirldslabs Dec 17, 2024
de3c113
Merge branch 'develop' into 11751-determine-node-ids-to-start-without…
edward-swirldslabs Dec 17, 2024
e94d18f
Fix Ability To Run Locally
edward-swirldslabs Dec 17, 2024
40f61cb
unused methods.
edward-swirldslabs Dec 17, 2024
99c58bf
adding environment nodesToRun.
edward-swirldslabs Dec 19, 2024
c22de65
Merge branch 'develop' into 11751-determine-node-ids-to-start-without…
edward-swirldslabs Dec 19, 2024
b595450
Merge branch 'develop' into 11751-determine-node-ids-to-start-without…
edward-swirldslabs Dec 20, 2024
4327bd3
Merge branch 'develop' into 11751-determine-node-ids-to-start-without…
edward-swirldslabs Dec 20, 2024
997ff3e
code review and post merge of develop
edward-swirldslabs Dec 20, 2024
e3d685b
Merge branch 'develop' into 11751-determine-node-ids-to-start-without…
edward-swirldslabs Dec 20, 2024
d31f0ee
Merge branch 'main' into 11751-determine-node-ids-to-start-without-ne…
edward-swirldslabs Dec 22, 2024
3a1fdbc
cleaning up imports
edward-swirldslabs Dec 23, 2024
6450780
removing broken unit tests
edward-swirldslabs Dec 23, 2024
2f63a17
adding NodeIdConverter
edward-swirldslabs Dec 23, 2024
3bb8795
simplify GUI invocation
edward-swirldslabs Dec 23, 2024
a7ea4d8
Merge branch 'main' into 11751-determine-node-ids-to-start-without-ne…
edward-swirldslabs Dec 23, 2024
a4868a1
spotless apply after merging main.
edward-swirldslabs Dec 23, 2024
a3b8a81
updating name of NetworkEndpointConverter
edward-swirldslabs Dec 23, 2024
0fb9830
chore: add consensus node id support to the docker images
nathanklick Dec 31, 2024
8394a9f
chore: add diagnostic output to container images
nathanklick Dec 31, 2024
bc0466f
Merge branch 'main' into 11751-determine-node-ids-to-start-without-ne…
edward-swirldslabs Jan 2, 2025
1e0a6d7
chore: apply spotless formatting
nathanklick Jan 3, 2025
ccc462a
Merge branch 'main' into 11751-determine-node-ids-to-start-without-ne…
nathanklick Jan 3, 2025
21e5ef6
Merge branch 'main' into 11751-determine-node-ids-to-start-without-ne…
edward-swirldslabs Jan 6, 2025
45e8990
Merge branch 'main' into 11751-determine-node-ids-to-start-without-ne…
edward-swirldslabs Jan 7, 2025
4ff5141
Updating log text
edward-swirldslabs Jan 7, 2025
3060319
Merge branch 'main' into 11751-determine-node-ids-to-start-without-ne…
edward-swirldslabs Jan 7, 2025
29ba5c2
Update hedera-node/hedera-app/src/main/java/com/hedera/node/app/Servi…
edward-swirldslabs Jan 8, 2025
f9ea609
Update hedera-node/hedera-app/src/main/java/com/hedera/node/app/info/…
edward-swirldslabs Jan 8, 2025
8a8c6b6
Merge branch 'main' into 11751-determine-node-ids-to-start-without-ne…
edward-swirldslabs Jan 8, 2025
9ebbbbe
Merge branch 'refs/heads/main' into 17261-restore-ServicesMainTest.java
edward-swirldslabs Jan 8, 2025
5dd6f24
chore: restoring ServicesMainTest
edward-swirldslabs Jan 8, 2025
30826e0
deleting commented out line
edward-swirldslabs Jan 8, 2025
0224833
updated test names to reflect actual behavior.
edward-swirldslabs Jan 8, 2025
acd05d5
Merge branch 'main' into 17261-restore-ServicesMainTest.java
edward-swirldslabs Jan 8, 2025
fc7388f
fixes the unit test problems.
edward-swirldslabs Jan 8, 2025
940f735
Merge branch 'main' into 17261-restore-ServicesMainTest.java
edward-swirldslabs Jan 8, 2025
367ae25
squash the bug
edward-swirldslabs Jan 9, 2025
7c9e1fc
adding documenting comments.
edward-swirldslabs Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ public static void main(final String... args) throws Exception {
EXCEPTION.getMarker(),
"Multiple nodes were supplied via the command line. Only one node can be started per java process.");
exitSystem(NODE_ADDRESS_MISMATCH);
// the following throw is not reachable in production,
// but reachable in testing with static mocked system exit calls.
throw new ConfigurationException();
}
final var platformConfig = buildPlatformConfig();
// Determine which nodes were _requested_ to run from the command line
Expand Down Expand Up @@ -410,12 +413,14 @@ private static NodeId ensureSingleNode(@NonNull final List<NodeId> nodesToRun) {
if (nodesToRun.isEmpty()) {
final String errorMessage = "No nodes are configured to run locally.";
logger.error(STARTUP.getMarker(), errorMessage);
exitSystem(NODE_ADDRESS_MISMATCH, errorMessage);
throw new ConfigurationException(errorMessage);
}

if (nodesToRun.size() > 1) {
final String errorMessage = "Multiple nodes are configured to run locally.";
logger.error(EXCEPTION.getMarker(), errorMessage);
exitSystem(NODE_ADDRESS_MISMATCH, errorMessage);
throw new ConfigurationException(errorMessage);
}
return nodesToRun.getFirst();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2021-2025 Hedera Hashgraph, LLC
*
* 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 com.hedera.node.app;

import static com.swirlds.platform.system.SystemExitCode.NODE_ADDRESS_MISMATCH;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mock.Strictness.LENIENT;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.node.app.version.ServicesSoftwareVersion;
import com.swirlds.metrics.api.Metrics;
import com.swirlds.platform.config.legacy.ConfigurationException;
import com.swirlds.platform.config.legacy.LegacyConfigProperties;
import com.swirlds.platform.config.legacy.LegacyConfigPropertiesLoader;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.system.SystemExitUtils;
import com.swirlds.platform.system.address.AddressBook;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
final class ServicesMainTest {
private static final MockedStatic<LegacyConfigPropertiesLoader> legacyConfigPropertiesLoaderMockedStatic =
mockStatic(LegacyConfigPropertiesLoader.class);

@Mock(strictness = LENIENT)
private LegacyConfigProperties legacyConfigProperties = mock(LegacyConfigProperties.class);

@Mock(strictness = LENIENT)
private Metrics metrics;

@Mock(strictness = LENIENT)
private Hedera hedera;

@Mock
private PlatformMerkleStateRoot merkleStateRoot;

private final ServicesMain subject = new ServicesMain();

// no local nodes specified, no environment nodes specified
@Test
void throwsExceptionOnNoNodesToRun() {
withBadCommandLineArgs();
String[] args = {};
try (MockedStatic<SystemExitUtils> systemExitUtilsMockedStatic = mockStatic(SystemExitUtils.class)) {
assertThatThrownBy(() -> ServicesMain.main(args)).isInstanceOf(ConfigurationException.class);
systemExitUtilsMockedStatic.verify(
() -> SystemExitUtils.exitSystem(eq(NODE_ADDRESS_MISMATCH), anyString()));
}
}

// local node specified which does not match the address book
@Test
void hardExitOnNonMatchingNodeId() {
withBadCommandLineArgs();
String[] args = {"-local", "1234"}; // 1234 does not match anything in address book

try (MockedStatic<SystemExitUtils> systemExitUtilsMockedStatic = mockStatic(SystemExitUtils.class)) {
assertThatThrownBy(() -> ServicesMain.main(args)).isInstanceOf(ConfigurationException.class);
systemExitUtilsMockedStatic.verify(
() -> SystemExitUtils.exitSystem(eq(NODE_ADDRESS_MISMATCH), anyString()));
}
}

// more than one local node specified on the commandline
@Test
void hardExitOnTooManyCliNodes() {
withBadCommandLineArgs();
String[] args = {"-local", "1", "2"}; // both "1" and "2" match entries in address book

try (MockedStatic<SystemExitUtils> systemExitUtilsMockedStatic = mockStatic(SystemExitUtils.class)) {
assertThatThrownBy(() -> ServicesMain.main(args)).isInstanceOf(ConfigurationException.class);
systemExitUtilsMockedStatic.verify(() -> SystemExitUtils.exitSystem(NODE_ADDRESS_MISMATCH));
}
}

@Test
void delegatesSoftwareVersion() {
ServicesMain.initGlobal(hedera, metrics);
final var mockVersion = new ServicesSoftwareVersion(SemanticVersion.DEFAULT);
given(hedera.getSoftwareVersion()).willReturn(mockVersion);
assertSame(mockVersion, subject.getSoftwareVersion());
}

@Test
void noopsAsExpected() {
ServicesMain.initGlobal(hedera, metrics);
assertDoesNotThrow(subject::run);
}

@Test
void createsNewMerkleStateRoot() {
ServicesMain.initGlobal(hedera, metrics);
given(hedera.newMerkleStateRoot()).willReturn(merkleStateRoot);
assertSame(merkleStateRoot, subject.newMerkleStateRoot());
}

private void withBadCommandLineArgs() {
legacyConfigPropertiesLoaderMockedStatic
.when(() -> LegacyConfigPropertiesLoader.loadConfigFile(any()))
.thenReturn(legacyConfigProperties);

when(legacyConfigProperties.getAddressBook()).thenReturn(new AddressBook());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.swirlds.platform.config.PathsConfig;
import com.swirlds.platform.config.internal.ConfigMappings;
import com.swirlds.platform.config.internal.PlatformConfigUtils;
import com.swirlds.platform.config.legacy.ConfigurationException;
import com.swirlds.platform.gui.WindowConfig;
import com.swirlds.platform.health.OSHealthCheckConfig;
import com.swirlds.platform.health.OSHealthChecker;
Expand Down Expand Up @@ -372,11 +373,13 @@ public static void writeSettingsUsed(@NonNull final Configuration configuration)

for (final NodeId nodeId : nodesToRun) {
if (!addressBook.contains(nodeId)) {
final String errorMessage = "Node " + nodeId + " is not in the address book and cannot be started.";
// all nodes to start must exist in the address book
logger.error(
EXCEPTION.getMarker(), "Node {} is not in the address book and cannot be started.", nodeId);
exitSystem(
NODE_ADDRESS_MISMATCH, "Node " + nodeId + " is not in the address book and cannot be started.");
logger.error(EXCEPTION.getMarker(), errorMessage);
exitSystem(NODE_ADDRESS_MISMATCH, errorMessage);
// the following throw is not reachable in production,
// but reachable in testing with static mocked system exit calls.
throw new ConfigurationException(errorMessage);
}
}

Expand Down
Loading