|
| 1 | +package cloud.localstack; |
| 2 | + |
| 3 | +import cloud.localstack.docker.annotation.LocalstackDockerProperties; |
| 4 | +import com.amazonaws.services.cloudwatch.AmazonCloudWatch; |
| 5 | +import com.amazonaws.services.cloudwatch.model.*; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | + |
| 10 | +import java.text.ParseException; |
| 11 | +import java.text.SimpleDateFormat; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test integration of CloudWatch metrics with LocalStack |
| 15 | + * Issue: https://github.com/localstack/localstack/issues/712 |
| 16 | + */ |
| 17 | +@RunWith(LocalstackTestRunner.class) |
| 18 | +@LocalstackDockerProperties(ignoreDockerRunErrors=true) |
| 19 | +public class CWMetricsTest { |
| 20 | + @Test |
| 21 | + public void testCWMetricsAPIs() throws ParseException { |
| 22 | + final AmazonCloudWatch cw = TestUtils.getClientCloudWatch(); |
| 23 | + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| 24 | + |
| 25 | + Dimension dimension = new Dimension() |
| 26 | + .withName("UNIQUE_PAGES") |
| 27 | + .withValue("URLS"); |
| 28 | + |
| 29 | + /* Put metric data without value */ |
| 30 | + MetricDatum datum = new MetricDatum() |
| 31 | + .withMetricName("PAGES_VISITED") |
| 32 | + .withUnit(StandardUnit.None) |
| 33 | + .withTimestamp(dateFormat.parse("2019-01-02")) |
| 34 | + .withDimensions(dimension); |
| 35 | + |
| 36 | + PutMetricDataRequest putMetricDataRequest = new PutMetricDataRequest() |
| 37 | + .withNamespace("SITE/TRAFFIC") |
| 38 | + .withMetricData(datum); |
| 39 | + |
| 40 | + cw.putMetricData(putMetricDataRequest); |
| 41 | + |
| 42 | + /* Get metric statistics */ |
| 43 | + GetMetricStatisticsRequest getMetricStatisticsRequest = new GetMetricStatisticsRequest() |
| 44 | + .withMetricName("PAGES_VISITED") |
| 45 | + .withNamespace("SITE/TRAFFIC") |
| 46 | + /* When calling GetMetricStatistics, must specify either Statistics or ExtendedStatistics, but not both. |
| 47 | + https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-metric-statistics.html */ |
| 48 | + .withStatistics("Statistics") |
| 49 | + .withStartTime(dateFormat.parse("2019-01-01")) |
| 50 | + .withEndTime(dateFormat.parse("2019-01-03")) |
| 51 | + .withPeriod(360); |
| 52 | + |
| 53 | + GetMetricStatisticsResult metricStatistics = cw.getMetricStatistics(getMetricStatisticsRequest); |
| 54 | + Assert.assertEquals(metricStatistics.getDatapoints().size(), 1); |
| 55 | + |
| 56 | + /* List metric work as expectation */ |
| 57 | + ListMetricsResult metrics = cw.listMetrics(new ListMetricsRequest()); |
| 58 | + Assert.assertEquals(metrics.getMetrics().size(), 1); |
| 59 | + } |
| 60 | +} |
0 commit comments