Add command context testing

This commit is contained in:
Harshavardhana
2015-07-24 12:42:33 -07:00
parent 4ab2f637a4
commit b90e016892
7 changed files with 161 additions and 54 deletions
+11 -1
View File
@@ -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
}
+13
View File
@@ -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
}
+8 -10
View File
@@ -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)
}
+12
View File
@@ -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
}
+53 -40
View File
@@ -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()
}
+25
View File
@@ -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
}
+39 -3
View File
@@ -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
}