Browse Source

[2710] Better handling of nonexistent file

Jelte Jansen 12 years ago
parent
commit
d27e68b1e1
2 changed files with 20 additions and 1 deletions
  1. 4 1
      src/bin/cmdctl/cmdctl.py.in
  2. 16 0
      src/bin/cmdctl/tests/cmdctl_test.py

+ 4 - 1
src/bin/cmdctl/cmdctl.py.in

@@ -509,8 +509,11 @@ class SecureHTTPServer(socketserver_mixin.NoPollMixIn,
         '''Read all user's name and its' salt, hashed password
         from accounts file.'''
 
-        # If the file does not exist, do nothing
+        # If the file does not exist, set accounts to empty, and return
         if not os.path.exists(accounts_file):
+            self._user_infos = {}
+            self._accounts_file = accounts_file
+            self.__accounts_file_mtime = 0
             return
 
         # If the filename hasn't changed, and the file itself

+ 16 - 0
src/bin/cmdctl/tests/cmdctl_test.py

@@ -554,6 +554,22 @@ class TestSecureHTTPServer(unittest.TestCase):
                 self.assertEqual(1, len(self.server._user_infos))
                 self.assertTrue('otherroot' in self.server._user_infos)
 
+    def test_create_user_info_nonexistent_file(self):
+        # Even if there was data initially, if set to a nonexistent
+        # file it should result in no users
+        accounts_file = BUILD_FILE_PATH + 'new_file.csv'
+        self.assertFalse(os.path.exists(accounts_file))
+        fake_users_val = { 'notinfile': [] }
+        self.server._user_infos = fake_users_val
+        self.server._create_user_info(accounts_file)
+        self.assertEqual({}, self.server._user_infos)
+
+        # Should it now be created it should be read
+        with TmpTextFile(accounts_file, ['root,foo,bar']):
+            self.server._create_user_info(accounts_file)
+            self.assertEqual(1, len(self.server._user_infos))
+            self.assertTrue('root' in self.server._user_infos)
+
     def test_check_file(self):
         # Just some file that we know exists
         file_name = BUILD_FILE_PATH + 'cmdctl-keyfile.pem'