Skip to content
Merged
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
106 changes: 83 additions & 23 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,75 @@ import (
"time"

"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)

const DefaultTimeout = 15 * time.Second

func TestConfig_Validate(t *testing.T) {
c := &Config{}
assert.Error(t, c.Validate())

errVal := c.Validate()

if errVal == nil {
t.Error("Did expect error got nil")
}

// Most basic settings
c.Host = "localhost"
c.Command = "Get-Something"
c.User = "administrator"
c.Password = "verysecret"

assert.NoError(t, c.Validate())
assert.Equal(t, c.Port, TlsPort)
assert.False(t, c.NoTls)
assert.Equal(t, c.AuthType, AuthDefault)
assert.True(t, c.validated)
errVal = c.Validate()

if errVal != nil {
t.Error("Did not expect error got", errVal)
}

if c.Port != TlsPort {
t.Error("Actual", c.Port, "Expected", TlsPort)
}

if c.NoTls != false {
t.Error("Expected NoTls to be false, got true")
}

if c.AuthType != AuthDefault {
t.Error("Actual", c.AuthType, "Expected", AuthDefault)
}

if c.validated != true {
t.Error("Expected validated to be true, got false")
}
}

func TestBuildConfigFlags(t *testing.T) {
fs := &pflag.FlagSet{}
config := BuildConfigFlags(fs)

assert.True(t, fs.HasFlags())
assert.False(t, config.validated)
if fs.HasFlags() != true {
t.Error("Expected hasFalgs to be true, got false")
}

if config.validated != false {
t.Error("Expected config.validated to be false, got true")
}

}

func TestConfig_BuildCommand(t *testing.T) {
c := &Config{Command: "Get-Something"}
assert.Contains(t, c.BuildCommand(), "powershell.exe -EncodedCommand")

cmd := c.BuildCommand()
if !strings.Contains(c.BuildCommand(), "powershell.exe -EncodedCommand") {
t.Error("\nExpected 'powershell.exe -EncodedCommand': ", cmd)
}

c = &Config{IcingaCommand: "Icinga-CheckSomething"}
assert.Contains(t, c.BuildCommand(), "powershell.exe -EncodedCommand")
cmd = c.BuildCommand()
if !strings.Contains(cmd, "powershell.exe -EncodedCommand") {
t.Error("\nExpected 'powershell.exe -EncodedCommand': ", cmd)
}
}

func TestConfig_Run_WithError(t *testing.T) {
Expand All @@ -57,11 +90,18 @@ func TestConfig_Run_WithError(t *testing.T) {
}

err := c.Validate()
assert.NoError(t, err)
if err != nil {
t.Error("Did not expect error got", err)
}

_, _, err = c.Run(1 * time.Second)
assert.Error(t, err)
assert.Contains(t, err.Error(), "dial tcp 192.0.2.11:")
if err == nil {
t.Error("Did expect error got nil")
}

if !strings.Contains(err.Error(), "dial tcp 192.0.2.11:") {
t.Error("\nExpected 'dial tcp 192.0.2.11:'", err.Error())
}
}

func TestConfig_Run_Basic(t *testing.T) {
Expand Down Expand Up @@ -90,7 +130,9 @@ func TestConfig_Run_Basic_WithTLS(t *testing.T) {
setupTlsFromEnv(t, c)

err := c.Validate()
assert.NoError(t, err)
if err != nil {
t.Error("Did not expect error got", err)
}

fmt.Printf("%v\n", c)

Expand All @@ -106,7 +148,9 @@ func TestConfig_Run_NTLM(t *testing.T) {
c.NoTls = true

err := c.Validate()
assert.NoError(t, err)
if err != nil {
t.Error("Did not expect error got", err)
}

fmt.Printf("%v\n", c)

Expand All @@ -118,7 +162,9 @@ func TestConfig_Run_NTLM_WithTls(t *testing.T) {
setupTlsFromEnv(t, c)

err := c.Validate()
assert.NoError(t, err)
if err != nil {
t.Error("Did not expect error got", err)
}

fmt.Printf("%v\n", c)

Expand All @@ -134,7 +180,9 @@ func TestConfig_Run_TLS(t *testing.T) {
}

err := c.Validate()
assert.NoError(t, err)
if err != nil {
t.Error("Did not expect error got", err)
}

fmt.Printf("%v\n", c)

Expand All @@ -143,12 +191,22 @@ func TestConfig_Run_TLS(t *testing.T) {

func runCheck(t *testing.T, c *Config) {
err := c.Validate()
assert.NoError(t, err)
if err != nil {
t.Error("Did not expect error got", err)
}

rc, output, err := c.Run(DefaultTimeout)
assert.NoError(t, err)
assert.Equal(t, 0, rc)
assert.Contains(t, output, "ConsoleHost")
if err != nil {
t.Error("Did not expect error got", err)
}

if 0 != rc {
t.Error("Actual", rc, "Expected", 0)
}

if !strings.Contains(output, "ConsoleHost") {
t.Error("\nExpected 'ConsoleHost'", output)
}
}

func buildEnvConfig(t *testing.T, auth string) *Config {
Expand Down Expand Up @@ -201,7 +259,9 @@ func setupTlsFromEnv(t *testing.T, c *Config) {

if file := os.Getenv("WINRM_TLS_PORT"); file != "" {
tmp, err := strconv.ParseInt(file, 10, 16)
assert.NoError(t, err)
if err != nil {
t.Error("Did not expect error got", err)
}

c.Port = int(tmp)
}
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/masterzen/winrm v0.0.0-20260407182533-5570be7f80cf
github.com/sirupsen/logrus v1.9.4
github.com/spf13/pflag v1.0.10
github.com/stretchr/testify v1.11.1
golang.org/x/crypto v0.50.0
)

Expand All @@ -16,7 +15,6 @@ require (
github.com/ChrisTrenkamp/goxpath v0.0.0-20210404020558-97928f7e12b6 // indirect
github.com/bodgit/ntlmssp v0.0.0-20240506230425-31973bb52d9b // indirect
github.com/bodgit/windows v1.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -28,10 +26,9 @@ 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/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.11.1 // indirect
github.com/tidwall/transform v0.0.0-20201103190739-32f242e2dbde // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.36.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading