github.com/openebs/sparse-tools@v1.1.0/sparse/test/client_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/openebs/sparse-tools/sparse"
     7  	"github.com/openebs/sparse-tools/sparse/rest"
     8  )
     9  
    10  const (
    11  	localhost = "127.0.0.1"
    12  	timeout   = 5 //seconds
    13  	port      = "5000"
    14  )
    15  
    16  func TestSyncSmallFile1(t *testing.T) {
    17  	localPath := tempFilePath("ssync-small-src-")
    18  	remotePath := tempFilePath("ssync-small-dst-")
    19  
    20  	filesCleanup(localPath, remotePath)
    21  	defer filesCleanup(localPath, remotePath)
    22  
    23  	data := []byte("json-fault")
    24  	createTestSmallFile(localPath, len(data), data)
    25  	testSyncAnyFile(t, localPath, remotePath)
    26  }
    27  
    28  func TestSyncSmallFile2(t *testing.T) {
    29  	localPath := tempFilePath("ssync-small-src-")
    30  	remotePath := tempFilePath("ssync-small-dst-")
    31  
    32  	filesCleanup(localPath, remotePath)
    33  	defer filesCleanup(localPath, remotePath)
    34  
    35  	data := []byte("json-fault")
    36  	data1 := []byte("json")
    37  	createTestSmallFile(localPath, len(data), data)
    38  	createTestSmallFile(remotePath, len(data1), data1)
    39  	testSyncAnyFile(t, localPath, remotePath)
    40  }
    41  
    42  func TestSyncSmallFile3(t *testing.T) {
    43  	localPath := tempFilePath("ssync-small-src-")
    44  	remotePath := tempFilePath("ssync-small-dst-")
    45  
    46  	filesCleanup(localPath, remotePath)
    47  	defer filesCleanup(localPath, remotePath)
    48  
    49  	data := []byte("json-fault")
    50  	createTestSmallFile(localPath, len(data), data)
    51  	createTestSmallFile(remotePath, len(data), data)
    52  	testSyncAnyFile(t, localPath, remotePath)
    53  }
    54  
    55  func TestSyncSmallFile4(t *testing.T) {
    56  	localPath := tempFilePath("ssync-small-src-")
    57  	remotePath := tempFilePath("ssync-small-dst-")
    58  
    59  	filesCleanup(localPath, remotePath)
    60  	defer filesCleanup(localPath, remotePath)
    61  
    62  	data := []byte("json-fault")
    63  	createTestSmallFile(localPath, 0, make([]byte, 0))
    64  	createTestSmallFile(remotePath, len(data), data)
    65  	testSyncAnyFile(t, localPath, remotePath)
    66  }
    67  
    68  func TestSyncAnyFile(t *testing.T) {
    69  	src := "src.bar"
    70  	dst := "dst.bar"
    71  	run := false
    72  	// ad hoc test for testing specific problematic files
    73  	// disabled by default
    74  	if run {
    75  		testSyncAnyFile(t, src, dst)
    76  	}
    77  }
    78  
    79  func testSyncAnyFile(t *testing.T, src, dst string) {
    80  	// Sync
    81  	go rest.TestServer(port, dst, timeout)
    82  	err := SyncFile(src, localhost+":"+port, timeout)
    83  
    84  	// Verify
    85  	if err != nil {
    86  		t.Fatal("sync error")
    87  	}
    88  	if !filesAreEqual(src, dst) {
    89  		t.Fatal("file content diverged")
    90  	}
    91  }
    92  
    93  func TestSyncFile1(t *testing.T) {
    94  	// D H D => D D H
    95  	layoutLocal := []FileInterval{
    96  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 1 * Blocks}},
    97  		{Kind: SparseHole, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
    98  		{Kind: SparseData, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
    99  	}
   100  	layoutRemote := []FileInterval{
   101  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   102  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   103  		{Kind: SparseHole, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   104  	}
   105  	testSyncFile(t, layoutLocal, layoutRemote)
   106  }
   107  
   108  func TestSyncFile2(t *testing.T) {
   109  	// H D H  => D H H
   110  	layoutLocal := []FileInterval{
   111  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   112  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   113  		{Kind: SparseHole, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   114  	}
   115  	layoutRemote := []FileInterval{
   116  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   117  		{Kind: SparseHole, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   118  		{Kind: SparseHole, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   119  	}
   120  	testSyncFile(t, layoutLocal, layoutRemote)
   121  }
   122  
   123  func TestSyncFile3(t *testing.T) {
   124  	// D H D => D D
   125  	layoutLocal := []FileInterval{
   126  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   127  		{Kind: SparseHole, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   128  		{Kind: SparseData, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   129  	}
   130  	layoutRemote := []FileInterval{
   131  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   132  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   133  	}
   134  	testSyncFile(t, layoutLocal, layoutRemote)
   135  }
   136  
   137  func TestSyncFile4(t *testing.T) {
   138  	// H D H  => D H
   139  	layoutLocal := []FileInterval{
   140  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   141  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   142  		{Kind: SparseHole, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   143  	}
   144  	layoutRemote := []FileInterval{
   145  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   146  		{Kind: SparseHole, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   147  	}
   148  	testSyncFile(t, layoutLocal, layoutRemote)
   149  }
   150  
   151  func TestSyncFile5(t *testing.T) {
   152  	// H D H  => H D
   153  	layoutLocal := []FileInterval{
   154  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   155  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   156  		{Kind: SparseHole, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   157  	}
   158  	layoutRemote := []FileInterval{
   159  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   160  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   161  	}
   162  	testSyncFile(t, layoutLocal, layoutRemote)
   163  }
   164  
   165  func TestSyncFile6(t *testing.T) {
   166  	// H D H  => D
   167  	layoutLocal := []FileInterval{
   168  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   169  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   170  		{Kind: SparseHole, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   171  	}
   172  	layoutRemote := []FileInterval{
   173  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   174  	}
   175  	testSyncFile(t, layoutLocal, layoutRemote)
   176  }
   177  
   178  func TestSyncFile7(t *testing.T) {
   179  	// H D H  => H
   180  	layoutLocal := []FileInterval{
   181  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   182  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   183  		{Kind: SparseHole, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   184  	}
   185  	layoutRemote := []FileInterval{
   186  		{Kind: SparseHole, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   187  	}
   188  	testSyncFile(t, layoutLocal, layoutRemote)
   189  }
   190  
   191  func TestSyncFile8(t *testing.T) {
   192  	// D H D =>
   193  	layoutLocal := []FileInterval{
   194  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   195  		{Kind: SparseHole, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   196  		{Kind: SparseData, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   197  	}
   198  	layoutRemote := []FileInterval{}
   199  	testSyncFile(t, layoutLocal, layoutRemote)
   200  }
   201  
   202  func TestSyncFile9(t *testing.T) {
   203  	// H D H  =>
   204  	layoutLocal := []FileInterval{
   205  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   206  		{Kind: SparseData, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   207  		{Kind: SparseHole, Interval: Interval{Begin: 2 * Blocks, End: 3 * Blocks}},
   208  	}
   209  	layoutRemote := []FileInterval{}
   210  	testSyncFile(t, layoutLocal, layoutRemote)
   211  }
   212  
   213  func TestSyncDiff1(t *testing.T) {
   214  	layoutLocal := []FileInterval{
   215  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   216  	}
   217  	layoutRemote := []FileInterval{
   218  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   219  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   220  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   221  	}
   222  	testSyncFile(t, layoutLocal, layoutRemote)
   223  }
   224  
   225  func TestSyncDiff2(t *testing.T) {
   226  	layoutLocal := []FileInterval{
   227  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   228  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   229  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   230  	}
   231  	layoutRemote := []FileInterval{
   232  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   233  	}
   234  	testSyncFile(t, layoutLocal, layoutRemote)
   235  }
   236  
   237  func TestSyncDiff3(t *testing.T) {
   238  	layoutLocal := []FileInterval{
   239  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   240  	}
   241  	layoutRemote := []FileInterval{
   242  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   243  		{Kind: SparseHole, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   244  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   245  	}
   246  	testSyncFile(t, layoutLocal, layoutRemote)
   247  }
   248  
   249  func TestSyncDiff4(t *testing.T) {
   250  	layoutLocal := []FileInterval{
   251  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   252  		{Kind: SparseHole, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   253  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   254  	}
   255  	layoutRemote := []FileInterval{
   256  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   257  	}
   258  	testSyncFile(t, layoutLocal, layoutRemote)
   259  }
   260  
   261  func TestSyncDiff5(t *testing.T) {
   262  	layoutLocal := []FileInterval{
   263  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   264  	}
   265  	layoutRemote := []FileInterval{
   266  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   267  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   268  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   269  	}
   270  	testSyncFile(t, layoutLocal, layoutRemote)
   271  }
   272  
   273  func TestSyncDiff6(t *testing.T) {
   274  	layoutLocal := []FileInterval{
   275  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   276  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   277  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   278  	}
   279  	layoutRemote := []FileInterval{
   280  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   281  	}
   282  	testSyncFile(t, layoutLocal, layoutRemote)
   283  }
   284  
   285  func TestSyncDiff7(t *testing.T) {
   286  	layoutLocal := []FileInterval{
   287  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   288  	}
   289  	layoutRemote := []FileInterval{
   290  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   291  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   292  		{Kind: SparseHole, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   293  	}
   294  	testSyncFile(t, layoutLocal, layoutRemote)
   295  }
   296  
   297  func TestSyncDiff8(t *testing.T) {
   298  	layoutLocal := []FileInterval{
   299  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   300  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   301  		{Kind: SparseHole, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   302  	}
   303  	layoutRemote := []FileInterval{
   304  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   305  	}
   306  	testSyncFile(t, layoutLocal, layoutRemote)
   307  }
   308  
   309  func TestSyncDiff9(t *testing.T) {
   310  	layoutLocal := []FileInterval{
   311  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   312  	}
   313  	layoutRemote := []FileInterval{
   314  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   315  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   316  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   317  	}
   318  	testSyncFile(t, layoutLocal, layoutRemote)
   319  }
   320  
   321  func TestSyncDiff10(t *testing.T) {
   322  	layoutLocal := []FileInterval{
   323  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   324  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   325  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   326  	}
   327  	layoutRemote := []FileInterval{
   328  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   329  	}
   330  	testSyncFile(t, layoutLocal, layoutRemote)
   331  }
   332  
   333  func TestSyncDiff11(t *testing.T) {
   334  	layoutLocal := []FileInterval{
   335  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   336  	}
   337  	layoutRemote := []FileInterval{
   338  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   339  		{Kind: SparseHole, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   340  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   341  	}
   342  	testSyncFile(t, layoutLocal, layoutRemote)
   343  }
   344  
   345  func TestSyncDiff12(t *testing.T) {
   346  	layoutLocal := []FileInterval{
   347  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   348  		{Kind: SparseHole, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   349  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   350  	}
   351  	layoutRemote := []FileInterval{
   352  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   353  	}
   354  	testSyncFile(t, layoutLocal, layoutRemote)
   355  }
   356  
   357  func TestSyncDiff13(t *testing.T) {
   358  	layoutLocal := []FileInterval{
   359  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   360  	}
   361  	layoutRemote := []FileInterval{
   362  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   363  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   364  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   365  	}
   366  	testSyncFile(t, layoutLocal, layoutRemote)
   367  }
   368  
   369  func TestSyncDiff14(t *testing.T) {
   370  	layoutLocal := []FileInterval{
   371  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   372  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   373  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   374  	}
   375  	layoutRemote := []FileInterval{
   376  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   377  	}
   378  	testSyncFile(t, layoutLocal, layoutRemote)
   379  }
   380  
   381  func TestSyncDiff15(t *testing.T) {
   382  	layoutLocal := []FileInterval{
   383  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   384  	}
   385  	layoutRemote := []FileInterval{
   386  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   387  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   388  		{Kind: SparseHole, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   389  	}
   390  	testSyncFile(t, layoutLocal, layoutRemote)
   391  }
   392  
   393  func TestSyncDiff16(t *testing.T) {
   394  	layoutLocal := []FileInterval{
   395  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   396  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   397  		{Kind: SparseHole, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   398  	}
   399  	layoutRemote := []FileInterval{
   400  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 100 * Blocks}},
   401  	}
   402  	testSyncFile(t, layoutLocal, layoutRemote)
   403  }
   404  
   405  func TestSyncDiff17(t *testing.T) {
   406  	layoutLocal := []FileInterval{
   407  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 28 * Blocks}},
   408  		{Kind: SparseHole, Interval: Interval{Begin: 28 * Blocks, End: 32 * Blocks}},
   409  		{Kind: SparseData, Interval: Interval{Begin: 32 * Blocks, End: 100 * Blocks}},
   410  	}
   411  	layoutRemote := []FileInterval{
   412  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   413  		{Kind: SparseHole, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   414  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   415  	}
   416  	testSyncFile(t, layoutLocal, layoutRemote)
   417  }
   418  
   419  func TestSyncDiff18(t *testing.T) {
   420  	layoutLocal := []FileInterval{
   421  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 28 * Blocks}},
   422  		{Kind: SparseHole, Interval: Interval{Begin: 28 * Blocks, End: 36 * Blocks}},
   423  		{Kind: SparseData, Interval: Interval{Begin: 36 * Blocks, End: 100 * Blocks}},
   424  	}
   425  	layoutRemote := []FileInterval{
   426  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   427  		{Kind: SparseHole, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   428  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   429  	}
   430  	testSyncFile(t, layoutLocal, layoutRemote)
   431  }
   432  
   433  func TestSyncDiff19(t *testing.T) {
   434  	layoutLocal := []FileInterval{
   435  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 31 * Blocks}},
   436  		{Kind: SparseHole, Interval: Interval{Begin: 31 * Blocks, End: 33 * Blocks}},
   437  		{Kind: SparseData, Interval: Interval{Begin: 33 * Blocks, End: 100 * Blocks}},
   438  	}
   439  	layoutRemote := []FileInterval{
   440  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   441  		{Kind: SparseHole, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   442  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   443  	}
   444  	testSyncFile(t, layoutLocal, layoutRemote)
   445  }
   446  
   447  func TestSyncDiff20(t *testing.T) {
   448  	layoutLocal := []FileInterval{
   449  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 32 * Blocks}},
   450  		{Kind: SparseHole, Interval: Interval{Begin: 32 * Blocks, End: 36 * Blocks}},
   451  		{Kind: SparseData, Interval: Interval{Begin: 36 * Blocks, End: 100 * Blocks}},
   452  	}
   453  	layoutRemote := []FileInterval{
   454  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   455  		{Kind: SparseHole, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   456  		{Kind: SparseData, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   457  	}
   458  	testSyncFile(t, layoutLocal, layoutRemote)
   459  }
   460  
   461  func TestSyncDiff21(t *testing.T) {
   462  	layoutLocal := []FileInterval{
   463  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 28 * Blocks}},
   464  		{Kind: SparseData, Interval: Interval{Begin: 28 * Blocks, End: 32 * Blocks}},
   465  		{Kind: SparseHole, Interval: Interval{Begin: 32 * Blocks, End: 100 * Blocks}},
   466  	}
   467  	layoutRemote := []FileInterval{
   468  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   469  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   470  		{Kind: SparseHole, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   471  	}
   472  	testSyncFile(t, layoutLocal, layoutRemote)
   473  }
   474  
   475  func TestSyncDiff22(t *testing.T) {
   476  	layoutLocal := []FileInterval{
   477  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 28 * Blocks}},
   478  		{Kind: SparseData, Interval: Interval{Begin: 28 * Blocks, End: 36 * Blocks}},
   479  		{Kind: SparseHole, Interval: Interval{Begin: 36 * Blocks, End: 100 * Blocks}},
   480  	}
   481  	layoutRemote := []FileInterval{
   482  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   483  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   484  		{Kind: SparseHole, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   485  	}
   486  	testSyncFile(t, layoutLocal, layoutRemote)
   487  }
   488  
   489  func TestSyncDiff23(t *testing.T) {
   490  	layoutLocal := []FileInterval{
   491  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 31 * Blocks}},
   492  		{Kind: SparseData, Interval: Interval{Begin: 31 * Blocks, End: 33 * Blocks}},
   493  		{Kind: SparseHole, Interval: Interval{Begin: 33 * Blocks, End: 100 * Blocks}},
   494  	}
   495  	layoutRemote := []FileInterval{
   496  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   497  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   498  		{Kind: SparseHole, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   499  	}
   500  	testSyncFile(t, layoutLocal, layoutRemote)
   501  }
   502  
   503  func TestSyncDiff24(t *testing.T) {
   504  	layoutLocal := []FileInterval{
   505  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 32 * Blocks}},
   506  		{Kind: SparseData, Interval: Interval{Begin: 32 * Blocks, End: 36 * Blocks}},
   507  		{Kind: SparseHole, Interval: Interval{Begin: 36 * Blocks, End: 100 * Blocks}},
   508  	}
   509  	layoutRemote := []FileInterval{
   510  		{Kind: SparseHole, Interval: Interval{Begin: 0, End: 30 * Blocks}},
   511  		{Kind: SparseData, Interval: Interval{Begin: 30 * Blocks, End: 34 * Blocks}},
   512  		{Kind: SparseHole, Interval: Interval{Begin: 34 * Blocks, End: 100 * Blocks}},
   513  	}
   514  	testSyncFile(t, layoutLocal, layoutRemote)
   515  }
   516  
   517  func TestSyncFileHashRetry(t *testing.T) {
   518  	layoutLocal := []FileInterval{
   519  		{Kind: SparseData, Interval: Interval{Begin: 0, End: 1 * Blocks}},
   520  		{Kind: SparseHole, Interval: Interval{Begin: 1 * Blocks, End: 2 * Blocks}},
   521  	}
   522  	layoutRemote := []FileInterval{}
   523  
   524  	// Simulate file hash mismatch
   525  	SetFailPointFileHashMatch(true)
   526  	testSyncFile(t, layoutLocal, layoutRemote)
   527  }
   528  
   529  func testSyncFile(t *testing.T, layoutLocal, layoutRemote []FileInterval) (hashLocal []byte) {
   530  	localPath := tempFilePath("ssync-src-")
   531  	remotePath := tempFilePath("ssync-dst-")
   532  
   533  	filesCleanup(localPath, remotePath)
   534  	defer filesCleanup(localPath, remotePath)
   535  
   536  	// Create test files
   537  	createTestSparseFile(localPath, layoutLocal)
   538  	if len(layoutRemote) > 0 {
   539  		// only create destination test file if layout is speciifed
   540  		createTestSparseFile(remotePath, layoutRemote)
   541  	}
   542  
   543  	// Sync
   544  	go rest.TestServer(port, remotePath, timeout)
   545  	err := SyncFile(localPath, localhost+":"+port, timeout)
   546  
   547  	// Verify
   548  	if err != nil {
   549  		t.Fatal("sync error")
   550  	}
   551  	if !filesAreEqual(localPath, remotePath) {
   552  		t.Fatal("file content diverged")
   553  	}
   554  	return
   555  }
   556  
   557  // created in current dir for benchmark tests
   558  var localBigPath = "ssync-src-file.bar"
   559  var remoteBigPath = "ssync-dst-file.bar"
   560  
   561  func Test_1G_cleanup(*testing.T) {
   562  	// remove temporaries if the benchmarks below are not run
   563  	filesCleanup(localBigPath, remoteBigPath)
   564  }
   565  
   566  func Benchmark_1G_InitFiles(b *testing.B) {
   567  	// Setup files
   568  	layoutLocal := []FileInterval{
   569  		{Kind: SparseData, Interval: Interval{Begin: 0, End: (256 << 10) * Blocks}},
   570  	}
   571  	layoutRemote := []FileInterval{}
   572  
   573  	filesCleanup(localBigPath, remoteBigPath)
   574  	createTestSparseFile(localBigPath, layoutLocal)
   575  	createTestSparseFile(remoteBigPath, layoutRemote)
   576  }
   577  
   578  func Benchmark_1G_SendFiles_Whole(b *testing.B) {
   579  	go rest.TestServer(port, remoteBigPath, timeout)
   580  	err := SyncFile(localBigPath, localhost+":"+port, timeout)
   581  
   582  	if err != nil {
   583  		b.Fatal("sync error")
   584  	}
   585  }
   586  
   587  func Benchmark_1G_SendFiles_Diff(b *testing.B) {
   588  
   589  	go rest.TestServer(port, remoteBigPath, timeout)
   590  	err := SyncFile(localBigPath, localhost+":"+port, timeout)
   591  
   592  	if err != nil {
   593  		b.Fatal("sync error")
   594  	}
   595  }
   596  
   597  func Benchmark_1G_CheckFiles(b *testing.B) {
   598  	if !filesAreEqual(localBigPath, remoteBigPath) {
   599  		b.Error("file content diverged")
   600  		return
   601  	}
   602  	filesCleanup(localBigPath, remoteBigPath)
   603  }