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

apollo-mockserver #55

Closed
3 tasks done
cheese8 opened this issue Mar 29, 2024 · 6 comments
Closed
3 tasks done

apollo-mockserver #55

cheese8 opened this issue Mar 29, 2024 · 6 comments

Comments

@cheese8
Copy link
Contributor

cheese8 commented Mar 29, 2024

咨询使用方式

微服务X通过app.id, env, namespaces等接入Apollo,现在想对微服务X进行接口e2e的集成测试,apollo-mockserver是否支持在应用启动后(拉取全量apollo配置)不同的集成测试case中,mock注入和覆盖apollo key & value?

@nobodyiam
Copy link
Member

可以参考下测试实例

@cheese8
Copy link
Contributor Author

cheese8 commented Apr 2, 2024

多谢回复,但是好像没效果,效果是:测试方法testInterfaceA跑起来后,发现注入的值是value1,而不是value2,期望是value2。

大佬帮忙看看 @nobodyiam ,大致使用方式伪代码如下:

@AutoConfigureMockMvc
@SpringBootTest
abstract class ControllerIntegrationTest {
   @ClassRule EmbeddedApollo apollo = new EmbeddedApollo();

  @Autowired
   MockMvc mockMvc
}
class Controller1Test extends ControllerIntegrationTest{
   @Test testInterfaceA() {
         apollo.addOrModifyProperty("application", "key1", "value2");
         mockMvc.perform(MockMvcRequestBuilders.post("/controller1/url1"));
    }
}
@RestController
class Controller1 {
   @Value("#{'${key1}'}")
   String key1;
}

配置文件

test
    resources
          META-INF
               app.properties
                    app.id = appId1
          mockdata-application.properties
               key1 = value1

@nobodyiam
Copy link
Member

上面贴的测试示例能正常跑吗?

@cheese8
Copy link
Contributor Author

cheese8 commented Apr 3, 2024

上面贴的测试示例能正常跑吗?

伪代码,我整一个能跑的demo出来,晚点上传,请帮忙看看,多谢。

https://github.com/cheese8/apollo-mockserver-demo

@nobodyiam 大佬,请帮忙看看,感谢。

好像是版本错配的原因,我调整版本再测试下,稍晚反馈,感谢。

@kimmking
Copy link
Contributor

kimmking commented Apr 8, 2024

我觉得可以这么玩:添加一个方法 loadOverriddenConfigForFilename
同时把需要更新的值放到额外的配置文件里去。
当我们首次加载以后,每次需要使用更新的值,调用一下这个方法去读取额外的k-v配置。

from: https://github.com/kimmking/apollo-java/blob/3399e941979e26a6ab0605c54a432c3cf94b4e0d/apollo-mockserver/src/main/java/com/ctrip/framework/apollo/mockserver/ApolloTestingServer.java

@cheese8
Copy link
Contributor Author

cheese8 commented Apr 8, 2024

我觉得可以这么玩:添加一个方法 loadOverriddenConfigForFilename , 同时把需要更新的值放到额外的配置文件里去。 当我们首次加载以后,每次需要使用更新的值,调用一下这个方法去读取额外的k-v配置。

from: https://github.com/kimmking/apollo-java/blob/3399e941979e26a6ab0605c54a432c3cf94b4e0d/apollo-mockserver/src/main/java/com/ctrip/framework/apollo/mockserver/ApolloTestingServer.java

感谢大佬提供新玩法。

上面贴的测试示例能正常跑吗?

伪代码,我整一个能跑的demo出来,晚点上传,请帮忙看看,多谢。

https://github.com/cheese8/apollo-mockserver-demo

@nobodyiam 大佬,请帮忙看看,感谢。

好像是版本错配的原因,我调整版本再测试下,稍晚反馈,感谢。

反馈验证结论:
1、使用上没问题。
2、apollo-client和apollo-mockserver的版本,都使用2.2.0没问题,apollo-client 1.2.0和apollo-mockserver 2.2.0报错如下:

2024-04-08 22:27:41.419  WARN 29211 --- [           main] c.c.f.a.i.AbstractConfigRepository       : Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository, reason: Get config services failed from http://apollo.meta/services/config?appId=apollo-mockserver-demo&ip=192.168.2.5 [Cause: Could not complete get operation [Cause: Unexpected end of file from server]]
2024-04-08 22:27:45.419  WARN 29211 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService    : Long polling failed, will retry in 1 seconds. appId: apollo-mockserver-demo, cluster: default, namespaces: application, long polling url: null, reason: Get config services failed from http://apollo.meta/services/config?appId=apollo-mockserver-demo&ip=192.168.2.5 [Cause: Could not complete get operation [Cause: Unexpected end of file from server]]
2024-04-08 22:27:48.924  WARN 29211 --- [           main] c.c.f.a.i.LocalFileConfigRepository      : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Get config services failed from http://apollo.meta/services/config?appId=apollo-mockserver-demo&ip=192.168.2.5 [Cause: Could not complete get operation [Cause: Unexpected end of file from server]]
2024-04-08 22:27:53.642  WARN 29211 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService    : Long polling failed, will retry in 2 seconds. appId: apollo-mockserver-demo, cluster: default, namespaces: application, long polling url: null, reason: Get config services failed from http://apollo.meta/services/config?appId=apollo-mockserver-demo&ip=192.168.2.5 [Cause: Could not complete get operation [Cause: Unexpected end of file from server]]
2024-04-08 22:27:57.461  WARN 29211 --- [           main] c.c.f.a.i.LocalFileConfigRepository      : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Get config services failed from http://apollo.meta/services/config?appId=apollo-mockserver-demo&ip=192.168.2.5 [Cause: Could not complete get operation [Cause: Unexpected end of file from server]]
2024-04-08 22:27:57.462  WARN 29211 --- [           main] c.c.f.a.i.AbstractConfigRepository       : Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.LocalFileConfigRepository, reason: Load config from local config failed! [Cause: Cannot read from local cache file /Users/yin/Downloads/apollo-mockserver-demo/target/test-classes/config-cache/apollo-mockserver-demo+default+application.properties]
2024-04-08 22:28:01.196  WARN 29211 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService    : Long polling failed, will retry in 4 seconds. appId: apollo-mockserver-demo, cluster: default, namespaces: application, long polling url: null, reason: Get config services failed from http://apollo.meta/services/config?appId=apollo-mockserver-demo&ip=192.168.2.5 [Cause: Could not complete get operation [Cause: Unexpected end of file from server]]
2024-04-08 22:28:05.224  WARN 29211 --- [           main] c.c.f.a.i.LocalFileConfigRepository      : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Get config services failed from http://apollo.meta/services/config?appId=apollo-mockserver-demo&ip=192.168.2.5 [Cause: Could not complete get operation [Cause: Unexpected end of file from server]]
2024-04-08 22:28:05.225  WARN 29211 --- [           main] c.c.f.apollo.internals.DefaultConfig     : Init Apollo Local Config failed - namespace: application, reason: Load config from local config failed! [Cause: Cannot read from local cache file /Users/yin/Downloads/apollo-mockserver-demo/target/test-classes/config-cache/apollo-mockserver-demo+default+application.properties].

3、apollo-client 2.2.0和apollo-mockserver 1.2.0报错,代码略有问题,ImmutableMap再put肯定会挂

  public void addOrModifyProperty(String namespace, String someKey, String someValue) {
    if (addedOrModifiedPropertiesOfNamespace.containsKey(namespace)) {
      addedOrModifiedPropertiesOfNamespace.get(namespace).put(someKey, someValue);
    } else {
      addedOrModifiedPropertiesOfNamespace.put(namespace, ImmutableMap.of(someKey, someValue));
    }
  }

4、必须把系统启动依赖apollo配置,全部整理成mockdata-namespace.properties放在src/test/resources/下面,namespace一多,管理维护起来还是挺烦的,不知道是否有啥好建议?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants