sync_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package term
  11. import (
  12. "bytes"
  13. "fmt"
  14. "io/ioutil"
  15. "os"
  16. "testing"
  17. "devt.de/krotik/rufs"
  18. "devt.de/krotik/rufs/config"
  19. )
  20. func TestSyncOperation(t *testing.T) {
  21. var buf bytes.Buffer
  22. // Build up a tree with multiple branches
  23. cfg := map[string]interface{}{
  24. config.TreeSecret: "123",
  25. }
  26. ioutil.WriteFile("./bar/testfile66", []byte("write test"), 0660)
  27. defer os.Remove("./bar/testfile66")
  28. tree, _ := rufs.NewTree(cfg, clientCert)
  29. fooRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
  30. fooFP := footest.SSLFingerprint()
  31. barRPC := fmt.Sprintf("%v:%v", branchConfigs["bartest"][config.RPCHost], branchConfigs["bartest"][config.RPCPort])
  32. barFP := bartest.SSLFingerprint()
  33. tmpRPC := fmt.Sprintf("%v:%v", branchConfigs["tmptest"][config.RPCHost], branchConfigs["tmptest"][config.RPCPort])
  34. tmpFP := tmptest.SSLFingerprint()
  35. tree.AddBranch(footest.Name(), fooRPC, fooFP)
  36. tree.AddBranch(bartest.Name(), barRPC, barFP)
  37. tree.AddBranch(tmptest.Name(), tmpRPC, tmpFP)
  38. tree.AddMapping("/1", footest.Name(), false)
  39. tree.AddMapping("/1", bartest.Name(), false)
  40. tree.AddMapping("/2", tmptest.Name(), true)
  41. term := NewTreeTerm(tree, &buf)
  42. if res, err := term.Run("tree"); err != nil || (res != `
  43. /
  44. drwxrwxrwx 0 B 1
  45. drwxrwxrwx 0 B 2
  46. /1
  47. drwxrwx--- 4.0 KiB sub1
  48. -rwxrwx--- 10 B test1
  49. -rwxrwx--- 10 B test2
  50. -rw-rw---- 10 B testfile66
  51. /1/sub1
  52. -rwxrwx--- 17 B test3
  53. /2
  54. `[1:] && res != `
  55. /
  56. drwxrwxrwx 0 B 1
  57. drwxrwxrwx 0 B 2
  58. /1
  59. drwxr-x--- 4.0 KiB sub1
  60. -rwxr-x--- 10 B test1
  61. -rwxr-x--- 10 B test2
  62. -rw-r----- 10 B testfile66
  63. /1/sub1
  64. -rwxr-x--- 17 B test3
  65. /2
  66. `[1:] && res != `
  67. /
  68. drwxrwxrwx 0 B 1
  69. drwxrwxrwx 0 B 2
  70. /1
  71. drwxrwxrwx 4.0 KiB sub1
  72. -rw-rw-rw- 10 B test1
  73. -rw-rw-rw- 10 B test2
  74. -rw-rw-rw- 10 B testfile66
  75. /1/sub1
  76. -rw-rw-rw- 17 B test3
  77. /2
  78. `[1:]) {
  79. t.Error("Unexpected result: ", res, err)
  80. return
  81. }
  82. res, err := term.Run("sync /1 /2")
  83. if err != nil || res != "Done" {
  84. t.Error(res, err)
  85. return
  86. }
  87. if buf.String() != "Create directory (1/5) -> /2/sub1\n\r"+
  88. "Copy file (2/5) writing: /1/test1 -> /2/test1 10 B / 10 B\r \r"+
  89. "Copy file (2/5) /1/test1 -> /2/test1\n\r"+
  90. "Copy file (3/5) writing: /1/test2 -> /2/test2 10 B / 10 B\r \r"+
  91. "Copy file (3/5) /1/test2 -> /2/test2\n\r"+
  92. "Copy file (4/5) writing: /1/testfile66 -> /2/testfile66 10 B / 10 B\r \r"+
  93. "Copy file (4/5) /1/testfile66 -> /2/testfile66\n\r"+
  94. "Copy file (5/5) writing: /1/sub1/test3 -> /2/sub1/test3 17 B / 17 B\r \r"+
  95. "Copy file (5/5) /1/sub1/test3 -> /2/sub1/test3\n" {
  96. t.Errorf("Unexpected buffer: %#v", buf.String())
  97. return
  98. }
  99. if res, err := term.Run("tree"); err != nil || (res != `
  100. /
  101. drwxrwxrwx 0 B 1
  102. drwxrwxrwx 0 B 2
  103. /1
  104. drwxrwx--- 4.0 KiB sub1
  105. -rwxrwx--- 10 B test1
  106. -rwxrwx--- 10 B test2
  107. -rw-rw---- 10 B testfile66
  108. /1/sub1
  109. -rwxrwx--- 17 B test3
  110. /2
  111. drwxr-xr-x 4.0 KiB sub1
  112. -rw-r--r-- 10 B test1
  113. -rw-r--r-- 10 B test2
  114. -rw-r--r-- 10 B testfile66
  115. /2/sub1
  116. -rw-r--r-- 17 B test3
  117. `[1:] && res != `
  118. /
  119. drwxrwxrwx 0 B 1
  120. drwxrwxrwx 0 B 2
  121. /1
  122. drwxr-x--- 4.0 KiB sub1
  123. -rwxr-x--- 10 B test1
  124. -rwxr-x--- 10 B test2
  125. -rw-r----- 10 B testfile66
  126. /1/sub1
  127. -rwxr-x--- 17 B test3
  128. /2
  129. drwxr-xr-x 4.0 KiB sub1
  130. -rw-r--r-- 10 B test1
  131. -rw-r--r-- 10 B test2
  132. -rw-r--r-- 10 B testfile66
  133. /2/sub1
  134. -rw-r--r-- 17 B test3
  135. `[1:] && res != `
  136. /
  137. drwxrwxrwx 0 B 1
  138. drwxrwxrwx 0 B 2
  139. /1
  140. drwxrwxrwx 4.0 KiB sub1
  141. -rw-rw-rw- 10 B test1
  142. -rw-rw-rw- 10 B test2
  143. -rw-rw-rw- 10 B testfile66
  144. /1/sub1
  145. -rw-rw-rw- 17 B test3
  146. /2
  147. drwxrwxrwx 4.0 KiB sub1
  148. -rw-rw-rw- 10 B test1
  149. -rw-rw-rw- 10 B test2
  150. -rw-rw-rw- 10 B testfile66
  151. /2/sub1
  152. -rw-rw-rw- 17 B test3
  153. `[1:]) {
  154. t.Error("Unexpected result: ", res, err)
  155. return
  156. }
  157. }