Exemplo n.º 1
0
 def test_farmer_run(self):
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch(
                 'six.moves.urllib.request.urlopen') as patch:
         r.side_effect = MockRestore(
             {'historyfile': dict(), 'identityfile': dict()})
         farmer = Farmer(self.test_args)
         farmer.api = API()
     with mock.patch(
         'downstream_farmer.shell.DownstreamClient') as patch,\
             mock.patch(
                 'downstream_farmer.shell.save', autospec=True) as s:
         patch.return_value.token = 'foo'
         patch.return_value.address = 'bar'
         farmer.run()
         patch.assert_called_with(
             farmer.url, farmer.token, farmer.address, farmer.size, '', '',
             farmer.api)
         self.assertTrue(patch.return_value.connect.called)
         self.assertEqual(farmer
                          .state['nodes'][patch.return_value
                                          .server]['token'],
                          patch.return_value.token)
         self.assertEqual(farmer
                          .state['nodes'][patch.return_value
                                          .server]['address'],
                          patch.return_value.address)
         self.assertTrue(s.called)
         patch.return_value.run.assert_called_with(farmer.number)
Exemplo n.º 2
0
    def test_run_working_block(self):
        client = mock.MagicMock(spec=DownstreamClient)
        client.get_total_size.return_value = 0
        client.desired_size = 100
        client.api = API()

        contract = mock.MagicMock(spec=DownstreamContract)
        contract.time_remaining.return_value = 1
        contract.hash = '1'
        contract.api = client.api

        def patch_get_chunk(size):
            client.get_total_size.return_value = \
                client.get_total_size.return_value + \
                size
            client.contracts = [contract]

        client.get_chunk.side_effect = patch_get_chunk
        client.get_next_contract.return_value = contract
        with mock.patch('time.sleep') as a:
            DownstreamClient.run(client, 1)
        self.assertEqual(client.get_chunk.call_count, 1)
        self.assertEqual(contract.update_challenge.call_count, 1)
        self.assertEqual(contract.answer_challenge.call_count, 1)
        self.assertEqual(a.call_count, 1)
Exemplo n.º 3
0
    def test_run_obtain_contract_fail(self):
        client = mock.MagicMock(spec=DownstreamClient)
        client.get_total_size.return_value = 0
        client.desired_size = 100
        client.api = API()

        client.get_chunk.side_effect = DownstreamError('test error')
        client.contracts = []
        with self.assertRaises(DownstreamError) as ex:
            DownstreamClient.run(client, 1)
        self.assertEqual(
            str(ex.exception), 'Unable to obtain a contract: test error')
Exemplo n.º 4
0
 def setUp(self):
     self._old_argv = sys.argv
     sys.argv = [
         'downstream'
     ]
     self.test_args = mock.MagicMock()
     self.test_args.number = None
     self.test_args.node_url = 'http://testurl/'
     self.test_args.api_path = '/api/downstream/v1'
     self.test_args.token = 'testtoken'
     self.test_args.address = 'testaddress'
     self.test_args.size = 100
     self.test_args.history = 'historyfile'
     self.test_args.forcenew = False
     self.test_args.identity = 'identityfile'
     self.test_args.api = API()
Exemplo n.º 5
0
    def test_run_answer_failed(self):
        client = mock.MagicMock(spec=DownstreamClient)
        client.get_total_size.return_value = 100
        client.desired_size = 100
        client.api = API()

        contract = mock.MagicMock(spec=DownstreamContract)
        contract.time_remaining.return_value = 0
        contract.hash = '1'
        contract.api = client.api

        client.contracts = mock.MagicMock()
        client.contracts.remove = mock.MagicMock()

        contract.answer_challenge.side_effect = DownstreamError('test error')
        client.get_next_contract.return_value = contract
        DownstreamClient.run(client, 1)
        self.assertTrue(client.contracts.remove.called)
Exemplo n.º 6
0
 def setUp(self):
     self.challenge = Heartbeat.challenge_type().\
         fromdict(MockValues.get_challenge_response['challenge'])
     self.heartbeat = Heartbeat.fromdict(
         MockValues.connect_response['heartbeat'])
     self.tag = Heartbeat.tag_type().fromdict(
         MockValues.get_chunk_response['tag'])
     self.expiration = datetime.utcnow(
     ) + timedelta(int(MockValues.get_chunk_response['due']))
     self.client = mock.MagicMock()
     self.api = API()
     self.contract = DownstreamContract(self.client,
                                        'hash',
                                        'seed',
                                        100,
                                        self.challenge,
                                        self.expiration,
                                        self.tag,
                                        self.api)
Exemplo n.º 7
0
 def setUp(self):
     self.server_url = 'https://test.url/'
     self.api_path = '/api/downstream/v1'
     self.size = 100
     self.address = base58.b58encode_check(b'\x00' + os.urandom(20))
     self.token = binascii.hexlify(os.urandom(16)).decode('ascii')
     self.msg = ''
     self.sig = ''
     self.api = API()
     self.client = DownstreamClient(self.server_url,
                                    self.token,
                                    self.address,
                                    self.size,
                                    self.msg,
                                    self.sig,
                                    self.api)
     self.test_contract = DownstreamContract(self.client,
                                             MockValues.get_chunk_response[
                                                 'file_hash'],
                                             MockValues.get_chunk_response[
                                                 'seed'],
                                             MockValues.get_chunk_response[
                                                 'size'],
                                             Heartbeat.challenge_type()
                                             .fromdict(
                                                 MockValues.
                                                 get_chunk_response['challe'
                                                                    'nge']),
                                             datetime.utcnow() +
                                             timedelta(
                                                 seconds=int(
                                                     MockValues.
                                                     get_chunk_response['du'
                                                                        'e']
                                                 )),
                                             Heartbeat.tag_type().fromdict(
                                                 MockValues
                                                 .get_chunk_response['ta'
                                                                     'g'],
                                             ),
                                             self.api)
     self.test_heartbeat = Heartbeat.fromdict(
         MockValues.connect_response['heartbeat'])
Exemplo n.º 8
0
 def test_update_challenge_working(self):
     self.contract.answered = True
     self.client.heartbeat = self.heartbeat
     self.api = API()
     self.contract.time_remaining = mock.MagicMock()
     self.contract.time_remaining.return_value = 0
     with mock.patch('downstream_farmer.contract.requests.get') as getpatch:
         getpatch.return_value.json.return_value =\
             MockValues.get_challenge_response
         self.contract.update_challenge()
         self.assertEqual(self.contract.challenge,
                          Heartbeat.challenge_type().fromdict(
                              MockValues
                              .get_challenge_response['challenge']),
                          self.api)
         self.assertAlmostEqual((self.
                                 contract.expiration - datetime.utcnow()).
                                total_seconds(),
                                int(MockValues.
                                    get_challenge_response['due']),
                                delta=0.5)
Exemplo n.º 9
0
 def test_api_retrieve_contracts(self):
     api = API()
     self.assertEqual(api.contracts, 0)
Exemplo n.º 10
0
 def test_api_plus_contracts(self):
     api = API()
     api.plus_contracts(1)
     self.assertEqual(api.contracts, 1)
Exemplo n.º 11
0
 def test_api_retrieve_heartbeats(self):
     api = API()
     self.assertEqual(api.heartbeats, 0)
Exemplo n.º 12
0
 def test_api_plus_heartbeats(self):
     api = API()
     api.plus_heartbeats(1)
     self.assertEqual(api.heartbeats, 1)
Exemplo n.º 13
0
 def test_api_stop(self):
     api = API()
     api.stop()
     self.assertEqual(api.running, 0)
Exemplo n.º 14
0
 def test_api_start(self):
     api = API()
     api.start()
     self.assertEqual(api.running, 1)