-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage_management.rs
More file actions
176 lines (148 loc) · 6.72 KB
/
storage_management.rs
File metadata and controls
176 lines (148 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! Storage management integration test for the Storage Rust bindings
//!
//! This test demonstrates how to manage storage operations:
//! - List manifests
//! - Check storage space
//! - Fetch manifest information
//! - Delete content
//! - Check content existence
use std::fs::File;
use std::io::Write;
use storage_bindings::{LogLevel, StorageConfig, StorageNode};
use tempfile::tempdir;
#[tokio::test(flavor = "multi_thread")]
async fn test_storage_management() -> Result<(), Box<dyn std::error::Error>> {
let _ = env_logger::try_init();
let temp_dir = tempdir()?;
let file_path = temp_dir.path().join("test_file.txt");
// Create a test file to upload
let mut file = File::create(&file_path)?;
file.write_all(b"This is a test file for storage management example.")?;
file.sync_all()?;
let config = StorageConfig::new()
.log_level(LogLevel::Error)
.data_dir(temp_dir.path().join("storage_data"))
.block_retries(3000)
.discovery_port(8097);
let node = StorageNode::new(config).await?;
node.start().await?;
// Get initial storage information
println!("Initial storage information:");
let space_info = storage_bindings::space(&node).await?;
println!(" Quota: {} bytes", space_info.quota_max_bytes);
println!(" Used: {} bytes", space_info.quota_used_bytes);
println!(" Reserved: {} bytes", space_info.quota_reserved_bytes);
println!(" Total blocks: {}", space_info.total_blocks);
// List initial manifests (should be empty)
println!("\nInitial manifests:");
let manifests = storage_bindings::manifests(&node).await?;
println!(" Number of manifests: {}", manifests.len());
assert_eq!(manifests.len(), 0, "Should start with no manifests");
// Upload a file to have some content
println!("\nUploading test file:");
let upload_options = storage_bindings::UploadOptions::new()
.filepath(&file_path)
.on_progress(|progress| {
println!(
" Upload progress: {} bytes ({}%)",
progress.bytes_uploaded,
(progress.percentage * 100.0) as u32
);
});
let upload_result = storage_bindings::upload_file(&node, upload_options).await?;
println!(" CID: {}", upload_result.cid);
println!(" Size: {} bytes", upload_result.size);
// Check if content exists
println!("\nChecking content existence:");
let exists = storage_bindings::exists(&node, &upload_result.cid).await?;
assert!(exists, "Uploaded content should exist");
println!(" Content exists: {}", exists);
// Check non-existent content
let non_existent_cid = "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi";
let non_existent = storage_bindings::exists(&node, non_existent_cid).await?;
assert!(!non_existent, "Non-existent content should not exist");
println!(" Non-existent content exists: {}", non_existent);
// Fetch manifest information
println!("\nFetching manifest information:");
let manifest = storage_bindings::fetch(&node, &upload_result.cid).await?;
println!(" CID: {}", manifest.cid);
println!(" Size: {} bytes", manifest.dataset_size);
println!(" Block size: {} bytes", manifest.block_size);
println!(" Filename: {}", manifest.filename);
println!(" Mimetype: {}", manifest.mimetype);
println!(" Protected: {}", manifest.protected);
// List manifests after upload
println!("\nManifests after upload:");
let manifests = storage_bindings::manifests(&node).await?;
println!(" Number of manifests: {}", manifests.len());
assert_eq!(manifests.len(), 1, "Should have 1 manifest after upload");
for (i, manifest) in manifests.iter().enumerate() {
println!(
" Manifest {}: CID={}, Size={} bytes",
i, manifest.cid, manifest.dataset_size
);
}
// Get updated storage information
println!("\nUpdated storage information:");
let space_info = storage_bindings::space(&node).await?;
println!(" Quota: {} bytes", space_info.quota_max_bytes);
println!(" Used: {} bytes", space_info.quota_used_bytes);
println!(" Reserved: {} bytes", space_info.quota_reserved_bytes);
println!(" Total blocks: {}", space_info.total_blocks);
// Upload another file for more content
println!("\nUploading second test file:");
let file_path2 = temp_dir.path().join("test_file2.txt");
let mut file2 = File::create(&file_path2)?;
file2.write_all(b"This is a second test file for storage management.")?;
file2.sync_all()?;
let upload_options2 = storage_bindings::UploadOptions::new().filepath(&file_path2);
let upload_result2 = storage_bindings::upload_file(&node, upload_options2).await?;
println!(" CID: {}", upload_result2.cid);
println!(" Size: {} bytes", upload_result2.size);
// List manifests after second upload
println!("\nManifests after second upload:");
let manifests = storage_bindings::manifests(&node).await?;
println!(" Number of manifests: {}", manifests.len());
assert_eq!(
manifests.len(),
2,
"Should have 2 manifests after second upload"
);
for (i, manifest) in manifests.iter().enumerate() {
println!(
" Manifest {}: CID={}, Size={} bytes",
i, manifest.cid, manifest.dataset_size
);
}
// Delete the first file
println!("\nDeleting first file:");
storage_bindings::delete(&node, &upload_result.cid).await?;
println!(" First file deleted successfully");
// Check if deleted content still exists
println!("\nChecking deleted content:");
let exists_after_delete = storage_bindings::exists(&node, &upload_result.cid).await?;
assert!(!exists_after_delete, "Deleted content should not exist");
println!(" Deleted content exists: {}", exists_after_delete);
// List manifests after deletion
println!("\nManifests after deletion:");
let manifests = storage_bindings::manifests(&node).await?;
println!(" Number of manifests: {}", manifests.len());
assert_eq!(manifests.len(), 1, "Should have 1 manifest after deletion");
for (i, manifest) in manifests.iter().enumerate() {
println!(
" Manifest {}: CID={}, Size={} bytes",
i, manifest.cid, manifest.dataset_size
);
}
// Get final storage information
println!("\nFinal storage information:");
let space_info = storage_bindings::space(&node).await?;
println!(" Quota: {} bytes", space_info.quota_max_bytes);
println!(" Used: {} bytes", space_info.quota_used_bytes);
println!(" Reserved: {} bytes", space_info.quota_reserved_bytes);
println!(" Total blocks: {}", space_info.total_blocks);
// Cleanup
node.stop().await?;
node.destroy().await?;
Ok(())
}