Skip to content

Commit

Permalink
Improve testing auth session, improve disabled VC recovery in tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Didainius authored Feb 19, 2025
1 parent f2cb388 commit 7f77623
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
69 changes: 42 additions & 27 deletions govcd/api_vcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,34 +638,8 @@ func (vcd *TestVCD) SetUpSuite(check *C) {
panic(err)
}
vcd.client = vcdClient
token := os.Getenv("VCD_TOKEN")
if token == "" {
token = config.Provider.Token
}

apiToken := os.Getenv("VCD_API_TOKEN")
if apiToken == "" {
apiToken = config.Provider.ApiToken
}
authenticationMode := testingAuthenticate(vcd.client, vcd.config)

authenticationMode := "password"
if apiToken != "" {
authenticationMode = "API-token"
err = vcd.client.SetToken(config.Provider.SysOrg, ApiTokenHeader, apiToken)
} else {
if token != "" {
authenticationMode = "token"
err = vcd.client.SetToken(config.Provider.SysOrg, AuthorizationHeader, token)
} else {
err = vcd.client.Authenticate(config.Provider.User, config.Provider.Password, config.Provider.SysOrg)
}
}
if config.Provider.UseSamlAdfs {
authenticationMode = "SAML password"
}
if err != nil {
panic(err)
}
versionInfo := ""
version, versionTime, err := vcd.client.Client.GetVcdVersion()
if err == nil {
Expand Down Expand Up @@ -756,6 +730,47 @@ func (vcd *TestVCD) SetUpSuite(check *C) {
}
}

// SetUpTest is a fixture that is triggered before each test
func (vcd *TestVCD) SetUpTest(check *C) {
authmode := testingAuthenticate(vcd.client, vcd.config)

printVerbose("# Refreshed session using '%s'\n", authmode)
}

func testingAuthenticate(vcdClient *VCDClient, config TestConfig) string {
var err error
token := os.Getenv("VCD_TOKEN")
if token == "" {
token = config.Provider.Token
}

apiToken := os.Getenv("VCD_API_TOKEN")
if apiToken == "" {
apiToken = config.Provider.ApiToken
}

authenticationMode := "password"
if apiToken != "" {
authenticationMode = "API-token"
err = vcdClient.SetToken(config.Provider.SysOrg, ApiTokenHeader, apiToken)
} else {
if token != "" {
authenticationMode = "token"
err = vcdClient.SetToken(config.Provider.SysOrg, AuthorizationHeader, token)
} else {
err = vcdClient.Authenticate(config.Provider.User, config.Provider.Password, config.Provider.SysOrg)
}
}
if config.Provider.UseSamlAdfs {
authenticationMode = "SAML password"
}
if err != nil {
panic(err)
}

return authenticationMode
}

// Shows the detail of cleanup operations only if the relevant verbosity
// has been enabled
func (vcd *TestVCD) infoCleanup(format string, args ...interface{}) {
Expand Down
6 changes: 6 additions & 0 deletions govcd/tm_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func getOrCreateVCenter(vcd *TestVCD, check *C) (*VCenter, func()) {
vc.VSphereVCenter.IsEnabled = true
vc, err = vc.Update(vc.VSphereVCenter)
check.Assert(err, IsNil)
err = waitForListenerStatusConnected(vc)
check.Assert(err, IsNil)
err = vc.Refresh()
check.Assert(err, IsNil)
err = vc.RefreshStorageProfiles()
check.Assert(err, IsNil)
}

return vc, func() {}
Expand Down

0 comments on commit 7f77623

Please sign in to comment.