Skip to content

Commit

Permalink
improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Dec 3, 2024
1 parent 81a4fdf commit 1f759cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
33 changes: 29 additions & 4 deletions provisioner/communication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func TestProvisionerDetermineCommunication(test *testing.T) {
delete(provisioner.generatedData, "WinRMPassword")

_, err = provisioner.determineCommunication(ui)
if err == nil {
test.Errorf("determineCommunication function did not fail on no available password")
if err == nil || err.Error() != "unknown winrm password" {
test.Error("determineCommunication function did not fail on no available password")
}

// test docker
Expand Down Expand Up @@ -129,6 +129,11 @@ func TestProvisionerDetermineCommunication(test *testing.T) {
test.Errorf("communication string slice for lxc incorrectly determined: %v", communication)
}

delete(provisioner.generatedData, "ID")
if _, err = provisioner.determineCommunication(ui); err == nil || err.Error() != "unknown instance id" {
test.Error("determineCommunication did not fail on unknown instance id")
}

// test fails on no communication
provisioner.generatedData = map[string]interface{}{
"ConnType": "unknown",
Expand All @@ -139,8 +144,8 @@ func TestProvisionerDetermineCommunication(test *testing.T) {
}

_, err = provisioner.determineCommunication(ui)
if err == nil {
test.Errorf("determineCommunication function did not fail on unknown connection type")
if err == nil || err.Error() != "unsupported communication type" {
test.Error("determineCommunication function did not fail on unsupported connection type")
}
}

Expand Down Expand Up @@ -169,6 +174,21 @@ func TestDetermineUserAddr(test *testing.T) {
test.Error("address was incorrectly determined")
test.Errorf("expected: %s, actual: %s", expectedHttpAddr, httpAddr)
}

delete(provisioner.generatedData, "Port")
if _, _, err = provisioner.determineUserAddr("ssh"); err == nil || err.Error() != "unknown host port" {
test.Error("determineCommunication did not fail on unknown port")
}

delete(provisioner.generatedData, "Host")
if _, _, err = provisioner.determineUserAddr("ssh"); err == nil || err.Error() != "unknown host address" {
test.Error("determineCommunication did not fail on unknown host")
}

delete(provisioner.generatedData, "User")
if _, _, err = provisioner.determineUserAddr("ssh"); err == nil || err.Error() != "unknown remote user" {
test.Error("determineCommunication did not fail on unknown user")
}
}

// test provisioner determineSSHAuth properly determines authentication information
Expand Down Expand Up @@ -243,6 +263,11 @@ func TestProvisionerDetermineSSHAuth(test *testing.T) {
if sshPrivateKey, _ := os.ReadFile(sshAuthString); string(sshPrivateKey) != provisioner.generatedData["SSHPrivateKey"] {
test.Errorf("temporary ssh key file content is not the ssh private key: %s", sshPrivateKey)
}

delete(provisioner.generatedData, "SSHPrivateKey")
if _, _, err = provisioner.determineSSHAuth(); err == nil || err.Error() != "no ssh authentication" {
test.Error("sshauth did not fail on no available ssh authentication information")
}
}

func TestProvisionerDetermineWinRMArgs(test *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion provisioner/testinfra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ var CI bool = os.Getenv("CIRCLECI") == "true" || os.Getenv("GITHUB_ACTIONS") ==
// global helper vars for tests
var basicConfig = &Config{
Chdir: "/tmp",
InstallCmd: []string{"/bin/false"},
Keyword: "not slow",
Local: false,
Marker: "fast",
Parallel: true,
PytestPath: "/usr/local/bin/py.test",
Expand All @@ -32,7 +34,7 @@ func TestProvisionerConfig(test *testing.T) {
config: *basicConfig,
}

if provisioner.config.PytestPath != basicConfig.PytestPath || !slices.Equal(provisioner.config.TestFiles, basicConfig.TestFiles) || provisioner.config.Chdir != basicConfig.Chdir || provisioner.config.Keyword != basicConfig.Keyword || provisioner.config.Marker != basicConfig.Marker || provisioner.config.Parallel != basicConfig.Parallel || provisioner.config.Sudo != basicConfig.Sudo || provisioner.config.SudoUser != basicConfig.SudoUser || provisioner.config.Verbose != basicConfig.Verbose {
if provisioner.config.PytestPath != basicConfig.PytestPath || !slices.Equal(provisioner.config.TestFiles, basicConfig.TestFiles) || provisioner.config.Chdir != basicConfig.Chdir || !slices.Equal(provisioner.config.InstallCmd, basicConfig.InstallCmd) || provisioner.config.Keyword != basicConfig.Keyword || provisioner.config.Local != basicConfig.Local || provisioner.config.Marker != basicConfig.Marker || provisioner.config.Parallel != basicConfig.Parallel || provisioner.config.Sudo != basicConfig.Sudo || provisioner.config.SudoUser != basicConfig.SudoUser || provisioner.config.Verbose != basicConfig.Verbose {
test.Errorf("provisioner config struct not initialized correctly")
}
}
Expand Down

0 comments on commit 1f759cd

Please sign in to comment.