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

View File

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

View File

@@ -78,13 +78,13 @@ func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *syn
return return
} }
if !globalQuietFlag || !globalJSONFlag { if !globalQuietFlag && !globalJSONFlag {
bar.SetCaption(sURLs.SourceContent.Name + ": ") bar.SetCaption(sURLs.SourceContent.Name + ": ")
} }
reader, length, err := getSource(sURLs.SourceContent.Name) reader, length, err := getSource(sURLs.SourceContent.Name)
if err != nil { if err != nil {
if !globalQuietFlag || !globalJSONFlag { if !globalQuietFlag && !globalJSONFlag {
bar.ErrorGet(int64(length)) bar.ErrorGet(int64(length))
} }
sURLs.Error = err.Trace() sURLs.Error = err.Trace()
@@ -112,7 +112,7 @@ func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *syn
err = putTargets(targetURLs, length, newReader) err = putTargets(targetURLs, length, newReader)
if err != nil { if err != nil {
if !globalQuietFlag || !globalJSONFlag { if !globalQuietFlag && !globalJSONFlag {
bar.ErrorPut(int64(length)) bar.ErrorPut(int64(length))
} }
sURLs.Error = err.Trace() 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. // Create a session data file to store the processed URLs.
dataFP := session.NewDataWriter() dataFP := session.NewDataWriter()
scanBar := scanBarFactory("") var scanBar scanBarFunc
if !globalQuietFlag && !globalJSONFlag { // set up progress bar
scanBar = scanBarFactory("")
}
URLsCh := prepareMirrorURLs(sourceURL, targetURLs) URLsCh := prepareMirrorURLs(sourceURL, targetURLs)
done := false done := false
for done == false { for done == false {
@@ -152,6 +156,7 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) {
break break
} }
if sURLs.Error != nil { if sURLs.Error != nil {
console.PrintC("Failed to prepare mirror URLs, ")
errorIf(sURLs.Error) errorIf(sURLs.Error)
break 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)) console.Fatalf("Unable to marshal URLs to JSON. %s\n", probe.NewError(err))
} }
fmt.Fprintln(dataFP, string(jsonData)) fmt.Fprintln(dataFP, string(jsonData))
scanBar(sURLs.SourceContent.Name) if !globalQuietFlag && !globalJSONFlag {
scanBar(sURLs.SourceContent.Name)
}
totalBytes += sURLs.SourceContent.Size totalBytes += sURLs.SourceContent.Size
totalObjects++ totalObjects++
@@ -188,7 +195,7 @@ func doMirrorCmdSession(session *sessionV2) {
// Set up progress bar. // Set up progress bar.
var bar barSend var bar barSend
if !globalQuietFlag || !globalJSONFlag { if !globalQuietFlag && !globalJSONFlag {
bar = newCpBar() bar = newCpBar()
bar.Extend(session.Header.TotalBytes) bar.Extend(session.Header.TotalBytes)
} }
@@ -214,14 +221,16 @@ func doMirrorCmdSession(session *sessionV2) {
select { select {
case sURLs, ok := <-statusCh: // Receive status. case sURLs, ok := <-statusCh: // Receive status.
if !ok { // We are done here. Top level function has returned. if !ok { // We are done here. Top level function has returned.
bar.Finish() if !globalQuietFlag && !globalJSONFlag {
bar.Finish()
}
return return
} }
if sURLs.Error == nil { if sURLs.Error == nil {
session.Header.LastCopied = sURLs.SourceContent.Name session.Header.LastCopied = sURLs.SourceContent.Name
} else { } else {
console.Println() 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) errorIf(sURLs.Error)
} }
case <-trapCh: // Receive interrupt notification. 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) console.Fatalf("Unable to get current working folder. %s\n", err)
} }
{ // extract URLs.
// extract URLs. var perr *probe.Error
var err *probe.Error session.Header.CommandArgs, perr = args2URLs(ctx.Args())
session.Header.CommandArgs, err = args2URLs(ctx.Args()) if perr != nil {
if err != nil { session.Close()
session.Close() session.Delete()
session.Delete() console.Fatalf("One or more unknown URL types found in %s. %s\n", ctx.Args(), perr)
console.Fatalf("One or more unknown URL types found in %s. %s\n", ctx.Args(), err)
}
} }
doMirrorCmdSession(session) 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 // IsTesting this flag indicates if IsExited should be set or not, false by default
var IsTesting = false 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 var IsExited = false
// IsError sets this boolean value if Error is called when IsTesting is enabled
var IsError = false
// Theme holds console color scheme // Theme holds console color scheme
type Theme struct { type Theme struct {
Fatal *color.Color Fatal *color.Color
@@ -139,7 +142,7 @@ var (
Error = func(data ...interface{}) { Error = func(data ...interface{}) {
if IsTesting { if IsTesting {
defer func() { defer func() {
IsExited = true IsError = true
}() }()
} }
print(themesDB[currThemeName].Error, data...) print(themesDB[currThemeName].Error, data...)
@@ -150,7 +153,7 @@ var (
Errorf = func(f string, data ...interface{}) { Errorf = func(f string, data ...interface{}) {
if IsTesting { if IsTesting {
defer func() { defer func() {
IsExited = true IsError = true
}() }()
} }
printf(themesDB[currThemeName].Error, f, data...) printf(themesDB[currThemeName].Error, f, data...)
@@ -161,7 +164,7 @@ var (
Errorln = func(data ...interface{}) { Errorln = func(data ...interface{}) {
if IsTesting { if IsTesting {
defer func() { defer func() {
IsExited = true IsError = true
}() }()
} }
println(themesDB[currThemeName].Error, data...) println(themesDB[currThemeName].Error, data...)