Skip to content
This repository was archived by the owner on Jul 28, 2020. It is now read-only.

Commit 891f40e

Browse files
Anthony Emengopivotal
authored andcommitted
Refactor contract in driver/hyperv abstractions
1 parent 59212e3 commit 891f40e

File tree

5 files changed

+30
-46
lines changed

5 files changed

+30
-46
lines changed

driver/hyperv/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ func (d *HyperV) Prestart() error {
5757

5858
func (d *HyperV) Start(cpus int, memory int, efiPath string) error {
5959
d.UI.Say("Creating the VM...")
60-
err := d.CreateVM(driver.VMName, cpus, memory, efiPath)
60+
vmGUID, err := d.CreateVM(driver.VMName, cpus, memory, efiPath)
6161
if err != nil {
6262
return e.SafeWrap(err, "creating the vm")
6363
}
6464

6565
d.UI.Say("Starting VPNKit...")
66-
vmGUID, err := d.SetupNetworking()
66+
err = d.SetupNetworking()
6767
if err != nil {
6868
return e.SafeWrap(err, "setting up networking")
6969
}

driver/hyperv/hyperv.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"strings"
77
)
88

9-
func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) error {
9+
func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) (string, error) {
1010
var (
1111
cfDevVHD = filepath.Join(d.Config.StateDir, "disk.vhdx")
1212
)
1313

1414
command := fmt.Sprintf("New-VM -Name %s -Generation 2 -NoVHD", name)
1515
_, err := d.Powershell.Output(command)
1616
if err != nil {
17-
return fmt.Errorf("creating new vm: %s", err)
17+
return "", fmt.Errorf("creating new vm: %s", err)
1818
}
1919

2020
command = fmt.Sprintf("Set-VM -Name %s "+
@@ -27,13 +27,13 @@ func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) err
2727
name)
2828
_, err = d.Powershell.Output(command)
2929
if err != nil {
30-
return fmt.Errorf("setting vm properites (memoryMB:%d, cpus:%d): %s", memory, cpus, err)
30+
return "", fmt.Errorf("setting vm properites (memoryMB:%d, cpus:%d): %s", memory, cpus, err)
3131
}
3232

3333
command = fmt.Sprintf(`Add-VMDvdDrive -VMName %s -Path "%s"`, name, efiPath)
3434
_, err = d.Powershell.Output(command)
3535
if err != nil {
36-
return fmt.Errorf("adding dvd drive %s: %s", efiPath, err)
36+
return "", fmt.Errorf("adding dvd drive %s: %s", efiPath, err)
3737
}
3838

3939
command = fmt.Sprintf("Remove-VMNetworkAdapter -VMName %s", name)
@@ -46,7 +46,7 @@ func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) err
4646
`-Path "%s"`, name, cfDevVHD)
4747
_, err = d.Powershell.Output(command)
4848
if err != nil {
49-
return fmt.Errorf("adding vhd %s : %s", cfDevVHD, err)
49+
return "", fmt.Errorf("adding vhd %s : %s", cfDevVHD, err)
5050
}
5151

5252
command = fmt.Sprintf("Set-VMFirmware "+
@@ -56,7 +56,7 @@ func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) err
5656
name)
5757
_, err = d.Powershell.Output(command)
5858
if err != nil {
59-
return fmt.Errorf("setting firmware: %s", err)
59+
return "", fmt.Errorf("setting firmware: %s", err)
6060
}
6161

6262
command = fmt.Sprintf("Set-VMComPort "+
@@ -66,10 +66,16 @@ func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) err
6666
name)
6767
_, err = d.Powershell.Output(command)
6868
if err != nil {
69-
return fmt.Errorf("setting com port: %s", err)
69+
return "", fmt.Errorf("setting com port: %s", err)
7070
}
7171

72-
return nil
72+
output, err := d.Powershell.Output("((Get-VM -Name cfdev).Id).Guid")
73+
if err != nil {
74+
return "", fmt.Errorf("fetching VM Guid: %s", err)
75+
}
76+
77+
vmGUID := strings.TrimSpace(output)
78+
return vmGUID, nil
7379
}
7480

7581
func (d *HyperV) StartVM(vmName string) error {

driver/hyperv/hyperv_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ var _ = Describe("HyperV", func() {
7373
})
7474

7575
It("creates hyperv VM", func() {
76-
Expect(hyperV.CreateVM(vmName, 1, 2000, filepath.Join(assetDir, "cfdev-efi-v2.iso"))).To(Succeed())
76+
guid, err := hyperV.CreateVM(vmName, 1, 2000, filepath.Join(assetDir, "cfdev-efi-v2.iso"))
77+
Expect(err).NotTo(HaveOccurred())
78+
Expect(guid).NotTo(BeEmpty())
7779

7880
cmd := exec.Command("powershell.exe", "-Command", fmt.Sprintf("Get-VM -Name %s | format-list -Property MemoryStartup,ProcessorCount", vmName))
7981
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)

driver/hyperv/network.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,24 @@ var (
1818
`Foreach-Object { Remove-Item (Join-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices" $_.PSChildName) }`
1919
)
2020

21-
func (d *HyperV) SetupNetworking() (string, error) {
21+
func (d *HyperV) SetupNetworking() error {
2222
if err := d.registerServiceGUIDs(); err != nil {
23-
return "", fmt.Errorf("generating service guids: %s", err)
23+
return fmt.Errorf("generating service guids: %s", err)
2424
}
2525

2626
if err := driver.WriteHttpConfig(d.Config); err != nil {
27-
return "", err
27+
return err
2828
}
2929

3030
if err := d.writeResolvConf(); err != nil {
31-
return "", fmt.Errorf("writing resolv.conf: %s", err)
31+
return fmt.Errorf("writing resolv.conf: %s", err)
3232
}
3333

3434
if err := d.writeDHCPJSON(); err != nil {
35-
return "", fmt.Errorf("writing dhcp.json: %s", err)
36-
}
37-
38-
output, err := d.Powershell.Output("((Get-VM -Name cfdev).Id).Guid")
39-
if err != nil {
40-
return "", fmt.Errorf("fetching VM Guid: %s", err)
35+
return fmt.Errorf("writing dhcp.json: %s", err)
4136
}
4237

43-
vmGUID := strings.TrimSpace(output)
44-
return vmGUID, nil
38+
return nil
4539
}
4640

4741
func (d *HyperV) networkingDaemonSpec(label, vmGUID string) daemon.DaemonSpec {

driver/hyperv/network_test.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,28 @@ package hyperv_test
55
import (
66
"code.cloudfoundry.org/cfdev/config"
77
"code.cloudfoundry.org/cfdev/driver/hyperv"
8-
"code.cloudfoundry.org/cfdev/driver/hyperv/mocks"
98
"code.cloudfoundry.org/cfdev/runner"
10-
"github.com/golang/mock/gomock"
119
. "github.com/onsi/ginkgo"
1210
. "github.com/onsi/gomega"
1311
"github.com/onsi/gomega/gexec"
1412
"io/ioutil"
1513
"os"
1614
"os/exec"
1715
"path/filepath"
18-
"strings"
1916
"time"
2017
)
2118

2219
var _ = Describe("Network", func() {
2320
var (
2421
tempDir string
2522
hyperV *hyperv.HyperV
26-
mockController *gomock.Controller
27-
mockRunner *mocks.MockRunner
2823
)
2924

3025
BeforeEach(func() {
3126
var err error
3227
tempDir, err = ioutil.TempDir("", "cfdev-test-")
3328
Expect(err).NotTo(HaveOccurred())
3429

35-
mockController = gomock.NewController(GinkgoT())
36-
mockRunner = mocks.NewMockRunner(mockController)
37-
3830
hyperV = &hyperv.HyperV{
3931
Config: config.Config{
4032
CFDevHome: tempDir,
@@ -43,36 +35,26 @@ var _ = Describe("Network", func() {
4335
EthernetGUID: "65319afc-c1a2-4ad9-97a0-0058737b94c2",
4436
PortGUID: "d611c86d-22c9-417c-ac09-3ed4ce5fbfb0",
4537
ForwarderGUID: "2ad4fb96-7f0b-4ff9-b42b-8e25da407647",
46-
Powershell: mockRunner,
38+
Powershell: &runner.Powershell{},
4739
}
48-
49-
mockRunner.EXPECT().Output(gomock.Any()).DoAndReturn(func(command string) (string, error) {
50-
if strings.Contains(command, "Get-VM") {
51-
return "some-guild", nil
52-
}
53-
54-
runner := &runner.Powershell{}
55-
return runner.Output(command)
56-
}).AnyTimes()
5740
})
5841

5942
AfterEach(func() {
60-
mockController.Finish()
61-
6243
registryDeleteCmd := `Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices" | ` +
6344
`Where-Object { $_.GetValue("ElementName") -match "CF Dev VPNKit" } | ` +
6445
`Foreach-Object { Remove-Item (Join-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices" $_.PSChildName) }`
6546

6647
command := exec.Command("powershell.exe", "-Command", registryDeleteCmd)
6748
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
6849
Expect(err).NotTo(HaveOccurred())
69-
Eventually(session, 10*time.Second).Should(gexec.Exit())
50+
<-session.Exited
51+
7052
os.RemoveAll(tempDir)
7153
})
7254

7355
Describe("Setup", func() {
7456
It("writes the dhcp and resolv conf files in the cfdevDir", func() {
75-
_, err := hyperV.SetupNetworking()
57+
err := hyperV.SetupNetworking()
7658
Expect(err).NotTo(HaveOccurred())
7759

7860
dnsPath := filepath.Join(tempDir, "resolv.conf")
@@ -92,7 +74,7 @@ var _ = Describe("Network", func() {
9274
})
9375

9476
It("writes service guids to the registry", func() {
95-
_, err := hyperV.SetupNetworking()
77+
err := hyperV.SetupNetworking()
9678
Expect(err).NotTo(HaveOccurred())
9779

9880
command := exec.Command("powershell.exe", "-Command", `dir "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices"`)

0 commit comments

Comments
 (0)