def test_secret_append(self): fd, filename = mkstemp() os.close(fd) try: generate_secret(filename, 'node') time.sleep(1.1) generate_secret(filename, 'node') secrets = Secrets(filename) self.assertEqual(len(secrets.get('node')), 2) finally: os.remove(filename)
def generate_secret(filename, node): """Generates a new secret for the given node and saves it to a secrets file. :param filename: the complete path to the filename we want to put the secret into. :param node: the node we want to generate the secret for. """ if os.path.exists(filename): secrets = Secrets(filename) else: secrets = Secrets() secrets.add(node) secrets.save(filename) return node, secrets.get(node)[0]
def test_read_write(self): secrets = Secrets() secrets.add('phx23456') secrets.add('phx23456') secrets.add('phx23') phx23456_secrets = secrets.get('phx23456') self.assertEqual(len(secrets.get('phx23456')), 2) self.assertEqual(len(secrets.get('phx23')), 1) path = self.tempfile() secrets.save(path) secrets2 = Secrets(path) self.assertEqual(len(secrets2.get('phx23456')), 2) self.assertEqual(len(secrets2.get('phx23')), 1) self.assertEquals(secrets2.get('phx23456'), phx23456_secrets)
def test_multiple_files(self): # creating two distinct files secrets = Secrets() secrets.add('phx23456') one = self.tempfile() secrets.save(one) secrets = Secrets() secrets.add('phx123') two = self.tempfile() secrets.save(two) # loading the two files files = one, two secrets = Secrets(files) keys = secrets.keys() keys.sort() self.assertEqual(keys, ['phx123', 'phx23456'])
def test_read_write(self): secrets = Secrets() # We can only add one secret per second to the file, since # they are timestamped to 1s resolution. Fake it. real_time = time.time time.time = itertools.count(int(real_time())).next try: secrets.add('phx23456') secrets.add('phx23456') secrets.add('phx23') finally: time.time = real_time phx23456_secrets = secrets.get('phx23456') self.assertEqual(len(secrets.get('phx23456')), 2) self.assertEqual(len(secrets.get('phx23')), 1) path = self.tempfile() secrets.save(path) secrets2 = Secrets(path) self.assertEqual(len(secrets2.get('phx23456')), 2) self.assertEqual(len(secrets2.get('phx23')), 1) self.assertEquals(secrets2.get('phx23456'), phx23456_secrets)
def test_multiple_files(self): # creating two distinct files secrets = Secrets() secrets.add("phx23456") one = self.tempfile() secrets.save(one) secrets = Secrets() secrets.add("phx123") two = self.tempfile() secrets.save(two) # loading the two files files = one, two secrets = Secrets(files) keys = secrets.keys() keys.sort() self.assertEqual(keys, ["phx123", "phx23456"])
def test_read_write(self): secrets = Secrets() # We can only add one secret per second to the file, since # they are timestamped to 1s resolution. Fake it. real_time = time.time time.time = itertools.count(int(real_time())).next try: secrets.add("phx23456") secrets.add("phx23456") secrets.add("phx23") finally: time.time = real_time phx23456_secrets = secrets.get("phx23456") self.assertEqual(len(secrets.get("phx23456")), 2) self.assertEqual(len(secrets.get("phx23")), 1) path = self.tempfile() secrets.save(path) secrets2 = Secrets(path) self.assertEqual(len(secrets2.get("phx23456")), 2) self.assertEqual(len(secrets2.get("phx23")), 1) self.assertEquals(secrets2.get("phx23456"), phx23456_secrets)