-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
S3DestinationAcceptanceTest.java
168 lines (150 loc) · 6.53 KB
/
S3DestinationAcceptanceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* MIT License
*
* Copyright (c) 2020 Airbyte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.airbyte.integrations.destination.s3;
import static io.airbyte.integrations.destination.s3.S3DestinationConstants.NAME_TRANSFORMER;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion;
import com.amazonaws.services.s3.model.DeleteObjectsResult;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.airbyte.commons.io.IOs;
import io.airbyte.commons.jackson.MoreMappers;
import io.airbyte.commons.json.Jsons;
import io.airbyte.integrations.destination.s3.util.S3OutputPathHelper;
import io.airbyte.integrations.standardtest.destination.DestinationAcceptanceTest;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* When adding a new S3 destination acceptance test, extend this class and do the following:
* <li>Implement {@link #getFormatConfig} that returns a {@link S3FormatConfig}</li>
* <li>Implement {@link #retrieveRecords} that returns the Json records for the test</li>
*
* Under the hood, a {@link S3DestinationConfig} is constructed as follows:
* <li>Retrieve the secrets from "secrets/config.json"</li>
* <li>Get the S3 bucket path from the constructor</li>
* <li>Get the format config from {@link #getFormatConfig}</li>
*/
public abstract class S3DestinationAcceptanceTest extends DestinationAcceptanceTest {
protected static final Logger LOGGER = LoggerFactory.getLogger(S3DestinationAcceptanceTest.class);
protected static final ObjectMapper MAPPER = MoreMappers.initMapper();
protected final String secretFilePath = "secrets/config.json";
protected final S3Format outputFormat;
protected JsonNode configJson;
protected S3DestinationConfig config;
protected AmazonS3 s3Client;
protected S3DestinationAcceptanceTest(S3Format outputFormat) {
this.outputFormat = outputFormat;
}
protected JsonNode getBaseConfigJson() {
return Jsons.deserialize(IOs.readFile(Path.of(secretFilePath)));
}
@Override
protected String getImageName() {
return "airbyte/destination-s3:dev";
}
@Override
protected JsonNode getConfig() {
return configJson;
}
@Override
protected JsonNode getFailCheckConfig() {
JsonNode baseJson = getBaseConfigJson();
JsonNode failCheckJson = Jsons.clone(baseJson);
// invalid credential
((ObjectNode) failCheckJson).put("access_key_id", "fake-key");
((ObjectNode) failCheckJson).put("secret_access_key", "fake-secret");
return failCheckJson;
}
/**
* Helper method to retrieve all synced objects inside the configured bucket path.
*/
protected List<S3ObjectSummary> getAllSyncedObjects(String streamName, String namespace) {
String outputPrefix = S3OutputPathHelper
.getOutputPrefix(config.getBucketPath(), namespace, streamName);
List<S3ObjectSummary> objectSummaries = s3Client
.listObjects(config.getBucketName(), outputPrefix)
.getObjectSummaries()
.stream()
.filter(o -> o.getKey().contains(NAME_TRANSFORMER.convertStreamName(streamName) + "/"))
.sorted(Comparator.comparingLong(o -> o.getLastModified().getTime()))
.collect(Collectors.toList());
LOGGER.info(
"All objects: {}",
objectSummaries.stream().map(o -> String.format("%s/%s", o.getBucketName(), o.getKey())).collect(Collectors.toList()));
return objectSummaries;
}
protected abstract JsonNode getFormatConfig();
/**
* This method does the following:
* <li>Construct the S3 destination config.</li>
* <li>Construct the S3 client.</li>
*/
@Override
protected void setup(TestDestinationEnv testEnv) {
JsonNode baseConfigJson = getBaseConfigJson();
// Set a random s3 bucket path for each integration test
JsonNode configJson = Jsons.clone(baseConfigJson);
String testBucketPath = String.format(
"%s_test_%s",
outputFormat.name().toLowerCase(Locale.ROOT),
RandomStringUtils.randomAlphanumeric(5));
((ObjectNode) configJson)
.put("s3_bucket_path", testBucketPath)
.set("format", getFormatConfig());
this.configJson = configJson;
this.config = S3DestinationConfig.getS3DestinationConfig(configJson);
LOGGER.info("Test full path: {}/{}", config.getBucketName(), config.getBucketPath());
this.s3Client = config.getS3Client();
}
/**
* Remove all the S3 output from the tests.
*/
@Override
protected void tearDown(TestDestinationEnv testEnv) {
List<KeyVersion> keysToDelete = new LinkedList<>();
List<S3ObjectSummary> objects = s3Client
.listObjects(config.getBucketName(), config.getBucketPath())
.getObjectSummaries();
for (S3ObjectSummary object : objects) {
keysToDelete.add(new KeyVersion(object.getKey()));
}
if (keysToDelete.size() > 0) {
LOGGER.info("Tearing down test bucket path: {}/{}", config.getBucketName(),
config.getBucketPath());
DeleteObjectsResult result = s3Client
.deleteObjects(new DeleteObjectsRequest(config.getBucketName()).withKeys(keysToDelete));
LOGGER.info("Deleted {} file(s).", result.getDeletedObjects().size());
}
}
}