tests/Copy-DbaDatabase.Tests.ps1

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
$commandname = $MyInvocation.MyCommand.Name.Replace(".ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
    BeforeAll {
        $BackupLocation = "$script:appeyorlabrepo\singlerestore\singlerestore.bak"
        $NetworkPath = "C:\temp"
        $DBNameBackupRestore = "dbatoolsci_singlerestore"
        $DBNameAttachDetach = "dbatoolsci_detachattach"
        # cleanup
        foreach ($instance in $Instances) {
            Remove-DbaDatabase -SqlInstance $instance -Confirm:$false -Database $DBNameBackupRestore,$DBNameAttachDetach
        }
    }
    AfterAll {
        foreach ($instance in $Instances) {
            Remove-DbaDatabase -SqlInstance $instance -Confirm:$false -Database $DBNameBackupRestore,$DBNameAttachDetach
        }
    }
    
    
    # Restore and set owner for Single Restore
    $null = Restore-DbaDatabase -SqlInstance $script:instance1 -Path $script:appeyorlabrepo\singlerestore\singlerestore.bak -WithReplace -DatabaseName $DBNameBackupRestore
    Set-DbaDatabaseOwner -SqlInstance $script:instance1 -Database $DBNameBackupRestore -TargetLogin sa
    
    Context "Restores database with the same properties." {
        It "Should copy a database and retain its name, recovery model, and status." {
            
            Copy-DbaDatabase -Source $script:instance1 -Destination $script:instance2 -Database $DBNameBackupRestore -BackupRestore -NetworkShare $NetworkPath
            
            $db1 = Get-DbaDatabase -SqlInstance $script:instance1 -Database $DBNameBackupRestore
            $db1 | Should Not BeNullOrEmpty
            $db2 = Get-DbaDatabase -SqlInstance $script:instance2 -Database $DBNameBackupRestore
            $db2 | Should Not BeNullOrEmpty
            
            # Compare its valuable.
            $db1.Name | Should Be $db2.Name
            $db1.RecoveryModel | Should Be $db2.RecoveryModel
            $db1.Status | Should be $db2.Status
        }
    }
    
    Context "Doesn't write over existing databases" {
        It "Should just warn" {
            $result = Copy-DbaDatabase -Source $script:instance1 -Destination $script:instance2 -Database $DBNameBackupRestore -BackupRestore -NetworkShare $NetworkPath -WarningVariable warning  3>&1
            $warning | Should Match "exists at destination"
        }
    }
    
    foreach ($instance in $Instances) {
        Remove-DbaDatabase -SqlInstance $instance -Confirm:$false -Database $DBNameBackupRestore
    }
    
    Context "Detach, copies and attaches database successfully." {
        It "Should be success" {
            $null = Restore-DbaDatabase -SqlInstance $script:instance1 -Path $script:appeyorlabrepo\detachattach\detachattach.bak -WithReplace -DatabaseName $DBNameAttachDetach
            $results = Copy-DbaDatabase -Source $script:instance1 -Destination $script:instance2 -Database $DBNameAttachDetach -DetachAttach -Reattach -Force -WarningAction SilentlyContinue
            $results.Status | Should Be "Successful"
        }
    }
    
    Context "Database with the same properties." {
        It "should not be null" {
            
            $db1 = (Connect-DbaSqlServer -SqlInstance localhost).Databases[$DBNameAttachDetach]
            $db2 = (Connect-DbaSqlServer -SqlInstance localhost\sql2016).Databases[$DBNameAttachDetach]
            
            $db1 | Should Not Be $null
            $db2 | Should Not Be $null
            
            $db1.Name | Should Be $DBNameAttachDetach
            $db2.Name | Should Be $DBNameAttachDetach
        }
    <#
  It "Name, recovery model, and status should match" {
   # This is crazy
   (Connect-DbaSqlServer -SqlInstance localhost).Databases['detachattach'].Name | Should Be (Connect-DbaSqlServer -SqlInstance localhost\sql2016).Databases['detachattach'].Name
   (Connect-DbaSqlServer -SqlInstance localhost).Databases['detachattach'].Tables.Count | Should Be (Connect-DbaSqlServer -SqlInstance localhost\sql2016).Databases['detachattach'].Tables.Count
   (Connect-DbaSqlServer -SqlInstance localhost).Databases['detachattach'].Status | Should Be (Connect-DbaSqlServer -SqlInstance localhost\sql2016).Databases['detachattach'].Status
    
  }
 }
  
 Context "Clean up" {
  foreach ($instance in $instances) {
   Get-DbaDatabase -SqlInstance $instance -NoSystemDb | Remove-DbaDatabase -Confirm:$false
  }
  #>

    }
    
}