UpgradeCatalog220 extends AbstractUpgradeCatalog { protected void updateAlertDefinitions() { LOG.info("Updating alert definitions."); AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class); AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class); Clusters clusters = ambariManagementController.getClusters(); Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters); for (final Cluster cluster : clusterMap.values()) { long clusterID = cluster.getClusterId(); final AlertDefinitionEntity journalNodeProcessAlertDefinitionEntity = alertDefinitionDAO.findByName( clusterID, "journalnode_process"); final AlertDefinitionEntity hostDiskUsageAlertDefinitionEntity = alertDefinitionDAO.findByName( clusterID, "ambari_agent_disk_usage"); if (journalNodeProcessAlertDefinitionEntity != null) { String source = journalNodeProcessAlertDefinitionEntity.getSource(); journalNodeProcessAlertDefinitionEntity.setSource(modifyJournalnodeProcessAlertSource(source)); journalNodeProcessAlertDefinitionEntity.setSourceType(SourceType.WEB); journalNodeProcessAlertDefinitionEntity.setHash(UUID.randomUUID().toString()); alertDefinitionDAO.merge(journalNodeProcessAlertDefinitionEntity); LOG.info("journalnode_process alert definition was updated."); } if (hostDiskUsageAlertDefinitionEntity != null) { hostDiskUsageAlertDefinitionEntity.setDescription("This host-level alert is triggered if the amount of disk space " + "used goes above specific thresholds. The default threshold values are 50% for WARNING and 80% for CRITICAL."); hostDiskUsageAlertDefinitionEntity.setLabel("Host Disk Usage"); alertDefinitionDAO.merge(hostDiskUsageAlertDefinitionEntity); LOG.info("ambari_agent_disk_usage alert definition was updated."); } } } @Inject  UpgradeCatalog220(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion(); @Transactional void bootstrapRepoVersionForHDP21(); static final String UPGRADE_PACKAGE_COL; static final String UPGRADE_TYPE_COL; static final String REPO_VERSION_TABLE; }
@Test public void testUpdateAlertDefinitions() { EasyMockSupport easyMockSupport = new EasyMockSupport(); UpgradeCatalog220 upgradeCatalog220 = new UpgradeCatalog220(injector); long clusterId = 1; final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final AlertDefinitionEntity mockJournalNodeProcessAlertDefinitionEntity = easyMockSupport.createNiceMock(AlertDefinitionEntity.class); final AlertDefinitionEntity mockHostDiskUsageAlertDefinitionEntity = easyMockSupport.createNiceMock(AlertDefinitionEntity.class); final String journalNodeProcessAlertSource = "{\"uri\":\"{{hdfs-site/dfs.journalnode.http-address}}\",\"default_port\":8480," + "\"type\":\"PORT\",\"reporting\":{\"ok\":{\"text\":\"TCP OK - {0:.3f}s response on port {1}\"}," + "\"warning\":{\"text\":\"TCP OK - {0:.3f}s response on port {1}\",\"value\":1.5}," + "\"critical\":{\"text\":\"Connection failed: {0} to {1}:{2}\",\"value\":5.0}}}"; final String journalNodeProcessAlertSourceExpected = "{\"reporting\":{\"ok\":{\"text\":\"HTTP {0} response in {2:.3f}s\"}," + "\"warning\":{\"text\":\"HTTP {0} response from {1} in {2:.3f}s ({3})\"}," + "\"critical\":{\"text\":\"Connection failed to {1} ({3})\"}},\"type\":\"WEB\"," + "\"uri\":{\"http\":\"{{hdfs-site/dfs.journalnode.http-address}}\"," + "\"https\":\"{{hdfs-site/dfs.journalnode.https-address}}\"," + "\"kerberos_keytab\":\"{{hdfs-site/dfs.web.authentication.kerberos.keytab}}\","+ "\"kerberos_principal\":\"{{hdfs-site/dfs.web.authentication.kerberos.principal}}\"," + "\"https_property\":\"{{hdfs-site/dfs.http.policy}}\"," + "\"https_property_value\":\"HTTPS_ONLY\",\"connection_timeout\":5.0}}"; final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(Clusters.class).toInstance(mockClusters); bind(EntityManager.class).toInstance(entityManager); bind(AlertDefinitionDAO.class).toInstance(mockAlertDefinitionDAO); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); } }); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("normal", mockClusterExpected); }}).atLeastOnce(); expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes(); expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("journalnode_process"))).andReturn(mockJournalNodeProcessAlertDefinitionEntity).atLeastOnce(); expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("ambari_agent_disk_usage"))).andReturn(mockHostDiskUsageAlertDefinitionEntity).atLeastOnce(); expect(mockJournalNodeProcessAlertDefinitionEntity.getSource()).andReturn(journalNodeProcessAlertSource).atLeastOnce(); Assert.assertEquals(journalNodeProcessAlertSourceExpected, upgradeCatalog220.modifyJournalnodeProcessAlertSource(journalNodeProcessAlertSource)); mockHostDiskUsageAlertDefinitionEntity.setDescription(eq("This host-level alert is triggered if the amount of disk space " + "used goes above specific thresholds. The default threshold values are 50% for WARNING and 80% for CRITICAL.")); expectLastCall().atLeastOnce(); mockHostDiskUsageAlertDefinitionEntity.setLabel(eq("Host Disk Usage")); expectLastCall().atLeastOnce(); easyMockSupport.replayAll(); mockInjector.getInstance(UpgradeCatalog220.class).updateAlertDefinitions(); easyMockSupport.verifyAll(); }