7167: Break all the code from keep-rsync main method into a separate func so that...
authorradhika <radhika@curoverse.com>
Fri, 16 Oct 2015 02:55:03 +0000 (22:55 -0400)
committerradhika <radhika@curoverse.com>
Fri, 16 Oct 2015 02:55:03 +0000 (22:55 -0400)
Rather than using default flag parsing, use FlagSet so that flags can be set multiple times from multiple tests.

tools/keep-rsync/keep-rsync.go
tools/keep-rsync/keep-rsync_test.go

index 705025b07970f720388f4139a6143234e8bd2e12..7cd795ecf32160e3e2a799e3d8ee72edd2f77321 100644 (file)
@@ -18,77 +18,79 @@ import (
 )
 
 func main() {
-       var srcConfigFile, dstConfigFile, srcKeepServicesJSON, dstKeepServicesJSON, prefix string
-       var replications int
-       var srcBlobSigningKey string
+       err := doMain()
+       if err != nil {
+               log.Fatalf("%v", err)
+       }
+}
 
-       flag.StringVar(
-               &srcConfigFile,
+func doMain() error {
+       flags := flag.NewFlagSet("keep-rsync", flag.ExitOnError)
+
+       srcConfigFile := flags.String(
                "src",
                "",
                "Source configuration filename. May be either a pathname to a config file, or (for example) 'foo' as shorthand for $HOME/.config/arvados/foo.conf")
 
-       flag.StringVar(
-               &dstConfigFile,
+       dstConfigFile := flags.String(
                "dst",
                "",
                "Destination configuration filename. May be either a pathname to a config file, or (for example) 'foo' as shorthand for $HOME/.config/arvados/foo.conf")
 
-       flag.StringVar(
-               &srcKeepServicesJSON,
+       srcKeepServicesJSON := flags.String(
                "src-keep-services-json",
                "",
                "An optional list of available source keepservices. "+
                        "If not provided, this list is obtained from api server configured in src-config-file.")
 
-       flag.StringVar(
-               &dstKeepServicesJSON,
+       dstKeepServicesJSON := flags.String(
                "dst-keep-services-json",
                "",
                "An optional list of available destination keepservices. "+
                        "If not provided, this list is obtained from api server configured in dst-config-file.")
 
-       flag.IntVar(
-               &replications,
+       replications := flags.Int(
                "replications",
                0,
                "Number of replications to write to the destination. If replications not specified, "+
                        "default replication level configured on destination server will be used.")
 
-       flag.StringVar(
-               &prefix,
+       prefix := flags.String(
                "prefix",
                "",
                "Index prefix")
 
-       flag.Parse()
+       // Parse args; omit the first arg which is the command name
+       flags.Parse(os.Args[1:])
 
-       srcConfig, srcBlobSigningKey, err := loadConfig(srcConfigFile)
+       srcConfig, srcBlobSigningKey, err := loadConfig(*srcConfigFile)
        if err != nil {
-               log.Fatalf("Error loading src configuration from file: %s", err.Error())
+               return fmt.Errorf("Error loading src configuration from file: %s", err.Error())
        }
 
-       dstConfig, _, err := loadConfig(dstConfigFile)
+       dstConfig, _, err := loadConfig(*dstConfigFile)
        if err != nil {
-               log.Fatalf("Error loading dst configuration from file: %s", err.Error())
+               return fmt.Errorf("Error loading dst configuration from file: %s", err.Error())
        }
 
        // setup src and dst keepclients
-       kcSrc, err := setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0)
+       kcSrc, err := setupKeepClient(srcConfig, *srcKeepServicesJSON, false, 0)
        if err != nil {
-               log.Fatalf("Error configuring src keepclient: %s", err.Error())
+               return fmt.Errorf("Error configuring src keepclient: %s", err.Error())
        }
 
-       kcDst, err := setupKeepClient(dstConfig, dstKeepServicesJSON, true, replications)
+       kcDst, err := setupKeepClient(dstConfig, *dstKeepServicesJSON, true, *replications)
        if err != nil {
-               log.Fatalf("Error configuring dst keepclient: %s", err.Error())
+               return fmt.Errorf("Error configuring dst keepclient: %s", err.Error())
        }
 
        // Copy blocks not found in dst from src
-       err = performKeepRsync(kcSrc, kcDst, srcBlobSigningKey, prefix)
+       err = performKeepRsync(kcSrc, kcDst, srcBlobSigningKey, *prefix)
        if err != nil {
-               log.Fatalf("Error while syncing data: %s", err.Error())
+               return fmt.Errorf("Error while syncing data: %s", err.Error())
        }
+
+       return nil
 }
 
 type apiConfig struct {
index 299df5aeb0a6317a71677b76ac76a6ef813e9036..f1091887d5f50ae71a416a6ffb73ae55fc081b3a 100644 (file)
@@ -23,10 +23,12 @@ func Test(t *testing.T) {
 // Gocheck boilerplate
 var _ = Suite(&ServerRequiredSuite{})
 var _ = Suite(&ServerNotRequiredSuite{})
+var _ = Suite(&DoMainTestSuite{})
 
 // Tests that require the Keep server running
 type ServerRequiredSuite struct{}
 type ServerNotRequiredSuite struct{}
+type DoMainTestSuite struct{}
 
 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
        // Start API server
@@ -38,6 +40,12 @@ func (s *ServerRequiredSuite) TearDownSuite(c *C) {
        arvadostest.ResetEnv()
 }
 
+var initialArgs []string
+
+func (s *DoMainTestSuite) SetUpSuite(c *C) {
+       initialArgs = os.Args
+}
+
 var kcSrc, kcDst *keepclient.KeepClient
 var srcKeepServicesJSON, dstKeepServicesJSON, blobSigningKey string
 
@@ -54,6 +62,15 @@ func (s *ServerRequiredSuite) TearDownTest(c *C) {
        arvadostest.StopKeepWithParams(3)
 }
 
+func (s *DoMainTestSuite) SetUpTest(c *C) {
+       args := []string{"keep-rsync"}
+       os.Args = args
+}
+
+func (s *DoMainTestSuite) TearDownTest(c *C) {
+       os.Args = initialArgs
+}
+
 var testKeepServicesJSON = "{ \"kind\":\"arvados#keepServiceList\", \"etag\":\"\", \"self_link\":\"\", \"offset\":null, \"limit\":null, \"items\":[ { \"href\":\"/keep_services/zzzzz-bi6l4-123456789012340\", \"kind\":\"arvados#keepService\", \"etag\":\"641234567890enhj7hzx432e5\", \"uuid\":\"zzzzz-bi6l4-123456789012340\", \"owner_uuid\":\"zzzzz-tpzed-123456789012345\", \"service_host\":\"keep0.zzzzz.arvadosapi.com\", \"service_port\":25107, \"service_ssl_flag\":false, \"service_type\":\"disk\", \"read_only\":false }, { \"href\":\"/keep_services/zzzzz-bi6l4-123456789012341\", \"kind\":\"arvados#keepService\", \"etag\":\"641234567890enhj7hzx432e5\", \"uuid\":\"zzzzz-bi6l4-123456789012341\", \"owner_uuid\":\"zzzzz-tpzed-123456789012345\", \"service_host\":\"keep0.zzzzz.arvadosapi.com\", \"service_port\":25108, \"service_ssl_flag\":false, \"service_type\":\"disk\", \"read_only\":false } ], \"items_available\":2 }"
 
 // Testing keep-rsync needs two sets of keep services: src and dst.
@@ -77,7 +94,6 @@ func setupRsync(c *C, enforcePermissions bool, replications int) {
        }
 
        // Start Keep servers
-       arvadostest.StartAPI()
        arvadostest.StartKeepWithParams(3, enforcePermissions)
 
        // setup keepclients
@@ -369,17 +385,17 @@ func (s *ServerNotRequiredSuite) TestLoadConfig(c *C) {
        srcConfig, srcBlobSigningKey, err := loadConfig(srcConfigFile)
        c.Check(err, IsNil)
 
-       c.Assert(srcConfig.APIHost, Equals, "testhost")
-       c.Assert(srcConfig.APIToken, Equals, "testtoken")
-       c.Assert(srcConfig.APIHostInsecure, Equals, true)
+       c.Assert(srcConfig.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
+       c.Assert(srcConfig.APIToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
+       c.Assert(srcConfig.APIHostInsecure, Equals, matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")))
        c.Assert(srcConfig.ExternalClient, Equals, false)
 
        dstConfig, _, err := loadConfig(dstConfigFile)
        c.Check(err, IsNil)
 
-       c.Assert(dstConfig.APIHost, Equals, "testhost")
-       c.Assert(dstConfig.APIToken, Equals, "testtoken")
-       c.Assert(dstConfig.APIHostInsecure, Equals, true)
+       c.Assert(dstConfig.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
+       c.Assert(dstConfig.APIToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
+       c.Assert(dstConfig.APIHostInsecure, Equals, matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")))
        c.Assert(dstConfig.ExternalClient, Equals, false)
 
        c.Assert(srcBlobSigningKey, Equals, "abcdefg")
@@ -402,9 +418,9 @@ func setupConfigFile(c *C, name string) *os.File {
        file, err := ioutil.TempFile(os.TempDir(), name)
        c.Check(err, IsNil)
 
-       fileContent := "ARVADOS_API_HOST=testhost\n"
-       fileContent += "ARVADOS_API_TOKEN=testtoken\n"
-       fileContent += "ARVADOS_API_HOST_INSECURE=true\n"
+       fileContent := "ARVADOS_API_HOST=" + os.Getenv("ARVADOS_API_HOST") + "\n"
+       fileContent += "ARVADOS_API_TOKEN=" + os.Getenv("ARVADOS_API_TOKEN") + "\n"
+       fileContent += "ARVADOS_API_HOST_INSECURE=" + os.Getenv("ARVADOS_API_HOST_INSECURE") + "\n"
        fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg"
 
        _, err = file.Write([]byte(fileContent))
@@ -412,3 +428,48 @@ func setupConfigFile(c *C, name string) *os.File {
 
        return file
 }
+
+func (s *DoMainTestSuite) Test_doMain_NoSrcConfig(c *C) {
+       err := doMain()
+       c.Check(err, NotNil)
+       c.Assert(err.Error(), Equals, "Error loading src configuration from file: config file not specified")
+}
+
+func (s *DoMainTestSuite) Test_doMain_SrcButNoDstConfig(c *C) {
+       srcConfig := setupConfigFile(c, "src")
+       args := []string{"-replications", "3", "-src", srcConfig.Name()}
+       os.Args = append(os.Args, args...)
+       err := doMain()
+       c.Check(err, NotNil)
+       c.Assert(err.Error(), Equals, "Error loading dst configuration from file: config file not specified")
+}
+
+func (s *DoMainTestSuite) Test_doMain_BadSrcConfig(c *C) {
+       args := []string{"-src", "abcd"}
+       os.Args = append(os.Args, args...)
+       err := doMain()
+       c.Check(err, NotNil)
+       c.Assert(strings.HasPrefix(err.Error(), "Error loading src configuration from file: Error reading config file"), Equals, true)
+}
+
+func (s *DoMainTestSuite) Test_doMain_WithReplicationsButNoSrcConfig(c *C) {
+       args := []string{"-replications", "3"}
+       os.Args = append(os.Args, args...)
+       err := doMain()
+       c.Check(err, NotNil)
+       c.Assert(err.Error(), Equals, "Error loading src configuration from file: config file not specified")
+}
+
+func (s *DoMainTestSuite) Test_doMainWithSrcAndDstConfig(c *C) {
+       srcConfig := setupConfigFile(c, "src")
+       dstConfig := setupConfigFile(c, "dst")
+       args := []string{"-src", srcConfig.Name(), "-dst", dstConfig.Name()}
+       os.Args = append(os.Args, args...)
+
+       // Start keepservers. Since we are not doing any tweaking as in setupRsync func,
+       // kcSrc and kcDst will be the same and no actual copying to dst will happen, but that's ok.
+       arvadostest.StartKeep()
+
+       err := doMain()
+       c.Check(err, IsNil)
+}