@@ -3,7 +3,6 @@ pub mod btrfs;
33
44use crate :: {
55 TrustifyTestContext ,
6- ctx:: migration:: snapshot:: btrfs:: SnapshotProvider ,
76 resource:: { TestResourceExt , defer} ,
87} ;
98use anyhow:: Context ;
@@ -26,95 +25,13 @@ pub struct Snapshot {
2625 pub base : PathBuf ,
2726 pub db_file : String ,
2827 pub storage_file : String ,
28+ #[ cfg( target_os = "linux" ) ]
2929 pub snapshot_file : Option < String > ,
3030 pub strip : usize ,
3131 pub fix_zstd : bool ,
3232}
3333
34- #[ cfg( not( target_os = "linux" ) ) ]
35- impl Snapshot {
36- pub async fn materialize ( self ) -> anyhow:: Result < TrustifyTestContext > {
37- let tmp = tempfile:: TempDir :: new ( ) ?;
38- let ( db, storage, psql) = self . setup ( & tmp) . await ?;
39-
40- Ok ( TrustifyTestContext :: new (
41- db,
42- psql. settings ( ) . port ,
43- storage,
44- defer ( psql) . then ( defer ( tmp) ) ,
45- ) )
46- }
47-
48- pub fn is_supported ( ) -> bool {
49- false
50- }
51- }
52-
53- #[ cfg( target_os = "linux" ) ]
5434impl Snapshot {
55- pub fn is_supported ( ) -> bool {
56- btrfs:: is_supported ( )
57- }
58-
59- /// Ensure that a snapshot, with data, is available
60- ///
61- /// Either by running with a fresh import in a temporary directory. Or, if available, with
62- /// a BTRFS snapshot of an import. If such a snapshot doesn't exist yet, it will be created
63- /// first.
64- pub async fn materialize ( self ) -> anyhow:: Result < TrustifyTestContext > {
65- let running = btrfs:: Running :: new ( SnapshotProvider {
66- id : self . id . clone ( ) ,
67- file : self . snapshot_file . as_ref ( ) . map ( |file| self . base . join ( file) ) ,
68- } )
69- . await ?;
70-
71- log:: info!( "Snapshot state: {running:?}" ) ;
72-
73- Ok ( match running {
74- // We are running with a normal, temporary directory
75- btrfs:: Running :: Temporary ( tmp) => {
76- // set up the content in the target directory
77- let ( db, storage, psql) = self . setup ( & tmp) . await ?;
78-
79- TrustifyTestContext :: new (
80- db,
81- psql. settings ( ) . port ,
82- storage,
83- defer ( psql) . then ( defer ( tmp) ) ,
84- )
85- }
86- // We are running with an existing snapshot, just enable it
87- btrfs:: Running :: Existing ( snapshot) => {
88- // activate the snapshot, starts the database
89- let started = snapshot. start ( ) . await ?;
90-
91- TrustifyTestContext :: new (
92- started. db ( ) . clone ( ) ,
93- started. settings ( ) . port ,
94- started. storage ( ) . clone ( ) ,
95- started,
96- )
97- }
98- // We need to create the snapshot first, then run it
99- btrfs:: Running :: Collecting ( collect) => {
100- // set up the content in preparation directory
101- let ( _, _, psql) = self . setup ( & collect) . await ?;
102-
103- // create the snapshot
104- let snapshot = collect. create ( psql) . await ?;
105- // and activate it
106- let started = snapshot. start ( ) . await ?;
107-
108- TrustifyTestContext :: new (
109- started. db ( ) . clone ( ) ,
110- started. settings ( ) . port ,
111- started. storage ( ) . clone ( ) ,
112- started,
113- )
114- }
115- } )
116- }
117-
11835 /// This performs the actual DB and storage preparation
11936 async fn setup (
12037 self ,
@@ -130,7 +47,8 @@ impl Snapshot {
13047 base,
13148 db_file,
13249 storage_file,
133- snapshot_file : _, // we don't work with the snapshot file
50+ #[ cfg( target_os = "linux" ) ]
51+ snapshot_file: _, // we don't work with the snapshot file
13452 strip,
13553 fix_zstd,
13654 } = self ;
@@ -226,3 +144,90 @@ impl Snapshot {
226144 Ok ( ( db, storage, postgresql) )
227145 }
228146}
147+
148+ #[ cfg( not( target_os = "linux" ) ) ]
149+ impl Snapshot {
150+ pub async fn materialize ( self ) -> anyhow:: Result < TrustifyTestContext > {
151+ let tmp = tempfile:: TempDir :: new ( ) ?;
152+ let ( db, storage, psql) = self . setup ( & tmp) . await ?;
153+
154+ Ok ( TrustifyTestContext :: new (
155+ db,
156+ psql. settings ( ) . port ,
157+ storage,
158+ defer ( psql) . then ( defer ( tmp) ) ,
159+ ) )
160+ }
161+
162+ pub fn is_supported ( ) -> bool {
163+ false
164+ }
165+ }
166+
167+ #[ cfg( target_os = "linux" ) ]
168+ impl Snapshot {
169+ pub fn is_supported ( ) -> bool {
170+ btrfs:: is_supported ( )
171+ }
172+
173+ /// Ensure that a snapshot, with data, is available
174+ ///
175+ /// Either by running with a fresh import in a temporary directory. Or, if available, with
176+ /// a BTRFS snapshot of an import. If such a snapshot doesn't exist yet, it will be created
177+ /// first.
178+ pub async fn materialize ( self ) -> anyhow:: Result < TrustifyTestContext > {
179+ use crate :: ctx:: migration:: snapshot:: btrfs:: SnapshotProvider ;
180+
181+ let running = btrfs:: Running :: new ( SnapshotProvider {
182+ id : self . id . clone ( ) ,
183+ file : self . snapshot_file . as_ref ( ) . map ( |file| self . base . join ( file) ) ,
184+ } )
185+ . await ?;
186+
187+ log:: info!( "Snapshot state: {running:?}" ) ;
188+
189+ Ok ( match running {
190+ // We are running with a normal, temporary directory
191+ btrfs:: Running :: Temporary ( tmp) => {
192+ // set up the content in the target directory
193+ let ( db, storage, psql) = self . setup ( & tmp) . await ?;
194+
195+ TrustifyTestContext :: new (
196+ db,
197+ psql. settings ( ) . port ,
198+ storage,
199+ defer ( psql) . then ( defer ( tmp) ) ,
200+ )
201+ }
202+ // We are running with an existing snapshot, just enable it
203+ btrfs:: Running :: Existing ( snapshot) => {
204+ // activate the snapshot, starts the database
205+ let started = snapshot. start ( ) . await ?;
206+
207+ TrustifyTestContext :: new (
208+ started. db ( ) . clone ( ) ,
209+ started. settings ( ) . port ,
210+ started. storage ( ) . clone ( ) ,
211+ started,
212+ )
213+ }
214+ // We need to create the snapshot first, then run it
215+ btrfs:: Running :: Collecting ( collect) => {
216+ // set up the content in preparation directory
217+ let ( _, _, psql) = self . setup ( & collect) . await ?;
218+
219+ // create the snapshot
220+ let snapshot = collect. create ( psql) . await ?;
221+ // and activate it
222+ let started = snapshot. start ( ) . await ?;
223+
224+ TrustifyTestContext :: new (
225+ started. db ( ) . clone ( ) ,
226+ started. settings ( ) . port ,
227+ started. storage ( ) . clone ( ) ,
228+ started,
229+ )
230+ }
231+ } )
232+ }
233+ }
0 commit comments