From: Brian Warner <warner@lothar.com>
Date: Wed, 3 Dec 2008 03:20:22 +0000 (-0700)
Subject: CLI: check for pre-existing aliases in 'tahoe create-alias' and 'tahoe add-alias'
X-Git-Url: https://git.rkrishnan.org/components/index.php?a=commitdiff_plain;h=a2ce315074bc5b2cb5961d52d36241ca4a62e61a;p=tahoe-lafs%2Ftahoe-lafs.git

CLI: check for pre-existing aliases in 'tahoe create-alias' and 'tahoe add-alias'
---

diff --git a/src/allmydata/scripts/tahoe_add_alias.py b/src/allmydata/scripts/tahoe_add_alias.py
index 383bb1ab..e74a13ee 100644
--- a/src/allmydata/scripts/tahoe_add_alias.py
+++ b/src/allmydata/scripts/tahoe_add_alias.py
@@ -10,11 +10,15 @@ def add_alias(options):
     cap = options.cap
     stdout = options.stdout
     stderr = options.stderr
-    aliasfile = os.path.join(nodedir, "private", "aliases")
-    cap = uri.from_string_dirnode(cap).to_string()
     assert ":" not in alias
     assert " " not in alias
-    # probably check for others..
+
+    old_aliases = get_aliases(nodedir)
+    if alias in old_aliases:
+        print >>stderr, "Alias '%s' already exists!" % alias
+        return 1
+    aliasfile = os.path.join(nodedir, "private", "aliases")
+    cap = uri.from_string_dirnode(cap).to_string()
     f = open(aliasfile, "a")
     f.write("%s: %s\n" % (alias, cap))
     f.close()
@@ -27,10 +31,16 @@ def create_alias(options):
     alias = options.alias
     stdout = options.stdout
     stderr = options.stderr
-    aliasfile = os.path.join(nodedir, "private", "aliases")
     assert ":" not in alias
     assert " " not in alias
 
+    old_aliases = get_aliases(nodedir)
+    if alias in old_aliases:
+        print >>stderr, "Alias '%s' already exists!" % alias
+        return 1
+
+    aliasfile = os.path.join(nodedir, "private", "aliases")
+
     nodeurl = options['node-url']
     if not nodeurl.endswith("/"):
         nodeurl += "/"
diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py
index b9d312dd..fdd6f05d 100644
--- a/src/allmydata/test/test_cli.py
+++ b/src/allmydata/test/test_cli.py
@@ -307,8 +307,32 @@ class CreateAlias(SystemTestMixin, CLITestMixin, unittest.TestCase):
             self.tahoe_url = uribase + urllib.quote(aliases["tahoe"])
             self.tahoe_subdir_url = self.tahoe_url + "/subdir"
             self.two_url = uribase + urllib.quote(aliases["two"])
+            self.two_uri = aliases["two"]
         d.addCallback(_stash_urls)
 
+        d.addCallback(lambda res: self.do_cli("create-alias", "two")) # dup
+        def _check_create_duplicate((rc,stdout,stderr)):
+            self.failIfEqual(rc, 0)
+            self.failUnless("Alias 'two' already exists!" in stderr)
+            aliases = get_aliases(self.getdir("client0"))
+            self.failUnlessEqual(aliases["two"], self.two_uri)
+        d.addCallback(_check_create_duplicate)
+
+        d.addCallback(lambda res: self.do_cli("add-alias", "added", self.two_uri))
+        def _check_add((rc,stdout,stderr)):
+            self.failUnlessEqual(rc, 0)
+            self.failUnless("Alias 'added' added" in stdout)
+        d.addCallback(_check_add)
+
+        # check add-alias with a duplicate
+        d.addCallback(lambda res: self.do_cli("add-alias", "two", self.two_uri))
+        def _check_add_duplicate((rc,stdout,stderr)):
+            self.failIfEqual(rc, 0)
+            self.failUnless("Alias 'two' already exists!" in stderr)
+            aliases = get_aliases(self.getdir("client0"))
+            self.failUnlessEqual(aliases["two"], self.two_uri)
+        d.addCallback(_check_add_duplicate)
+
         def _test_urls(junk):
             self._test_webopen([], self.tahoe_url)
             self._test_webopen(["/"], self.tahoe_url)