A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.5 KiB

5 years ago
5 years ago
5 years ago
  1. use schematics::symbols::file::File as FileSymbol;
  2. use schematics::symbols::Symbol;
  3. use std::fs::File;
  4. use std::io::Write;
  5. use std::path::Path;
  6. use tempdir::TempDir;
  7. fn get_dir(content: Option<&str>) -> TempDir {
  8. let tmp_dir = TempDir::new("unittest").expect("create temp dir");
  9. if content.is_none() {
  10. return tmp_dir;
  11. }
  12. let file_path = tmp_dir.path().join("filename");
  13. let mut tmp_file = File::create(file_path).expect("create temp file");
  14. tmp_file
  15. .write_all(content.unwrap().as_bytes())
  16. .expect("write temp file");
  17. tmp_dir
  18. }
  19. fn get_symbol(path: &Path) -> FileSymbol<&str, String> {
  20. FileSymbol::new(
  21. String::from(path.join("filename").to_str().unwrap()),
  22. "target content",
  23. )
  24. }
  25. // Normal cases
  26. // Good
  27. #[test]
  28. fn already_reached() {
  29. let dir = get_dir(Some("target content"));
  30. let symbol = get_symbol(dir.path());
  31. assert_eq!(symbol.target_reached().unwrap(), true);
  32. }
  33. // Bad
  34. #[test]
  35. fn wrong_prefix() {
  36. let dir = get_dir(Some("not target content"));
  37. let symbol = get_symbol(dir.path());
  38. assert_eq!(symbol.target_reached().unwrap(), false);
  39. symbol.execute().unwrap();
  40. assert_eq!(symbol.target_reached().unwrap(), true);
  41. }
  42. #[test]
  43. fn wrong_postfix() {
  44. let dir = get_dir(Some("target content not"));
  45. let symbol = get_symbol(dir.path());
  46. assert_eq!(symbol.target_reached().unwrap(), false);
  47. symbol.execute().unwrap();
  48. assert_eq!(symbol.target_reached().unwrap(), true);
  49. }
  50. #[test]
  51. fn empty_file() {
  52. let dir = get_dir(Some(""));
  53. let symbol = get_symbol(dir.path());
  54. assert_eq!(symbol.target_reached().unwrap(), false);
  55. symbol.execute().unwrap();
  56. assert_eq!(symbol.target_reached().unwrap(), true);
  57. }
  58. #[test]
  59. fn no_file() {
  60. let dir = get_dir(None);
  61. let symbol = get_symbol(dir.path());
  62. assert_eq!(symbol.target_reached().unwrap(), false);
  63. symbol.execute().unwrap();
  64. assert_eq!(symbol.target_reached().unwrap(), true);
  65. }
  66. // Exceptional cases
  67. #[test]
  68. fn may_not_read_file() {
  69. let symbol = get_symbol(&Path::new("/etc/passwd"));
  70. assert_eq!(symbol.target_reached().is_err(), true);
  71. }
  72. #[test]
  73. fn may_not_create_file() {
  74. let symbol = get_symbol(&Path::new("/proc/somefile"));
  75. // Could also return an error
  76. assert_eq!(symbol.target_reached().unwrap(), false);
  77. assert!(symbol.execute().is_err());
  78. }
  79. #[test]
  80. fn directory_missing() {
  81. let symbol = get_symbol(&Path::new("/nonexisting"));
  82. // Could also return an error
  83. assert_eq!(symbol.target_reached().unwrap(), false);
  84. assert!(symbol.execute().is_err());
  85. }