Skip to content

Commit

Permalink
Add SQL Power Plan and Remote Connectivity Tests (#798)
Browse files Browse the repository at this point in the history
* Add power plan and remote auth tests for SQL

WIP: Investigating an issue with converting the file output

* Change error approach on server db query

Fixes unused variable issue doing it the other way

* Switching test approach

Easier to run/debug

* Update to use return from script instead of metadata

Much simpler

* Update result logic

Had it backwards at first
  • Loading branch information
jjerger authored Sep 21, 2023
1 parent a369f05 commit 907340d
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 2 deletions.
79 changes: 77 additions & 2 deletions imagetest/test_suites/sql/setup.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,94 @@
package sql

import (
"embed"

"github.com/GoogleCloudPlatform/guest-test-infra/imagetest"
)

// Name is the name of the test package. It must match the directory name.
var Name = "sql"

// InstanceConfig for setting up test VMs.
type InstanceConfig struct {
name string
ip string
}

var serverConfig = InstanceConfig{name: "server-vm", ip: "192.168.0.10"}
var clientConfig = InstanceConfig{name: "client-vm", ip: "192.168.0.11"}

//go:embed *
var scripts embed.FS

const (
serverStartupScriptURL = "startupscripts/remote_auth_server_setup.ps1"
clientStartupScriptURL = "startupscripts/remote_auth_client_setup.ps1"
)

// TestSetup sets up the test workflow.
func TestSetup(t *imagetest.TestWorkflow) error {
vm, err := t.CreateTestVM("vm")
defaultNetwork, err := t.CreateNetwork("default-network", false)
if err != nil {
return err
}
defaultSubnetwork, err := defaultNetwork.CreateSubnetwork("default-subnetwork", "192.168.0.0/24")
if err != nil {
return err
}
if err := defaultNetwork.CreateFirewallRule("allow-sql-tcp", "tcp", []string{"135", "1433", "1434", "4022", "5022"}, []string{"192.168.0.0/24"}); err != nil {
return err
}
if err := defaultNetwork.CreateFirewallRule("allow-sql-udp", "udp", []string{"1434"}, []string{"192.168.0.0/24"}); err != nil {
return err
}

// Get the startup scripts as byte arrays.
serverStartupByteArr, err := scripts.ReadFile(serverStartupScriptURL)
if err != nil {
return err
}
clientStartupByteArr, err := scripts.ReadFile(clientStartupScriptURL)
if err != nil {
return err
}
serverStartup := string(serverStartupByteArr)
clientStartup := string(clientStartupByteArr)

serverVM, err := t.CreateTestVM(serverConfig.name)
if err != nil {
return err
}
if err := serverVM.AddCustomNetwork(defaultNetwork, defaultSubnetwork); err != nil {
return err
}
if err := serverVM.SetPrivateIP(defaultNetwork, serverConfig.ip); err != nil {
return err
}

clientVM, err := t.CreateTestVM(clientConfig.name)
if err != nil {
return err
}
if err := clientVM.AddCustomNetwork(defaultNetwork, defaultSubnetwork); err != nil {
return err
}
if err := clientVM.SetPrivateIP(defaultNetwork, clientConfig.ip); err != nil {
return err
}
clientVM.AddMetadata("enable-guest-attributes", "TRUE")
clientVM.AddMetadata("sqltarget", serverConfig.ip)

serverVM.AddMetadata("windows-startup-script-ps1", serverStartup)
clientVM.AddMetadata("windows-startup-script-ps1", clientStartup)

vm1, err := t.CreateTestVM("vm1")
if err != nil {
return err
}
vm.RunTests("TestSqlVersion")
vm1.RunTests("TestSqlVersion|TestPowerPlan")
clientVM.RunTests("TestRemoteConnectivity")
serverVM.RunTests("TestPowerPlan")

return nil
}
53 changes: 53 additions & 0 deletions imagetest/test_suites/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package sql

import (
"fmt"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -51,3 +52,55 @@ func TestSqlVersion(t *testing.T) {
t.Fatalf("Installed Windows Server version does not match image version: %s not found in %s", serverVerString, sqlOutput)
}
}

func TestPowerPlan(t *testing.T) {
utils.WindowsOnly(t)

command := fmt.Sprintf("powercfg /getactivescheme")
output, err := utils.RunPowershellCmd(command)
if err != nil {
t.Fatalf("Unable to query active power plan: %v", err)
}

activePlan := strings.ToLower(strings.TrimSpace(output.Stdout))
expectedPlan := "high performance"
if !strings.Contains(activePlan, expectedPlan) {
t.Fatalf("Active power plan is not %s: got %s", expectedPlan, activePlan)
}
}
func TestRemoteConnectivity(t *testing.T) {
utils.WindowsOnly(t)

connectionCmd := `$SQLServer = (Invoke-RestMethod -Headers @{'Metadata-Flavor' = 'Google'} -Uri 'http://metadata.google.internal/computeMetadata/v1/instance/attributes/sqltarget')
$SQLDBName = 'master'
$DBUser = 'sa'
$DBPass = 'remoting@123'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; User ID = $DBUser; Password = $DBPass"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = 'SELECT * FROM information_schema.tables'
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$result = $SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
Write-Output $result`

command := fmt.Sprintf(connectionCmd)
output, err := utils.RunPowershellCmd(command)
if err != nil {
t.Fatalf("Unable to query server database: %v", err)
}

resultStr := strings.TrimSpace(output.Stdout)
result, err := strconv.Atoi(resultStr)
if 1 > result {
t.Fatalf("Test output returned invalid rows; got %d, expected > 0", result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$sqlservice = Get-Service 'MSSQLSERVER'
$sqlservice.WaitForStatus('Running', '00:10:00')
Write-Host 'MSSQLSERVER is now running.'

$cn = [ADSI]"WinNT://$env:COMPUTERNAME"
$user = $cn.Create('User', 'SqlTests')
$user.SetPassword('remoting@123')
$user.SetInfo()
$user.description = 'Admin user to install new software'
$user.SetInfo()
$group = [ADSI]"WinNT://$env:COMPUTERNAME/Administrators"
$group.Add($user.Path)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$sqlservice = Get-Service 'MSSQLSERVER'
$sqlservice.WaitForStatus('Running', '00:10:00')
Write-Host 'MSSQLSERVER is now running.'

$cn = [ADSI]"WinNT://$env:COMPUTERNAME"
$user = $cn.Create('User', 'SqlTests')
$user.SetPassword('remoting@123')
$user.SetInfo()
$user.description = 'Admin user to install new software'
$user.SetInfo()
$group = [ADSI]"WinNT://$env:COMPUTERNAME/Administrators"
$group.Add($user.Path)

$AUTH_SCRIPT = 'https://storage.googleapis.com/windows-utils/change_auth.sql'
Invoke-WebRequest -Uri $AUTH_SCRIPT -OutFile c:\\change_auth.sql

try {
sqlcmd -S localhost -i c:\change_auth.sql
} catch {
Write-Host "Failed to set auth config: $_"
} finally {
Restart-Service MSSQLSERVER
Write-Host "auth config updated"
}

0 comments on commit 907340d

Please sign in to comment.