From b90e0168923da41fcdd90b347f4d4ba6a0863591 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 24 Jul 2015 12:42:22 -0700 Subject: [PATCH] Add command context testing --- api_handlers_test.go | 12 +++++- cat_test.go | 13 ++++++ cmd_test.go | 18 ++++---- ls_test.go | 12 ++++++ main.go | 93 ++++++++++++++++++++++++------------------ mb_access_test.go | 25 ++++++++++++ pkg/console/console.go | 42 +++++++++++++++++-- 7 files changed, 161 insertions(+), 54 deletions(-) diff --git a/api_handlers_test.go b/api_handlers_test.go index 45dd800c..b9011896 100644 --- a/api_handlers_test.go +++ b/api_handlers_test.go @@ -89,7 +89,17 @@ func (h objectAPIHandler) putHandler(w http.ResponseWriter, r *http.Request) { case r.URL.Path == "/bucket": _, ok := r.URL.Query()["acl"] if ok { - if r.Header.Get("x-amz-acl") != "public-read-write" { + switch r.Header.Get("x-amz-acl") { + case "public-read-write": + fallthrough + case "public-read": + fallthrough + case "private": + fallthrough + case "authenticated-read": + w.WriteHeader(http.StatusOK) + return + default: w.WriteHeader(http.StatusNotImplemented) return } diff --git a/cat_test.go b/cat_test.go index 2b61125d..fdd73a79 100644 --- a/cat_test.go +++ b/cat_test.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" + "github.com/minio/mc/pkg/console" . "gopkg.in/check.v1" ) @@ -52,3 +53,15 @@ func (s *CmdTestSuite) TestCatCmd(c *C) { _, err = doCatCmd(objectPath) c.Assert(err, Not(IsNil)) } + +func (s *CmdTestSuite) TestCatContext(c *C) { + err := app.Run([]string{os.Args[0], "cat", server.URL + "/bucket/object1"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, false) + + err = app.Run([]string{os.Args[0], "cat", server.URL + "/invalid"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, true) + // reset back + console.IsExited = false +} diff --git a/cmd_test.go b/cmd_test.go index ae3254f3..4f82c9a9 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -26,6 +26,8 @@ import ( "net/http/httptest" + "github.com/minio/cli" + "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/quick" . "gopkg.in/check.v1" ) @@ -39,6 +41,7 @@ type CmdTestSuite struct{} var _ = Suite(&CmdTestSuite{}) var server *httptest.Server +var app *cli.App func (s *CmdTestSuite) SetUpSuite(c *C) { // do not set it elsewhere, leads to data races since this is a global flag @@ -76,8 +79,10 @@ func (s *CmdTestSuite) SetUpSuite(c *C) { _, err = doConfig("alias", []string{"readonly", "https://new.test.io"}) c.Assert(err, Not(IsNil)) + app = registerApp() objectAPI := objectAPIHandler(objectAPIHandler{lock: &sync.Mutex{}, bucket: "bucket", object: make(map[string][]byte)}) server = httptest.NewServer(objectAPI) + console.IsTesting = true } func (s *CmdTestSuite) TearDownSuite(c *C) { @@ -284,14 +289,7 @@ func (s *CmdTestSuite) TestEmptyExpansions(c *C) { c.Assert(err, IsNil) } -type testAddr struct{} - -func (ta *testAddr) Network() string { - return ta.String() -} -func (ta *testAddr) Error() string { - return ta.String() -} -func (ta *testAddr) String() string { - return "testAddr" +func (s *CmdTestSuite) TestApp(c *C) { + err := app.Run([]string{""}) + c.Assert(err, IsNil) } diff --git a/ls_test.go b/ls_test.go index d9e4341e..9111b82c 100644 --- a/ls_test.go +++ b/ls_test.go @@ -23,6 +23,7 @@ import ( "path/filepath" "strconv" + "github.com/minio/mc/pkg/console" . "gopkg.in/check.v1" ) @@ -58,5 +59,16 @@ func (s *CmdTestSuite) TestLSCmd(c *C) { err = doListCmd(server.URL+"/bucket", true) c.Assert(err, IsNil) +} +func (s *CmdTestSuite) TestLSContext(c *C) { + err := app.Run([]string{os.Args[0], "ls", server.URL + "/bucket"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, false) + + err = app.Run([]string{os.Args[0], "ls", server.URL + "/invalid"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, true) + // reset back + console.IsExited = false } diff --git a/main.go b/main.go index 51e729b2..fc4dfbbd 100644 --- a/main.go +++ b/main.go @@ -85,13 +85,40 @@ func migrate() { migrateSession() } -func main() { - // Migrate any old version of config / state files to newer format. - migrate() +func registerBefore(ctx *cli.Context) error { + if ctx.GlobalString("config") != "" { + setMcConfigDir(ctx.GlobalString("config")) + } - // Enable GOMAXPROCS to default to number of CPUs. - runtime.GOMAXPROCS(runtime.NumCPU()) + globalQuietFlag = ctx.GlobalBool("quiet") + globalForceFlag = ctx.GlobalBool("force") + globalAliasFlag = ctx.GlobalBool("alias") + globalDebugFlag = ctx.GlobalBool("debug") + globalJSONFlag = ctx.GlobalBool("json") + themeName := ctx.GlobalString("theme") + switch { + case console.IsValidTheme(themeName) != true: + console.Errorf("Invalid theme, please choose from the following list: %s.\n", console.GetThemeNames()) + return errInvalidTheme{Theme: themeName} + default: + err := console.SetTheme(themeName) + if err != nil { + console.Errorf("Failed to set theme ‘%s’.", themeName) + return err + } + } + checkConfig() + return nil +} +func registerAfter(ctx *cli.Context) error { + if !isMcConfigExists() { + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) + } + return nil +} + +func registerApp() *cli.App { // Register all the commands registerCmd(lsCmd) // List contents of a bucket registerCmd(mbCmd) // make a bucket @@ -120,41 +147,6 @@ func main() { app.Compiled = getVersion() app.Flags = flags app.Author = "Minio.io" - app.Before = func(ctx *cli.Context) error { - if ctx.GlobalString("config") != "" { - setMcConfigDir(ctx.GlobalString("config")) - } - - globalQuietFlag = ctx.GlobalBool("quiet") - globalForceFlag = ctx.GlobalBool("force") - globalAliasFlag = ctx.GlobalBool("alias") - globalDebugFlag = ctx.GlobalBool("debug") - globalJSONFlag = ctx.GlobalBool("json") - if globalDebugFlag { - app.ExtraInfo = getSystemData() - console.NoDebugPrint = false - } - themeName := ctx.GlobalString("theme") - switch { - case console.IsValidTheme(themeName) != true: - console.Errorf("Invalid theme, please choose from the following list: %s.\n", console.GetThemeNames()) - return errInvalidTheme{Theme: themeName} - default: - err := console.SetTheme(themeName) - if err != nil { - console.Errorf("Failed to set theme ‘%s’.", themeName) - return err - } - } - checkConfig() - return nil - } - app.After = func(ctx *cli.Context) error { - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } - return nil - } app.CustomAppHelpTemplate = `NAME: {{.Name}} - {{.Usage}} @@ -175,5 +167,26 @@ VERSION: {{$value}} {{end}} ` + app.CommandNotFound = func(ctx *cli.Context, command string) { + console.Fatalf("Command not found: ‘%s’\n", command) + } + return app +} + +func main() { + // Enable GOMAXPROCS to default to number of CPUs. + runtime.GOMAXPROCS(runtime.NumCPU()) + + // Migrate any old version of config / state files to newer format. + migrate() + + app := registerApp() + app.Before = registerBefore + app.After = registerAfter + if globalDebugFlag { + app.ExtraInfo = getSystemData() + console.NoDebugPrint = false + } + app.RunAndExitOnError() } diff --git a/mb_access_test.go b/mb_access_test.go index e6934cc2..e04d2383 100644 --- a/mb_access_test.go +++ b/mb_access_test.go @@ -21,6 +21,7 @@ import ( "os" "path/filepath" + "github.com/minio/mc/pkg/console" . "gopkg.in/check.v1" ) @@ -49,3 +50,27 @@ func (s *CmdTestSuite) TestMbAndAccessCmd(c *C) { c.Assert(err, Not(IsNil)) } + +func (s *CmdTestSuite) TestAccessContext(c *C) { + err := app.Run([]string{os.Args[0], "access", "private", server.URL + "/bucket"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, false) + + err = app.Run([]string{os.Args[0], "access", "public", server.URL + "/bucket"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, false) + + err = app.Run([]string{os.Args[0], "access", "readonly", server.URL + "/bucket"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, false) + + err = app.Run([]string{os.Args[0], "access", "authenticated", server.URL + "/bucket"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, false) + + err = app.Run([]string{os.Args[0], "access", "invalid", server.URL + "/bucket"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, true) + // reset back + console.IsExited = false +} diff --git a/pkg/console/console.go b/pkg/console/console.go index 86b1d944..247660e2 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -31,6 +31,12 @@ import ( // NoDebugPrint defines if the input should be printed in debug or not. By default it's set to true. var NoDebugPrint = true +// IsTesting this flag indicates if IsExited should be set or not, false by default +var IsTesting = false + +// IsExited sets this boolean value if Fatal is called instead of os.Exit(1) +var IsExited = false + // Theme holds console color scheme type Theme struct { Fatal *color.Color @@ -102,39 +108,69 @@ var ( // Fatal print a error message and exit Fatal = func(data ...interface{}) { - defer os.Exit(1) print(themesDB[currThemeName].Fatal, data...) + if !IsTesting { + os.Exit(1) + } + defer func() { + IsExited = true + }() return } // Fatalf print a error message with a format specified and exit Fatalf = func(f string, data ...interface{}) { - defer os.Exit(1) printf(themesDB[currThemeName].Fatal, f, data...) + if !IsTesting { + os.Exit(1) + } + defer func() { + IsExited = true + }() return } // Fatalln print a error message with a new line and exit Fatalln = func(data ...interface{}) { - defer os.Exit(1) println(themesDB[currThemeName].Fatal, data...) + if !IsTesting { + os.Exit(1) + } + defer func() { + IsExited = true + }() return } // Error prints a error message Error = func(data ...interface{}) { + if IsTesting { + defer func() { + IsExited = true + }() + } print(themesDB[currThemeName].Error, data...) return } // Errorf print a error message with a format specified Errorf = func(f string, data ...interface{}) { + if IsTesting { + defer func() { + IsExited = true + }() + } printf(themesDB[currThemeName].Error, f, data...) return } // Errorln prints a error message with a new line Errorln = func(data ...interface{}) { + if IsTesting { + defer func() { + IsExited = true + }() + } println(themesDB[currThemeName].Error, data...) return }