azkaban-aplcache

Minor Refactor. Added a unit test for Web server instantiation

5/4/2017 7:11:11 PM

Details

diff --git a/azkaban-common/src/main/java/azkaban/AzkabanCommonModule.java b/azkaban-common/src/main/java/azkaban/AzkabanCommonModule.java
index 57752cf..b5bbd30 100644
--- a/azkaban-common/src/main/java/azkaban/AzkabanCommonModule.java
+++ b/azkaban-common/src/main/java/azkaban/AzkabanCommonModule.java
@@ -19,6 +19,8 @@ package azkaban;
 import azkaban.db.DatabaseOperator;
 import azkaban.db.DatabaseOperatorImpl;
 
+import azkaban.executor.ExecutorLoader;
+import azkaban.executor.JdbcExecutorLoader;
 import azkaban.project.JdbcProjectLoader;
 import azkaban.project.ProjectLoader;
 import azkaban.spi.Storage;
@@ -48,6 +50,7 @@ public class AzkabanCommonModule extends AbstractModule {
 
   @Override
   protected void configure() {
+    bind(ExecutorLoader.class).to(JdbcExecutorLoader.class).in(Scopes.SINGLETON);
     bind(ProjectLoader.class).to(JdbcProjectLoader.class).in(Scopes.SINGLETON);
     bind(Props.class).toInstance(config.getProps());
     bind(Storage.class).to(resolveStorageClassType()).in(Scopes.SINGLETON);
diff --git a/azkaban-common/src/main/java/azkaban/executor/ExecutorManager.java b/azkaban-common/src/main/java/azkaban/executor/ExecutorManager.java
index 65834a7..1b0a86c 100644
--- a/azkaban-common/src/main/java/azkaban/executor/ExecutorManager.java
+++ b/azkaban-common/src/main/java/azkaban/executor/ExecutorManager.java
@@ -75,7 +75,7 @@ public class ExecutorManager extends EventHandler implements
       "azkaban.executorselector.comparator.";
   static final String AZKABAN_QUEUEPROCESSING_ENABLED =
     "azkaban.queueprocessing.enabled";
-  static final String AZKABAN_USE_MULTIPLE_EXECUTORS =
+  public static final String AZKABAN_USE_MULTIPLE_EXECUTORS =
     "azkaban.use.multiple.executors";
   private static final String AZKABAN_WEBSERVER_QUEUE_SIZE =
     "azkaban.webserver.queue.size";
diff --git a/azkaban-web-server/build.gradle b/azkaban-web-server/build.gradle
index 77b7e98..fc94882 100644
--- a/azkaban-web-server/build.gradle
+++ b/azkaban-web-server/build.gradle
@@ -66,9 +66,7 @@ dependencies {
   generateRestli('com.linkedin.pegasus:generator:' + pegasusVersion)
   generateRestli('com.linkedin.pegasus:restli-tools:' + pegasusVersion)
 
-  // Needed by Velocity at runtime
-  testRuntime('commons-collections:commons-collections:3.2.2')
-  testCompile('org.hamcrest:hamcrest-all:1.3')
+  testRuntime "com.h2database:h2:1.4.193"
 }
 
 sourceSets {
diff --git a/azkaban-web-server/src/test/java/azkaban/webapp/AzkabanWebServerTest.java b/azkaban-web-server/src/test/java/azkaban/webapp/AzkabanWebServerTest.java
new file mode 100644
index 0000000..bb6cf7e
--- /dev/null
+++ b/azkaban-web-server/src/test/java/azkaban/webapp/AzkabanWebServerTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2017 LinkedIn Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ *
+ */
+
+package azkaban.webapp;
+
+import azkaban.AzkabanCommonModule;
+import azkaban.Constants;
+import azkaban.database.AzkabanDatabaseSetup;
+import azkaban.database.AzkabanDatabaseUpdater;
+import azkaban.executor.Executor;
+import azkaban.executor.ExecutorLoader;
+import azkaban.utils.Props;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static azkaban.ServiceProvider.*;
+import static azkaban.executor.ExecutorManager.*;
+import static java.util.Objects.*;
+import static org.apache.commons.io.FileUtils.*;
+import static org.junit.Assert.*;
+
+
+public class AzkabanWebServerTest {
+  public static final String AZKABAN_DB_SQL_PATH = "azkaban-db/src/main/sql";
+
+  private static final Props props = new Props();
+
+  private static String getUserManagerXmlFile() {
+    URL resource = AzkabanWebServerTest.class.getClassLoader().getResource("azkaban-users.xml");
+    return requireNonNull(resource).getPath();
+  }
+
+  private static String getSqlScriptsDir() throws IOException {
+    // Dummy because any resource file works.
+    final String dummyResourcePath = getUserManagerXmlFile();
+    Path resources = Paths.get(dummyResourcePath).getParent();
+    Path azkabanRoot = resources.getParent().getParent().getParent().getParent();
+
+    File sqlScriptDir = Paths.get(azkabanRoot.toString(), AZKABAN_DB_SQL_PATH).toFile();
+    return sqlScriptDir.getCanonicalPath();
+  }
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    tearDown();
+
+    String sqlScriptsDir = getSqlScriptsDir();
+    props.put(AzkabanDatabaseSetup.DATABASE_SQL_SCRIPT_DIR, sqlScriptsDir);
+
+    props.put("database.type", "h2");
+    props.put("h2.path", "./h2");
+
+    props.put(AZKABAN_USE_MULTIPLE_EXECUTORS, "true");
+    props.put("server.port", "0");
+    props.put("jetty.port", "0");
+    props.put("server.useSSL", "true");
+    props.put("jetty.use.ssl", "false");
+    props.put("user.manager.xml.file", getUserManagerXmlFile());
+
+    AzkabanDatabaseUpdater.runDatabaseUpdater(props, sqlScriptsDir, true);
+  }
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    SERVICE_PROVIDER.unsetInjector();
+
+    deleteQuietly(new File("h2.mv.db"));
+    deleteQuietly(new File("h2.trace.db"));
+    deleteQuietly(new File("executor.port"));
+    deleteQuietly(new File("executions"));
+    deleteQuietly(new File("projects"));
+  }
+
+  @Test
+  public void testInjection() throws Exception {
+    Injector injector = Guice.createInjector(
+        new AzkabanCommonModule(props),
+        new AzkabanWebServerModule()
+    );
+    SERVICE_PROVIDER.unsetInjector();
+    SERVICE_PROVIDER.setInjector(injector);
+
+    ExecutorLoader executorLoader = injector.getInstance(ExecutorLoader.class);
+    assertNotNull(executorLoader);
+
+    Executor executor = executorLoader.addExecutor("localhost", 60000);
+    executor.setActive(true);
+    executorLoader.updateExecutor(executor);
+
+    assertNotNull(injector.getInstance(AzkabanWebServer.class));
+    SERVICE_PROVIDER.unsetInjector();
+  }
+}
diff --git a/azkaban-web-server/src/test/resources/azkaban-users.xml b/azkaban-web-server/src/test/resources/azkaban-users.xml
new file mode 100644
index 0000000..a13035d
--- /dev/null
+++ b/azkaban-web-server/src/test/resources/azkaban-users.xml
@@ -0,0 +1,7 @@
+<azkaban-users>
+	<user username="azkaban" password="azkaban" roles="admin" groups="azkaban" />
+	<user username="metrics" password="metrics" roles="metrics"/>
+
+	<role name="admin" permissions="ADMIN" />
+	<role name="metrics" permissions="METRICS"/>
+</azkaban-users>