Skip to content

Commit

Permalink
Add a UT for topology build
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-sheng committed Mar 10, 2023
1 parent 80683b0 commit 23eeb46
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.oap.server.core;

import com.google.common.collect.Maps;
import java.util.Map;
import org.apache.skywalking.oap.server.library.module.ModuleManager;
import org.apache.skywalking.oap.server.library.module.ModuleNotFoundRuntimeException;
import org.apache.skywalking.oap.server.library.module.ModuleProviderHolder;

public abstract class MockModuleManager extends ModuleManager {
private final Map<String, ModuleProviderHolder> moduleProviderHolderMap = Maps.newHashMap();

public MockModuleManager() {
init();
}

protected abstract void init();

protected void register(String name, ModuleProviderHolder provider) {
moduleProviderHolderMap.put(name, provider);
}

@Override
public boolean has(String moduleName) {
return moduleProviderHolderMap.containsKey(moduleName);
}

@Override
public ModuleProviderHolder find(String moduleName) throws ModuleNotFoundRuntimeException {
if (!moduleProviderHolderMap.containsKey(moduleName)) {
throw new ModuleNotFoundRuntimeException("ModuleProviderHolder[" + moduleName + "] cannot found in MOCK.");
}
return moduleProviderHolderMap.get(moduleName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.oap.server.core;

import com.google.common.collect.Maps;
import java.util.Map;
import org.apache.skywalking.oap.server.library.module.ModuleServiceHolder;
import org.apache.skywalking.oap.server.library.module.Service;
import org.apache.skywalking.oap.server.library.module.ServiceNotProvidedException;

public abstract class MockModuleProvider implements ModuleServiceHolder {
protected Map<Class<? extends Service>, Service> serviceMap = Maps.newHashMap();

public MockModuleProvider() {
register();
}

protected abstract void register();

@Override
public void registerServiceImplementation(final Class<? extends Service> serviceType,
final Service service) throws ServiceNotProvidedException {
serviceMap.put(serviceType, service);
}

@Override
public <T extends Service> T getService(final Class<T> serviceType) throws ServiceNotProvidedException {
return (T) serviceMap.get(serviceType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.oap.server.core.query;

import java.util.ArrayList;
import java.util.List;
import org.apache.skywalking.oap.server.core.CoreModule;
import org.apache.skywalking.oap.server.core.CoreModuleConfig;
import org.apache.skywalking.oap.server.core.CoreModuleProvider;
import org.apache.skywalking.oap.server.core.MockModuleManager;
import org.apache.skywalking.oap.server.core.MockModuleProvider;
import org.apache.skywalking.oap.server.core.analysis.IDManager;
import org.apache.skywalking.oap.server.core.cache.NetworkAddressAliasCache;
import org.apache.skywalking.oap.server.core.config.ComponentLibraryCatalogService;
import org.apache.skywalking.oap.server.core.config.IComponentLibraryCatalogService;
import org.apache.skywalking.oap.server.core.query.type.Call;
import org.apache.skywalking.oap.server.core.query.type.Node;
import org.apache.skywalking.oap.server.core.query.type.Topology;
import org.apache.skywalking.oap.server.core.source.DetectPoint;
import org.apache.skywalking.oap.server.library.module.ModuleManager;
import org.apache.skywalking.oap.server.telemetry.TelemetryModule;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class ServiceTopologyBuilderTest {
private CoreModuleProvider moduleProvider;
private ModuleManager moduleManager;

@BeforeEach
public void setupMetrics() throws Throwable {
moduleProvider = Mockito.mock(CoreModuleProvider.class);
moduleManager = new MockModuleManager() {
@Override
protected void init() {
register(CoreModule.NAME, () -> new MockModuleProvider() {
@Override
protected void register() {
registerServiceImplementation(
IComponentLibraryCatalogService.class, new ComponentLibraryCatalogService());
}
});
register(TelemetryModule.NAME, () -> new MockModuleProvider() {
@Override
protected void register() {
registerServiceImplementation(
NetworkAddressAliasCache.class, new NetworkAddressAliasCache(new CoreModuleConfig()));
}
});
}
};
}

@Test
public void testServiceTopologyBuild() {
final ServiceTopologyBuilder serviceTopologyBuilder = new ServiceTopologyBuilder(moduleManager);
List<Call.CallDetail> serviceRelationClientCalls = new ArrayList<>();
Call.CallDetail call_1 = new Call.CallDetail();
call_1.buildFromServiceRelation(
IDManager.ServiceID.buildRelationId(
new IDManager.ServiceID.ServiceRelationDefine(
IDManager.ServiceID.buildId("SvrA", true),
IDManager.ServiceID.buildId("SvrB", true)
)
),
142,// mtls
DetectPoint.SERVER
);
serviceRelationClientCalls.add(call_1);
Call.CallDetail call_2 = new Call.CallDetail();
call_2.buildFromServiceRelation(
IDManager.ServiceID.buildRelationId(
new IDManager.ServiceID.ServiceRelationDefine(
IDManager.ServiceID.buildId("SvrA", true),
IDManager.ServiceID.buildId("SvrB", true)
)
),
49,// http
DetectPoint.SERVER
);
serviceRelationClientCalls.add(call_2);
final Topology topology = serviceTopologyBuilder.build(new ArrayList<>(), serviceRelationClientCalls);
Assertions.assertEquals(2, topology.getNodes().size());
for (final Node node : topology.getNodes()) {
if (node.getName().equals("SvrB")) {
Assertions.assertEquals("http", node.getType());
} else if (node.getName().equals("SvrA")) {
Assertions.assertEquals(null, node.getType());
}
}
for (final Call call : topology.getCalls()) {
Assertions.assertEquals(0, call.getSourceComponents().size());
Assertions.assertEquals(List.of("mtls", "http"), call.getTargetComponents());
}
}
}

0 comments on commit 23eeb46

Please sign in to comment.