### Question:
EcsResource extends ResourceProvider { @Override public Attributes getAttributes() { if (!isOnEcs()) { return Attributes.empty(); } Attributes.Builder attrBuilders = Attributes.newBuilder(); attrBuilders.setAttribute( ResourceAttributes.CLOUD_PROVIDER, AwsResourceConstants.cloudProvider()); try { String hostName = InetAddress.getLocalHost().getHostName(); attrBuilders.setAttribute(ResourceAttributes.CONTAINER_NAME, hostName); } catch (UnknownHostException e) { logger.log(Level.WARNING, "Could not get docker container name from hostname.", e); } String containerId = dockerHelper.getContainerId(); if (!Strings.isNullOrEmpty(containerId)) { attrBuilders.setAttribute(ResourceAttributes.CONTAINER_ID, containerId); } return attrBuilders.build(); } EcsResource(); @VisibleForTesting  EcsResource(Map<String, String> sysEnv, DockerHelper dockerHelper); @Override Attributes getAttributes();  }### Answer:
@Test void testCreateAttributes() throws UnknownHostException { when(mockDockerHelper.getContainerId()).thenReturn("0123456789A"); Map<String, String> mockSysEnv = new HashMap<>(); mockSysEnv.put(ECS_METADATA_KEY_V3, "ecs_metadata_v3_uri"); EcsResource populator = new EcsResource(mockSysEnv, mockDockerHelper); Attributes attributes = populator.getAttributes(); assertThat(attributes) .isEqualTo( Attributes.of( ResourceAttributes.CLOUD_PROVIDER, "aws", ResourceAttributes.CONTAINER_NAME, InetAddress.getLocalHost().getHostName(), ResourceAttributes.CONTAINER_ID, "0123456789A")); }
@Test void testNotOnEcs() { Map<String, String> mockSysEnv = new HashMap<>(); mockSysEnv.put(ECS_METADATA_KEY_V3, ""); mockSysEnv.put(ECS_METADATA_KEY_V4, ""); EcsResource populator = new EcsResource(mockSysEnv, mockDockerHelper); Attributes attributes = populator.getAttributes(); assertThat(attributes.isEmpty()).isTrue(); }
@Test void testContainerIdMissing() throws UnknownHostException { when(mockDockerHelper.getContainerId()).thenReturn(""); Map<String, String> mockSysEnv = new HashMap<>(); mockSysEnv.put(ECS_METADATA_KEY_V4, "ecs_metadata_v4_uri"); EcsResource populator = new EcsResource(mockSysEnv, mockDockerHelper); Attributes attributes = populator.getAttributes(); assertThat(attributes) .isEqualTo( Attributes.of( ResourceAttributes.CLOUD_PROVIDER, "aws", ResourceAttributes.CONTAINER_NAME, InetAddress.getLocalHost().getHostName())); }