| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2016 Minio, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | // Fixed volume name that could be used across tests
 | 
					
						
							|  |  |  | const volume = "testvolume" | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | // Test for delayIsLeafCheck.
 | 
					
						
							|  |  |  | func TestDelayIsLeafCheck(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		entries []string | 
					
						
							|  |  |  | 		delay   bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// Test cases where isLeaf check can't be delayed.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"a-b/", "a/"}, | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"a%b/", "a/"}, | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"a-b-c", "a-b/"}, | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Test cases where isLeaf check can be delayed.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"a-b/", "aa/"}, | 
					
						
							|  |  |  | 			true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"a", "a-b"}, | 
					
						
							|  |  |  | 			true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"aaa", "bbb"}, | 
					
						
							|  |  |  | 			true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		expected := testCase.delay | 
					
						
							|  |  |  | 		got := delayIsLeafCheck(testCase.entries) | 
					
						
							|  |  |  | 		if expected != got { | 
					
						
							|  |  |  | 			t.Errorf("Test %d : Expected %t got %t", i+1, expected, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test for filterMatchingPrefix.
 | 
					
						
							|  |  |  | func TestFilterMatchingPrefix(t *testing.T) { | 
					
						
							|  |  |  | 	entries := []string{"a", "aab", "ab", "abbbb", "zzz"} | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		prefixEntry string | 
					
						
							|  |  |  | 		result      []string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Empty prefix should match all entries.
 | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			[]string{"a", "aab", "ab", "abbbb", "zzz"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"a", | 
					
						
							|  |  |  | 			[]string{"a", "aab", "ab", "abbbb"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"aa", | 
					
						
							|  |  |  | 			[]string{"aab"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Does not match any of the entries.
 | 
					
						
							|  |  |  | 			"c", | 
					
						
							|  |  |  | 			[]string{}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		expected := testCase.result | 
					
						
							|  |  |  | 		got := filterMatchingPrefix(entries, testCase.prefixEntry) | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(expected, got) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d : expected %v, got %v", i+1, expected, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | // Helper function that creates a volume and files in it.
 | 
					
						
							|  |  |  | func createNamespace(disk StorageAPI, volume string, files []string) error { | 
					
						
							|  |  |  | 	// Make a volume.
 | 
					
						
							|  |  |  | 	err := disk.MakeVol(volume) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// Create files.
 | 
					
						
							|  |  |  | 	for _, file := range files { | 
					
						
							|  |  |  | 		err = disk.AppendFile(volume, file, []byte{}) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test if tree walker returns entries matching prefix alone are received
 | 
					
						
							|  |  |  | // when a non empty prefix is supplied.
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | func testTreeWalkPrefix(t *testing.T, listDir listDirFunc, isLeaf isLeafFunc) { | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	// Start the tree walk go-routine.
 | 
					
						
							|  |  |  | 	prefix := "d/" | 
					
						
							|  |  |  | 	endWalkCh := make(chan struct{}) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	twResultCh := startTreeWalk(volume, prefix, "", true, listDir, isLeaf, endWalkCh) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check if all entries received on the channel match the prefix.
 | 
					
						
							|  |  |  | 	for res := range twResultCh { | 
					
						
							|  |  |  | 		if !strings.HasPrefix(res.entry, prefix) { | 
					
						
							|  |  |  | 			t.Errorf("Entry %s doesn't match prefix %s", res.entry, prefix) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test if entries received on tree walk's channel appear after the supplied marker.
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | func testTreeWalkMarker(t *testing.T, listDir listDirFunc, isLeaf isLeafFunc) { | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	// Start the tree walk go-routine.
 | 
					
						
							|  |  |  | 	prefix := "" | 
					
						
							|  |  |  | 	endWalkCh := make(chan struct{}) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	twResultCh := startTreeWalk(volume, prefix, "d/g", true, listDir, isLeaf, endWalkCh) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check if only 3 entries, namely d/g/h, i/j/k, lmn are received on the channel.
 | 
					
						
							|  |  |  | 	expectedCount := 3 | 
					
						
							|  |  |  | 	actualCount := 0 | 
					
						
							|  |  |  | 	for range twResultCh { | 
					
						
							|  |  |  | 		actualCount++ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if expectedCount != actualCount { | 
					
						
							|  |  |  | 		t.Errorf("Expected %d entries, actual no. of entries were %d", expectedCount, actualCount) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | // Test tree-walk.
 | 
					
						
							|  |  |  | func TestTreeWalk(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-12-16 14:25:05 +08:00
										 |  |  | 	fsDir, err := ioutil.TempDir(globalTestTmpDir, "minio-") | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		t.Fatalf("Unable to create tmp directory: %s", err) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints([]string{fsDir}) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		t.Fatalf("Unexpected error %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	disk, err := newStorageAPI(endpoints[0]) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unable to create StorageAPI: %s", err) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	var files = []string{ | 
					
						
							|  |  |  | 		"d/e", | 
					
						
							|  |  |  | 		"d/f", | 
					
						
							|  |  |  | 		"d/g/h", | 
					
						
							|  |  |  | 		"i/j/k", | 
					
						
							|  |  |  | 		"lmn", | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	err = createNamespace(disk, volume, files) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	isLeaf := func(volume, prefix string) bool { | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 		return !strings.HasSuffix(prefix, slashSeparator) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-16 04:43:40 +08:00
										 |  |  | 	listDir := listDirFactory(isLeaf, xlTreeWalkIgnoredErrs, disk) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// Simple test for prefix based walk.
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	testTreeWalkPrefix(t, listDir, isLeaf) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// Simple test when marker is set.
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	testTreeWalkMarker(t, listDir, isLeaf) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	err = removeAll(fsDir) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | // Test if tree walk go-routine exits cleanly if tree walk is aborted because of timeout.
 | 
					
						
							|  |  |  | func TestTreeWalkTimeout(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-12-16 14:25:05 +08:00
										 |  |  | 	fsDir, err := ioutil.TempDir(globalTestTmpDir, "minio-") | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		t.Fatalf("Unable to create tmp directory: %s", err) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints([]string{fsDir}) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		t.Fatalf("Unexpected error %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	disk, err := newStorageAPI(endpoints[0]) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unable to create StorageAPI: %s", err) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	var myfiles []string | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// Create maxObjectsList+1 number of entries.
 | 
					
						
							|  |  |  | 	for i := 0; i < maxObjectList+1; i++ { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 		myfiles = append(myfiles, fmt.Sprintf("file.%d", i)) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	err = createNamespace(disk, volume, myfiles) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	isLeaf := func(volume, prefix string) bool { | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 		return !strings.HasSuffix(prefix, slashSeparator) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-16 04:43:40 +08:00
										 |  |  | 	listDir := listDirFactory(isLeaf, xlTreeWalkIgnoredErrs, disk) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// TreeWalk pool with 2 seconds timeout for tree-walk go routines.
 | 
					
						
							|  |  |  | 	pool := newTreeWalkPool(2 * time.Second) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	endWalkCh := make(chan struct{}) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	prefix := "" | 
					
						
							|  |  |  | 	marker := "" | 
					
						
							|  |  |  | 	recursive := true | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	resultCh := startTreeWalk(volume, prefix, marker, recursive, listDir, isLeaf, endWalkCh) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	params := listParams{ | 
					
						
							|  |  |  | 		bucket:    volume, | 
					
						
							|  |  |  | 		recursive: recursive, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Add Treewalk to the pool.
 | 
					
						
							|  |  |  | 	pool.Set(params, resultCh, endWalkCh) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Wait for the Treewalk to timeout.
 | 
					
						
							|  |  |  | 	<-time.After(3 * time.Second) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Read maxObjectList number of entries from the channel.
 | 
					
						
							|  |  |  | 	// maxObjectsList number of entries would have been filled into the resultCh
 | 
					
						
							|  |  |  | 	// buffered channel. After the timeout resultCh would get closed and hence the
 | 
					
						
							|  |  |  | 	// maxObjectsList+1 entry would not be sent in the channel.
 | 
					
						
							|  |  |  | 	i := 0 | 
					
						
							|  |  |  | 	for range resultCh { | 
					
						
							|  |  |  | 		i++ | 
					
						
							|  |  |  | 		if i == maxObjectList { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// The last entry will not be received as the Treewalk goroutine would have exited.
 | 
					
						
							|  |  |  | 	_, ok := <-resultCh | 
					
						
							|  |  |  | 	if ok { | 
					
						
							|  |  |  | 		t.Error("Tree-walk go routine has not exited after timeout.") | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	err = removeAll(fsDir) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | // Test ListDir - listDir should list entries from the first disk, if the first disk is down,
 | 
					
						
							|  |  |  | // it should list from the next disk.
 | 
					
						
							|  |  |  | func TestListDir(t *testing.T) { | 
					
						
							|  |  |  | 	file1 := "file1" | 
					
						
							|  |  |  | 	file2 := "file2" | 
					
						
							|  |  |  | 	// Create two backend directories fsDir1 and fsDir2.
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:25:05 +08:00
										 |  |  | 	fsDir1, err := ioutil.TempDir(globalTestTmpDir, "minio-") | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to create tmp directory: %s", err) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-16 14:25:05 +08:00
										 |  |  | 	fsDir2, err := ioutil.TempDir(globalTestTmpDir, "minio-") | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to create tmp directory: %s", err) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints([]string{fsDir1, fsDir2}) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected error %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// Create two StorageAPIs disk1 and disk2.
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	disk1, err := newStorageAPI(endpoints[0]) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 		t.Errorf("Unable to create StorageAPI: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	disk2, err := newStorageAPI(endpoints[1]) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to create StorageAPI: %s", err) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// create listDir function.
 | 
					
						
							|  |  |  | 	listDir := listDirFactory(func(volume, prefix string) bool { | 
					
						
							|  |  |  | 		return !strings.HasSuffix(prefix, slashSeparator) | 
					
						
							| 
									
										
										
										
											2016-09-16 04:43:40 +08:00
										 |  |  | 	}, xlTreeWalkIgnoredErrs, disk1, disk2) | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create file1 in fsDir1 and file2 in fsDir2.
 | 
					
						
							|  |  |  | 	disks := []StorageAPI{disk1, disk2} | 
					
						
							|  |  |  | 	for i, disk := range disks { | 
					
						
							|  |  |  | 		err = createNamespace(disk, volume, []string{fmt.Sprintf("file%d", i+1)}) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// Should list "file1" from fsDir1.
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	entries, _, err := listDir(volume, "", "") | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(entries) != 1 { | 
					
						
							|  |  |  | 		t.Fatal("Expected the number of entries to be 1") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if entries[0] != file1 { | 
					
						
							|  |  |  | 		t.Fatal("Expected the entry to be file1") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// Remove fsDir1 to test failover.
 | 
					
						
							|  |  |  | 	err = removeAll(fsDir1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	// Should list "file2" from fsDir2.
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	entries, _, err = listDir(volume, "", "") | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(entries) != 1 { | 
					
						
							|  |  |  | 		t.Fatal("Expected the number of entries to be 1") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if entries[0] != file2 { | 
					
						
							|  |  |  | 		t.Fatal("Expected the entry to be file2") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = removeAll(fsDir2) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// None of the disks are available, should get errDiskNotFound.
 | 
					
						
							| 
									
										
										
										
											2016-08-12 15:26:30 +08:00
										 |  |  | 	_, _, err = listDir(volume, "", "") | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 	if errorCause(err) != errDiskNotFound { | 
					
						
							| 
									
										
										
										
											2016-07-04 16:49:27 +08:00
										 |  |  | 		t.Error("expected errDiskNotFound error.") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-29 13:32:00 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TestRecursiveWalk - tests if treeWalk returns entries correctly with and
 | 
					
						
							|  |  |  | // without recursively traversing prefixes.
 | 
					
						
							|  |  |  | func TestRecursiveTreeWalk(t *testing.T) { | 
					
						
							|  |  |  | 	// Create a backend directories fsDir1.
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:25:05 +08:00
										 |  |  | 	fsDir1, err := ioutil.TempDir(globalTestTmpDir, "minio-") | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		t.Fatalf("Unable to create tmp directory: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints([]string{fsDir1}) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected error %s", err) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	disk1, err := newStorageAPI(endpoints[0]) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		t.Fatalf("Unable to create StorageAPI: %s", err) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	// Simple isLeaf check, returns true if there is no trailing "/"
 | 
					
						
							|  |  |  | 	isLeaf := func(volume, prefix string) bool { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 		return !strings.HasSuffix(prefix, slashSeparator) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create listDir function.
 | 
					
						
							| 
									
										
										
										
											2016-09-16 04:43:40 +08:00
										 |  |  | 	listDir := listDirFactory(isLeaf, xlTreeWalkIgnoredErrs, disk1) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create the namespace.
 | 
					
						
							|  |  |  | 	var files = []string{ | 
					
						
							|  |  |  | 		"d/e", | 
					
						
							|  |  |  | 		"d/f", | 
					
						
							|  |  |  | 		"d/g/h", | 
					
						
							|  |  |  | 		"i/j/k", | 
					
						
							|  |  |  | 		"lmn", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = createNamespace(disk1, volume, files) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	endWalkCh := make(chan struct{}) | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		prefix    string | 
					
						
							|  |  |  | 		marker    string | 
					
						
							|  |  |  | 		recursive bool | 
					
						
							|  |  |  | 		expected  map[string]struct{} | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// with no prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"", "", false, map[string]struct{}{ | 
					
						
							|  |  |  | 			"d/":  {}, | 
					
						
							|  |  |  | 			"i/":  {}, | 
					
						
							|  |  |  | 			"lmn": {}, | 
					
						
							|  |  |  | 		}}, | 
					
						
							|  |  |  | 		// with no prefix, no marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"", "", true, map[string]struct{}{ | 
					
						
							|  |  |  | 			"d/f":   {}, | 
					
						
							|  |  |  | 			"d/g/h": {}, | 
					
						
							|  |  |  | 			"d/e":   {}, | 
					
						
							|  |  |  | 			"i/j/k": {}, | 
					
						
							|  |  |  | 			"lmn":   {}, | 
					
						
							|  |  |  | 		}}, | 
					
						
							|  |  |  | 		// with no prefix, marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"", "d/e", false, map[string]struct{}{ | 
					
						
							|  |  |  | 			"d/f":  {}, | 
					
						
							|  |  |  | 			"d/g/": {}, | 
					
						
							|  |  |  | 			"i/":   {}, | 
					
						
							|  |  |  | 			"lmn":  {}, | 
					
						
							|  |  |  | 		}}, | 
					
						
							|  |  |  | 		// with no prefix, marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"", "d/e", true, map[string]struct{}{ | 
					
						
							|  |  |  | 			"d/f":   {}, | 
					
						
							|  |  |  | 			"d/g/h": {}, | 
					
						
							|  |  |  | 			"i/j/k": {}, | 
					
						
							|  |  |  | 			"lmn":   {}, | 
					
						
							|  |  |  | 		}}, | 
					
						
							|  |  |  | 		// with prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "", false, map[string]struct{}{ | 
					
						
							|  |  |  | 			"d/e":  {}, | 
					
						
							|  |  |  | 			"d/f":  {}, | 
					
						
							|  |  |  | 			"d/g/": {}, | 
					
						
							|  |  |  | 		}}, | 
					
						
							|  |  |  | 		// with prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "", true, map[string]struct{}{ | 
					
						
							|  |  |  | 			"d/e":   {}, | 
					
						
							|  |  |  | 			"d/f":   {}, | 
					
						
							|  |  |  | 			"d/g/h": {}, | 
					
						
							|  |  |  | 		}}, | 
					
						
							|  |  |  | 		// with prefix, marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "d/e", false, map[string]struct{}{ | 
					
						
							|  |  |  | 			"d/f":  {}, | 
					
						
							|  |  |  | 			"d/g/": {}, | 
					
						
							|  |  |  | 		}}, | 
					
						
							|  |  |  | 		// with prefix, marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "d/e", true, map[string]struct{}{ | 
					
						
							|  |  |  | 			"d/f":   {}, | 
					
						
							|  |  |  | 			"d/g/h": {}, | 
					
						
							|  |  |  | 		}}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		for entry := range startTreeWalk(volume, | 
					
						
							|  |  |  | 			testCase.prefix, testCase.marker, testCase.recursive, | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 			listDir, isLeaf, endWalkCh) { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 			if _, found := testCase.expected[entry.entry]; !found { | 
					
						
							|  |  |  | 				t.Errorf("Test %d: Expected %s, but couldn't find", i+1, entry.entry) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = removeAll(fsDir1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestSortedness(t *testing.T) { | 
					
						
							|  |  |  | 	// Create a backend directories fsDir1.
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:25:05 +08:00
										 |  |  | 	fsDir1, err := ioutil.TempDir(globalTestTmpDir, "minio-") | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to create tmp directory: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints([]string{fsDir1}) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected error %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	disk1, err := newStorageAPI(endpoints[0]) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 		t.Fatalf("Unable to create StorageAPI: %s", err) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	// Simple isLeaf check, returns true if there is no trailing "/"
 | 
					
						
							|  |  |  | 	isLeaf := func(volume, prefix string) bool { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 		return !strings.HasSuffix(prefix, slashSeparator) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// Create listDir function.
 | 
					
						
							| 
									
										
										
										
											2016-09-16 04:43:40 +08:00
										 |  |  | 	listDir := listDirFactory(isLeaf, xlTreeWalkIgnoredErrs, disk1) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create the namespace.
 | 
					
						
							|  |  |  | 	var files = []string{ | 
					
						
							|  |  |  | 		"d/e", | 
					
						
							|  |  |  | 		"d/f", | 
					
						
							|  |  |  | 		"d/g/h", | 
					
						
							|  |  |  | 		"i/j/k", | 
					
						
							|  |  |  | 		"lmn", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = createNamespace(disk1, volume, files) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	endWalkCh := make(chan struct{}) | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		prefix    string | 
					
						
							|  |  |  | 		marker    string | 
					
						
							|  |  |  | 		recursive bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// with no prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"", "", false}, | 
					
						
							|  |  |  | 		// with no prefix, no marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"", "", true}, | 
					
						
							|  |  |  | 		// with no prefix, marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"", "d/e", false}, | 
					
						
							|  |  |  | 		// with no prefix, marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"", "d/e", true}, | 
					
						
							|  |  |  | 		// with prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "", false}, | 
					
						
							|  |  |  | 		// with prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "", true}, | 
					
						
							|  |  |  | 		// with prefix, marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "d/e", false}, | 
					
						
							|  |  |  | 		// with prefix, marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "d/e", true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							|  |  |  | 		var actualEntries []string | 
					
						
							|  |  |  | 		for entry := range startTreeWalk(volume, | 
					
						
							|  |  |  | 			test.prefix, test.marker, test.recursive, | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 			listDir, isLeaf, endWalkCh) { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 			actualEntries = append(actualEntries, entry.entry) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !sort.IsSorted(sort.StringSlice(actualEntries)) { | 
					
						
							|  |  |  | 			t.Error(i+1, "Expected entries to be sort, but it wasn't") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Remove directory created for testing
 | 
					
						
							|  |  |  | 	err = removeAll(fsDir1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestTreeWalkIsEnd(t *testing.T) { | 
					
						
							|  |  |  | 	// Create a backend directories fsDir1.
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:25:05 +08:00
										 |  |  | 	fsDir1, err := ioutil.TempDir(globalTestTmpDir, "minio-") | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to create tmp directory: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	endpoints, err := parseStorageEndpoints([]string{fsDir1}) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected error %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	disk1, err := newStorageAPI(endpoints[0]) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 		t.Fatalf("Unable to create StorageAPI: %s", err) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	isLeaf := func(volume, prefix string) bool { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 		return !strings.HasSuffix(prefix, slashSeparator) | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// Create listDir function.
 | 
					
						
							| 
									
										
										
										
											2016-09-16 04:43:40 +08:00
										 |  |  | 	listDir := listDirFactory(isLeaf, xlTreeWalkIgnoredErrs, disk1) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create the namespace.
 | 
					
						
							|  |  |  | 	var files = []string{ | 
					
						
							|  |  |  | 		"d/e", | 
					
						
							|  |  |  | 		"d/f", | 
					
						
							|  |  |  | 		"d/g/h", | 
					
						
							|  |  |  | 		"i/j/k", | 
					
						
							|  |  |  | 		"lmn", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = createNamespace(disk1, volume, files) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	endWalkCh := make(chan struct{}) | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		prefix        string | 
					
						
							|  |  |  | 		marker        string | 
					
						
							|  |  |  | 		recursive     bool | 
					
						
							|  |  |  | 		expectedEntry string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// with no prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"", "", false, "lmn"}, | 
					
						
							|  |  |  | 		// with no prefix, no marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"", "", true, "lmn"}, | 
					
						
							|  |  |  | 		// with no prefix, marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"", "d/e", false, "lmn"}, | 
					
						
							|  |  |  | 		// with no prefix, marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"", "d/e", true, "lmn"}, | 
					
						
							|  |  |  | 		// with prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "", false, "d/g/"}, | 
					
						
							|  |  |  | 		// with prefix, no marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "", true, "d/g/h"}, | 
					
						
							|  |  |  | 		// with prefix, marker and no recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "d/e", false, "d/g/"}, | 
					
						
							|  |  |  | 		// with prefix, marker and recursive traversal
 | 
					
						
							|  |  |  | 		{"d/", "d/e", true, "d/g/h"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							|  |  |  | 		var entry treeWalkResult | 
					
						
							| 
									
										
										
										
											2016-07-18 06:16:52 +08:00
										 |  |  | 		for entry = range startTreeWalk(volume, test.prefix, test.marker, test.recursive, listDir, isLeaf, endWalkCh) { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:37:43 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if entry.entry != test.expectedEntry { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected entry %s, but received %s with the EOF marker", i, test.expectedEntry, entry.entry) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !entry.end { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Last entry %s, doesn't have EOF marker set", i, entry.entry) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Remove directory created for testing
 | 
					
						
							|  |  |  | 	err = removeAll(fsDir1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |