DatabaseChecker { public static void checkDBVersion() throws AmbariException { LOG.info("Checking DB store version"); if (metainfoDAO == null) { metainfoDAO = injector.getInstance(MetainfoDAO.class); } MetainfoEntity schemaVersionEntity = metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY); String schemaVersion = null; if (schemaVersionEntity != null) { schemaVersion = schemaVersionEntity.getMetainfoValue(); } Configuration conf = injector.getInstance(Configuration.class); File versionFile = new File(conf.getServerVersionFilePath()); if (!versionFile.exists()) { throw new AmbariException("Server version file does not exist."); } String serverVersion = null; try (Scanner scanner = new Scanner(versionFile)) { serverVersion = scanner.useDelimiter("\\Z").next(); } catch (IOException ioe) { throw new AmbariException("Unable to read server version file."); } if (schemaVersionEntity==null || VersionUtils.compareVersions(schemaVersion, serverVersion, 3) != 0) { String error = "Current database store version is not compatible with " + "current server version" + ", serverVersion=" + serverVersion + ", schemaVersion=" + schemaVersion; LOG.warn(error); throw new AmbariException(error); } LOG.info("DB store version is compatible"); }  static void checkDBConsistency(); static void checkDBConfigsConsistency(); static void checkDBVersion();  }
@Ignore @Test public void testCheckDBVersion_Valid() throws Exception { MetainfoDAO metainfoDAO = createMock(MetainfoDAO.class); MetainfoEntity metainfoEntity = new MetainfoEntity(); String serverVersion = ambariMetaInfo.getServerVersion(); metainfoEntity.setMetainfoName(Configuration.SERVER_VERSION_KEY); metainfoEntity.setMetainfoValue(serverVersion); expect(metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY)). andReturn(metainfoEntity); replay(metainfoDAO); DatabaseChecker.metainfoDAO = metainfoDAO; DatabaseChecker.ambariMetaInfo = ambariMetaInfo; try { DatabaseChecker.checkDBVersion(); } catch (AmbariException ae) { fail("DB versions check failed."); } }
@Ignore @Test(expected = AmbariException.class) public void testCheckDBVersionInvalid() throws Exception { MetainfoDAO metainfoDAO = createMock(MetainfoDAO.class); MetainfoEntity metainfoEntity = new MetainfoEntity(); metainfoEntity.setMetainfoName(Configuration.SERVER_VERSION_KEY); metainfoEntity.setMetainfoValue("0.0.0"); expect(metainfoDAO.findByKey(Configuration.SERVER_VERSION_KEY)). andReturn(metainfoEntity); replay(metainfoDAO); DatabaseChecker.metainfoDAO = metainfoDAO; DatabaseChecker.ambariMetaInfo = ambariMetaInfo; DatabaseChecker.checkDBVersion(); }