0

要求:需要模拟 aws-sdk-java-ec2 对 ec2.us-east-1.amazonaws.com 进行的内部调用,作为单元测试的一部分。该调用应该检索与 Ec2 实例关联的所有标签。

问题:

  • 我无法进行环境变量更改(例如 AWS_EC2_METADATA_SERVICE_ENDPOINT 到 localhost - 让您模拟元数据 url),它可以将 url 切换到 localhost,然后可以由 wiremock 处理。
  • 父方法上的静态模拟(api 调用在 sdk 内部)失败,因为它是静态类的一部分。

这是进行该调用的方法:

  public static List<TagDescription> getEC2EnvTags(InstanceInfo instanceInfo){
     AmazonEC2 client = AmazonEC2ClientBuilder.defaultClient();
     Filter filter = new Filter("resource-id", Collections.singletonList(instanceInfo.getInstanceId()));  
     // the endpoint(ec2.us-east-1.amazonaws.com) get configured in the above statement

     DescribeTagsRequest requestMade = new DescribeTagsRequest().withFilters(filter);
     DescribeTagsResult responseGotten = client.describeTags(requestMade);

     List<TagDescription> tagsList = responseGotten.getTags(); 
     // the internal call is made over here. We need amend the response gotten over here

return tagsList;

}

仅供参考,上面的函数是从另一个静态类调用的

  public static Map<String, String> captureAWSMetadata(Map<String, String> metadata) {
     instanceInfo = EC2MetadataUtils.getInstanceInfo();
     if(instanceInfo!=null) tagDescriptionList = getEC2EnvTags(instanceInfo); 
     // and tagList from above function becomes tagDesriptionList

     ...
     return metadata;
  }
  • 我可以更改任何环境变量吗?

  • 任何真正可以让我捕获内部调用的模拟框架(因为我没有发起调用,因此无法更改单元测试)?

  • 当父函数调用它时,我有什么方法可以模拟它getEC2EnvTags()本身(因为它是静态方法) ?captureAWSMetadata()

  • 我完全缺少一个模拟aws框架​​吗?

任何输入将不胜感激。

4

0 回答 0