Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cleanups #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.go text eol=lf
26 changes: 0 additions & 26 deletions pkg/hardware/network/netadapter/netadapter_test.go

This file was deleted.

9 changes: 0 additions & 9 deletions pkg/hardware/network/netroute/netroute_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/hardware/network/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func GetNetworkAdapterByName(whost *host.WmiHost, name string) (adapter *netadap
if err != nil {
return
}
return &netadapter.NetworkAdapter{tmp}, nil
return &netadapter.NetworkAdapter{MSFT_NetAdapter: tmp}, nil
}

// GetNetworkAdapterByName
Expand All @@ -48,7 +48,7 @@ func GetNetworkAdapterByInterfaceIndex(whost *host.WmiHost, ifindex int32) (adap
if err != nil {
return
}
return &netadapter.NetworkAdapter{tmp}, nil
return &netadapter.NetworkAdapter{MSFT_NetAdapter: tmp}, nil
}

// FindDefaultExternalAdapter - finds the first physical adapter which has a default gateway
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (

"github.com/microsoft/wmi/pkg/base/host"
_ "github.com/microsoft/wmi/pkg/base/session"
"testing"

"github.com/microsoft/wmi/pkg/virtualization/core/storage/disk"
"testing"
)

var (
Expand All @@ -34,7 +33,7 @@ func TestCreateDynamicVirtualHardDisk(t *testing.T) {
t.Fatal("Failed " + err.Error())
}
path := "c:\\test\\tmp.vhdx"
setting, err := disk.GetVirtualHardDiskSettingData(whost, path, 512, 512, 0, 1024*1024*10, true)
setting, err := disk.GetVirtualHardDiskSettingData(whost, path, 512, 512, 0, 1024*1024*10, true, disk.VirtualHardDiskFormat_2)
if err != nil {
t.Fatal("Failed " + err.Error())
}
Expand All @@ -57,7 +56,7 @@ func TestCreateStaticVirtualHardDisk(t *testing.T) {
t.Fatal("Failed " + err.Error())
}
path := "c:\\test\\tmp.vhdx"
setting, err := disk.GetVirtualHardDiskSettingData(whost, path, 512, 512, 0, 1024*1024*10, false)
setting, err := disk.GetVirtualHardDiskSettingData(whost, path, 512, 512, 0, 1024*1024*10, false, disk.VirtualHardDiskFormat_2)
if err != nil {
t.Fatal("Failed " + err.Error())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,4 @@ func (vsms *VirtualEthernetSwitchManagementService) DeleteVirtualSwitch(vswitch
defer job.Close()
return job.WaitForJobCompletion(result.ReturnValue, -1)
}
return
}
2 changes: 1 addition & 1 deletion pkg/wmiinstance/WmiClass.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c *WmiClass) GetDerivation() []string {
defer valueNameProperty.Clear()

derivations, err := GetVariantValues(valueNameProperty)
if len(derivations) < 1 {
if err != nil || len(derivations) < 1 {
panic("GetDerivation() failed to get the Derivation_ values")
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/wmiinstance/WmiEventSink.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/go-ole/go-ole/oleutil"
)

var IID_ISWbemObjectPath = &ole.GUID{0x5791BC27, 0xCE9C, 0x11d1, [8]byte{0x97, 0xBF, 0x00, 0x00, 0xF8, 0x1E, 0x84, 0x9C}}
var IID_ISWbemSinkEvents = &ole.GUID{0x75718CA0, 0xF029, 0x11d1, [8]byte{0xA1, 0xAC, 0x00, 0xC0, 0x4F, 0xB6, 0xC2, 0x23}}
var IID_ISWbemObjectPath = &ole.GUID{Data1: 0x5791BC27, Data2: 0xCE9C, Data3: 0x11d1, Data4: [8]byte{0x97, 0xBF, 0x00, 0x00, 0xF8, 0x1E, 0x84, 0x9C}}
var IID_ISWbemSinkEvents = &ole.GUID{Data1: 0x75718CA0, Data2: 0xF029, Data3: 0x11d1, Data4: [8]byte{0xA1, 0xAC, 0x00, 0xC0, 0x4F, 0xB6, 0xC2, 0x23}}

const (
eventSinkObjectName = "WbemScripting.SWbemSink"
Expand Down
10 changes: 9 additions & 1 deletion pkg/wmiinstance/WmiEventSink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,26 @@ func Test_WmiAsyncEvents(t *testing.T) {
return
}
err = cmd.Wait()
if err != nil {
t.Errorf("Could not wait for powershell on this computer")
return
}

completed := false
startTime := time.Now()
timeout, _ := time.ParseDuration("1s")
breakLoop := false
for time.Since(startTime) < timeout && !completed {
select {
case completed = <-context.completed:
break
breakLoop = true
default:
// continue waiting
time.Sleep(1 * time.Second)
}
if breakLoop {
break
}
for eventSink.PeekAndDispatchMessages() {
// Continue pumping for message while they arrive
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/wmiinstance/WmiInstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ func (c *WmiInstance) Equals(instance *WmiInstance) bool {
// Clone
func (c *WmiInstance) Clone() (*WmiInstance, error) {
rawResult, err := oleutil.CallMethod(c.instance, "Clone_")
winstance, err := CreateWmiInstance(rawResult, c.session)
return winstance, err
if err != nil {
return nil, err
}
return CreateWmiInstance(rawResult, c.session)
}

// Refresh
Expand All @@ -232,7 +234,6 @@ func (c *WmiInstance) Commit() error {
}
defer rawResult.Clear()
return nil

}

// Modify
Expand Down Expand Up @@ -411,7 +412,7 @@ func (c *WmiInstance) GetAssociated(associatedClassName, resultClassName, result
return nil, err
}
if enum == nil {
return nil, fmt.Errorf("Enum is nil")
return nil, fmt.Errorf("enum is nil")
}

defer enum.Release()
Expand Down Expand Up @@ -472,7 +473,7 @@ func (c *WmiInstance) EnumerateReferencingInstances(resultClassName, sourceRole
return nil, err
}
if enum == nil {
return nil, fmt.Errorf("Enum is nil")
return nil, fmt.Errorf("enum is nil")
}

defer enum.Release()
Expand Down
4 changes: 4 additions & 0 deletions pkg/wmiinstance/WmiInstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func Test_WmiInstance(t *testing.T) {
defer wmiClass.Close()

className, err := wmiClass.GetSystemProperty("__CLASS")
if err != nil {
t.Errorf("GetSystemProperty() failed with error: %v", err)
return
}
defer className.Close()

if className.Value().(string) != "Win32_Process" {
Expand Down
13 changes: 6 additions & 7 deletions pkg/wmiinstance/WmiJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ func (job *WmiJob) String() string {
if err != nil {
return ""
}
return fmt.Sprintf("Type[%s] State[%s]", jtype, job.GetJobState())
return fmt.Sprintf("Type[%d] State[%d]", jtype, job.GetJobState())
}

// GetJobType gets the value of JobType for the instance
func (job *WmiJob) JobType() (value int32, err error) {
func (job *WmiJob) JobType() (int32, error) {
retValue, err := job.GetProperty("JobType")
if err != nil {
return
return 0, err
}
value, ok := retValue.(int32)
if !ok {
// TODO: Set an error
return 0, fmt.Errorf("property JobType is not an int32: %T", retValue)
}
return
return value, nil
}

// WaitForPercentComplete waits for the percentComplete or timeout
Expand Down Expand Up @@ -150,7 +150,7 @@ func (job *WmiJob) GetJobState() (js JobState) {
func (job *WmiJob) IsComplete() bool {
err := job.Refresh()
if err != nil {

return false
}
state := job.GetJobState()
switch state {
Expand Down Expand Up @@ -206,7 +206,6 @@ func (job *WmiJob) WaitForJobCompletion(result int32, timeoutSeconds int16) erro
} else {
return errors.Wrapf(errors.Failed, "Unable to Wait for Job on Result[%d] ", result)
}

}

type WmiJobCollection []*WmiJob
Expand Down
2 changes: 1 addition & 1 deletion pkg/wmiinstance/WmiSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (c *WmiSession) PerformRawQuery(queryExpression string) (*ole.IEnumVARIANT,
return nil, err
}
if enum == nil {
return nil, fmt.Errorf("Enum is nil")
return nil, fmt.Errorf("enum is nil")
}

return enum, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/wmiinstance/WmiSessionManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const S_FALSE = 0x00000001

var IID_ISWbemLocator = &ole.GUID{0x76a6415b, 0xcb41, 0x11d1, [8]byte{0x8b, 0x02, 0x00, 0x60, 0x08, 0x06, 0xd9, 0xb6}}
var IID_ISWbemLocator = &ole.GUID{Data1: 0x76a6415b, Data2: 0xcb41, Data3: 0x11d1, Data4: [8]byte{0x8b, 0x02, 0x00, 0x60, 0x08, 0x06, 0xd9, 0xb6}}

// Reference https://github.com/StackExchange/wmi
// Reference https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/swbemlocator-connectserver
Expand Down
4 changes: 4 additions & 0 deletions pkg/wmiinstance/WmiSynchronousEvents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func Test_WmiSynchronousEvents(t *testing.T) {
return
}
err = cmd.Wait()
if err != nil {
t.Errorf("Could not wait for powershell on this computer")
return
}

_, err = instances.WaitForNextEvent()
if err != nil {
Expand Down