libside 0.3.0

a library for building configuration management tools
Documentation
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, fmt::Display, path::PathBuf};

use super::apt::AptPackage;
use super::systemd::ServiceRunning;
use super::{
    path::{FromPackage, Path},
    systemd::SystemdService,
    Context, Group, User,
};
use crate::graph::GraphNodeReference;
use crate::requirements::{Requirement, Supports};
use crate::system::{NeverError, System};

pub struct Database {
    name: String,
    node: GraphNodeReference,
}

impl std::fmt::Display for Database {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.name)
    }
}

pub struct MySqlUser {
    name: String,
    node: GraphNodeReference,
}

impl std::fmt::Display for MySqlUser {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.name)
    }
}

pub struct MySqlService {
    service: SystemdService,
}

pub struct MariaDb {
    service: MySqlService,
    node: GraphNodeReference,
}

impl AptPackage for MariaDb {
    const NAME: &'static str = "mariadb-server";

    fn create(node: GraphNodeReference) -> Self {
        MariaDb {
            service: MySqlService {
                service: SystemdService::from_name_unchecked("mariadb", node, vec![node]),
            },
            node,
        }
    }

    fn graph_node(&self) -> GraphNodeReference {
        self.node
    }
}

impl MariaDb {
    pub fn binary(&self) -> Path<FromPackage> {
        Path {
            base: PathBuf::from("/usr/sbin/mariadb"),
            path: PathBuf::new(),
            loc: FromPackage,
            node: Some(self.graph_node()),
        }
    }

    pub fn default_service(&mut self) -> &mut MySqlService {
        &mut self.service
    }

    pub fn mysql_user(&self) -> User {
        User {
            uid: None,
            name: "mysql".to_owned(),
            node: self.graph_node(),
        }
    }

    pub fn mysql_group(&self) -> Group {
        Group {
            gid: None,
            name: "mysql".to_owned(),
            node: self.graph_node(),
        }
    }
}

pub struct RunningMySqlService<'a>(GraphNodeReference, &'a ());

impl MySqlService {
    /// Makes sure mysqld is running (see [ServiceRunning::is_running])
    pub fn run<R: Requirement>(&mut self, context: &mut Context<R>) -> RunningMySqlService
    where
        R: Supports<ServiceRunning>,
    {
        let node = ServiceRunning::is_running(context, &self.service);
        RunningMySqlService(node, &())
    }

    /// Restarts mysqld (see [ServiceRunning::restart])
    pub fn restart<R: Requirement>(&mut self, context: &mut Context<R>) -> RunningMySqlService
    where
        R: Supports<ServiceRunning>,
    {
        let node = ServiceRunning::restart(context, &self.service);
        RunningMySqlService(node, &())
    }

    /// Return a reference to the actual SystemdService.
    pub fn inner_service(&self) -> &SystemdService {
        &self.service
    }
}

impl<'a> RunningMySqlService<'a> {
    pub fn create_database<R: Requirement>(&self, context: &mut Context<R>, name: &str) -> Database
    where
        R: Supports<CreateMySqlDatabase>,
    {
        let deps = [self.0];
        let node = context.add_node(CreateMySqlDatabase::new(name), &deps);
        Database {
            name: name.to_string(),
            node,
        }
    }

    pub fn create_user<R: Requirement>(
        &self,
        context: &mut Context<R>,
        name: &str,
        pass: &str,
    ) -> MySqlUser
    where
        R: Supports<CreateMySqlUser>,
    {
        let deps = [self.0];
        let node = context.add_node(CreateMySqlUser::new(name, pass), &deps);
        MySqlUser {
            name: name.to_string(),
            node,
        }
    }

    pub fn unix_socket(&self) -> Path<FromPackage> {
        Path {
            base: PathBuf::from("/"),
            path: PathBuf::from("var/run/mysqld/mysqld.sock"),
            loc: FromPackage,
            node: Some(self.0),
        }
    }
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Privilege {
    Alter,
    Create,
    Delete,
    Drop,
    GrantOption,
    Index,
    Insert,
    LockTables,
    Select,
    Update,
    ShowView,
    Trigger,
}

impl AsRef<str> for Privilege {
    fn as_ref(&self) -> &str {
        use Privilege::*;
        match self {
            Alter => "ALTER",
            Create => "CREATE",
            Delete => "DELETE",
            Drop => "DROP",
            GrantOption => "GRANT OPTION",
            Index => "INDEX",
            Insert => "INSERT",
            LockTables => "LOCK TABLES",
            Select => "SELECT",
            Update => "UPDATE",
            ShowView => "SHOW VIEW",
            Trigger => "TRIGGER",
        }
    }
}

impl PartialOrd for Privilege {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.as_ref().partial_cmp(other.as_ref())
    }
}

impl Ord for Privilege {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.as_ref().cmp(other.as_ref())
    }
}

impl MySqlUser {
    pub fn grant<R: Requirement>(
        &self,
        context: &mut Context<R>,
        privileges: HashSet<Privilege>,
        on: &Database,
    ) -> GraphNodeReference
    where
        R: Supports<CreateMySqlGrant>,
    {
        let privileges = privileges.iter().sorted().map(Privilege::as_ref).join(", ");
        context.add_node(
            CreateMySqlGrant::new(&self.name, on.name.to_string(), &privileges),
            &[self.node, on.node],
        )
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CreateMySqlDatabase {
    name: String,
}

impl CreateMySqlDatabase {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum MySqlError<S: System> {
    #[error("unable to execute mysql: {0}")]
    FailedToStart(S::CommandError),

    #[error("mysql query '{query}' failed: {stdout}{stderr}")]
    Unsuccessful {
        query: String,
        stdout: String,
        stderr: String,
    },
}

impl Requirement for CreateMySqlDatabase {
    type CreateError<S: System> = MySqlError<S>;
    type ModifyError<S: System> = NeverError;
    type DeleteError<S: System> = NeverError;
    type HasBeenCreatedError<S: System> = MySqlError<S>;

    fn create<S: crate::system::System>(&self, system: &mut S) -> Result<(), Self::CreateError<S>> {
        let query = format!("CREATE DATABASE `{}`;", self.name);
        let result = system
            .execute_command_with_input("mysql", &[], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        Ok(())
    }

    fn modify<S: crate::system::System>(
        &self,
        _system: &mut S,
    ) -> Result<(), Self::ModifyError<S>> {
        Ok(())
    }

    fn delete<S: crate::system::System>(
        &self,
        _system: &mut S,
    ) -> Result<(), Self::DeleteError<S>> {
        unimplemented!()
    }

    fn has_been_created<S: crate::system::System>(
        &self,
        system: &mut S,
    ) -> Result<bool, Self::HasBeenCreatedError<S>> {
        let query = format!("SHOW DATABASES LIKE '{}';", self.name);
        let result = system
            .execute_command_with_input("mysql", &["--column-names=false"], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        return Ok(result.stdout_as_str().trim() == self.name);
    }

    fn affects(&self, other: &Self) -> bool {
        self.name == other.name
    }

    fn supports_modifications(&self) -> bool {
        false
    }
    fn can_undo(&self) -> bool {
        false
    }
    fn may_pre_exist(&self) -> bool {
        true
    }

    fn verify<S: System>(&self, system: &mut S) -> Result<bool, ()> {
        Ok(self.has_been_created(system).unwrap())
    }

    const NAME: &'static str = "mysql_database";
}

impl Display for CreateMySqlDatabase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "mysqldb({})", self.name)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CreateMySqlUser {
    name: String,
    pass: String,
}

impl CreateMySqlUser {
    pub fn new(name: &str, pass: &str) -> Self {
        Self {
            name: name.to_string(),
            pass: pass.to_string(),
        }
    }
}

impl Requirement for CreateMySqlUser {
    type CreateError<S: System> = MySqlError<S>;
    type ModifyError<S: System> = MySqlError<S>;
    type DeleteError<S: System> = MySqlError<S>;
    type HasBeenCreatedError<S: System> = MySqlError<S>;

    fn create<S: crate::system::System>(&self, system: &mut S) -> Result<(), Self::CreateError<S>> {
        // TODO: Escape username & password
        let query = format!(
            "CREATE USER '{}'@'localhost' IDENTIFIED BY '{}'; FLUSH PRIVILEGES;",
            self.name, self.pass
        );
        let result = system
            .execute_command_with_input("mysql", &[], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        Ok(())
    }

    fn modify<S: crate::system::System>(&self, system: &mut S) -> Result<(), Self::ModifyError<S>> {
        // TODO: Escape username & password
        let query = format!(
            "ALTER USER '{}'@'localhost' IDENTIFIED BY '{}'; FLUSH PRIVILEGES;",
            self.name, self.pass
        );
        let result = system
            .execute_command_with_input("mysql", &[], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        Ok(())
    }

    fn delete<S: crate::system::System>(&self, system: &mut S) -> Result<(), Self::DeleteError<S>> {
        let query = format!("DROP USER '{}'@'localhost'; FLUSH PRIVILEGES;", self.name);
        let result = system
            .execute_command_with_input("mysql", &[], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        Ok(())
    }

    fn has_been_created<S: crate::system::System>(
        &self,
        system: &mut S,
    ) -> Result<bool, Self::HasBeenCreatedError<S>> {
        let query = format!(
            "SELECT User FROM mysql.user WHERE User = '{}' AND Host = 'localhost';",
            self.name
        );
        let result = system
            .execute_command_with_input("mysql", &["--column-names=false"], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        return Ok(result.stdout_as_str().trim() == self.name);
    }

    fn affects(&self, other: &Self) -> bool {
        self.name == other.name
    }

    fn supports_modifications(&self) -> bool {
        true
    }
    fn can_undo(&self) -> bool {
        true
    }
    fn may_pre_exist(&self) -> bool {
        false
    }

    fn verify<S: System>(&self, system: &mut S) -> Result<bool, ()> {
        Ok(self.has_been_created(system).unwrap())
    }

    const NAME: &'static str = "mysql_user";
}

impl Display for CreateMySqlUser {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "mysqluser({})", self.name)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CreateMySqlGrant {
    user: String,
    database: String,
    privileges: String,
}

impl CreateMySqlGrant {
    pub fn new(name: &str, database: String, privileges: &str) -> Self {
        Self {
            user: name.to_string(),
            database: database.to_string(),
            privileges: privileges.to_string(),
        }
    }
}

impl Requirement for CreateMySqlGrant {
    type CreateError<S: System> = MySqlError<S>;
    type ModifyError<S: System> = MySqlError<S>;
    type DeleteError<S: System> = MySqlError<S>;
    type HasBeenCreatedError<S: System> = MySqlError<S>;

    fn create<S: crate::system::System>(&self, system: &mut S) -> Result<(), Self::CreateError<S>> {
        let query = format!(
            "GRANT {p} ON `{db}`.* TO '{u}'@'localhost'; FLUSH PRIVILEGES;",
            p = self.privileges,
            db = self.database,
            u = self.user
        );
        let result = system
            .execute_command_with_input("mysql", &[], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        Ok(())
    }

    fn modify<S: crate::system::System>(&self, system: &mut S) -> Result<(), Self::ModifyError<S>> {
        let query = format!("REVOKE ALL PRIVILEGES ON `{db}`.* FROM '{u}'@'localhost'; GRANT {p} ON `{db}`.* TO '{u}'@'localhost'; FLUSH PRIVILEGES; FLUSH PRIVILEGES;", p = self.privileges, db = self.database, u = self.user);
        let result = system
            .execute_command_with_input("mysql", &[], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        Ok(())
    }

    fn delete<S: crate::system::System>(&self, system: &mut S) -> Result<(), Self::DeleteError<S>> {
        let query = format!(
            "REVOKE ALL PRIVILEGES ON `{db}`.* FROM '{u}'@'localhost'; FLUSH PRIVILEGES;",
            db = self.database,
            u = self.user
        );
        let result = system
            .execute_command_with_input("mysql", &[], query.as_bytes())
            .map_err(MySqlError::FailedToStart)?;
        result
            .successful()
            .map_err(|(stdout, stderr)| MySqlError::Unsuccessful {
                query,
                stdout: stdout.to_string(),
                stderr: stderr.to_string(),
            })?;

        Ok(())
    }

    fn has_been_created<S: crate::system::System>(
        &self,
        system: &mut S,
    ) -> Result<bool, Self::HasBeenCreatedError<S>> {
        let query = format!("SHOW GRANTS FOR {}@'localhost'", self.user);
        let result = system
            .execute_command_with_input("mysql", &["--column-names=false"], query.as_bytes())
            .unwrap();
        if !result.is_success() && result.stderr_as_str().contains("ERROR 1141 (42000)") {
            // ERROR 1141 (42000) at line 1: There is no such grant defined for user ...
            return Ok(false);
        }

        assert!(result.is_success()); // TODO

        let grants = result.stdout_as_str();
        println!("Grants: {}", grants);

        Ok(grants.contains(&format!(
            "ON `{}`.* TO `{}`@`localhost`",
            self.database, self.user
        )))
    }

    fn affects(&self, other: &Self) -> bool {
        self.user == other.user
    }

    fn supports_modifications(&self) -> bool {
        true
    }
    fn can_undo(&self) -> bool {
        true
    }
    fn may_pre_exist(&self) -> bool {
        true
    }

    fn verify<S: System>(&self, system: &mut S) -> Result<bool, ()> {
        Ok(self.has_been_created(system).unwrap())
    }

    const NAME: &'static str = "mysql_grant";
}

impl Display for CreateMySqlGrant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "grant({})", self.user)
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        builder::mysql::{CreateMySqlDatabase, CreateMySqlGrant, CreateMySqlUser},
        requirements::Requirement,
        system::System,
        testing::LxcInstance,
    };

    #[test]
    pub fn serialize_deserialize_create_mysql_database() {
        let r = CreateMySqlDatabase {
            name: String::from("foo"),
        };
        let json = r#"{"name":"foo"}"#;

        assert_eq!(serde_json::to_string(&r).unwrap(), json);
        assert_eq!(r, serde_json::from_str(json).unwrap());
    }

    #[test]
    #[ignore]
    pub fn lxc_create_mysql_database() {
        let mut sys = LxcInstance::start(LxcInstance::DEFAULT_IMAGE);
        let p = CreateMySqlDatabase {
            name: String::from("foo"),
        };

        sys.execute_command("apt-get", &["install", "-y", "mariadb-server"])
            .unwrap();

        assert!(!p.has_been_created(&mut sys).unwrap());
        assert!(!p.verify(&mut sys).unwrap());

        p.create(&mut sys).unwrap();

        assert!(p.has_been_created(&mut sys).unwrap());
        assert!(p.verify(&mut sys).unwrap());

        // p.delete(&mut sys).unwrap();

        // assert!(!p.has_been_created(&mut sys).unwrap());
        // assert!(!p.verify(&mut sys).unwrap());
    }

    #[test]
    pub fn serialize_deserialize_create_mysql_user() {
        let r = CreateMySqlUser {
            name: String::from("foo"),
            pass: String::from("bar"),
        };
        let json = r#"{"name":"foo","pass":"bar"}"#;

        assert_eq!(serde_json::to_string(&r).unwrap(), json);
        assert_eq!(r, serde_json::from_str(json).unwrap());
    }

    #[test]
    #[ignore]
    pub fn lxc_create_mysql_user() {
        let mut sys = LxcInstance::start(LxcInstance::DEFAULT_IMAGE);
        let p = CreateMySqlUser {
            name: String::from("foo"),
            pass: String::from("bar"),
        };

        sys.execute_command("apt-get", &["install", "-y", "mariadb-server"])
            .unwrap();

        assert!(!p.has_been_created(&mut sys).unwrap());
        assert!(!p.verify(&mut sys).unwrap());

        p.create(&mut sys).unwrap();

        assert!(p.has_been_created(&mut sys).unwrap());
        assert!(p.verify(&mut sys).unwrap());

        assert!(
            sys.execute_command_with_input("mysql", &["-ufoo", "-pbar"], "SELECT 1;".as_bytes())
                .unwrap()
                .is_success(),
            "User was not created correctly"
        );

        p.delete(&mut sys).unwrap();

        assert!(!p.has_been_created(&mut sys).unwrap());
        assert!(!p.verify(&mut sys).unwrap());
    }

    #[test]
    pub fn serialize_deserialize_create_mysql_grant() {
        let r = CreateMySqlGrant {
            user: String::from("foo"),
            database: String::from("bar"),
            privileges: String::from("baz"),
        };
        let json = r#"{"user":"foo","database":"bar","privileges":"baz"}"#;

        assert_eq!(serde_json::to_string(&r).unwrap(), json);
        assert_eq!(r, serde_json::from_str(json).unwrap());
    }

    #[test]
    #[ignore]
    pub fn lxc_create_mysql_grant() {
        let mut sys = LxcInstance::start(LxcInstance::DEFAULT_IMAGE);
        let pre1 = CreateMySqlUser {
            name: String::from("foo"),
            pass: String::from("bar"),
        };
        let pre2 = CreateMySqlDatabase {
            name: String::from("baz"),
        };
        let p = CreateMySqlGrant {
            user: String::from("foo"),
            database: String::from("baz"),
            privileges: String::from("SELECT"),
        };

        sys.execute_command("apt-get", &["install", "-y", "mariadb-server"])
            .unwrap();

        // Check when user and db don't exist
        assert!(!p.has_been_created(&mut sys).unwrap());
        assert!(!p.verify(&mut sys).unwrap());

        pre1.create(&mut sys).unwrap();

        // Check when only user exists
        assert!(!p.has_been_created(&mut sys).unwrap());
        assert!(!p.verify(&mut sys).unwrap());

        pre1.delete(&mut sys).unwrap();
        pre2.create(&mut sys).unwrap();

        // Check when only db exists
        assert!(!p.has_been_created(&mut sys).unwrap());
        assert!(!p.verify(&mut sys).unwrap());

        pre1.create(&mut sys).unwrap();

        // Check when both db and user exist
        assert!(!p.has_been_created(&mut sys).unwrap());
        assert!(!p.verify(&mut sys).unwrap());

        p.create(&mut sys).unwrap();

        assert!(p.has_been_created(&mut sys).unwrap());
        assert!(p.verify(&mut sys).unwrap());

        p.delete(&mut sys).unwrap();

        assert!(!p.has_been_created(&mut sys).unwrap());
        assert!(!p.verify(&mut sys).unwrap());
    }
}