我们正在使用localstack版本0.1.21在我们的项目中运行单元测试。到2019年底为止一切正常,它开始在运行测试用例的错误下方显示。
java.lang.IllegalArgumentException:服务的未知端口映射。搜寻可能的问题后,我们发现了以下问题
https://github.com/localstack/localstack/issues/1293
根据建议,我们已升级到最新版本(0.2.1)。但是现在,我们面临另一个问题。 s3 get作为响应返回了一些额外的字节。升级之前它正在工作。我们已经打印了响应,发现它打印了86个字节(块签名)。先前的vesrion字节大小为零。
您能否解释一下这是错误还是缺少某些配置
[LocalstackTestRunner和Localstack是0.2.1的新文件,而0.1.21具有LocalstackDockerTestRunner和LocalstackDocker。
@RunWith(LocalstackTestRunner.class)
@LocalstackDockerProperties(randomizePorts = true, services = {"s3"})
public class S3Test{
private static final String BUCKET = "my-test-bucket";
@Autowired
@Qualifier("awsS3Client")//it is created with default configuration using s3clientbuilder
private AmazonS3Client amazonS3Client;
@Before
public void setUp() throws Exception {
System.setProperty("aws.client", "localstack");
System.setProperty("aws.s3.endpoint", Localstack.INSTANCE.getEndpointS3());
...................
}
@Test
public void test1() throws IOException, ClassNotFoundException {
File file = Files.createTempFile("testfile", ".txt").toFile();
String key = "testkey";
amazonS3Client.putObject(BUCKET, key, file);
S3Object value1 = amazonS3Client.getObject(BUCKET, key);
byte[] bytes = IOUtils.toByteArray(value1.getObjectContent());
printResults(bytes);
assertArrayEquals(com.google.common.io.Files.toByteArray(file), bytes);
}
private void printResults(byte [] arr) throws IOException {
File file1 = new File("d:\\demo.txt");
OutputStream os = new FileOutputStream(file1);
os.write(arr);
BufferedReader br = new BufferedReader(new FileReader(file1));
String line = null;
System.out.println("File contents: ");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
0.2.1上的输出:测试失败,并出现以下断言错误:
java.lang.AssertionError: array lengths differed, expected.length=0 actual.length=86
文件内容:
0;chunk-signature=00fa94dcf5a419048a6cd61137dfdca2633bcfe528e508a80e98f97d5911a1fe
0.1.21上的输出为空,并且测试用例通过。这是预期的,因为文件为空文件已保存在s3中。
通过在AmazonS3ClientBuilder上设置chunkedEncodingDisabled属性来解决。下面是完整的bean。引用的字段是标准Bean
<bean id="amazonS3ClientBuilder" class="com.amazonaws.services.s3.AmazonS3ClientBuilder"
factory-method="standard"
p:endpointConfiguration-ref="s3EndpointConfig"
p:pathStyleAccessEnabled="true"
p:clientConfiguration-ref="awsClientConfiguration"
p:chunkedEncodingDisabled="true"
/>