class TestRunningServices(unittest.TestCase):
    def setUp(self):
        self.processes = common.GetProcessesDetails()

    def AssertServiceIsRunning(self, service):
        exists = False
        pid = 'Undefined'
        for pid in self.processes:
            if self.processes[pid] and service in self.processes[pid][
                    'cmdline']:
                exists = True
                break
        error_msg = 'Service: %s is NOT running' % service
        self.assertTrue(exists, error_msg)
        print 'Service %s is running with pid %s.' % (service, pid)

    @unittest.skipUnless(common.IsFusionInstalled(), 'Fusion is not installed')
    def testRunningServiceGesystemmanager(self):
        self.AssertServiceIsRunning('gesystemmanager')

    @unittest.skipUnless(common.IsFusionInstalled(), 'Fusion is not installed')
    def testRunningServiceGeresourceprovider(self):
        self.AssertServiceIsRunning('geresourceprovider')

    @unittest.skipUnless(common.IsGeeServerInstalled(),
                         'GEE Server is not installed')
    def testRunningServiceGehttpd(self):
        self.AssertServiceIsRunning('gehttpd')

    @unittest.skipUnless(common.IsGeeServerInstalled(),
                         'GEE Server is not installed')
    def testRunningServicepostgres(self):
        self.AssertServiceIsRunning('/opt/google/bin/postgres')
class TestVersion(unittest.TestCase):

  @unittest.skipUnless(common.IsFusionInstalled(), 'Fusion is not installed')
  def testFusionVersion(self):
    """Check if Fusion release is the latest available."""

    latest_version = common.GetLatestVersion()
    # Extract the main version information.
    fusion_version = common.GetFusionVersion()
    fusion_version = ".".join(fusion_version.split('.')[0:3])

    error_msg = ('Running Fusion version %s. Upgrade to version %s.' %
                 (fusion_version, latest_version))
    self.assertEqual(fusion_version, latest_version, msg=error_msg)

    print ('Currently running the latest version of Fusion (%s).' %
           fusion_version)

  @unittest.skipUnless(common.IsGeeServerInstalled(),
                       'GEE Server is not installed')
  def testGeeServerVersion(self):
    """Check if GEE Server release is the latest available."""

    latest_version = common.GetLatestVersion()
    # Extract the main version information.
    gee_server_version = common.GetGeeServerVersion()
    gee_server_version = ".".join(gee_server_version.split('.')[0:3])

    error_msg = ('Running GEE Server version %s. Upgrade to (%s).' %
                 (gee_server_version, latest_version))
    self.assertEqual(gee_server_version, latest_version, msg=error_msg)

    print ('Currently running the latest version of GEE Server (%s).' %
           gee_server_version)

  @unittest.skipUnless(common.IsFusionInstalled(), 'Fusion is not installed')
  @unittest.skipUnless(common.IsGeeServerInstalled(),
                       'GEE Server is not installed')
  def testFusionVersionsMatch(self):
    """Check Fusion and server versions are aligned."""
    fusion_version = common.GetFusionVersion()
    gee_server_version = common.GetGeeServerVersion()

    error_msg = ('Fusion and GEE Server versions DO NOT match. '
                 'Currently running Fusion v. %s and GEE Server v. %s.' %
                 (fusion_version, gee_server_version))
    self.assertEqual(fusion_version, gee_server_version, msg=error_msg)

    print 'Fusion and GEE Server versions match. Current version is %s.' % (
        fusion_version)