TaupageYamlProviderImpl implements TaupageYamlProvider { @Override public Optional<TaupageYaml> apply(final EC2InstanceContext context) { return cache.getUnchecked(context); }  @Override Optional<TaupageYaml> apply(final EC2InstanceContext context);  }
@Test public void testApplyWithTaupageAmi() throws Exception { when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true)); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult(). withInstanceAttribute(new InstanceAttribute() .withUserData(Base64.encodeAsString("blub: fdsa".getBytes())))); final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock); assertThat(result).isPresent(); verify(ec2InstanceContextMock).isTaupageAmi(); verify(ec2InstanceContextMock).getInstanceId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeInstanceAttribute(any()); }
@Test public void testApplyWithVersionSimilarToNumber() throws Exception { when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true)); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult(). withInstanceAttribute(new InstanceAttribute() .withUserData(Base64.encodeAsString("application_id: fdsa\napplication_version: 6478e18".getBytes())))); final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock); assertThat(result).isPresent(); assertThat(result.get().getApplicationId()).isEqualTo("fdsa"); assertThat(result.get().getApplicationVersion()).isEqualTo("6478e18"); verify(ec2InstanceContextMock).isTaupageAmi(); verify(ec2InstanceContextMock).getInstanceId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeInstanceAttribute(any()); }
@Test public void testApplyWithVersionSimilarToNumber1() throws Exception { when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true)); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult(). withInstanceAttribute(new InstanceAttribute() .withUserData(Base64.encodeAsString("application_id: fdsa\napplication_version: '6478e18'".getBytes())))); final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock); assertThat(result).isPresent(); assertThat(result.get().getApplicationId()).isEqualTo("fdsa"); assertThat(result.get().getApplicationVersion()).isEqualTo("6478e18"); verify(ec2InstanceContextMock).isTaupageAmi(); verify(ec2InstanceContextMock).getInstanceId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeInstanceAttribute(any()); }
@Test public void testApplyWithTaupageAmiButInvalidYaml() throws Exception { final String yamlData = "- a\n- b\n- c\n"; when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true)); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult(). withInstanceAttribute(new InstanceAttribute() .withUserData(Base64.encodeAsString(yamlData.getBytes())))); final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock); assertThat(result).isEmpty(); verify(ec2InstanceContextMock).isTaupageAmi(); verify(ec2InstanceContextMock).getInstanceId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeInstanceAttribute(any()); }
@Test public void testApplyWithOtherAmi() throws Exception { when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(false)); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock); assertThat(result).isEmpty(); verify(ec2InstanceContextMock).isTaupageAmi(); }
@Test public void testApplyWithOtherAmiWithException() throws Exception { when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true)); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenThrow(new AmazonClientException("ups...")); final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock); assertThat(result).isEmpty(); verify(ec2InstanceContextMock).isTaupageAmi(); verify(ec2InstanceContextMock).getInstanceId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeInstanceAttribute(any()); }
@Test public void testApplyWithNoAmi() throws Exception { when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.empty()); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock); assertThat(result).isEmpty(); verify(ec2InstanceContextMock).isTaupageAmi(); }