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.

119 lines
2.8 KiB

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