Beispiel #1
0
    def testGenerateWithDbError(self):
        """Tests Generate() where put() raises db.Error."""
        name = 'goodname'
        catalog = self.mox.CreateMockAnything()
        plist1 = '<plist><dict><key>foo</key><string>bar</string></dict></plist>'
        mock_plist1 = self.mox.CreateMockAnything()
        pkg1 = test.GenericContainer(plist=mock_plist1, name='foo')
        plist2 = '<plist><dict><key>foo</key><string>bar</string></dict></plist>'
        mock_plist2 = self.mox.CreateMockAnything()
        pkg2 = test.GenericContainer(plist=mock_plist2, name='bar')

        self._MockObtainLock('catalog_lock_%s' % name)

        mock_model = self.mox.CreateMockAnything()
        self.mox.StubOutWithMock(models.PackageInfo, 'all')
        models.PackageInfo.all().AndReturn(mock_model)
        mock_model.filter('catalogs =', name).AndReturn(mock_model)
        mock_model.fetch(None).AndReturn([pkg1, pkg2])
        mock_plist1.GetXmlContent(indent_num=1).AndReturn(plist1)
        mock_plist2.GetXmlContent(indent_num=1).AndReturn(plist2)

        mock_catalog = self.mox.CreateMockAnything()
        self.mox.StubOutWithMock(models.Catalog, 'get_or_insert')
        models.Catalog.get_or_insert(name).AndReturn(mock_catalog)
        mock_catalog.put().AndRaise(models.db.Error)
        self._MockReleaseLock('catalog_lock_%s' % name)

        self.mox.ReplayAll()
        self.assertRaises(models.db.Error, models.Catalog.Generate, name)
        self.mox.VerifyAll()
Beispiel #2
0
    def testGenerateSuccess(self):
        """Tests the success path for Generate()."""
        name = 'goodname'
        plist1 = '<dict><key>foo</key><string>bar</string></dict>'
        mock_plist1 = self.mox.CreateMockAnything()
        pkg1 = test.GenericContainer(plist=mock_plist1,
                                     name='foo',
                                     mtime=datetime.datetime.utcnow())
        plist2 = '<dict><key>foo</key><string>bar</string></dict>'
        mock_plist2 = self.mox.CreateMockAnything()
        pkg2 = test.GenericContainer(plist=mock_plist2,
                                     name='bar',
                                     mtime=datetime.datetime.utcnow())

        self.mox.StubOutWithMock(models.Manifest, 'Generate')
        self.mox.StubOutWithMock(models.PackageInfo, 'all')
        self.mox.StubOutWithMock(models.Catalog, 'get_or_insert')
        self.mox.StubOutWithMock(models.Catalog, 'DeleteMemcacheWrap')

        mock_model = self.mox.CreateMockAnything()
        models.PackageInfo.all().AndReturn(mock_model)
        mock_model.filter('catalogs =', name).AndReturn(mock_model)
        mock_model.fetch(None).AndReturn([pkg1, pkg2])
        pkg1.plist.GetXmlContent(indent_num=1).AndReturn(plist1)
        pkg2.plist.GetXmlContent(indent_num=1).AndReturn(plist2)

        mock_catalog = self.mox.CreateMockAnything()
        models.Catalog.get_or_insert(name).AndReturn(mock_catalog)
        mock_catalog.put(avoid_mtime_update=True).AndReturn(None)

        models.Catalog.DeleteMemcacheWrap(name).AndReturn(None)
        models.Manifest.Generate(name, delay=1).AndReturn(None)

        m = mock.Mock()
        with mock.patch.object(datastore_locks,
                               'DatastoreLock',
                               return_value=m) as lock_mock:
            self.mox.ReplayAll()
            models.Catalog.Generate(name)
            self.mox.VerifyAll()

            lock_mock.assert_called_once_with('catalog_lock_goodname')

        m.assert_has_calls([
            mock.call.Acquire(timeout=600, max_acquire_attempts=2),
            mock.call.Release()
        ])

        self.assertEqual(mock_catalog.name, name)
        xml = '\n'.join([plist1, plist2])
        expected_plist = models.constants.CATALOG_PLIST_XML % xml
        self.assertEqual(expected_plist, mock_catalog.plist)
        self.assertEqual(mock_catalog.package_names, ['foo', 'bar'])
Beispiel #3
0
  def testGetComputerManifestWhenEmptyDynamic(self):
    """Test ComputerInstallsPending()."""
    uuid = 'uuid'
    last_notified_datetime = self.mox.CreateMockAnything()

    client_id = {
        'uuid': 'uuid',
        'owner': 'owner',
        'hostname': 'hostname',
        'serial': 'serial',
        'config_track': 'config_track',
        'track': 'track',
        'site': 'site',
        'office': 'office',
        'os_version': 'os_version',
        'client_version': 'client_version',
        'on_corp': True,
        'last_notified_datetime': last_notified_datetime,
        'uptime': None,
        'root_disk_free': None,
        'user_disk_free': None,
    }

    computer = test.GenericContainer(**client_id)
    computer.connections_on_corp = 2
    computer.connections_off_corp = 1
    computer.user_settings = None

    packagemap = {}

    self.mox.StubOutWithMock(common.models, 'Computer')
    self.mox.StubOutWithMock(common, 'IsPanicModeNoPackages')
    self.mox.StubOutWithMock(common.models, 'Manifest')
    self.mox.StubOutWithMock(common, 'GenerateDynamicManifest')
    self.mox.StubOutWithMock(common.plist_module, 'MunkiManifestPlist')
    self.mox.StubOutWithMock(common.models, 'PackageInfo')
    self.mox.StubOutWithMock(common.plist_module, 'MunkiPackageInfoPlist')

    # mock manifest creation
    common.models.Computer.get_by_key_name(uuid).AndReturn(computer)
    common.IsPanicModeNoPackages().AndReturn(False)
    mock_plist = self.mox.CreateMockAnything()
    common.models.Manifest.MemcacheWrappedGet('track').AndReturn(
        test.GenericContainer(enabled=True, plist=mock_plist))
    common.GenerateDynamicManifest(
        mock_plist, client_id, user_settings=None).AndReturn(None)

    self.mox.ReplayAll()
    self.assertRaises(
        common.ManifestNotFoundError,
        common.GetComputerManifest, uuid=uuid)
    self.mox.VerifyAll()
Beispiel #4
0
  def testGetComputerManifestIsPanicMode(self):
    """Test ComputerInstallsPending()."""
    uuid = 'uuid'
    last_notified_datetime = self.mox.CreateMockAnything()

    client_id = {
        'uuid': 'uuid',
        'owner': 'owner',
        'hostname': 'hostname',
        'serial': 'serial',
        'config_track': 'config_track',
        'track': 'track',
        'site': 'site',
        'office': 'office',
        'os_version': 'os_version',
        'client_version': 'client_version',
        'on_corp': True,
        'last_notified_datetime': last_notified_datetime,
        'uptime': None,
        'root_disk_free': None,
        'user_disk_free': None,
    }

    computer = test.GenericContainer(**client_id)
    computer.connections_on_corp = 2
    computer.connections_off_corp = 1
    computer.user_settings = None

    # PackageInfo entities
    package_infos = [
        test.GenericContainer(plist='plistOtherPackage', version='1.0'),
        test.GenericContainer(plist='plistPackageInstalled1', version='1.0'),
        test.GenericContainer(plist='plistPackageInstalled2', version='1.0'),
        test.GenericContainer(plist='plistPackageNotInstalled1', version='1.0'),
    ]

    packagemap = {}

    self.mox.StubOutWithMock(common.models, 'Computer')
    self.mox.StubOutWithMock(common, 'IsPanicModeNoPackages')

    common.models.Computer.get_by_key_name(uuid).AndReturn(computer)
    common.IsPanicModeNoPackages().AndReturn(True)

    manifest_expected = '%s%s' % (
        common.plist_module.PLIST_HEAD,
        common.plist_module.PLIST_FOOT)

    self.mox.ReplayAll()
    manifest = common.GetComputerManifest(uuid=uuid)
    self.assertEqual(manifest, manifest_expected)
    self.mox.VerifyAll()
Beispiel #5
0
  def testGetComputerManifestWhenManifestNotFound(self):
    """Test ComputerInstallsPending()."""
    uuid = 'uuid'
    last_notified_datetime = self.mox.CreateMockAnything()

    client_id = {
        'uuid': 'uuid',
        'owner': 'owner',
        'hostname': 'hostname',
        'serial': 'serial',
        'config_track': 'config_track',
        'track': 'track',
        'site': 'site',
        'office': 'office',
        'os_version': 'os_version',
        'client_version': 'client_version',
        'on_corp': True,
        'last_notified_datetime': last_notified_datetime,
        'uptime': None,
        'root_disk_free': None,
        'user_disk_free': None,
    }

    computer = test.GenericContainer(**client_id)
    computer.connections_on_corp = 2
    computer.connections_off_corp = 1
    computer.user_settings = None

    # PackageInfo entities
    package_infos = [
        test.GenericContainer(plist='plistOtherPackage', version='1.0'),
        test.GenericContainer(plist='plistPackageInstalled1', version='1.0'),
        test.GenericContainer(plist='plistPackageInstalled2', version='1.0'),
        test.GenericContainer(plist='plistPackageNotInstalled1', version='1.0'),
    ]

    packagemap = {}

    self.mox.StubOutWithMock(common.models, 'Computer')
    self.mox.StubOutWithMock(common, 'IsPanicModeNoPackages')
    self.mox.StubOutWithMock(common.models, 'Manifest')

    # mock manifest creation
    common.models.Computer.get_by_key_name(uuid).AndReturn(computer)
    common.IsPanicModeNoPackages().AndReturn(False)
    common.models.Manifest.MemcacheWrappedGet('track').AndReturn(None)

    self.mox.ReplayAll()
    self.assertRaises(
        common.ManifestNotFoundError,
        common.GetComputerManifest, uuid=uuid)
    self.mox.VerifyAll()
Beispiel #6
0
    def testGenerateSuccess(self):
        """Tests the success path for Manifest.Generate()."""
        name = 'goodname'
        pkg1 = test.GenericContainer(install_types=['footype1'], name='pkg1')
        pkg2 = test.GenericContainer(install_types=['footype1', 'footype2'],
                                     name='pkg2')
        manifest_dict = {
            'catalogs': [name, 'apple_update_metadata'],
            pkg1.install_types[0]: [pkg1.name, pkg2.name],
            pkg2.install_types[1]: [pkg2.name],
        }
        self.stubs.Set(
            models.plist_lib, 'MunkiManifestPlist',
            self.mox.CreateMock(models.plist_lib.MunkiManifestPlist))

        mock_model = self.mox.CreateMockAnything()
        self.mox.StubOutWithMock(models.PackageInfo, 'all')
        models.PackageInfo.all().AndReturn(mock_model)
        mock_model.filter('manifests =', name).AndReturn(mock_model)
        mock_model.fetch(None).AndReturn([pkg1, pkg2])

        mock_manifest = self.mox.CreateMockAnything()
        mock_manifest.plist = self.mox.CreateMockAnything()

        self.mox.StubOutWithMock(models.Manifest, 'get_or_insert')
        models.Manifest.get_or_insert(name).AndReturn(mock_manifest)
        mock_manifest.plist.SetContents(manifest_dict)
        mock_manifest.put().AndReturn(None)
        self.mox.StubOutWithMock(models.Manifest, 'DeleteMemcacheWrap')
        models.Manifest.DeleteMemcacheWrap(name).AndReturn(None)

        m = mock.Mock()
        with mock.patch.object(datastore_locks,
                               'DatastoreLock',
                               return_value=m) as lock_mock:
            self.mox.ReplayAll()
            models.Manifest.Generate(name)
            self.mox.VerifyAll()

            lock_mock.assert_called_once_with('manifest_lock_goodname')

        m.assert_has_calls([
            mock.call.Acquire(timeout=30, max_acquire_attempts=1),
            mock.call.Release()
        ])
Beispiel #7
0
    def testGenerateSuccess(self):
        """Tests the success path for Generate()."""
        name = 'goodname'
        plist1 = '<dict><key>foo</key><string>bar</string></dict>'
        mock_plist1 = self.mox.CreateMockAnything()
        pkg1 = test.GenericContainer(plist=mock_plist1, name='foo')
        plist2 = '<dict><key>foo</key><string>bar</string></dict>'
        mock_plist2 = self.mox.CreateMockAnything()
        pkg2 = test.GenericContainer(plist=mock_plist2, name='bar')

        self.mox.StubOutWithMock(models.Manifest, 'Generate')
        self.mox.StubOutWithMock(models.PackageInfo, 'all')
        self.mox.StubOutWithMock(models.Catalog, 'get_or_insert')
        self.mox.StubOutWithMock(models.Catalog, 'DeleteMemcacheWrap')

        self._MockObtainLock('catalog_lock_%s' % name)

        mock_model = self.mox.CreateMockAnything()
        models.PackageInfo.all().AndReturn(mock_model)
        mock_model.filter('catalogs =', name).AndReturn(mock_model)
        mock_model.fetch(None).AndReturn([pkg1, pkg2])
        pkg1.plist.GetXmlContent(indent_num=1).AndReturn(plist1)
        pkg2.plist.GetXmlContent(indent_num=1).AndReturn(plist2)

        mock_catalog = self.mox.CreateMockAnything()
        models.Catalog.get_or_insert(name).AndReturn(mock_catalog)
        mock_catalog.put().AndReturn(None)

        models.Catalog.DeleteMemcacheWrap(
            name, prop_name='plist_xml').AndReturn(None)
        models.Manifest.Generate(name, delay=1).AndReturn(None)
        self._MockReleaseLock('catalog_lock_%s' % name)

        self.mox.ReplayAll()
        models.Catalog.Generate(name)
        self.assertEqual(mock_catalog.name, name)
        xml = '\n'.join([plist1, plist2])
        expected_plist = models.constants.CATALOG_PLIST_XML % xml
        self.assertEqual(expected_plist, mock_catalog.plist)
        self.assertEqual(mock_catalog.package_names, ['foo', 'bar'])
        self.mox.VerifyAll()
Beispiel #8
0
  def testGenerateSuccess(self):
    """Tests the success path for Manifest.Generate()."""
    name = 'goodname'
    pkg1 = test.GenericContainer(install_types=['footype1'], name='pkg1')
    pkg2 = test.GenericContainer(
        install_types=['footype1', 'footype2'], name='pkg2')
    manifest_dict = {
        'catalogs': [name, 'apple_update_metadata'],
        pkg1.install_types[0]: [pkg1.name, pkg2.name],
        pkg2.install_types[1]: [pkg2.name],
    }
    self._MockObtainLock('manifest_lock_%s' % name)
    self.stubs.Set(
        models.plist_lib,
        'MunkiManifestPlist',
        self.mox.CreateMock(models.plist_lib.MunkiManifestPlist))

    mock_model = self.mox.CreateMockAnything()
    self.mox.StubOutWithMock(models.PackageInfo, 'all')
    models.PackageInfo.all().AndReturn(mock_model)
    mock_model.filter('manifests =', name).AndReturn(mock_model)
    mock_model.fetch(None).AndReturn([pkg1, pkg2])

    mock_manifest = self.mox.CreateMockAnything()
    mock_manifest.plist = self.mox.CreateMockAnything()

    self.mox.StubOutWithMock(models.Manifest, 'get_or_insert')
    models.Manifest.get_or_insert(name).AndReturn(mock_manifest)
    mock_manifest.plist.SetContents(manifest_dict)
    mock_manifest.put().AndReturn(None)
    self.mox.StubOutWithMock(models.Manifest, 'DeleteMemcacheWrap')
    models.Manifest.DeleteMemcacheWrap(name).AndReturn(None)

    self._MockReleaseLock('manifest_lock_%s' % name)

    self.mox.ReplayAll()
    models.Manifest.Generate(name)
    self.mox.VerifyAll()
Beispiel #9
0
    def testGenerateWithPlistParseError(self):
        """Tests Generate() where plist.GetXmlDocument() raises plist.Error."""
        name = 'goodname'
        mock_plist1 = self.mox.CreateMockAnything()
        pkg1 = test.GenericContainer(plist=mock_plist1, name='foo')
        mock_model = self.mox.CreateMockAnything()
        self.mox.StubOutWithMock(models.PackageInfo, 'all')
        models.PackageInfo.all().AndReturn(mock_model)
        mock_model.filter('catalogs =', name).AndReturn(mock_model)
        mock_model.fetch(None).AndReturn([pkg1])
        mock_plist1.GetXmlContent(indent_num=1).AndRaise(
            models.plist_lib.Error)

        self.mox.ReplayAll()
        self.assertRaises(models.plist_lib.Error, models.Catalog.Generate,
                          name)
        self.mox.VerifyAll()
Beispiel #10
0
  def testGetComputerManifest(self):
    """Test ComputerInstallsPending()."""
    uuid = 'uuid'
    last_notified_datetime = self.mox.CreateMockAnything()

    client_id = {
        'uuid': 'uuid',
        'owner': 'owner',
        'hostname': 'hostname',
        'serial': 'serial',
        'config_track': 'config_track',
        'track': 'track',
        'site': 'site',
        'office': 'office',
        'os_version': 'os_version',
        'client_version': 'client_version',
        'on_corp': True,
        'last_notified_datetime': last_notified_datetime,
        'uptime': None,
        'root_disk_free': None,
        'user_disk_free': None,
    }

    computer = test.GenericContainer(**client_id)
    computer.connections_on_corp = 2
    computer.connections_off_corp = 1
    computer.user_settings = None

    # PackageInfo entities
    mock_pl1 = self.mox.CreateMockAnything()
    mock_pl2 = self.mox.CreateMockAnything()
    mock_pl3 = self.mox.CreateMockAnything()
    mock_pl4 = self.mox.CreateMockAnything()
    package_infos = [
        test.GenericContainer(plist=mock_pl1, version='1.0', name='fooname1'),
        test.GenericContainer(plist=mock_pl2, version='1.0', name='fooname2'),
        test.GenericContainer(plist=mock_pl3, version='1.0', name='fooname3'),
        test.GenericContainer(plist=mock_pl4, version='1.0', name='fooname4'),
    ]

    packagemap = {}

    self.mox.StubOutWithMock(common.models, 'Computer')
    self.mox.StubOutWithMock(common, 'IsPanicModeNoPackages')
    self.mox.StubOutWithMock(common.models, 'Manifest')
    self.mox.StubOutWithMock(common, 'GenerateDynamicManifest')
    self.mox.StubOutWithMock(common.plist_module, 'MunkiManifestPlist')
    self.mox.StubOutWithMock(common.models, 'PackageInfo')
    self.mox.StubOutWithMock(common.plist_module, 'MunkiPackageInfoPlist')

    # mock manifest creation
    common.models.Computer.get_by_key_name(uuid).AndReturn(computer)
    common.IsPanicModeNoPackages().AndReturn(False)
    mock_plist = self.mox.CreateMockAnything()
    common.models.Manifest.MemcacheWrappedGet('track').AndReturn(
        test.GenericContainer(enabled=True, plist=mock_plist))
    common.GenerateDynamicManifest(
        mock_plist, client_id, user_settings=None).AndReturn(
        'manifest_plist')

    # mock manifest parsing
    mock_manifest_plist = self.mox.CreateMockAnything()
    common.plist_module.MunkiManifestPlist('manifest_plist').AndReturn(
        mock_manifest_plist)
    mock_manifest_plist.Parse().AndReturn(None)

    # mock manifest reading and package map creation
    mock_package_info = self.mox.CreateMockAnything()
    common.models.PackageInfo.all().AndReturn(mock_package_info)
    iter_return = []

    for package_info in package_infos:
      iter_return.append(test.GenericContainer(
          plist=package_info.plist,
          name=package_info.name))
      package_info.plist.get('display_name', None).AndReturn(None)
      package_info.plist.get('name').AndReturn(package_info.name)
      package_info.plist.get('version', '').AndReturn(package_info.version)
      packagemap[package_info.name] = '%s-%s' % (
          package_info.name, package_info.version)

    def __iter_func():
      for i in iter_return:
        yield i

    mock_package_info.__iter__().AndReturn(__iter_func())

    manifest_expected = {
        'plist': mock_manifest_plist,
        'packagemap': packagemap,
    }

    self.mox.ReplayAll()
    manifest = common.GetComputerManifest(uuid=uuid, packagemap=True)
    self.assertEqual(manifest, manifest_expected)
    self.mox.VerifyAll()