From 38354fdf55e0895378f87e6d6bff62b7eb377a25 Mon Sep 17 00:00:00 2001 From: radhika Date: Mon, 12 Oct 2015 10:40:38 -0400 Subject: [PATCH] 7167: break load config logic out of main into loadConfig func and add several tests. --- tools/keep-rsync/keep-rsync.go | 48 ++++++++---- tools/keep-rsync/keep-rsync_test.go | 117 ++++++++++++++++++++++------ 2 files changed, 124 insertions(+), 41 deletions(-) diff --git a/tools/keep-rsync/keep-rsync.go b/tools/keep-rsync/keep-rsync.go index d619ff3ce2..de28b3347c 100644 --- a/tools/keep-rsync/keep-rsync.go +++ b/tools/keep-rsync/keep-rsync.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "errors" "flag" "git.curoverse.com/arvados.git/sdk/go/arvadosclient" "git.curoverse.com/arvados.git/sdk/go/keepclient" @@ -23,10 +24,10 @@ var ( prefix string ) -func main() { - var srcConfigFile string - var dstConfigFile string +var srcConfigFile string +var dstConfigFile string +func main() { flag.StringVar( &srcConfigFile, "src-config-file", @@ -73,21 +74,9 @@ func main() { var err error - // Load config - if srcConfigFile == "" { - log.Fatal("-src-config-file must be specified.") - } - srcConfig, err = readConfigFromFile(srcConfigFile) + err = loadConfig() if err != nil { - log.Fatal("Error reading source configuration: %s", err.Error()) - } - - if dstConfigFile == "" { - log.Fatal("-dst-config-file must be specified.") - } - dstConfig, err = readConfigFromFile(dstConfigFile) - if err != nil { - log.Fatal("Error reading destination configuration: %s", err.Error()) + log.Fatal("Error loading configuration from files: %s", err.Error()) } // Initialize keep-rsync @@ -103,6 +92,31 @@ func main() { } } +// Load src and dst config from given files +func loadConfig() error { + if srcConfigFile == "" { + return errors.New("-src-config-file must be specified") + } + + var err error + + srcConfig, err = readConfigFromFile(srcConfigFile) + if err != nil { + log.Printf("Error reading source configuration: %s", err.Error()) + return err + } + + if dstConfigFile == "" { + return errors.New("-dst-config-file must be specified") + } + dstConfig, err = readConfigFromFile(dstConfigFile) + if err != nil { + log.Printf("Error reading destination configuration: %s", err.Error()) + } + + return err +} + var matchTrue = regexp.MustCompile("^(?i:1|yes|true)$") // Reads config from file diff --git a/tools/keep-rsync/keep-rsync_test.go b/tools/keep-rsync/keep-rsync_test.go index 3ea1b95776..0696707818 100644 --- a/tools/keep-rsync/keep-rsync_test.go +++ b/tools/keep-rsync/keep-rsync_test.go @@ -41,6 +41,8 @@ func (s *ServerRequiredSuite) SetUpTest(c *C) { dstKeepServicesJSON = "" replications = 0 prefix = "" + srcConfigFile = "" + dstConfigFile = "" arvSrc = arvadosclient.ArvadosClient{} arvDst = arvadosclient.ArvadosClient{} kcSrc = &keepclient.KeepClient{} @@ -93,30 +95,6 @@ func setupRsync(c *C, enforcePermissions bool, setupDstServers bool) { } } -// Test readConfigFromFile method -func (s *ServerRequiredSuite) TestReadConfigFromFile(c *C) { - // Setup a test config file - file, err := ioutil.TempFile(os.TempDir(), "config") - c.Check(err, IsNil) - defer os.Remove(file.Name()) - - fileContent := "ARVADOS_API_HOST=testhost\n" - fileContent += "ARVADOS_API_TOKEN=testtoken\n" - fileContent += "ARVADOS_API_HOST_INSECURE=true\n" - fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg" - - _, err = file.Write([]byte(fileContent)) - - // Invoke readConfigFromFile method with this test filename - config, err := readConfigFromFile(file.Name()) - c.Check(err, IsNil) - c.Assert(config.APIHost, Equals, "testhost") - c.Assert(config.APIToken, Equals, "testtoken") - c.Assert(config.APIHostInsecure, Equals, true) - c.Assert(config.ExternalClient, Equals, false) - c.Assert(blobSigningKey, Equals, "abcdefg") -} - // Test keep-rsync initialization, with src and dst keep servers. // Do a Put and Get in src, both of which should succeed. // Do a Put and Get in dst, both of which should succeed. @@ -476,3 +454,94 @@ func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorPuttingBlockInDst(c *C) err := performKeepRsync() c.Check(err.Error(), Equals, "Could not write sufficient replicas") } + +// Test loadConfig func +func (s *ServerRequiredSuite) TestLoadConfig(c *C) { + // Setup a src config file + srcFile := setupConfigFile(c, "src-config") + defer os.Remove(srcFile.Name()) + srcConfigFile = srcFile.Name() + + // Setup a dst config file + dstFile := setupConfigFile(c, "dst-config") + defer os.Remove(dstFile.Name()) + dstConfigFile = dstFile.Name() + + // load configuration from those files + err := loadConfig() + 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.ExternalClient, Equals, false) + + c.Assert(dstConfig.APIHost, Equals, "testhost") + c.Assert(dstConfig.APIToken, Equals, "testtoken") + c.Assert(dstConfig.APIHostInsecure, Equals, true) + c.Assert(dstConfig.ExternalClient, Equals, false) + + c.Assert(blobSigningKey, Equals, "abcdefg") +} + +// Test loadConfig func without setting up the config files +func (s *ServerRequiredSuite) TestLoadConfig_MissingSrcConfig(c *C) { + srcConfigFile = "" + err := loadConfig() + c.Assert(err.Error(), Equals, "-src-config-file must be specified") +} + +// Test loadConfig func - error reading src config +func (s *ServerRequiredSuite) TestLoadConfig_ErrorLoadingSrcConfig(c *C) { + srcConfigFile = "no-such-config-file" + + // load configuration + err := loadConfig() + c.Assert(strings.HasSuffix(err.Error(), "no such file or directory"), Equals, true) +} + +// Test loadConfig func with no dst config file specified +func (s *ServerRequiredSuite) TestLoadConfig_MissingDstConfig(c *C) { + // Setup a src config file + srcFile := setupConfigFile(c, "src-config") + defer os.Remove(srcFile.Name()) + srcConfigFile = srcFile.Name() + + // But do not setup the dst config file + dstConfigFile = "" + + // load configuration + err := loadConfig() + c.Assert(err.Error(), Equals, "-dst-config-file must be specified") +} + +// Test loadConfig func +func (s *ServerRequiredSuite) TestLoadConfig_ErrorLoadingDstConfig(c *C) { + // Setup a src config file + srcFile := setupConfigFile(c, "src-config") + defer os.Remove(srcFile.Name()) + srcConfigFile = srcFile.Name() + + // But do not setup the dst config file + dstConfigFile = "no-such-config-file" + + // load configuration + err := loadConfig() + c.Assert(strings.HasSuffix(err.Error(), "no such file or directory"), Equals, true) +} + +func setupConfigFile(c *C, name string) *os.File { + // Setup a config 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_BLOB_SIGNING_KEY=abcdefg" + + _, err = file.Write([]byte(fileContent)) + c.Check(err, IsNil) + + return file +} -- 2.39.5