fileutils_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutils2
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "testing"
  19. )
  20. // TODO: rewrite this test
  21. /*
  22. func TestIsBlockDeviceUsed(t *testing.T) {
  23. type args struct {
  24. dev string
  25. }
  26. tests := []struct {
  27. name string
  28. args args
  29. want bool
  30. }{
  31. {
  32. name: "nbd1",
  33. args: args{"/dev/nbd1"},
  34. want: false,
  35. },
  36. {
  37. name: "sda",
  38. args: args{"/dev/sda"},
  39. want: true,
  40. },
  41. }
  42. for _, tt := range tests {
  43. t.Run(tt.name, func(t *testing.T) {
  44. if got := IsBlockDeviceUsed(tt.args.dev); got != tt.want {
  45. t.Errorf("IsBlockDeviceUsed() = %v, want %v", got, tt.want)
  46. }
  47. })
  48. }
  49. }
  50. func TestGetDevId(t *testing.T) {
  51. type args struct {
  52. spath string
  53. }
  54. tests := []struct {
  55. name string
  56. args args
  57. want string
  58. }{
  59. // TODO: Add test cases.
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. if got := GetDevId(tt.args.spath); got != tt.want {
  64. t.Errorf("GetDevId() = %v, want %v", got, tt.want)
  65. }
  66. })
  67. }
  68. }*/
  69. func TestGetAllBlkdevsIoScheduler(t *testing.T) {
  70. scheds, _ := GetAllBlkdevsIoSchedulers()
  71. t.Logf("scheduler: %#v", scheds)
  72. }
  73. func TestFilePutContents(t *testing.T) {
  74. cases := []struct {
  75. isAppend bool
  76. content string
  77. want string
  78. }{
  79. {
  80. isAppend: false,
  81. content: "123",
  82. want: "123",
  83. },
  84. {
  85. isAppend: false,
  86. content: "abc",
  87. want: "abc",
  88. },
  89. {
  90. isAppend: true,
  91. content: "123",
  92. want: "abc123",
  93. },
  94. {
  95. isAppend: true,
  96. content: "abc",
  97. want: "abc123abc",
  98. },
  99. {
  100. isAppend: false,
  101. content: "123",
  102. want: "123",
  103. },
  104. }
  105. file, err := ioutil.TempFile("/tmp", "test*.tmp")
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. defer os.Remove(file.Name())
  110. t.Log(file.Name())
  111. for _, c := range cases {
  112. err := FilePutContents(file.Name(), c.content, c.isAppend)
  113. if err != nil {
  114. t.Errorf("FilePutContents fail %s", err)
  115. } else {
  116. cont, err := FileGetContents(file.Name())
  117. if err != nil {
  118. t.Errorf("FileGetContents %s", err)
  119. } else if cont != c.want {
  120. t.Errorf("expect %s got %s", c.want, cont)
  121. }
  122. }
  123. }
  124. }