Skip to content

Commit

Permalink
testing: Set test case names
Browse files Browse the repository at this point in the history
Make this a little easier to understand failures when they occur.

Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed Jul 5, 2024
1 parent 3d5746e commit 82f9990
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
44 changes: 25 additions & 19 deletions testing/auth_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ func TestToTokenV3ScopeMap(t *testing.T) {
domainName := "Default"

var successCases = []struct {
name string
opts gophercloud.AuthOptions
expected map[string]any
}{
// System-scoped
{
"System-scoped",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
System: true,
Expand All @@ -30,8 +31,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
},
},
// Trust-scoped
{
"Trust-scoped",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
TrustID: "05144328-1f7d-46a9-a978-17eaad187077",
Expand All @@ -43,8 +44,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
},
},
// Project-scoped (ID)
{
"Project-scoped (ID)",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
ProjectID: projectID,
Expand All @@ -56,8 +57,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
},
},
// Project-scoped (name)
{
"Project-scoped (name)",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
ProjectName: projectName,
Expand All @@ -73,8 +74,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
},
},
// Domain-scoped (ID)
{
"Domain-scoped (ID)",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
DomainID: domainID,
Expand All @@ -86,8 +87,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
},
},
// Domain-scoped (name)
{
"Domain-scoped (name)",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
DomainName: domainName,
Expand All @@ -99,8 +100,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
},
},
// Empty with project fallback (ID)
{
"Empty with project fallback (ID)",
gophercloud.AuthOptions{
TenantID: projectID,
Scope: nil,
Expand All @@ -111,8 +112,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
},
},
// Empty with project fallback (name)
{
"Empty with project fallback (name)",
gophercloud.AuthOptions{
TenantName: projectName,
DomainName: domainName,
Expand All @@ -127,35 +128,38 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
},
},
// Empty without fallback
{
"Empty without fallback",
gophercloud.AuthOptions{
Scope: nil,
},
nil,
},
}
for _, successCase := range successCases {
actual, err := successCase.opts.ToTokenV3ScopeMap()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, successCase.expected, actual)
t.Run(successCase.name, func(t *testing.T) {
actual, err := successCase.opts.ToTokenV3ScopeMap()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, successCase.expected, actual)
})
}

var failCases = []struct {
name string
opts gophercloud.AuthOptions
expected error
}{
// Project-scoped with name but missing domain ID/name
{
"Project-scoped with name but missing domain ID/name",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
ProjectName: "admin",
},
},
gophercloud.ErrScopeDomainIDOrDomainName{},
},
// Project-scoped with both project name and project ID
{
"Project-scoped with both project name and project ID",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
ProjectName: "admin",
Expand All @@ -165,8 +169,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
gophercloud.ErrScopeProjectIDOrProjectName{},
},
// Project-scoped with name and unnecessary domain ID
{
"Project-scoped with name and unnecessary domain ID",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
ProjectID: "685038cd-3c25-4faf-8f9b-78c18e503190",
Expand All @@ -175,8 +179,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
gophercloud.ErrScopeProjectIDAlone{},
},
// Project-scoped with name and unnecessary domain name
{
"Project-scoped with name and unnecessary domain name",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
ProjectID: "685038cd-3c25-4faf-8f9b-78c18e503190",
Expand All @@ -185,8 +189,8 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
gophercloud.ErrScopeProjectIDAlone{},
},
// Domain-scoped with both domain name and domain ID
{
"Domain-scoped with both domain name and domain ID",
gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
DomainID: "e4b515b8-e453-49d8-9cce-4bec244fa84e",
Expand All @@ -197,7 +201,9 @@ func TestToTokenV3ScopeMap(t *testing.T) {
},
}
for _, failCase := range failCases {
_, err := failCase.opts.ToTokenV3ScopeMap()
th.AssertTypeEquals(t, failCase.expected, err)
t.Run(failCase.name, func(t *testing.T) {
_, err := failCase.opts.ToTokenV3ScopeMap()
th.AssertTypeEquals(t, failCase.expected, err)
})
}
}
22 changes: 17 additions & 5 deletions testing/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ func TestBuildRequestBody(t *testing.T) {
}

var successCases = []struct {
name string
opts AuthOptions
expected map[string]any
}{
{
"Password",
AuthOptions{
PasswordCredentials: &PasswordCredentials{
Username: "me",
Expand All @@ -186,6 +188,7 @@ func TestBuildRequestBody(t *testing.T) {
},
},
{
"Token",
AuthOptions{
TokenCredentials: &TokenCredentials{
ID: "1234567",
Expand All @@ -202,23 +205,28 @@ func TestBuildRequestBody(t *testing.T) {
}

for _, successCase := range successCases {
actual, err := gophercloud.BuildRequestBody(successCase.opts, "auth")
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, successCase.expected, actual)
t.Run(successCase.name, func(t *testing.T) {
actual, err := gophercloud.BuildRequestBody(successCase.opts, "auth")
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, successCase.expected, actual)
})
}

var failCases = []struct {
name string
opts AuthOptions
expected error
}{
{
"Conflicting tenant name and ID",
AuthOptions{
TenantID: "987654321",
TenantName: "me",
},
gophercloud.ErrMissingInput{},
},
{
"Conflicting password and token auth",
AuthOptions{
TokenCredentials: &TokenCredentials{
ID: "1234567",
Expand All @@ -231,6 +239,7 @@ func TestBuildRequestBody(t *testing.T) {
gophercloud.ErrMissingInput{},
},
{
"Missing Username or UserID",
AuthOptions{
PasswordCredentials: &PasswordCredentials{
Password: "swordfish",
Expand All @@ -239,6 +248,7 @@ func TestBuildRequestBody(t *testing.T) {
gophercloud.ErrMissingInput{},
},
{
"Missing filler fields",
AuthOptions{
PasswordCredentials: &PasswordCredentials{
Username: "me",
Expand All @@ -253,8 +263,10 @@ func TestBuildRequestBody(t *testing.T) {
}

for _, failCase := range failCases {
_, err := gophercloud.BuildRequestBody(failCase.opts, "auth")
th.AssertTypeEquals(t, failCase.expected, err)
t.Run(failCase.name, func(t *testing.T) {
_, err := gophercloud.BuildRequestBody(failCase.opts, "auth")
th.AssertTypeEquals(t, failCase.expected, err)
})
}

createdAt := time.Date(2018, 1, 4, 10, 00, 12, 0, time.UTC)
Expand Down

0 comments on commit 82f9990

Please sign in to comment.