StackUpgradeUtil { public void updateLocalRepo(String stackName, String stackVersion, String repoUrl, String repoUrlOs, String mirrorList) throws Exception { if (null == repoUrl || repoUrl.isEmpty() || !repoUrl.startsWith("http")) { return; } String[] oses = new String[0]; if (null != repoUrlOs) { oses = repoUrlOs.split(","); } AmbariMetaInfo ami = injector.getInstance(AmbariMetaInfo.class); MetainfoDAO metaDao = injector.getInstance(MetainfoDAO.class); OsFamily os_family = injector.getInstance(OsFamily.class); String stackRepoId = stackName + "-" + stackVersion; if (0 == oses.length) { for (OperatingSystemInfo osi : ami.getOperatingSystems(stackName, stackVersion)) { ami.updateRepo(stackName, stackVersion, osi.getOsType(), stackRepoId, repoUrl, mirrorList); } } else { for (String os : oses) { String family = os_family.find(os); if (null != family) { String key = ami.generateRepoMetaKey(stackName, stackVersion, os, stackRepoId, AmbariMetaInfo.REPOSITORY_XML_PROPERTY_BASEURL); String familyKey = ami.generateRepoMetaKey(stackName, stackVersion, family, stackRepoId, AmbariMetaInfo.REPOSITORY_XML_PROPERTY_BASEURL); MetainfoEntity entity = metaDao.findByKey(key); if (null == entity) { entity = new MetainfoEntity(); entity.setMetainfoName(key); entity.setMetainfoValue(repoUrl); metaDao.merge(entity); } else { entity.setMetainfoValue(repoUrl); metaDao.merge(entity); } entity = metaDao.findByKey(familyKey); if (null == entity) { entity = new MetainfoEntity(); entity.setMetainfoName(familyKey); entity.setMetainfoValue(repoUrl); metaDao.merge(entity); } else { entity.setMetainfoValue(repoUrl); metaDao.merge(entity); } } } } }  @Transactional void updateStackDetails(String stackName, String stackVersion); void updateLocalRepo(String stackName, String stackVersion,
                              String repoUrl, String repoUrlOs, String mirrorList);  }
@Test public void testUpgradeStack() throws Exception { StackUpgradeUtil stackUpgradeUtil = injector.getInstance(StackUpgradeUtil.class); String stackName = "HDP"; String stackVersion = "1.3.0"; String localRepoUrl = "http: stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, localRepoUrl, null, null); MetainfoDAO dao = injector.getInstance(MetainfoDAO.class); Collection<MetainfoEntity> entities = dao.findAll(); Assert.assertTrue(entities.size() > 0); for (MetainfoEntity entity : entities) { Assert.assertTrue(entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/")); Assert.assertEquals(localRepoUrl, entity.getMetainfoValue()); } reset (stackName, stackVersion); entities = dao.findAll(); Assert.assertEquals(0, entities.size()); stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, localRepoUrl, "centos6", null); entities = dao.findAll(); for (MetainfoEntity entity : entities) { Assert.assertTrue(entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/centos6") || entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/redhat6")); Assert.assertEquals(localRepoUrl, entity.getMetainfoValue()); } reset (stackName, stackVersion); entities = dao.findAll(); Assert.assertTrue(0 == entities.size()); stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, localRepoUrl, "centos6,centos5", null); entities = dao.findAll(); for (MetainfoEntity entity : entities) { Assert.assertTrue(entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/centos6") || entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/redhat6") || entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/redhat5") || entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/centos5")); Assert.assertEquals(localRepoUrl, entity.getMetainfoValue()); } localRepoUrl = "http: stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, localRepoUrl, "centos6", null); entities = dao.findAll(); boolean foundCentos6 = false; boolean foundRedhat6 = false; for (MetainfoEntity entity : entities) { if (-1 != entity.getMetainfoName().indexOf("centos6")) { foundCentos6 = true; Assert.assertEquals(localRepoUrl, entity.getMetainfoValue()); } else if (-1 != entity.getMetainfoName().indexOf("redhat6")) { foundRedhat6 = true; Assert.assertEquals(localRepoUrl, entity.getMetainfoValue()); } else { Assert.assertFalse(localRepoUrl.equals(entity.getMetainfoValue())); } } Assert.assertTrue(foundCentos6); Assert.assertTrue(foundRedhat6); }