sync_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. drwxrwxrwx 4.0 KiB sub1
  60. -rw-rw-rw- 10 B test1
  61. -rw-rw-rw- 10 B test2
  62. -rw-rw-rw- 10 B testfile66
  63. /1/sub1
  64. -rw-rw-rw- 17 B test3
  65. /2
  66. `[1:]) {
  67. t.Error("Unexpected result: ", res, err)
  68. return
  69. }
  70. res, err := term.Run("sync /1 /2")
  71. if err != nil || res != "Done" {
  72. t.Error(res, err)
  73. return
  74. }
  75. if buf.String() != "Create directory (1/5) -> /2/sub1\n\r"+
  76. "Copy file (2/5) writing: /1/test1 -> /2/test1 10 B / 10 B\r \r"+
  77. "Copy file (2/5) /1/test1 -> /2/test1\n\r"+
  78. "Copy file (3/5) writing: /1/test2 -> /2/test2 10 B / 10 B\r \r"+
  79. "Copy file (3/5) /1/test2 -> /2/test2\n\r"+
  80. "Copy file (4/5) writing: /1/testfile66 -> /2/testfile66 10 B / 10 B\r \r"+
  81. "Copy file (4/5) /1/testfile66 -> /2/testfile66\n\r"+
  82. "Copy file (5/5) writing: /1/sub1/test3 -> /2/sub1/test3 17 B / 17 B\r \r"+
  83. "Copy file (5/5) /1/sub1/test3 -> /2/sub1/test3\n" {
  84. t.Errorf("Unexpected buffer: %#v", buf.String())
  85. return
  86. }
  87. if res, err := term.Run("tree"); err != nil || (res != `
  88. /
  89. drwxrwxrwx 0 B 1
  90. drwxrwxrwx 0 B 2
  91. /1
  92. drwxrwx--- 4.0 KiB sub1
  93. -rwxrwx--- 10 B test1
  94. -rwxrwx--- 10 B test2
  95. -rw-rw---- 10 B testfile66
  96. /1/sub1
  97. -rwxrwx--- 17 B test3
  98. /2
  99. drwxr-xr-x 4.0 KiB sub1
  100. -rw-r--r-- 10 B test1
  101. -rw-r--r-- 10 B test2
  102. -rw-r--r-- 10 B testfile66
  103. /2/sub1
  104. -rw-r--r-- 17 B test3
  105. `[1:] && res != `
  106. /
  107. drwxrwxrwx 0 B 1
  108. drwxrwxrwx 0 B 2
  109. /1
  110. drwxrwxrwx 4.0 KiB sub1
  111. -rw-rw-rw- 10 B test1
  112. -rw-rw-rw- 10 B test2
  113. -rw-rw-rw- 10 B testfile66
  114. /1/sub1
  115. -rw-rw-rw- 17 B test3
  116. /2
  117. drwxrwxrwx 4.0 KiB sub1
  118. -rw-rw-rw- 10 B test1
  119. -rw-rw-rw- 10 B test2
  120. -rw-rw-rw- 10 B testfile66
  121. /2/sub1
  122. -rw-rw-rw- 17 B test3
  123. `[1:]) {
  124. t.Error("Unexpected result: ", res, err)
  125. return
  126. }
  127. }