1
0
mirror of https://github.com/minio/mc.git synced 2025-11-12 01:02:26 +03:00

Fix test cases where cp and mirror would fail

- globalQuietFlag and globalJSONFlag should be checked in conjunction
  for terminal features to be disabled. Otherwise ioctl fails for
  calculating the term size.

- Avoid unnecessary scoping for probe, it has two different error sets now
This commit is contained in:
Harshavardhana
2015-08-12 22:11:29 -07:00
parent e2c4d25e86
commit 99f53fe45d
6 changed files with 65 additions and 54 deletions

View File

@@ -44,6 +44,10 @@ var server *httptest.Server
var app *cli.App
func (s *CmdTestSuite) SetUpSuite(c *C) {
objectAPI := objectAPIHandler(objectAPIHandler{lock: &sync.Mutex{}, bucket: "bucket", object: make(map[string][]byte)})
server = httptest.NewServer(objectAPI)
console.IsTesting = true
// do not set it elsewhere, leads to data races since this is a global flag
globalQuietFlag = true
@@ -86,9 +90,6 @@ func (s *CmdTestSuite) SetUpSuite(c *C) {
c.Assert(perr, 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) {

View File

@@ -81,7 +81,7 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue <-chan bool, wg *sync.WaitGro
return
}
if !globalQuietFlag || !globalJSONFlag {
if !globalQuietFlag && !globalJSONFlag {
bar.SetCaption(cpURLs.SourceContent.Name + ": ")
}
@@ -125,7 +125,7 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue <-chan bool, wg *sync.WaitGro
// doCopyFake - Perform a fake copy to update the progress bar appropriately.
func doCopyFake(cURLs copyURLs, bar *barSend) {
if !globalQuietFlag || !globalJSONFlag {
if !globalQuietFlag && !globalJSONFlag {
bar.Progress(cURLs.SourceContent.Size)
}
}
@@ -142,7 +142,12 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) {
// Create a session data file to store the processed URLs.
dataFP := session.NewDataWriter()
scanBar := scanBarFactory("")
var scanBar scanBarFunc
if !globalQuietFlag && !globalJSONFlag { // set up progress bar
scanBar = scanBarFactory("")
}
URLsCh := prepareCopyURLs(sourceURLs, targetURL)
done := false
@@ -154,6 +159,7 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) {
break
}
if cpURLs.Error != nil {
console.PrintC("Failed to prepare copy URLs, ")
errorIf(cpURLs.Error)
break
}
@@ -164,7 +170,9 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) {
console.Fatalf("Unable to marshal URLs to JSON. %s\n", err)
}
fmt.Fprintln(dataFP, string(jsonData))
if !globalQuietFlag && !globalJSONFlag {
scanBar(cpURLs.SourceContent.Name)
}
totalBytes += cpURLs.SourceContent.Size
totalObjects++
@@ -187,7 +195,7 @@ func doCopyCmdSession(session *sessionV2) {
}
var bar barSend
if !globalQuietFlag || !globalJSONFlag { // set up progress bar
if !globalQuietFlag && !globalJSONFlag { // set up progress bar
bar = newCpBar()
bar.Extend(session.Header.TotalBytes)
}
@@ -214,14 +222,16 @@ func doCopyCmdSession(session *sessionV2) {
select {
case cpURLs, ok := <-statusCh: // Receive status.
if !ok { // We are done here. Top level function has returned.
if !globalQuietFlag && !globalJSONFlag {
bar.Finish()
}
return
}
if cpURLs.Error == nil {
session.Header.LastCopied = cpURLs.SourceContent.Name
} else {
console.Println()
console.Printf("Failed to copy %s, ", cpURLs.SourceContent.Name)
console.PrintC(fmt.Sprintf("Failed to copy %s, ", cpURLs.SourceContent.Name))
errorIf(cpURLs.Error)
}
case <-trapCh: // Receive interrupt notification.
@@ -257,7 +267,6 @@ func doCopyCmdSession(session *sessionV2) {
}
copyWg.Wait()
}()
wg.Wait()
}
@@ -277,14 +286,12 @@ func mainCopy(ctx *cli.Context) {
}
// extract URLs.
{
var err *probe.Error
session.Header.CommandArgs, err = args2URLs(ctx.Args())
if err != nil {
var perr *probe.Error
session.Header.CommandArgs, perr = args2URLs(ctx.Args())
if perr != nil {
session.Close()
session.Delete()
console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), err)
}
console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), perr)
}
doCopyCmdSession(session)

View File

@@ -55,25 +55,18 @@ func (s *CmdTestSuite) TestCopyURLType(c *C) {
// TODO fix both copy and mirror
func (s *CmdTestSuite) TestCopyContext(c *C) {
err := app.Run([]string{os.Args[0], "cp", server.URL + "/bucket...", server.URL + "/bucket"})
err := app.Run([]string{os.Args[0], "cp", server.URL + "/invalid...", server.URL + "/bucket"})
c.Assert(err, IsNil)
c.Assert(console.IsExited, Equals, true)
err = app.Run([]string{os.Args[0], "cp", server.URL + "/invalid...", server.URL + "/bucket"})
c.Assert(err, IsNil)
c.Assert(console.IsExited, Equals, true)
c.Assert(console.IsError, Equals, true)
// reset back
console.IsExited = false
console.IsError = false
}
func (s *CmdTestSuite) TestMirrorContext(c *C) {
err := app.Run([]string{os.Args[0], "mirror", server.URL + "/bucket...", server.URL + "/bucket"})
err := app.Run([]string{os.Args[0], "mirror", server.URL + "/invalid...", server.URL + "/bucket"})
c.Assert(err, IsNil)
c.Assert(console.IsExited, Equals, true)
c.Assert(console.IsError, Equals, true)
err = app.Run([]string{os.Args[0], "mirror", server.URL + "/invalid...", server.URL + "/bucket"})
c.Assert(err, IsNil)
c.Assert(console.IsExited, Equals, true)
// reset back
console.IsExited = false
console.IsError = false
}

View File

@@ -67,7 +67,7 @@ func (s *CmdTestSuite) TestLSCmd(c *C) {
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)
c.Assert(console.IsError, Equals, false)
err = app.Run([]string{os.Args[0], "ls", server.URL + "/invalid"})
c.Assert(err, IsNil)

View File

@@ -78,13 +78,13 @@ func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *syn
return
}
if !globalQuietFlag || !globalJSONFlag {
if !globalQuietFlag && !globalJSONFlag {
bar.SetCaption(sURLs.SourceContent.Name + ": ")
}
reader, length, err := getSource(sURLs.SourceContent.Name)
if err != nil {
if !globalQuietFlag || !globalJSONFlag {
if !globalQuietFlag && !globalJSONFlag {
bar.ErrorGet(int64(length))
}
sURLs.Error = err.Trace()
@@ -112,7 +112,7 @@ func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *syn
err = putTargets(targetURLs, length, newReader)
if err != nil {
if !globalQuietFlag || !globalJSONFlag {
if !globalQuietFlag && !globalJSONFlag {
bar.ErrorPut(int64(length))
}
sURLs.Error = err.Trace()
@@ -141,7 +141,11 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) {
// Create a session data file to store the processed URLs.
dataFP := session.NewDataWriter()
scanBar := scanBarFactory("")
var scanBar scanBarFunc
if !globalQuietFlag && !globalJSONFlag { // set up progress bar
scanBar = scanBarFactory("")
}
URLsCh := prepareMirrorURLs(sourceURL, targetURLs)
done := false
for done == false {
@@ -152,6 +156,7 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) {
break
}
if sURLs.Error != nil {
console.PrintC("Failed to prepare mirror URLs, ")
errorIf(sURLs.Error)
break
}
@@ -164,7 +169,9 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) {
console.Fatalf("Unable to marshal URLs to JSON. %s\n", probe.NewError(err))
}
fmt.Fprintln(dataFP, string(jsonData))
if !globalQuietFlag && !globalJSONFlag {
scanBar(sURLs.SourceContent.Name)
}
totalBytes += sURLs.SourceContent.Size
totalObjects++
@@ -188,7 +195,7 @@ func doMirrorCmdSession(session *sessionV2) {
// Set up progress bar.
var bar barSend
if !globalQuietFlag || !globalJSONFlag {
if !globalQuietFlag && !globalJSONFlag {
bar = newCpBar()
bar.Extend(session.Header.TotalBytes)
}
@@ -214,14 +221,16 @@ func doMirrorCmdSession(session *sessionV2) {
select {
case sURLs, ok := <-statusCh: // Receive status.
if !ok { // We are done here. Top level function has returned.
if !globalQuietFlag && !globalJSONFlag {
bar.Finish()
}
return
}
if sURLs.Error == nil {
session.Header.LastCopied = sURLs.SourceContent.Name
} else {
console.Println()
console.Printf("Failed to mirror %s, ", sURLs.SourceContent.Name)
console.PrintC(fmt.Sprintf("Failed to mirror %s, ", sURLs.SourceContent.Name))
errorIf(sURLs.Error)
}
case <-trapCh: // Receive interrupt notification.
@@ -275,15 +284,13 @@ func mainMirror(ctx *cli.Context) {
console.Fatalf("Unable to get current working folder. %s\n", err)
}
{
// extract URLs.
var err *probe.Error
session.Header.CommandArgs, err = args2URLs(ctx.Args())
if err != nil {
var perr *probe.Error
session.Header.CommandArgs, perr = args2URLs(ctx.Args())
if perr != nil {
session.Close()
session.Delete()
console.Fatalf("One or more unknown URL types found in %s. %s\n", ctx.Args(), err)
}
console.Fatalf("One or more unknown URL types found in %s. %s\n", ctx.Args(), perr)
}
doMirrorCmdSession(session)

View File

@@ -35,9 +35,12 @@ 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)
// IsExited sets this boolean value if Fatal is called when IsTestng is enabled
var IsExited = false
// IsError sets this boolean value if Error is called when IsTesting is enabled
var IsError = false
// Theme holds console color scheme
type Theme struct {
Fatal *color.Color
@@ -139,7 +142,7 @@ var (
Error = func(data ...interface{}) {
if IsTesting {
defer func() {
IsExited = true
IsError = true
}()
}
print(themesDB[currThemeName].Error, data...)
@@ -150,7 +153,7 @@ var (
Errorf = func(f string, data ...interface{}) {
if IsTesting {
defer func() {
IsExited = true
IsError = true
}()
}
printf(themesDB[currThemeName].Error, f, data...)
@@ -161,7 +164,7 @@ var (
Errorln = func(data ...interface{}) {
if IsTesting {
defer func() {
IsExited = true
IsError = true
}()
}
println(themesDB[currThemeName].Error, data...)