def run(self):
        self.test_instance.assertEqual(
            roster_client_lib.RunFunction(
                u'ListRecords',
                self.user_name,
                credstring=self.credential,
                kwargs={'target': self.host_name},
                server_name=self.test_instance.server_name)['core_return'], [])

        roster_client_lib.RunFunction(
            u'MakeRecord',
            self.user_name,
            credstring=self.credential,
            args=[
                'a', self.host_name, 'new_zone', {
                    'assignment_ip': self.ip_address
                }
            ],
            server_name=self.test_instance.server_name)

        self.test_instance.assertEqual(
            roster_client_lib.RunFunction(
                u'ListRecords',
                self.user_name,
                credstring=self.credential,
                kwargs={'target': self.host_name},
                server_name=self.test_instance.server_name)['core_return'],
            [{
                'target': self.host_name,
                'ttl': 3600,
                'record_type': 'a',
                'view_name': 'any',
                'last_user': self.user_name,
                'zone_name': 'new_zone',
                'assignment_ip': self.ip_address
            }])

        roster_client_lib.RunFunction(
            u'RemoveRecord',
            self.user_name,
            credstring=self.credential,
            args=[
                'a', self.host_name, 'new_zone', {
                    'assignment_ip': self.ip_address
                }, 'any'
            ],
            server_name=self.test_instance.server_name)

        self.test_instance.assertEqual(
            roster_client_lib.RunFunction(
                u'ListRecords',
                self.user_name,
                credstring=self.credential,
                kwargs={'target': self.host_name},
                server_name=self.test_instance.server_name)['core_return'], [])
 def testCredsStrings(self):
     credstring = u'81ffc6ea-4b38-45e2-8fce-e24636672b27'
     self.core_instance._MakeCredential(credstring,
                                        u'shuey',
                                        infinite_cred=True)
     self.assertEqual(
         roster_client_lib.RunFunction(u'ListUsers',
                                       USERNAME,
                                       credstring=credstring,
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'], {
                                           u'shuey': 64,
                                           u'tree_export_user': 0,
                                           u'jcollins': 32,
                                           u'sharrell': 128
                                       })
     self.assertEqual(
         roster_client_lib.RunFunction(u'ListUsers',
                                       USERNAME,
                                       credstring=credstring,
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'], {
                                           u'shuey': 64,
                                           'tree_export_user': 0,
                                           u'jcollins': 32,
                                           u'sharrell': 128
                                       })
     time.sleep(10)
     function_return = roster_client_lib.RunFunction(
         u'ListUsers',
         USERNAME,
         credstring=credstring,
         server_name=self.server_name,
         password=PASSWORD)
     self.assertEqual(function_return['core_return'], {
         u'shuey': 64,
         'tree_export_user': 0,
         u'jcollins': 32,
         u'sharrell': 128
     })
     if (function_return['new_credential'] != u''):
         credstring = function_return['new_credential']
     self.assertEqual(
         roster_client_lib.RunFunction(u'ListUsers',
                                       USERNAME,
                                       credstring=credstring,
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'], {
                                           u'shuey': 64,
                                           'tree_export_user': 0,
                                           u'jcollins': 32,
                                           u'sharrell': 128
                                       })
 def testCredFile(self):
     oldstdout = sys.stdout
     sys.stdout = StdOutStream()
     self.assertRaises(SystemExit,
                       roster_client_lib.RunFunction,
                       u'ListUsers',
                       USERNAME,
                       server_name=self.server_name,
                       password=PASSWORD,
                       credfile=CREDFILE)
     self.assertEqual(
         sys.stdout.flush(),
         'ERROR: Credential file not found, invalid credentials.\n')
     sys.stdout = oldstdout
     # create credential file for RunFunction
     roster_client_lib.GetCredentials(u'shuey',
                                      u'testpass',
                                      server_name=self.server_name,
                                      credfile=CREDFILE)
     # using file from function above
     self.assertEqual(
         roster_client_lib.RunFunction(u'ListUsers',
                                       u'shuey',
                                       credfile=CREDFILE,
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'], {
                                           'shuey': 64,
                                           'tree_export_user': 0,
                                           'jcollins': 32,
                                           'sharrell': 128
                                       })
    def testMultipleThreadedConnectionsWithDifferentUsers(self):
        data_exporter = tree_exporter.BindTreeExport(CONFIG_FILE)
        self.daemon_instance.core_die_time = 7200
        roster_client_lib.RunFunction(u'MakeZone',
                                      USERNAME,
                                      credstring=self.credential,
                                      args=['new_zone', 'master', 'zone.com.'],
                                      server_name=self.server_name,
                                      password=PASSWORD)

        cred_dict = {}
        for user_number in range(40):

            roster_client_lib.RunFunction(u'MakeUser',
                                          USERNAME,
                                          credstring=self.credential,
                                          args=['user%s' % user_number, 128],
                                          server_name=self.server_name)
            cred_dict['user%s' %
                      user_number] = roster_client_lib.GetCredentials(
                          'user%s' % user_number,
                          'tost',
                          server_name=self.server_name)

        client_threads = []
        for record_number in range(5):
            for user_number in range(40):
                new_client_thread = ClientRecordModifyThread(
                    'user%s' % user_number,
                    '192.168.%s.%s' % (user_number, record_number),
                    'host%s-%s' % (user_number, record_number),
                    cred_dict['user%s' % user_number], self)
                new_client_thread.daemon = True
                new_client_thread.start()
                client_threads.append(new_client_thread)
            data_exporter.db_instance.StartTransaction()
            try:
                data_exporter.db_instance.LockDb()
                try:
                    data_exporter.GetRawData()
                finally:
                    data_exporter.db_instance.UnlockDb()
            finally:
                data_exporter.db_instance.EndTransaction()

        for old_thread in client_threads:
            old_thread.join()
 def testKWArgsAndArgsClient(self):
     roster_client_lib.RunFunction(u'MakeUserGroupAssignment',
                                   USERNAME,
                                   credstring=self.credential,
                                   args=[u'sharrell', u'bio'],
                                   server_name=self.server_name,
                                   password=PASSWORD)['core_return']
     self.assertEqual(
         roster_client_lib.RunFunction(u'ListUserGroupAssignments',
                                       USERNAME,
                                       credstring=self.credential,
                                       kwargs={'key_by_group': True},
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'], {
                                           'bio': ['sharrell', 'shuey'],
                                           'cs': ['sharrell', 'shuey']
                                       })
    def testRunFunction(self):
        self.core_instance.MakeView(u'test_view')
        self.core_instance.MakeZone(u'test_zone',
                                    u'master',
                                    u'university.edu.',
                                    view_name=u'test_view')

        zones = roster_client_lib.RunFunction(u'ListZones',
                                              USERNAME,
                                              CREDFILE,
                                              server_name=self.server_name,
                                              password=PASSWORD)['core_return']
        self.assertEqual(
            zones, {
                'test_zone': {
                    'test_view': {
                        'zone_type': 'master',
                        'zone_options': '',
                        'zone_origin': 'university.edu.'
                    },
                    'any': {
                        'zone_type': 'master',
                        'zone_options': '',
                        'zone_origin': 'university.edu.'
                    }
                }
            })

        credfile_handle = open(CREDFILE, 'w')
        credfile_handle.writelines('invalid_file')
        credfile_handle.close()
        oldstdout = sys.stdout
        sys.stdout = StdOutStream()
        self.assertRaises(SystemExit,
                          roster_client_lib.RunFunction,
                          u'ListZones',
                          USERNAME,
                          CREDFILE,
                          server_name=self.server_name,
                          password='******')

        invalid_credfile = '/fake/credfile'
        self.assertEqual(sys.stdout.flush(),
                         'ERROR: Incorrect username/password.\n')
        self.assertRaises(SystemExit,
                          roster_client_lib.RunFunction,
                          u'ListZones',
                          USERNAME,
                          CREDFILE,
                          server_name=self.server_name,
                          password='******')
        self.assertEqual(sys.stdout.flush(),
                         'ERROR: Incorrect username/password.\n')
        sys.stdout = oldstdout
 def testNoArgsClient(self):
     self.assertEqual(
         roster_client_lib.RunFunction(u'ListUsers',
                                       USERNAME,
                                       credstring=self.credential,
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'], {
                                           u'shuey': 64,
                                           'tree_export_user': 0,
                                           u'jcollins': 32,
                                           u'sharrell': 128
                                       })
 def testArgsOnlyClient(self):
     roster_client_lib.RunFunction(u'MakeUser',
                                   USERNAME,
                                   args=[u'jake\xc6', 64],
                                   credstring=self.credential,
                                   server_name=self.server_name,
                                   password=PASSWORD)
     self.assertEqual(
         roster_client_lib.RunFunction(u'ListUsers',
                                       USERNAME,
                                       credstring=self.credential,
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'], {
                                           u'shuey': 64,
                                           u'tree_export_user': 0,
                                           u'jcollins': 32,
                                           u'sharrell': 128,
                                           u'jake\xc6': 64
                                       })
     self.assertTrue(
         roster_client_lib.RunFunction(u'RemoveUser',
                                       USERNAME,
                                       args=[u'shuey'],
                                       credstring=self.credential,
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'])
     self.assertEqual(
         roster_client_lib.RunFunction(u'ListUsers',
                                       USERNAME,
                                       credstring=self.credential,
                                       server_name=self.server_name,
                                       password=PASSWORD)['core_return'], {
                                           u'jcollins': 32,
                                           u'tree_export_user': 0,
                                           u'jake\xc6': 64,
                                           u'sharrell': 128
                                       })
    def testMultipleThreadedConnections(self):
        self.daemon_instance.core_die_time = 7200
        roster_client_lib.RunFunction(u'MakeZone',
                                      USERNAME,
                                      credstring=self.credential,
                                      args=['new_zone', 'master', 'zone.com.'],
                                      server_name=self.server_name,
                                      password=PASSWORD)
        client_threads = []
        for current_number in range(50):
            new_client_thread = ClientRecordModifyThread(
                USERNAME, '192.168.0.%s' % current_number,
                'host%s' % current_number, self.credential, self)
            new_client_thread.daemon = True
            new_client_thread.start()
            client_threads.append(new_client_thread)

        for old_thread in client_threads:
            old_thread.join()
    def testRaiseErrors(self):
        self.daemon_instance.core_die_time = 7200
        oldstdout = sys.stdout
        sys.stdout = StdOutStream()
        self.assertRaises(SystemExit,
                          roster_client_lib.RunFunction,
                          u'MakeZone',
                          USERNAME,
                          credstring=self.credential,
                          args=['new_zone', 'master', 'zone.com'],
                          server_name=self.server_name,
                          password=PASSWORD)
        self.assertEqual(
            sys.stdout.flush(),
            'USER ERROR: Invalid data type Hostname for zone_origin: zone.com\n'
        )
        sys.stdout = oldstdout

        sys.stdout = StdOutStream()
        self.assertRaises(SystemExit,
                          roster_client_lib.RunFunction,
                          u'MakeZone',
                          USERNAME,
                          credstring=self.credential,
                          args=['new_zone', 'no-exist', 'zone.com.'],
                          server_name=self.server_name,
                          password=PASSWORD)
        self.assertEqual(sys.stdout.flush(),
                         'USER ERROR: Invalid zone type.\n')
        sys.stdout = oldstdout

        sys.stdout = StdOutStream()
        self.assertRaises(SystemExit,
                          roster_client_lib.RunFunction,
                          u'MakeZone',
                          USERNAME,
                          credstring=self.credential,
                          args=['new_zone', 'master', 'zone.com.'],
                          kwargs={'view_name': 'no-exist'},
                          server_name=self.server_name,
                          password=PASSWORD)
        out = sys.stdout.flush()
        self.assertEqual(out, 'USER ERROR: Specified view does not exist.\n')
        sys.stdout = oldstdout

        sys.stdout = StdOutStream()
        core_return = roster_client_lib.RunFunction(
            u'MakeZone',
            USERNAME,
            credstring=self.credential,
            args=['new_zone', 'master', 'zone.com.'],
            server_name=self.server_name,
            password=PASSWORD)
        self.assertEqual(sys.stdout.flush(), '')
        sys.stdout = oldstdout
        self.assertEqual(
            core_return, {
                'new_credential': '',
                'core_return': None,
                'log_uuid_string': None,
                'error': None
            })
        sys.stdout = StdOutStream()
        self.assertRaises(SystemExit,
                          roster_client_lib.RunFunction,
                          u'MakeRecord',
                          USERNAME,
                          credstring=self.credential,
                          args=[
                              'a', 'new_host', 'bad_zone_bad', {
                                  'assignment_ip': "192.168.1.1"
                              }
                          ],
                          server_name=self.server_name)
        out = sys.stdout.flush()
        sys.stdout = oldstdout
        self.assertEqual(
            out,
            'USER ERROR: Specified zone-view assignment does not exist for '
            'zone bad_zone_bad view any\n')

        sys.stdout = StdOutStream()
        self.assertRaises(SystemExit,
                          roster_client_lib.RunFunction,
                          u'MakeRecord',
                          USERNAME,
                          credstring=self.credential,
                          args=[
                              'a', 'new_host', 'new_zone', {
                                  'assignment_ip': "192.168.1.1"
                              }
                          ],
                          kwargs={'view_name': u'bad_view'},
                          server_name=self.server_name)
        out = sys.stdout.flush()
        sys.stdout = oldstdout
        self.assertEqual(
            out,
            'USER ERROR: Specified zone-view assignment does not exist for '
            'zone new_zone view bad_view\n')