Pārlūkot izejas kodu

AMBARI-8321 HDP-UTILS repo URL reverts to default on ambari-server restart (trunk fix and unit tests) (dsen)

Dmytro Sen 10 gadi atpakaļ
vecāks
revīzija
a5c6834d6a

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java

@@ -712,7 +712,7 @@ public class AmbariMetaInfo {
    * @param field the field name
    * @param field the field name
    * @return the key for any repo value override
    * @return the key for any repo value override
    */
    */
-  public String generateRepoMetaKey(String stackName, String stackVersion,
+  public static String generateRepoMetaKey(String stackName, String stackVersion,
       String osType, String repoId, String field) {
       String osType, String repoId, String field) {
 
 
     StringBuilder sb = new StringBuilder("repo:/");
     StringBuilder sb = new StringBuilder("repo:/");

+ 6 - 10
ambari-server/src/main/java/org/apache/ambari/server/stack/StackContext.java

@@ -18,6 +18,7 @@
 
 
 package org.apache.ambari.server.stack;
 package org.apache.ambari.server.stack;
 
 
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.metadata.ActionMetadata;
 import org.apache.ambari.server.metadata.ActionMetadata;
 import org.apache.ambari.server.orm.dao.MetainfoDAO;
 import org.apache.ambari.server.orm.dao.MetainfoDAO;
 import org.apache.ambari.server.orm.entities.MetainfoEntity;
 import org.apache.ambari.server.orm.entities.MetainfoEntity;
@@ -88,22 +89,17 @@ public class StackContext {
    * Obtain an updated url for the repo.
    * Obtain an updated url for the repo.
    * This will check the database for a user update of the repo url.
    * This will check the database for a user update of the repo url.
    *
    *
-   * @param repoName      repository name
+   * @param stackName     stack name
    * @param stackVersion  stack version
    * @param stackVersion  stack version
    * @param osType        OS type
    * @param osType        OS type
    * @param repoId        repo id
    * @param repoId        repo id
    *
    *
    * @return  an update url or null if the url has not been updated
    * @return  an update url or null if the url has not been updated
    */
    */
-  public String getUpdatedRepoUrl(String repoName, String stackVersion, String osType, String repoId) {
-    StringBuilder sb = new StringBuilder("repo:/");
-    sb.append(repoName).append('/');
-    sb.append(stackVersion).append('/');
-    sb.append(osType).append('/');
-    sb.append(repoId);
-    sb.append(':').append(REPOSITORY_XML_PROPERTY_BASEURL);
-
-    MetainfoEntity entity = metaInfoDAO.findByKey(sb.toString());
+  public String getUpdatedRepoUrl(String stackName, String stackVersion, String osType, String repoId) {
+    String key = AmbariMetaInfo.generateRepoMetaKey(stackName, stackVersion,
+            osType, repoId, REPOSITORY_XML_PROPERTY_BASEURL);
+    MetainfoEntity entity = metaInfoDAO.findByKey(key);
     return entity != null ? entity.getMetainfoValue() : null;
     return entity != null ? entity.getMetainfoValue() : null;
   }
   }
 
 

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/stack/StackModule.java

@@ -482,7 +482,7 @@ public class StackModule extends BaseModule<StackModule, StackInfo> {
     ri.setLatestBaseUrl(r.getBaseUrl());
     ri.setLatestBaseUrl(r.getBaseUrl());
 
 
     LOG.debug("Checking for override for base_url");
     LOG.debug("Checking for override for base_url");
-    String updatedUrl = stackContext.getUpdatedRepoUrl(r.getRepoName(), stackInfo.getVersion(),
+    String updatedUrl = stackContext.getUpdatedRepoUrl(stackInfo.getName(), stackInfo.getVersion(),
         osFamily, r.getRepoId());
         osFamily, r.getRepoId());
 
 
     if (null != updatedUrl) {
     if (null != updatedUrl) {

+ 50 - 4
ambari-server/src/test/java/org/apache/ambari/server/api/services/AmbariMetaInfoTest.java

@@ -102,6 +102,9 @@ public class AmbariMetaInfoTest {
   private static String SERVICE_NAME_MAPRED2 = "MAPREDUCE2";
   private static String SERVICE_NAME_MAPRED2 = "MAPREDUCE2";
   private static String SERVICE_COMPONENT_NAME = "NAMENODE";
   private static String SERVICE_COMPONENT_NAME = "NAMENODE";
   private static final String OS_TYPE = "centos5";
   private static final String OS_TYPE = "centos5";
+  private static final String HDP_REPO_NAME = "HDP";
+  private static final String HDP_REPO_ID = "HDP-2.1.1";
+  private static final String HDP_UTILS_REPO_NAME = "HDP-UTILS";
   private static final String REPO_ID = "HDP-UTILS-1.1.0.15";
   private static final String REPO_ID = "HDP-UTILS-1.1.0.15";
   private static final String PROPERTY_NAME = "hbase.regionserver.msginterval";
   private static final String PROPERTY_NAME = "hbase.regionserver.msginterval";
   private static final String SHARED_PROPERTY_NAME = "content";
   private static final String SHARED_PROPERTY_NAME = "content";
@@ -236,9 +239,9 @@ public class AmbariMetaInfoTest {
     // Updating the baseUrl
     // Updating the baseUrl
     String newBaseUrl = "http://myprivate-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.0.6.0";
     String newBaseUrl = "http://myprivate-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.0.6.0";
     ambariMetaInfo.updateRepoBaseURL(STACK_NAME_HDP, "2.1.1", "redhat6",
     ambariMetaInfo.updateRepoBaseURL(STACK_NAME_HDP, "2.1.1", "redhat6",
-        STACK_NAME_HDP + "-2.1.1", newBaseUrl);
+            HDP_REPO_ID, newBaseUrl);
     RepositoryInfo repoInfo = ambariMetaInfo.getRepository(STACK_NAME_HDP, "2.1.1", "redhat6",
     RepositoryInfo repoInfo = ambariMetaInfo.getRepository(STACK_NAME_HDP, "2.1.1", "redhat6",
-        STACK_NAME_HDP + "-2.1.1");
+            HDP_REPO_ID);
     assertEquals(newBaseUrl, repoInfo.getBaseUrl());
     assertEquals(newBaseUrl, repoInfo.getBaseUrl());
     String prevBaseUrl = repoInfo.getDefaultBaseUrl();
     String prevBaseUrl = repoInfo.getDefaultBaseUrl();
 
 
@@ -258,7 +261,7 @@ public class AmbariMetaInfoTest {
         STACK_NAME_HDP, "2.1.1", "redhat6");
         STACK_NAME_HDP, "2.1.1", "redhat6");
     assertNotNull(redhat6Repo);
     assertNotNull(redhat6Repo);
     for (RepositoryInfo ri : redhat6Repo) {
     for (RepositoryInfo ri : redhat6Repo) {
-      if (STACK_NAME_HDP.equals(ri.getRepoName())) {
+      if (HDP_REPO_NAME.equals(ri.getRepoName())) {
         assertEquals(newBaseUrl, ri.getBaseUrl());
         assertEquals(newBaseUrl, ri.getBaseUrl());
         // defaultBaseUrl and baseUrl should not be same, since it is updated.
         // defaultBaseUrl and baseUrl should not be same, since it is updated.
         assertFalse(ri.getBaseUrl().equals(ri.getDefaultBaseUrl()));
         assertFalse(ri.getBaseUrl().equals(ri.getDefaultBaseUrl()));
@@ -267,7 +270,50 @@ public class AmbariMetaInfoTest {
 
 
     // Reset the database with the original baseUrl
     // Reset the database with the original baseUrl
     ambariMetaInfo.updateRepoBaseURL(STACK_NAME_HDP, "2.1.1", "redhat6",
     ambariMetaInfo.updateRepoBaseURL(STACK_NAME_HDP, "2.1.1", "redhat6",
-        STACK_NAME_HDP + "-2.1.1", prevBaseUrl);
+            HDP_REPO_ID, prevBaseUrl);
+  }
+
+  @Test
+  public void testGetRepositoryUpdatedUtilsBaseUrl() throws Exception {
+    // Scenario: user has internet and but calls to set repos via api
+    // use whatever they set
+    String stackVersion = "0.2";
+    String buildDir = tmpFolder.getRoot().getAbsolutePath();
+    TestAmbariMetaInfo ambariMetaInfo = setupTempAmbariMetaInfo(buildDir, true);
+
+    // Updating the baseUrl
+    String newBaseUrl = "http://myprivate-repo-1.hortonworks.com/HDP-Utils/centos6/2.x/updates/2.0.6.0";
+    ambariMetaInfo.updateRepoBaseURL(STACK_NAME_HDP, stackVersion, "redhat6",
+            REPO_ID, newBaseUrl);
+    RepositoryInfo repoInfo = ambariMetaInfo.getRepository(STACK_NAME_HDP, stackVersion, "redhat6",
+            REPO_ID);
+    assertEquals(newBaseUrl, repoInfo.getBaseUrl());
+    String prevBaseUrl = repoInfo.getDefaultBaseUrl();
+
+    // mock expectations
+    MetainfoDAO metainfoDAO = ambariMetaInfo.metaInfoDAO;
+    reset(metainfoDAO);
+    MetainfoEntity entity = createNiceMock(MetainfoEntity.class);
+    expect(metainfoDAO.findByKey("repo:/HDP/0.2/redhat6/HDP-UTILS-1.1.0.15:baseurl")).andReturn(entity).atLeastOnce();
+    expect(entity.getMetainfoValue()).andReturn(newBaseUrl).atLeastOnce();
+    replay(metainfoDAO, entity);
+
+    ambariMetaInfo.init();
+
+    List<RepositoryInfo> redhat6Repo = ambariMetaInfo.getRepositories(
+            STACK_NAME_HDP, stackVersion, "redhat6");
+    assertNotNull(redhat6Repo);
+    for (RepositoryInfo ri : redhat6Repo) {
+      if (HDP_UTILS_REPO_NAME.equals(ri.getRepoName())) {
+        assertEquals(newBaseUrl, ri.getBaseUrl());
+        // defaultBaseUrl and baseUrl should not be same, since it is updated.
+        assertFalse(ri.getBaseUrl().equals(ri.getDefaultBaseUrl()));
+      }
+    }
+
+    // Reset the database with the original baseUrl
+    ambariMetaInfo.updateRepoBaseURL(STACK_NAME_HDP, stackVersion, "redhat6",
+            REPO_ID, prevBaseUrl);
   }
   }
 
 
   @Test
   @Test