def test_add_to_testbed(self):
     testbed = AttrDict(servers=AttrDict())
     with FileServer(protocol='ftp',
                     subnet='127.0.0.1/32',
                     testbed=testbed,
                     name='myserver') as fs:
         self.assertEqual(testbed.servers.myserver, fs)
 def test_tftp(self):
     # Just starting the server performs a copy for validation
     with FileServer(protocol='tftp', subnet='127.0.0.1/32') as fs:
         self.assertEqual(fs['address'], '127.0.0.1')
         self.assertNotEqual(fs['port'], 0)
         self.assertEqual(fs['path'], '/')
         self.assertEqual(fs['protocol'], 'tftp')
 def test_ftp(self):
     with FileServer(protocol='ftp', subnet='127.0.0.1/32') as fs:
         self.assertEqual(fs['address'], '127.0.0.1')
         self.assertNotEqual(fs['port'], 0)
         self.assertEqual(fs['path'], '/')
         self.assertEqual(fs['protocol'], 'ftp')
         self.assertIsInstance(fs['credentials']['ftp']['password'],
                               SecretString)
 def test_copy_from_device_context_manager_tftp_server(self):
     with tempfile.TemporaryDirectory() as td:
         with FileServer(protocol='tftp',
                         path=td,
                         testbed=self.testbed,
                         name='mycontextserver'):
             self.device.execute = Mock()
             self.device.api.copy_from_device(protocol='tftp',
                                              server='mycontextserver',
                                              remote_path='test.txt',
                                              local_path='test.txt')
         assert re.search(r'copy test.txt tftp://127.0.0.1:\d+/test.txt',
                          str(self.device.execute.call_args))
 def test_http_auth(self):
     with FileServer(
             protocol='http',
             subnet='127.0.0.1/32',
             custom=dict(http_auth=True),
             credentials=dict(
                 http=dict(username='******', password='******'))) as fs:
         self.assertEqual(fs['address'], '127.0.0.1')
         self.assertIn('port', fs)
         self.assertEqual(fs['path'], '/')
         self.assertEqual(fs['protocol'], 'http')
         self.assertEqual(fs['credentials']['http']['username'], 'test')
         self.assertEqual(
             to_plaintext(fs['credentials']['http']['password']), 'test123')
 def test_http_multipart_multi(self):
     with tempfile.TemporaryDirectory() as td:
         with FileServer(protocol='http', subnet='127.0.0.1/32',
                         path=td) as fs:
             url = 'http://{u}:{p}@127.0.0.1:{port}/test.txt'.format(
                 port=fs['port'],
                 u=fs['credentials']['http']['username'],
                 p=to_plaintext(fs['credentials']['http']['password']))
             orig_data = ['test123'] * 2
             data, content_type = encode_multipart_formdata(orig_data)
             r = requests.post(url,
                               data=data,
                               headers={'Content-type': content_type})
             self.assertEqual(r.status_code, 201)
             with open(os.path.join(td, 'test_1.txt'), 'rb') as f:
                 test_data = f.read().decode()
             self.assertEqual(test_data, orig_data[1])
 def test_ftp_pass_args(self):
     with tempfile.TemporaryDirectory() as td:
         with FileServer(protocol='ftp',
                         subnet='127.0.0.1',
                         credentials={
                             'ftp': {
                                 'username': '******',
                                 'password': '******'
                             }
                         },
                         path=td) as fs:
             self.assertEqual(fs['address'], '127.0.0.1')
             self.assertEqual(fs['subnet'], '127.0.0.1')
             self.assertEqual(fs['path'], td)
             self.assertEqual(fs['protocol'], 'ftp')
             self.assertIsInstance(fs['credentials']['ftp']['password'],
                                   SecretString)
             self.assertEqual(
                 to_plaintext(fs['credentials']['ftp']['password']),
                 'mypass')
 def test_http(self):
     with FileServer(protocol='http', subnet='127.0.0.1/32') as fs:
         self.assertEqual(fs['address'], '127.0.0.1')
         self.assertIn('port', fs)
         self.assertEqual(fs['path'], '/')
         self.assertEqual(fs['protocol'], 'http')