env_file_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. "strings"
  20. "syscall"
  21. "testing"
  22. "github.com/rancher/os/config/cloudinit/config"
  23. )
  24. const (
  25. base = "# a file\nFOO=base\n\nBAR= hi there\n"
  26. baseNoNewline = "# a file\nFOO=base\n\nBAR= hi there"
  27. baseDos = "# a file\r\nFOO=base\r\n\r\nBAR= hi there\r\n"
  28. expectUpdate = "# a file\nFOO=test\n\nBAR= hi there\nNEW=a value\n"
  29. expectCreate = "FOO=test\nNEW=a value\n"
  30. )
  31. var (
  32. valueUpdate = map[string]string{
  33. "FOO": "test",
  34. "NEW": "a value",
  35. }
  36. valueNoop = map[string]string{
  37. "FOO": "base",
  38. }
  39. valueEmpty = map[string]string{}
  40. valueInvalid = map[string]string{
  41. "FOO-X": "test",
  42. }
  43. )
  44. func TestWriteEnvFileUpdate(t *testing.T) {
  45. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  46. if err != nil {
  47. t.Fatalf("Unable to create tempdir: %v", err)
  48. }
  49. defer os.RemoveAll(dir)
  50. name := "foo.conf"
  51. fullPath := path.Join(dir, name)
  52. ioutil.WriteFile(fullPath, []byte(base), 0644)
  53. oldStat, err := os.Stat(fullPath)
  54. if err != nil {
  55. t.Fatalf("Unable to stat file: %v", err)
  56. }
  57. ef := EnvFile{
  58. File: &File{config.File{
  59. Path: name,
  60. }},
  61. Vars: valueUpdate,
  62. }
  63. err = WriteEnvFile(&ef, dir)
  64. if err != nil {
  65. t.Fatalf("WriteFile failed: %v", err)
  66. }
  67. contents, err := ioutil.ReadFile(fullPath)
  68. if err != nil {
  69. t.Fatalf("Unable to read expected file: %v", err)
  70. }
  71. if string(contents) != expectUpdate {
  72. t.Fatalf("File has incorrect contents: %q", contents)
  73. }
  74. newStat, err := os.Stat(fullPath)
  75. if err != nil {
  76. t.Fatalf("Unable to stat file: %v", err)
  77. }
  78. if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
  79. t.Fatalf("File was not replaced: %s", fullPath)
  80. }
  81. }
  82. func TestWriteEnvFileUpdateNoNewline(t *testing.T) {
  83. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  84. if err != nil {
  85. t.Fatalf("Unable to create tempdir: %v", err)
  86. }
  87. defer os.RemoveAll(dir)
  88. name := "foo.conf"
  89. fullPath := path.Join(dir, name)
  90. ioutil.WriteFile(fullPath, []byte(baseNoNewline), 0644)
  91. oldStat, err := os.Stat(fullPath)
  92. if err != nil {
  93. t.Fatalf("Unable to stat file: %v", err)
  94. }
  95. ef := EnvFile{
  96. File: &File{config.File{
  97. Path: name,
  98. }},
  99. Vars: valueUpdate,
  100. }
  101. err = WriteEnvFile(&ef, dir)
  102. if err != nil {
  103. t.Fatalf("WriteFile failed: %v", err)
  104. }
  105. contents, err := ioutil.ReadFile(fullPath)
  106. if err != nil {
  107. t.Fatalf("Unable to read expected file: %v", err)
  108. }
  109. if string(contents) != expectUpdate {
  110. t.Fatalf("File has incorrect contents: %q", contents)
  111. }
  112. newStat, err := os.Stat(fullPath)
  113. if err != nil {
  114. t.Fatalf("Unable to stat file: %v", err)
  115. }
  116. if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
  117. t.Fatalf("File was not replaced: %s", fullPath)
  118. }
  119. }
  120. func TestWriteEnvFileCreate(t *testing.T) {
  121. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  122. if err != nil {
  123. t.Fatalf("Unable to create tempdir: %v", err)
  124. }
  125. defer os.RemoveAll(dir)
  126. name := "foo.conf"
  127. fullPath := path.Join(dir, name)
  128. ef := EnvFile{
  129. File: &File{config.File{
  130. Path: name,
  131. }},
  132. Vars: valueUpdate,
  133. }
  134. err = WriteEnvFile(&ef, dir)
  135. if err != nil {
  136. t.Fatalf("WriteFile failed: %v", err)
  137. }
  138. contents, err := ioutil.ReadFile(fullPath)
  139. if err != nil {
  140. t.Fatalf("Unable to read expected file: %v", err)
  141. }
  142. if string(contents) != expectCreate {
  143. t.Fatalf("File has incorrect contents: %q", contents)
  144. }
  145. }
  146. func TestWriteEnvFileNoop(t *testing.T) {
  147. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  148. if err != nil {
  149. t.Fatalf("Unable to create tempdir: %v", err)
  150. }
  151. defer os.RemoveAll(dir)
  152. name := "foo.conf"
  153. fullPath := path.Join(dir, name)
  154. ioutil.WriteFile(fullPath, []byte(base), 0644)
  155. oldStat, err := os.Stat(fullPath)
  156. if err != nil {
  157. t.Fatalf("Unable to stat file: %v", err)
  158. }
  159. ef := EnvFile{
  160. File: &File{config.File{
  161. Path: name,
  162. }},
  163. Vars: valueNoop,
  164. }
  165. err = WriteEnvFile(&ef, dir)
  166. if err != nil {
  167. t.Fatalf("WriteFile failed: %v", err)
  168. }
  169. contents, err := ioutil.ReadFile(fullPath)
  170. if err != nil {
  171. t.Fatalf("Unable to read expected file: %v", err)
  172. }
  173. if string(contents) != base {
  174. t.Fatalf("File has incorrect contents: %q", contents)
  175. }
  176. newStat, err := os.Stat(fullPath)
  177. if err != nil {
  178. t.Fatalf("Unable to stat file: %v", err)
  179. }
  180. if oldStat.Sys().(*syscall.Stat_t).Ino != newStat.Sys().(*syscall.Stat_t).Ino {
  181. t.Fatalf("File was replaced: %s", fullPath)
  182. }
  183. }
  184. func TestWriteEnvFileUpdateDos(t *testing.T) {
  185. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  186. if err != nil {
  187. t.Fatalf("Unable to create tempdir: %v", err)
  188. }
  189. defer os.RemoveAll(dir)
  190. name := "foo.conf"
  191. fullPath := path.Join(dir, name)
  192. ioutil.WriteFile(fullPath, []byte(baseDos), 0644)
  193. oldStat, err := os.Stat(fullPath)
  194. if err != nil {
  195. t.Fatalf("Unable to stat file: %v", err)
  196. }
  197. ef := EnvFile{
  198. File: &File{config.File{
  199. Path: name,
  200. }},
  201. Vars: valueUpdate,
  202. }
  203. err = WriteEnvFile(&ef, dir)
  204. if err != nil {
  205. t.Fatalf("WriteFile failed: %v", err)
  206. }
  207. contents, err := ioutil.ReadFile(fullPath)
  208. if err != nil {
  209. t.Fatalf("Unable to read expected file: %v", err)
  210. }
  211. if string(contents) != expectUpdate {
  212. t.Fatalf("File has incorrect contents: %q", contents)
  213. }
  214. newStat, err := os.Stat(fullPath)
  215. if err != nil {
  216. t.Fatalf("Unable to stat file: %v", err)
  217. }
  218. if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
  219. t.Fatalf("File was not replaced: %s", fullPath)
  220. }
  221. }
  222. // A middle ground noop, values are unchanged but we did have a value.
  223. // Seems reasonable to rewrite the file in Unix format anyway.
  224. func TestWriteEnvFileDos2Unix(t *testing.T) {
  225. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  226. if err != nil {
  227. t.Fatalf("Unable to create tempdir: %v", err)
  228. }
  229. defer os.RemoveAll(dir)
  230. name := "foo.conf"
  231. fullPath := path.Join(dir, name)
  232. ioutil.WriteFile(fullPath, []byte(baseDos), 0644)
  233. oldStat, err := os.Stat(fullPath)
  234. if err != nil {
  235. t.Fatalf("Unable to stat file: %v", err)
  236. }
  237. ef := EnvFile{
  238. File: &File{config.File{
  239. Path: name,
  240. }},
  241. Vars: valueNoop,
  242. }
  243. err = WriteEnvFile(&ef, dir)
  244. if err != nil {
  245. t.Fatalf("WriteFile failed: %v", err)
  246. }
  247. contents, err := ioutil.ReadFile(fullPath)
  248. if err != nil {
  249. t.Fatalf("Unable to read expected file: %v", err)
  250. }
  251. if string(contents) != base {
  252. t.Fatalf("File has incorrect contents: %q", contents)
  253. }
  254. newStat, err := os.Stat(fullPath)
  255. if err != nil {
  256. t.Fatalf("Unable to stat file: %v", err)
  257. }
  258. if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
  259. t.Fatalf("File was not replaced: %s", fullPath)
  260. }
  261. }
  262. // If it really is a noop (structure is empty) don't even do dos2unix
  263. func TestWriteEnvFileEmpty(t *testing.T) {
  264. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  265. if err != nil {
  266. t.Fatalf("Unable to create tempdir: %v", err)
  267. }
  268. defer os.RemoveAll(dir)
  269. name := "foo.conf"
  270. fullPath := path.Join(dir, name)
  271. ioutil.WriteFile(fullPath, []byte(baseDos), 0644)
  272. oldStat, err := os.Stat(fullPath)
  273. if err != nil {
  274. t.Fatalf("Unable to stat file: %v", err)
  275. }
  276. ef := EnvFile{
  277. File: &File{config.File{
  278. Path: name,
  279. }},
  280. Vars: valueEmpty,
  281. }
  282. err = WriteEnvFile(&ef, dir)
  283. if err != nil {
  284. t.Fatalf("WriteFile failed: %v", err)
  285. }
  286. contents, err := ioutil.ReadFile(fullPath)
  287. if err != nil {
  288. t.Fatalf("Unable to read expected file: %v", err)
  289. }
  290. if string(contents) != baseDos {
  291. t.Fatalf("File has incorrect contents: %q", contents)
  292. }
  293. newStat, err := os.Stat(fullPath)
  294. if err != nil {
  295. t.Fatalf("Unable to stat file: %v", err)
  296. }
  297. if oldStat.Sys().(*syscall.Stat_t).Ino != newStat.Sys().(*syscall.Stat_t).Ino {
  298. t.Fatalf("File was replaced: %s", fullPath)
  299. }
  300. }
  301. // no point in creating empty files
  302. func TestWriteEnvFileEmptyNoCreate(t *testing.T) {
  303. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  304. if err != nil {
  305. t.Fatalf("Unable to create tempdir: %v", err)
  306. }
  307. defer os.RemoveAll(dir)
  308. name := "foo.conf"
  309. fullPath := path.Join(dir, name)
  310. ef := EnvFile{
  311. File: &File{config.File{
  312. Path: name,
  313. }},
  314. Vars: valueEmpty,
  315. }
  316. err = WriteEnvFile(&ef, dir)
  317. if err != nil {
  318. t.Fatalf("WriteFile failed: %v", err)
  319. }
  320. contents, err := ioutil.ReadFile(fullPath)
  321. if err == nil {
  322. t.Fatalf("File has incorrect contents: %q", contents)
  323. } else if !os.IsNotExist(err) {
  324. t.Fatalf("Unexpected error while reading file: %v", err)
  325. }
  326. }
  327. func SvenBrokeTestWriteEnvFilePermFailure(t *testing.T) {
  328. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  329. if err != nil {
  330. t.Fatalf("Unable to create tempdir: %v", err)
  331. }
  332. defer os.RemoveAll(dir)
  333. name := "foo.conf"
  334. fullPath := path.Join(dir, name)
  335. ioutil.WriteFile(fullPath, []byte(base), 0000)
  336. ef := EnvFile{
  337. File: &File{config.File{
  338. Path: name,
  339. }},
  340. Vars: valueUpdate,
  341. }
  342. err = WriteEnvFile(&ef, dir)
  343. if !os.IsPermission(err) {
  344. t.Fatalf("Not a pemission denied error: %v", err)
  345. }
  346. }
  347. func TestWriteEnvFileNameFailure(t *testing.T) {
  348. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  349. if err != nil {
  350. t.Fatalf("Unable to create tempdir: %v", err)
  351. }
  352. defer os.RemoveAll(dir)
  353. name := "foo.conf"
  354. ef := EnvFile{
  355. File: &File{config.File{
  356. Path: name,
  357. }},
  358. Vars: valueInvalid,
  359. }
  360. err = WriteEnvFile(&ef, dir)
  361. if err == nil || !strings.HasPrefix(err.Error(), "Invalid name") {
  362. t.Fatalf("Not an invalid name error: %v", err)
  363. }
  364. }