首页 > 解决方案 > 用户定义的对 Aws Ec2 内部单元测试调用的响应

问题描述

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

问题:

这是进行该调用的方法:

  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;
  }

任何输入将不胜感激。

标签: unit-testingamazon-ec2mockingaws-sdk-java

解决方案


推荐阅读