storage_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package lfsutil
  5. import (
  6. "bytes"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestLocalStorage_storagePath(t *testing.T) {
  16. if runtime.GOOS == "windows" {
  17. t.Skip("Skipping testing on Windows")
  18. return
  19. }
  20. s := &LocalStorage{
  21. Root: "/lfs-objects",
  22. }
  23. tests := []struct {
  24. name string
  25. oid OID
  26. expPath string
  27. }{
  28. {
  29. name: "empty oid",
  30. oid: "",
  31. },
  32. {
  33. name: "valid oid",
  34. oid: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  35. expPath: "/lfs-objects/e/f/ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  36. },
  37. }
  38. for _, test := range tests {
  39. t.Run(test.name, func(t *testing.T) {
  40. assert.Equal(t, test.expPath, s.storagePath(test.oid))
  41. })
  42. }
  43. }
  44. func TestLocalStorage_Upload(t *testing.T) {
  45. s := &LocalStorage{
  46. Root: filepath.Join(os.TempDir(), "lfs-objects"),
  47. }
  48. t.Cleanup(func() {
  49. _ = os.RemoveAll(s.Root)
  50. })
  51. tests := []struct {
  52. name string
  53. oid OID
  54. content string
  55. expWritten int64
  56. expErr error
  57. }{
  58. {
  59. name: "invalid oid",
  60. oid: "bad_oid",
  61. expErr: ErrInvalidOID,
  62. },
  63. {
  64. name: "valid oid",
  65. oid: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  66. content: "Hello world!",
  67. expWritten: 12,
  68. },
  69. }
  70. for _, test := range tests {
  71. t.Run(test.name, func(t *testing.T) {
  72. written, err := s.Upload(test.oid, ioutil.NopCloser(strings.NewReader(test.content)))
  73. assert.Equal(t, test.expWritten, written)
  74. assert.Equal(t, test.expErr, err)
  75. })
  76. }
  77. }
  78. func TestLocalStorage_Download(t *testing.T) {
  79. oid := OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
  80. s := &LocalStorage{
  81. Root: filepath.Join(os.TempDir(), "lfs-objects"),
  82. }
  83. t.Cleanup(func() {
  84. _ = os.RemoveAll(s.Root)
  85. })
  86. fpath := s.storagePath(oid)
  87. err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. err = ioutil.WriteFile(fpath, []byte("Hello world!"), os.ModePerm)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. tests := []struct {
  96. name string
  97. oid OID
  98. expContent string
  99. expErr error
  100. }{
  101. {
  102. name: "object not exists",
  103. oid: "bad_oid",
  104. expErr: ErrObjectNotExist,
  105. },
  106. {
  107. name: "valid oid",
  108. oid: oid,
  109. expContent: "Hello world!",
  110. },
  111. }
  112. for _, test := range tests {
  113. t.Run(test.name, func(t *testing.T) {
  114. var buf bytes.Buffer
  115. err := s.Download(test.oid, &buf)
  116. assert.Equal(t, test.expContent, buf.String())
  117. assert.Equal(t, test.expErr, err)
  118. })
  119. }
  120. }