add startup conditions (#11305)

* add startup conditions

* user movkEnv
This commit is contained in:
黄科铭 2023-11-01 11:20:50 +08:00 committed by GitHub
parent 8ce06316f8
commit e7e3824aac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -18,14 +18,23 @@ package com.alibaba.nacos.core.remote.grpc.negotiator.tls;
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);
}
@ -40,6 +49,7 @@ public class DefaultTlsContextBuilderTest {
RpcServerTlsConfig.getInstance().setProtocols(null);
RpcServerTlsConfig.getInstance().setTrustCollectionCertFile(null);
RpcServerTlsConfig.getInstance().setSslProvider("");
clearRpcServerTlsConfigInstance();
}
@Test(expected = IllegalArgumentException.class)
@ -101,4 +111,10 @@ public class DefaultTlsContextBuilderTest {
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);
}
}

View File

@ -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();
}
@ -38,6 +47,7 @@ public class DefaultTlsProtocolNegotiatorBuilderTest {
RpcServerTlsConfig.getInstance().setEnableTls(false);
RpcServerTlsConfig.getInstance().setCertChainFile(null);
RpcServerTlsConfig.getInstance().setCertPrivateKey(null);
clearRpcServerTlsConfigInstance();
}
@Test
@ -52,4 +62,10 @@ public class DefaultTlsProtocolNegotiatorBuilderTest {
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);
}
}