|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.connector.cloudwatch.sink.test; |
| 19 | + |
| 20 | +import org.apache.flink.connector.aws.testutils.AWSServicesTestUtils; |
| 21 | +import org.apache.flink.connector.aws.testutils.LocalstackContainer; |
| 22 | +import org.apache.flink.connector.aws.util.AWSGeneralUtil; |
| 23 | +import org.apache.flink.connector.cloudwatch.sink.CloudWatchSink; |
| 24 | +import org.apache.flink.connector.cloudwatch.sink.MetricWriteRequest; |
| 25 | +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; |
| 26 | +import org.apache.flink.test.junit5.MiniClusterExtension; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.AfterEach; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | +import org.testcontainers.containers.Network; |
| 35 | +import org.testcontainers.junit.jupiter.Container; |
| 36 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 37 | +import org.testcontainers.utility.DockerImageName; |
| 38 | +import software.amazon.awssdk.core.SdkSystemSetting; |
| 39 | +import software.amazon.awssdk.http.SdkHttpClient; |
| 40 | +import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; |
| 41 | +import software.amazon.awssdk.services.cloudwatch.model.GetMetricDataRequest; |
| 42 | +import software.amazon.awssdk.services.cloudwatch.model.GetMetricDataResponse; |
| 43 | +import software.amazon.awssdk.services.cloudwatch.model.Metric; |
| 44 | +import software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery; |
| 45 | +import software.amazon.awssdk.services.cloudwatch.model.MetricStat; |
| 46 | + |
| 47 | +import java.time.Instant; |
| 48 | +import java.util.UUID; |
| 49 | + |
| 50 | +import static org.apache.flink.connector.aws.testutils.AWSServicesTestUtils.createConfig; |
| 51 | +import static org.assertj.core.api.Assertions.assertThat; |
| 52 | + |
| 53 | +/** |
| 54 | + * Integration test for {@link CloudWatchSink}. |
| 55 | + */ |
| 56 | +@Testcontainers |
| 57 | +@ExtendWith(MiniClusterExtension.class) |
| 58 | +public class CloudWatchSinkITCase { |
| 59 | + private static final Logger LOG = LoggerFactory.getLogger(CloudWatchSinkITCase.class); |
| 60 | + |
| 61 | + private static String testMetricName; |
| 62 | + private static final int NUMBER_OF_ELEMENTS = 50; |
| 63 | + |
| 64 | + private static StreamExecutionEnvironment env; |
| 65 | + |
| 66 | + private CloudWatchClient cloudWatchClient; |
| 67 | + private SdkHttpClient httpClient; |
| 68 | + private static final Network network = Network.newNetwork(); |
| 69 | + private static final String LOCALSTACK_DOCKER_IMAGE_VERSION = "localstack/localstack:3.7.2"; |
| 70 | + private static final String TEST_NAMESPACE = "test_namespace"; |
| 71 | + |
| 72 | + @Container |
| 73 | + private static final LocalstackContainer MOCK_CLOUDWATCH_CONTAINER = |
| 74 | + new LocalstackContainer(DockerImageName.parse(LOCALSTACK_DOCKER_IMAGE_VERSION)) |
| 75 | + .withNetwork(network) |
| 76 | + .withNetworkAliases("localstack"); |
| 77 | + |
| 78 | + @BeforeEach |
| 79 | + public void setup() { |
| 80 | + System.setProperty(SdkSystemSetting.CBOR_ENABLED.property(), "false"); |
| 81 | + |
| 82 | + testMetricName = UUID.randomUUID().toString(); |
| 83 | + |
| 84 | + env = StreamExecutionEnvironment.getExecutionEnvironment(); |
| 85 | + env.setParallelism(1); |
| 86 | + |
| 87 | + httpClient = AWSServicesTestUtils.createHttpClient(); |
| 88 | + |
| 89 | + cloudWatchClient = AWSServicesTestUtils.createAwsSyncClient( |
| 90 | + MOCK_CLOUDWATCH_CONTAINER.getEndpoint(), httpClient, CloudWatchClient.builder()); |
| 91 | + |
| 92 | + LOG.info("Done setting up the localstack."); |
| 93 | + } |
| 94 | + |
| 95 | + @AfterEach |
| 96 | + public void teardown() { |
| 97 | + System.clearProperty(SdkSystemSetting.CBOR_ENABLED.property()); |
| 98 | + AWSGeneralUtil.closeResources(httpClient, cloudWatchClient); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testRandomDataSuccessfullyWritten() throws Exception { |
| 103 | + CloudWatchSink<MetricWriteRequest> cloudWatchSink = CloudWatchSink.<MetricWriteRequest>builder() |
| 104 | + .setNamespace(TEST_NAMESPACE) |
| 105 | + .setCloudWatchClientProperties(createConfig(MOCK_CLOUDWATCH_CONTAINER.getEndpoint())) |
| 106 | + .build(); |
| 107 | + |
| 108 | + Instant testTimestamp = Instant.now(); |
| 109 | + |
| 110 | + env.fromSequence(1, NUMBER_OF_ELEMENTS) |
| 111 | + .map(data -> MetricWriteRequest.builder() |
| 112 | + .withMetricName(testMetricName) |
| 113 | + .addValue(1.0d) |
| 114 | + .withTimestamp(testTimestamp) |
| 115 | + .build()) |
| 116 | + .sinkTo(cloudWatchSink); |
| 117 | + |
| 118 | + env.execute("Integration Test"); |
| 119 | + |
| 120 | + GetMetricDataResponse response = cloudWatchClient.getMetricData(GetMetricDataRequest.builder() |
| 121 | + .metricDataQueries( |
| 122 | + MetricDataQuery.builder().metricStat(getMetricStat("Sum")).build(), |
| 123 | + MetricDataQuery.builder().metricStat(getMetricStat("SampleCount")).build() |
| 124 | + ) |
| 125 | + .startTime(testTimestamp.minusSeconds(300)) |
| 126 | + .endTime(testTimestamp.plusSeconds(300)) |
| 127 | + .build()); |
| 128 | + |
| 129 | + response.metricDataResults().forEach( |
| 130 | + result -> assertThat(result.values()).containsExactly(Double.valueOf(NUMBER_OF_ELEMENTS)) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + private static MetricStat getMetricStat(String stat) { |
| 135 | + return MetricStat.builder() |
| 136 | + .metric( |
| 137 | + Metric.builder() |
| 138 | + .namespace(TEST_NAMESPACE) |
| 139 | + .metricName(testMetricName) |
| 140 | + .build() |
| 141 | + ) |
| 142 | + .stat(stat) |
| 143 | + .period(300) |
| 144 | + .build(); |
| 145 | + } |
| 146 | +} |
0 commit comments