Skip to content

Commit

Permalink
test: add test for gowindows client
Browse files Browse the repository at this point in the history
  • Loading branch information
d-strobel committed Oct 15, 2023
1 parent 876993c commit 552eb29
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
64 changes: 64 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package gowindows

import (
"testing"

"github.com/d-strobel/gowindows/connection"
"github.com/d-strobel/gowindows/package/local"
"github.com/stretchr/testify/mock"
)

// MockConnection is a mock implementation of connection.Connection
type MockConnection struct {
mock.Mock
}

// MockNew is a mock implementation of connection.New
func (m *MockConnection) New(conf *connection.Config) (*connection.Connection, error) {
args := m.Called(conf)
return args.Get(0).(*connection.Connection), args.Error(1)
}

// MockClient is a mock implementation of local.Client
type MockLocalClient struct {
mock.Mock
}

// MockNew is a mock implementation of local.New
func (m *MockLocalClient) New(conn *connection.Connection) *local.Client {
args := m.Called(conn)
return args.Get(0).(*local.Client)
}

func TestNew(t *testing.T) {

// Create a mock connection
mockConn := new(MockConnection)

// Create a mock of packae clients
mockLocalClient := new(MockLocalClient)

// WinRM test config for mock
winRMConfigTest := connection.WinRMConfig{
WinRMUsername: "test",
WinRMPassword: "test",
WinRMHost: "test",
}

// Configure the expected behavior of the mocks
mockConn.On("New", mock.Anything).Return(&connection.Connection{}, nil)
mockLocalClient.On("New", mock.Anything).Return(&local.Client{})

// Call the New function with the mock objects
conn, err := mockConn.New(&connection.Config{WinRM: &winRMConfigTest})
_ = mockLocalClient.New(conn)

// Check if the NewClient function returned an error
if err != nil {
t.Errorf("NewClient returned an error: %v", err)
}

// Assert that the mocks were called as expected
mockConn.AssertExpectations(t)
mockLocalClient.AssertExpectations(t)
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ require (
golang.org/x/crypto v0.14.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
github.com/ChrisTrenkamp/goxpath v0.0.0-20210404020558-97928f7e12b6 // indirect
Expand All @@ -19,6 +26,7 @@ require (
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/masterzen/simplexml v0.0.0-20190410153822-31eea3082786 // indirect
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down

0 comments on commit 552eb29

Please sign in to comment.