file_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2015 CoreOS, Inc.
  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 system
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "testing"
  20. "github.com/rancher/os/config/cloudinit/config"
  21. )
  22. func TestWriteFileUnencodedContent(t *testing.T) {
  23. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  24. if err != nil {
  25. t.Fatalf("Unable to create tempdir: %v", err)
  26. }
  27. defer os.RemoveAll(dir)
  28. fn := "foo"
  29. fullPath := path.Join(dir, fn)
  30. wf := File{config.File{
  31. Path: fn,
  32. Content: "bar",
  33. RawFilePermissions: "0644",
  34. }}
  35. path, err := WriteFile(&wf, dir)
  36. if err != nil {
  37. t.Fatalf("Processing of WriteFile failed: %v", err)
  38. } else if path != fullPath {
  39. t.Fatalf("WriteFile returned bad path: want %s, got %s", fullPath, path)
  40. }
  41. fi, err := os.Stat(fullPath)
  42. if err != nil {
  43. t.Fatalf("Unable to stat file: %v", err)
  44. }
  45. if fi.Mode() != os.FileMode(0644) {
  46. t.Errorf("File has incorrect mode: %v", fi.Mode())
  47. }
  48. contents, err := ioutil.ReadFile(fullPath)
  49. if err != nil {
  50. t.Fatalf("Unable to read expected file: %v", err)
  51. }
  52. if string(contents) != "bar" {
  53. t.Fatalf("File has incorrect contents")
  54. }
  55. }
  56. func TestWriteFileInvalidPermission(t *testing.T) {
  57. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  58. if err != nil {
  59. t.Fatalf("Unable to create tempdir: %v", err)
  60. }
  61. defer os.RemoveAll(dir)
  62. wf := File{config.File{
  63. Path: path.Join(dir, "tmp", "foo"),
  64. Content: "bar",
  65. RawFilePermissions: "pants",
  66. }}
  67. if _, err := WriteFile(&wf, dir); err == nil {
  68. t.Fatalf("Expected error to be raised when writing file with invalid permission")
  69. }
  70. }
  71. func TestDecimalFilePermissions(t *testing.T) {
  72. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  73. if err != nil {
  74. t.Fatalf("Unable to create tempdir: %v", err)
  75. }
  76. defer os.RemoveAll(dir)
  77. fn := "foo"
  78. fullPath := path.Join(dir, fn)
  79. wf := File{config.File{
  80. Path: fn,
  81. RawFilePermissions: "744",
  82. }}
  83. path, err := WriteFile(&wf, dir)
  84. if err != nil {
  85. t.Fatalf("Processing of WriteFile failed: %v", err)
  86. } else if path != fullPath {
  87. t.Fatalf("WriteFile returned bad path: want %s, got %s", fullPath, path)
  88. }
  89. fi, err := os.Stat(fullPath)
  90. if err != nil {
  91. t.Fatalf("Unable to stat file: %v", err)
  92. }
  93. if fi.Mode() != os.FileMode(0744) {
  94. t.Errorf("File has incorrect mode: %v", fi.Mode())
  95. }
  96. }
  97. func TestWriteFilePermissions(t *testing.T) {
  98. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  99. if err != nil {
  100. t.Fatalf("Unable to create tempdir: %v", err)
  101. }
  102. defer os.RemoveAll(dir)
  103. fn := "foo"
  104. fullPath := path.Join(dir, fn)
  105. wf := File{config.File{
  106. Path: fn,
  107. RawFilePermissions: "0755",
  108. }}
  109. path, err := WriteFile(&wf, dir)
  110. if err != nil {
  111. t.Fatalf("Processing of WriteFile failed: %v", err)
  112. } else if path != fullPath {
  113. t.Fatalf("WriteFile returned bad path: want %s, got %s", fullPath, path)
  114. }
  115. fi, err := os.Stat(fullPath)
  116. if err != nil {
  117. t.Fatalf("Unable to stat file: %v", err)
  118. }
  119. if fi.Mode() != os.FileMode(0755) {
  120. t.Errorf("File has incorrect mode: %v", fi.Mode())
  121. }
  122. }
  123. func TestWriteFileInvalidEncodedContent(t *testing.T) {
  124. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  125. if err != nil {
  126. t.Fatalf("Unable to create tempdir: %v", err)
  127. }
  128. defer os.RemoveAll(dir)
  129. contentEncodings := []string{
  130. "base64",
  131. "b64",
  132. "gz",
  133. "gzip",
  134. "gz+base64",
  135. "gzip+base64",
  136. "gz+b64",
  137. "gzip+b64",
  138. }
  139. for _, encoding := range contentEncodings {
  140. wf := File{config.File{
  141. Path: path.Join(dir, "tmp", "foo"),
  142. Content: "@&*#%invalid data*@&^#*&",
  143. Encoding: encoding,
  144. }}
  145. if _, err := WriteFile(&wf, dir); err == nil {
  146. t.Fatalf("Expected error to be raised when writing file with encoding")
  147. }
  148. }
  149. }
  150. func TestWriteFileUnknownEncodedContent(t *testing.T) {
  151. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  152. if err != nil {
  153. t.Fatalf("Unable to create tempdir: %v", err)
  154. }
  155. defer os.RemoveAll(dir)
  156. wf := File{config.File{
  157. Path: path.Join(dir, "tmp", "foo"),
  158. Content: "",
  159. Encoding: "no-such-encoding",
  160. }}
  161. if _, err := WriteFile(&wf, dir); err == nil {
  162. t.Fatalf("Expected error to be raised when writing file with encoding")
  163. }
  164. }