Skip to content

Commit

Permalink
add startup conditions (#11305)
Browse files Browse the repository at this point in the history
* add startup conditions

* user movkEnv
  • Loading branch information
huangkemingyyds authored Nov 1, 2023
1 parent 8ce0631 commit e7e3824
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@

import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.core.remote.tls.RpcServerTlsConfig;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mock.env.MockEnvironment;

import java.lang.reflect.Field;

public class DefaultTlsContextBuilderTest {

private ConfigurableEnvironment environment;

@Before
public void setUp() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
RpcServerTlsConfig.getInstance().setEnableTls(true);
}

Expand All @@ -40,6 +49,7 @@ public void tearDown() throws Exception {
RpcServerTlsConfig.getInstance().setProtocols(null);
RpcServerTlsConfig.getInstance().setTrustCollectionCertFile(null);
RpcServerTlsConfig.getInstance().setSslProvider("");
clearRpcServerTlsConfigInstance();
}

@Test(expected = IllegalArgumentException.class)
Expand Down Expand Up @@ -101,4 +111,10 @@ public void testGetSslContextForNonExistFile() {
grpcServerConfig.setCertChainFile("non-exist-cert.pem");
DefaultTlsContextBuilder.getSslContext(RpcServerTlsConfig.getInstance());
}

private static void clearRpcServerTlsConfigInstance() throws Exception {
Field instanceField = RpcServerTlsConfig.class.getDeclaredField("instance");
instanceField.setAccessible(true);
instanceField.set(null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@
package com.alibaba.nacos.core.remote.grpc.negotiator.tls;

import com.alibaba.nacos.core.remote.tls.RpcServerTlsConfig;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mock.env.MockEnvironment;

import java.lang.reflect.Field;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class DefaultTlsProtocolNegotiatorBuilderTest {

private ConfigurableEnvironment environment;

private DefaultTlsProtocolNegotiatorBuilder builder;

@Before
public void setUp() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
builder = new DefaultTlsProtocolNegotiatorBuilder();
}

Expand All @@ -38,6 +47,7 @@ public void tearDown() throws Exception {
RpcServerTlsConfig.getInstance().setEnableTls(false);
RpcServerTlsConfig.getInstance().setCertChainFile(null);
RpcServerTlsConfig.getInstance().setCertPrivateKey(null);
clearRpcServerTlsConfigInstance();
}

@Test
Expand All @@ -52,4 +62,10 @@ public void testBuildEnabled() {
RpcServerTlsConfig.getInstance().setCertChainFile("test-server-cert.pem");
assertNotNull(builder.build());
}

private static void clearRpcServerTlsConfigInstance() throws Exception {
Field instanceField = RpcServerTlsConfig.class.getDeclaredField("instance");
instanceField.setAccessible(true);
instanceField.set(null, null);
}
}

0 comments on commit e7e3824

Please sign in to comment.