From 552eb2936e28f8e8201fe9ddf07e853cb0291cfd Mon Sep 17 00:00:00 2001 From: Dustin Strobel Date: Sun, 15 Oct 2023 13:01:37 +0200 Subject: [PATCH] test: add test for gowindows client --- client_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 8 +++++++ go.sum | 3 +++ 3 files changed, 75 insertions(+) create mode 100644 client_test.go diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..cd37702 --- /dev/null +++ b/client_test.go @@ -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) +} diff --git a/go.mod b/go.mod index 92cce97..915e966 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index dbe7bce..1dd3a0a 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,7 @@ 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= @@ -52,6 +53,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ 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=