View Javadoc
1   /**
2    * Copyright (c) 2012-2014, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.beanstalk.maven.plugin;
31  
32  import com.amazonaws.auth.AWSCredentials;
33  import com.amazonaws.auth.BasicAWSCredentials;
34  import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalk;
35  import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalkClient;
36  import com.amazonaws.services.elasticbeanstalk.model.ConfigurationSettingsDescription;
37  import com.amazonaws.services.elasticbeanstalk.model.DescribeConfigurationSettingsRequest;
38  import com.amazonaws.services.elasticbeanstalk.model.DescribeConfigurationSettingsResult;
39  import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsRequest;
40  import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsResult;
41  import com.amazonaws.services.elasticbeanstalk.model.EnvironmentDescription;
42  import com.jcabi.log.Logger;
43  import java.util.ArrayList;
44  import java.util.Arrays;
45  import org.apache.maven.plugin.logging.SystemStreamLog;
46  import org.hamcrest.MatcherAssert;
47  import org.hamcrest.Matchers;
48  import org.junit.Assume;
49  import org.junit.BeforeClass;
50  import org.junit.Test;
51  import org.mockito.Mockito;
52  import org.slf4j.impl.StaticLoggerBinder;
53  
54  /**
55   * Test case for {@link Environment}.
56   * @author Yegor Bugayenko (yegor@tpc2.com)
57   * @version $Id$
58   * @checkstyle ClassDataAbstractionCoupling (500 lines)
59   */
60  public final class EnvironmentTest {
61  
62      /**
63       * AWS key, if provided in command line.
64       */
65      private static final String AWS_KEY = System.getProperty("aws.key");
66  
67      /**
68       * AWS secret, if provided in command line.
69       */
70      private static final String AWS_SECRET = System.getProperty("aws.secret");
71  
72      /**
73       * Configure logging.
74       */
75      @BeforeClass
76      public static void initLog() {
77          StaticLoggerBinder.getSingleton().setMavenLog(new SystemStreamLog());
78      }
79  
80      /**
81       * Environment can check readiness of environment.
82       * @throws Exception If something is wrong
83       */
84      @Test
85      public void checksReadinessOfEnvironment() throws Exception {
86          final String eid = "some-env-id";
87          final AWSElasticBeanstalk ebt = Mockito.mock(AWSElasticBeanstalk.class);
88          Mockito.doReturn(
89              new DescribeConfigurationSettingsResult().withConfigurationSettings(
90                  new ArrayList<ConfigurationSettingsDescription>(0)
91              )
92          ).when(ebt)
93              .describeConfigurationSettings(
94                  Mockito.any(DescribeConfigurationSettingsRequest.class)
95              );
96          Mockito.doReturn(
97              new DescribeEnvironmentsResult().withEnvironments(
98                  Arrays.asList(
99                      new EnvironmentDescription()
100                         .withStatus("Ready")
101                         .withHealth("Red")
102                 )
103             )
104         ).when(ebt)
105             .describeEnvironments(
106                 Mockito.any(DescribeEnvironmentsRequest.class)
107             );
108         final Environment env = new Environment(ebt, eid);
109         MatcherAssert.assertThat(
110             env.green(),
111             Matchers.equalTo(false)
112         );
113     }
114 
115     /**
116      * Environment can fetch TAIL report from live environment.
117      * @throws Exception If something is wrong
118      */
119     @Test
120     public void fetchesTailReportFromLiveEnvironment() throws Exception {
121         Assume.assumeThat(EnvironmentTest.AWS_KEY, Matchers.notNullValue());
122         final AWSCredentials creds = new BasicAWSCredentials(
123             EnvironmentTest.AWS_KEY,
124             EnvironmentTest.AWS_SECRET
125         );
126         final AWSElasticBeanstalk ebt = new AWSElasticBeanstalkClient(creds);
127         final Environment env = new Environment(ebt, "e-2n2mqauqae");
128         Logger.info(this, "tail report:\n%s", env.tail());
129     }
130 
131     /**
132      * Environment can collect events from running environment.
133      * @throws Exception If something is wrong
134      */
135     @Test
136     public void collectsEventsFromLiveEnvironment() throws Exception {
137         Assume.assumeThat(EnvironmentTest.AWS_KEY, Matchers.notNullValue());
138         final AWSCredentials creds = new BasicAWSCredentials(
139             EnvironmentTest.AWS_KEY,
140             EnvironmentTest.AWS_SECRET
141         );
142         final AWSElasticBeanstalk ebt = new AWSElasticBeanstalkClient(creds);
143         final Environment env = new Environment(ebt, "e-nxmcbf3pvk");
144         Logger.info(
145             this,
146             "events: %[list]s",
147             Arrays.asList(env.events())
148         );
149     }
150 
151 }