Skip to content

Commit

Permalink
NFS exports restored as read-only - continued 2 rockstor#2912
Browse files Browse the repository at this point in the history
Resolve remaining TODOs in test_nfs_export.py.
Includes:
- Removing redundant tests.
- Modifying previously remarked out sanity check tests
to instead cover our umbrella low-level exception. Once
more specific sanity checks are in place, we can add
tests to prove their function.
  • Loading branch information
phillxnet committed Nov 7, 2024
1 parent 92d5cbf commit 2b2301a
Showing 1 changed file with 106 additions and 178 deletions.
284 changes: 106 additions & 178 deletions src/rockstor/storageadmin/tests/test_nfs_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,12 @@ def test_get(self):
self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response)

def test_invalid_get(self):

# get nfs-export with invalid id
response = self.client.get("{}/99999".format(self.BASE_URL))
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND, msg=response)

def test_post_requests(self):
"""
invalid nfs-export api operations
1. Add nfs-export without providing share names
2 Add nfs-export
3. Add nfs-export for the share that has already been exported
"""

# Add nfs-export without providing share names
self.mock_refresh_nfs_exports.side_effect = None
Expand All @@ -124,27 +118,17 @@ def test_post_requests(self):
e_msg = "Cannot export without specifying shares."
self.assertEqual(response.data[0], e_msg)

# AssertionError: ["{'share': [u'share instance with id 22 does not exist.']}",
# # happy path
# data1 = {'shares': ('share2',), 'host_str': '*.edu',
# 'mod_choice': 'rw', 'sync_choice': 'async', }
# response = self.client.post(self.BASE_URL, data=data1)
# self.assertEqual(response.status_code,
# status.HTTP_200_OK, msg=response.data)

# # Add nfs-export for the share that is already been exported
# data1 = {'shares': ('share1',), 'host_str': '*', 'mod_choice': 'rw',
# 'sync_choice': 'async', }
# response = self.client.post(self.BASE_URL, data=data1)
# self.assertEqual(response.status_code,
# status.HTTP_500_INTERNAL_SERVER_ERROR,
# msg=response.data)
# e_msg = 'An export already exists for the host string: (*).'
# self.assertEqual(response.data[0], e_msg)

# Add nfs-export with invalid nfs-client
# happy path
data1 = {'shares': ('share2',), 'host_str': '*.edu',
'mod_choice': 'rw', 'sync_choice': 'async', }
response = self.client.post(self.BASE_URL, data=data1)
self.assertEqual(response.status_code,
status.HTTP_200_OK, msg=response.data)

# Add NFS export - non-existent share
fake_share_name: str = "non-existent"
data1 = {
"shares": ("share1",),
"shares": (f"{fake_share_name}",),
"host_str": "*",
"mod_choice": "rw",
"sync_choice": "async",
Expand All @@ -155,70 +139,43 @@ def test_post_requests(self):
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.data,
)
# TODO: AssertionError: "{'share': [u'share instance with id 22 does not exist.']}
# e_msg = 'An export already exists for the host string: (*).'
# self.assertEqual(response.data[0], e_msg)

# TODO: Test needs updating
# AssertionError: ['Share with name (clone1) does not exist.',
# def test_no_nfs_client(self):
# # Add nfs-export without specifying nfs-clients(host string). The
# # server side defaults the host string to * so test for this.
#
# self.mock_refresh_nfs_exports.side_effect = None
# data1 = {'shares': ('clone1',), 'mod_choice': 'rw',
# 'sync_choice': 'async', }
# response = self.client.post(self.BASE_URL, data=data1)
# self.assertEqual(response.status_code,
# status.HTTP_200_OK, msg=response.data)
# self.assertEqual(response.data['host_str'], '*')

# def test_invalid_nfs_client2(self):
#
# # TODO: Test needs updating
# # invalid post request
# # Add nfs-export providing invalid nfs client
# self.mock_refresh_nfs_exports.side_effect = Exception()
#
# data1 = {'shares': ('clone1',), 'host_str': 'host%%%edu',
# 'mod_choice': 'rw', 'sync_choice': 'async', }
# response = self.client.post(self.BASE_URL, data=data1)
# self.assertEqual(response.status_code,
# status.HTTP_500_INTERNAL_SERVER_ERROR,
# msg=response.data)
#
# e_msg = 'Invalid Hostname or IP: host%%%edu'
# self.assertEqual(response.data[0], e_msg)
# self.mock_refresh_nfs_exports.side_effect = None
#
# def test_invalid_nfs_client3(self):
#
# # TODO: Test needs updating
# # invalid put request
# # edit nfs-export providing invalid nfs-client
# self.mock_refresh_nfs_exports.side_effect = Exception()
# nfs_id = 11
# data = {'shares': ('share2',), 'host_str': 'host%%%edu',
# 'admin_host': ' ', 'mod_choice': 'rw',
# 'sync_choice': 'async', }
# response = self.client.put('{}/{}'.format(self.BASE_URL, nfs_id),
# data=data)
# self.assertEqual(response.status_code,
# status.HTTP_500_INTERNAL_SERVER_ERROR,
# msg=response.data)
# e_msg = 'Invalid Hostname or IP: host%%%edu'
# self.assertEqual(response.data[0], e_msg)
# self.mock_refresh_nfs_exports.side_effect = None

def test_invalid_admin_host1(self):

# invalid post request
# Add nfs-export providing invalid admin host
self.mock_refresh_nfs_exports.side_effect = Exception()
e_msg = f'Share with name ({fake_share_name}) does not exist.'
self.assertEqual(response.data[0], e_msg)

def test_no_nfs_client(self):

# Add NFS export without host_str
# The server side defaults the host string to * so test for this.

self.mock_refresh_nfs_exports.side_effect = None

data = {'shares': ('share2',), 'mod_choice': 'rw', 'sync_choice': 'async', }
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_200_OK, msg=response.data)
self.assertEqual(response.data['host_str'], '*')

def test_share_already_exported(self):

# Add NFS export for share already exported
data = {'shares': ('share-nfs',), 'host_str': '*', 'mod_choice': 'rw', 'sync_choice': 'async', }
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.data)
e_msg = 'An export already exists for the host string: (*).'
self.assertEqual(response.data[0], e_msg)

def test_low_level_error_post(self):

# Invalid POST request
# Add NFS export - invalid admin host
ll_error: str = "post low level error"
self.mock_refresh_nfs_exports.side_effect = Exception(ll_error)
data = {
"shares": ("share2",),
"host_str": "*.edu",
"admin_host": "admin%host",
"admin_host": "admin-host",
"mod_choice": "rw",
"sync_choice": "async",
}
Expand All @@ -228,52 +185,47 @@ def test_invalid_admin_host1(self):
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.data,
)
# TODO: FAIL AssertionError:
# "{'share': [u'share instance with id 22 does not exist.']}"
# != 'Invalid admin host: admin%host'
# e_msg = 'Invalid admin host: admin%host'
# self.assertEqual(response.data[0], e_msg)

e_msg = f'A lower level error occurred while refreshing NFS exports: ({ll_error}).'
self.assertEqual(response.data[0], e_msg)

self.mock_refresh_nfs_exports.side_effect = None

def test_invalid_admin_host2(self):
# invalid put request
# edit nfs-export providing invalid admin host
self.mock_refresh_nfs_exports.side_effect = Exception()
nfs_id = 11
def test_low_level_error_put(self):

# Invalid PUT request
# Edit NFS export - invalid admin host
ll_error: str = "put low level error"
self.mock_refresh_nfs_exports.side_effect = Exception(ll_error)

data = {
"shares": ("share2",),
"shares": ("share-nfs",),
"host_str": "*.edu",
"admin_host": "admin%host",
"admin_host": "admin-host",
"mod_choice": "rw",
"sync_choice": "async",
}
response = self.client.put("{}/{}".format(self.BASE_URL, nfs_id), data=data)
nfs_id = 3 # from fixture
response = self.client.put(f"{self.BASE_URL}/{nfs_id}", data=data)
self.assertEqual(
response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.data,
)
# TODO: FAIL AssertionError:
# 'Share with name (share2) does not exist.' != 'Invalid admin host: admin%host'
# e_msg = 'Invalid admin host: admin%host'
# self.assertEqual(response.data[0], e_msg)

e_msg = f'A lower level error occurred while refreshing NFS exports: ({ll_error}).'
self.assertEqual(response.data[0], e_msg)

self.mock_refresh_nfs_exports.side_effect = None

def test_put_requests(self):
"""
. Edit nfs-export with no shares
. Edit nfs-export
. Edit nfs-export that does not exists
"""

# Edit nfs-export with no shares
# Edit NFS export without specifying share.
self.mock_refresh_nfs_exports.side_effect = None
self.mock_refresh_nfs_exports.return_value = "out", "err", 0

# from fixture, TODO remove hard-coding.
nfs_id = 3
nfs_id = 3 # from fixture
data = {"host_str": "*.edu", "mod_choice": "rw", "sync_choice": "async"}
response = self.client.put("{}/{}".format(self.BASE_URL, nfs_id), data=data)
response = self.client.put(f"{self.BASE_URL}/{nfs_id}", data=data)
self.assertEqual(
response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
Expand All @@ -282,79 +234,55 @@ def test_put_requests(self):
e_msg = "Cannot export without specifying shares."
self.assertEqual(response.data[0], e_msg)

# TODO: FAIL AssertionError: ["{'export_group': [u'This field cannot be null.'],
# 'share': [u'share instance with id 21 does not exist.']}",
# # happy path of editing existing nfs export
# nfs_id = self.temp_nfsexport.id
# data = {'shares': ('share-nfs',), 'host_str': '*.edu',
# 'mod_choice': 'rw', 'sync_choice': 'async', }
# response = self.client.put('{}/{}'.format(self.BASE_URL, nfs_id),
# data=data)
# self.assertEqual(response.status_code,
# status.HTTP_200_OK, msg=response.data)

# TODO: FAIL AssertionError: ["{'export_group': [u'This field cannot be null.'],
# 'share': [u'share instance with id 21 does not exist.']}",
# # happy path2 with admin host
# nfs_id = 11
# data = {'shares': ('share2',), 'host_str': '*.edu',
# 'admin_host': 'host', 'mod_choice': 'rw',
# 'sync_choice': 'async', }
# response = self.client.put('{}/{}'.format(self.BASE_URL, nfs_id),
# data=data)
# self.assertEqual(response.status_code,
# status.HTTP_200_OK, msg=response.data)

# TODO: FAIL AssertionError: "{'export_group': [u'This field cannot be null.'],
# 'share': [u'share instance with id 21 does not exist.']}"
# # edit nfs-export that does not exist
# nfs_id = 99999
# data = {'shares': ('share2',), 'host_str': '*.edu',
# 'mod_choice': 'rw', 'sync_choice': 'async', }
# response = self.client.put('{}/{}'.format(self.BASE_URL, nfs_id),
# data=data)
# self.assertEqual(response.status_code,
# status.HTTP_500_INTERNAL_SERVER_ERROR,
# msg=response.data)
# e_msg = 'NFS export with id ({}) does not exist.'.format(nfs_id)
# self.assertEqual(response.data[0], e_msg)
# Happy path - editing existing NFS export.
nfs_id = 3 # from fixture
data = {'shares': ('share-nfs',), 'host_str': '*.edu',
'mod_choice': 'rw', 'sync_choice': 'async', }
response = self.client.put(f'{self.BASE_URL}/{nfs_id}', data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data)

def test_delete_requests(self):
# Happy path - editing existing NFS export re adding admin host.
nfs_id = 3 # from fixture
data = {'shares': ('share-nfs',), 'host_str': '*.edu', 'admin_host': 'test-nfs-admin-host',
'mod_choice': 'rw', 'sync_choice': 'async', }
response = self.client.put(f'{self.BASE_URL}/{nfs_id}', data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data)

# Edit non-existent NFS export, existing share in fixtures.
nfs_id = 99999
data = {'shares': ('share2',), 'host_str': '*.edu',
'mod_choice': 'rw', 'sync_choice': 'async', }
response = self.client.put(f'{self.BASE_URL}/{nfs_id}', data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.data)
e_msg = f'NFS export with id ({nfs_id}) does not exist.'
self.assertEqual(response.data[0], e_msg)

"""
. Delete nfs-export that does not exist
. Delete nfs-export
"""
def test_delete_requests(self):

# happy path
nfs_id = 3
response = self.client.delete("{}/{}".format(self.BASE_URL, nfs_id))
# Happy path
nfs_id = 3 # from fixture
response = self.client.delete(f"{self.BASE_URL}/{nfs_id}")
self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data)

# TODO: FAIL
# mock_nfsexport.objects.get.side_effect = NFSExport.DoesNotExist
#
# # Delete nfs-export that does nor exists
# nfs_id = 99999
# response = self.client.delete('{}/{}'.format(self.BASE_URL, nfs_id))
# self.assertEqual(response.status_code,
# status.HTTP_500_INTERNAL_SERVER_ERROR,
# msg=response.data)
# e_msg = 'NFS export with id ({}) does not exist.'.format(nfs_id)
# self.assertEqual(response.data[0], e_msg)
# Delete nfs-export that does not exist
nfs_id = 99999
response = self.client.delete(f'{self.BASE_URL}/{nfs_id}')
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.data)
e_msg = f'NFS export with id ({nfs_id}) does not exist.'
self.assertEqual(response.data[0], e_msg)

def test_adv_nfs_get(self):
"""
Test GET request
1. Get base URL
2. Get request with id
"""
# get base URL

# Get advanced base URL
self.get_base("/api/adv-nfs-exports")

def test_adv_nfs_post_requests(self):

# without specifying entries
# Without specifying entries
data = {}
response = self.client.post("/api/adv-nfs-exports", data=data)
self.assertEqual(
Expand All @@ -365,7 +293,7 @@ def test_adv_nfs_post_requests(self):
e_msg = "Cannot export without specifying entries."
self.assertEqual(response.data[0], e_msg)

# happy path
# Happy path
data = {"entries": ["/export/share-nfs *(rw,async,insecure)"]}
response = self.client.post("/api/adv-nfs-exports", data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data)
Expand Down

0 comments on commit 2b2301a

Please sign in to comment.