/ tests / integration / test_configuration.rs
test_configuration.rs
  1  //! Integration tests for configuration management.
  2  //!
  3  //! Tests: defaults, custom config, migration, validation
  4  
  5  mod common;
  6  
  7  use common::assertions::*;
  8  use common::fixtures::*;
  9  use common::TestEnv;
 10  
 11  #[test]
 12  fn test_default_config_generation() {
 13      // Setup
 14      let env = TestEnv::new().expect("Failed to create test environment");
 15  
 16      // Generate default config
 17      let config_path = env.config_dir.join("adnet.toml");
 18      let default_config = r#"[install]
 19  method = "binary"
 20  registry_url = "https://releases.ac-dc.network/registry.toml"
 21  
 22  [verification]
 23  verify_checksums = true
 24  verify_signatures = false
 25  
 26  [service]
 27  auto_start = true
 28  auto_restart = true
 29  
 30  [network]
 31  rpc_host = "127.0.0.1"
 32  rpc_port = 3030
 33  "#;
 34  
 35      std::fs::write(&config_path, default_config).expect("Failed to write default config");
 36  
 37      // Verify config created
 38      assert!(config_path.exists());
 39  
 40      // Parse and validate
 41      let parsed: toml::Value =
 42          toml::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
 43  
 44      assert_eq!(parsed["install"]["method"], "binary");
 45      assert_eq!(parsed["verification"]["verify_checksums"], true);
 46      assert_eq!(parsed["service"]["auto_start"], true);
 47      assert_eq!(parsed["network"]["rpc_port"], 3030);
 48  }
 49  
 50  #[test]
 51  fn test_custom_config_validation() {
 52      // Setup
 53      let env = TestEnv::new().expect("Failed to create test environment");
 54  
 55      // Create custom config
 56      let config_path = env.config_dir.join("custom.toml");
 57      let custom_config = r#"[install]
 58  method = "source"
 59  registry_url = "https://custom.example.com/registry.toml"
 60  
 61  [verification]
 62  verify_checksums = true
 63  verify_signatures = true
 64  
 65  [service]
 66  auto_start = false
 67  auto_restart = false
 68  
 69  [network]
 70  rpc_host = "0.0.0.0"
 71  rpc_port = 8080
 72  
 73  [custom]
 74  node_name = "test-node-1"
 75  network_id = "testnet"
 76  "#;
 77  
 78      std::fs::write(&config_path, custom_config).expect("Failed to write custom config");
 79  
 80      // Verify and validate custom settings
 81      assert_config_contains(&config_path, "method = \"source\"");
 82      assert_config_contains(&config_path, "node_name = \"test-node-1\"");
 83      assert_config_contains(&config_path, "network_id = \"testnet\"");
 84  
 85      // Parse and validate structure
 86      let parsed: toml::Value =
 87          toml::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
 88  
 89      assert_eq!(parsed["install"]["method"], "source");
 90      assert_eq!(parsed["network"]["rpc_port"], 8080);
 91      assert_eq!(parsed["custom"]["node_name"], "test-node-1");
 92  }
 93  
 94  #[test]
 95  fn test_config_migration_v1_to_v2() {
 96      // Setup
 97      let env = TestEnv::new().expect("Failed to create test environment");
 98  
 99      // Create v1 config (old format)
100      let config_v1_path = env.config_dir.join("adnet.v1.toml");
101      let config_v1 = r#"# Version 1 config
102  registry = "https://old.ac-dc.network/registry.toml"
103  verify = true
104  port = 3030
105  "#;
106  
107      std::fs::write(&config_v1_path, config_v1).expect("Failed to write v1 config");
108  
109      // Simulate migration to v2
110      let config_v2_path = env.config_dir.join("adnet.toml");
111      let config_v2 = r#"# Version 2 config
112  version = "2.0"
113  
114  [install]
115  method = "binary"
116  registry_url = "https://old.ac-dc.network/registry.toml"
117  
118  [verification]
119  verify_checksums = true
120  
121  [network]
122  rpc_port = 3030
123  "#;
124  
125      std::fs::write(&config_v2_path, config_v2).expect("Failed to write v2 config");
126  
127      // Verify migration
128      assert!(config_v2_path.exists());
129      assert_config_contains(&config_v2_path, "version = \"2.0\"");
130      assert_config_contains(&config_v2_path, "[install]");
131      assert_config_contains(&config_v2_path, "[verification]");
132  
133      // Backup v1 config
134      let backup_path = env.config_dir.join("adnet.v1.toml.backup");
135      std::fs::copy(&config_v1_path, &backup_path).expect("Failed to backup v1 config");
136  
137      assert!(backup_path.exists());
138  }
139  
140  #[test]
141  fn test_invalid_config_rejection() {
142      // Setup
143      let env = TestEnv::new().expect("Failed to create test environment");
144  
145      // Create invalid config (malformed TOML)
146      let invalid_config_path = env.config_dir.join("invalid.toml");
147      let invalid_config = r#"[install
148  method = "binary"
149  [verification
150  verify_checksums = true
151  "#; // Missing closing brackets
152  
153      std::fs::write(&invalid_config_path, invalid_config).expect("Failed to write invalid config");
154  
155      // Attempt to parse - should fail
156      let content = std::fs::read_to_string(&invalid_config_path).unwrap();
157      let parse_result: Result<toml::Value, _> = toml::from_str(&content);
158  
159      assert!(parse_result.is_err(), "Invalid config should fail to parse");
160  
161      // Create config with invalid values
162      let bad_values_path = env.config_dir.join("bad-values.toml");
163      let bad_values_config = r#"[install]
164  method = "invalid_method"
165  
166  [network]
167  rpc_port = 999999  # Port out of range
168  "#;
169  
170      std::fs::write(&bad_values_path, bad_values_config).expect("Failed to write bad values");
171  
172      // Parse succeeds but validation should fail
173      let parsed: toml::Value =
174          toml::from_str(&std::fs::read_to_string(&bad_values_path).unwrap()).unwrap();
175  
176      // Verify invalid values detected
177      let method = parsed["install"]["method"].as_str().unwrap();
178      assert_eq!(method, "invalid_method");
179  
180      let port = parsed["network"]["rpc_port"].as_integer().unwrap();
181      assert!(port > 65535, "Port should be out of valid range");
182  
183      // Real validator would reject these values
184  }