Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 774a8b8

Browse files
committed
Rework discovery tests
Ahead of the addition of some new tests. Signed-off-by: Stephen Finucane <[email protected]>
1 parent 82c2ca0 commit 774a8b8

File tree

3 files changed

+137
-165
lines changed

3 files changed

+137
-165
lines changed

openstack/utils/testing/choose_version_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func TestChooseVersion(t *testing.T) {
1313
fakeServer := th.SetupHTTP()
1414
defer fakeServer.Teardown()
15-
setupVersionHandler(fakeServer)
15+
setupIdentityVersionHandler(fakeServer)
1616

1717
v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "blarg"}
1818
v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "hargl"}
@@ -40,7 +40,7 @@ func TestChooseVersion(t *testing.T) {
4040
func TestChooseVersionOpinionatedLink(t *testing.T) {
4141
fakeServer := th.SetupHTTP()
4242
defer fakeServer.Teardown()
43-
setupVersionHandler(fakeServer)
43+
setupIdentityVersionHandler(fakeServer)
4444

4545
v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "nope"}
4646
v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "northis"}

openstack/utils/testing/discovery_test.go

Lines changed: 102 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package testing
22

33
import (
44
"context"
5-
"fmt"
65
"strings"
76
"testing"
87

@@ -11,162 +10,144 @@ import (
1110
th "github.com/gophercloud/gophercloud/v2/testhelper"
1211
)
1312

14-
type getSupportedServiceMicroversions struct {
15-
Endpoint string
16-
ExpectedMax string
17-
ExpectedMin string
18-
ExpectedErr bool
19-
}
20-
21-
func TestGetSupportedVersions(t *testing.T) {
13+
func TestGetSupportedMicroversions(t *testing.T) {
2214
fakeServer := th.SetupHTTP()
2315
defer fakeServer.Teardown()
24-
setupVersionHandler(fakeServer)
16+
setupMultiServiceVersionHandler(fakeServer)
2517

26-
tests := []getSupportedServiceMicroversions{
18+
tests := []struct {
19+
name string
20+
endpoint string
21+
expectedVersions utils.SupportedMicroversions
22+
expectedErr string
23+
}{
2724
{
2825
// v2 does not support microversions and returns error
29-
Endpoint: fakeServer.Endpoint() + "compute/v2/",
30-
ExpectedMax: "",
31-
ExpectedMin: "",
32-
ExpectedErr: true,
26+
name: "compute legacy endpoint",
27+
endpoint: fakeServer.Endpoint() + "compute/v2/",
28+
expectedErr: "not supported",
3329
},
3430
{
35-
Endpoint: fakeServer.Endpoint() + "compute/v2.1/",
36-
ExpectedMax: "2.90",
37-
ExpectedMin: "2.1",
38-
ExpectedErr: false,
31+
name: "compute versioned endpoint",
32+
endpoint: fakeServer.Endpoint() + "compute/v2.1/",
33+
expectedVersions: utils.SupportedMicroversions{
34+
MaxMajor: 2, MaxMinor: 90, MinMajor: 2, MinMinor: 1,
35+
},
3936
},
4037
{
41-
Endpoint: fakeServer.Endpoint() + "ironic/v1/",
42-
ExpectedMax: "1.87",
43-
ExpectedMin: "1.1",
44-
ExpectedErr: false,
38+
name: "baremetal versioned endpoint",
39+
endpoint: fakeServer.Endpoint() + "baremetal/v1/",
40+
expectedVersions: utils.SupportedMicroversions{
41+
MaxMajor: 1, MaxMinor: 87, MinMajor: 1, MinMinor: 1,
42+
},
4543
},
4644
{
4745
// This endpoint returns multiple versions, which is not supported
48-
Endpoint: fakeServer.Endpoint() + "ironic/v1.2/",
49-
ExpectedMax: "not-relevant",
50-
ExpectedMin: "not-relevant",
51-
ExpectedErr: true,
46+
name: "fictional multi-version endpoint",
47+
endpoint: fakeServer.Endpoint() + "multi-version/v1.2/",
48+
expectedErr: "not supported",
5249
},
5350
}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
c := &gophercloud.ProviderClient{}
54+
client := &gophercloud.ServiceClient{
55+
ProviderClient: c,
56+
Endpoint: tt.endpoint,
57+
}
5458

55-
for _, test := range tests {
56-
c := &gophercloud.ProviderClient{
57-
IdentityBase: fakeServer.Endpoint(),
58-
IdentityEndpoint: fakeServer.Endpoint() + "v2.0/",
59-
}
60-
61-
client := &gophercloud.ServiceClient{
62-
ProviderClient: c,
63-
Endpoint: test.Endpoint,
64-
}
65-
66-
supported, err := utils.GetSupportedMicroversions(context.TODO(), client)
59+
actualVersions, err := utils.GetSupportedMicroversions(context.TODO(), client)
6760

68-
if test.ExpectedErr {
69-
if err == nil {
70-
t.Error("Expected error but got none!")
61+
if tt.expectedErr != "" {
62+
th.AssertErr(t, err)
63+
if !strings.Contains(err.Error(), tt.expectedErr) {
64+
t.Fatalf("Expected error to contain '%s', got '%s': %+v", tt.expectedErr, err, tt)
65+
}
66+
} else {
67+
th.AssertNoErr(t, err)
7168
}
72-
// Check for reasonable error message
73-
if !strings.Contains(err.Error(), "not supported") {
74-
t.Error("Expected error to contain 'not supported' but it did not!")
75-
}
76-
// No point parsing and comparing versions after error, so continue to next test case
77-
continue
78-
} else {
79-
if err != nil {
80-
t.Errorf("Expected no error but got %s", err.Error())
81-
}
82-
}
83-
84-
min := fmt.Sprintf("%d.%d", supported.MinMajor, supported.MinMinor)
85-
max := fmt.Sprintf("%d.%d", supported.MaxMajor, supported.MaxMinor)
8669

87-
if (min != test.ExpectedMin) || (max != test.ExpectedMax) {
88-
t.Errorf("Expected min=%s and max=%s but got min=%s and max=%s", test.ExpectedMin, test.ExpectedMax, min, max)
89-
}
70+
th.AssertDeepEquals(t, tt.expectedVersions, actualVersions)
71+
})
9072
}
9173
}
9274

93-
type microversionSupported struct {
94-
Version string
95-
MinVersion string
96-
MaxVersion string
97-
Supported bool
98-
Error bool
99-
}
100-
10175
func TestMicroversionSupported(t *testing.T) {
102-
tests := []microversionSupported{
76+
tests := []struct {
77+
name string
78+
version string
79+
minVersion string
80+
maxVersion string
81+
supported bool
82+
expectedError bool
83+
}{
10384
{
104-
// Checking min version
105-
Version: "2.1",
106-
MinVersion: "2.1",
107-
MaxVersion: "2.90",
108-
Supported: true,
109-
Error: false,
85+
name: "Checking min version",
86+
version: "2.1",
87+
minVersion: "2.1",
88+
maxVersion: "2.90",
89+
supported: true,
90+
expectedError: false,
11091
},
11192
{
112-
// Checking max version
113-
Version: "2.90",
114-
MinVersion: "2.1",
115-
MaxVersion: "2.90",
116-
Supported: true,
117-
Error: false,
93+
name: "Checking max version",
94+
version: "2.90",
95+
minVersion: "2.1",
96+
maxVersion: "2.90",
97+
supported: true,
98+
expectedError: false,
11899
},
119100
{
120-
// Checking too high version
121-
Version: "2.95",
122-
MinVersion: "2.1",
123-
MaxVersion: "2.90",
124-
Supported: false,
125-
Error: false,
101+
name: "Checking too high version",
102+
version: "2.95",
103+
minVersion: "2.1",
104+
maxVersion: "2.90",
105+
supported: false,
106+
expectedError: false,
126107
},
127108
{
128-
// Checking too low version
129-
Version: "2.1",
130-
MinVersion: "2.53",
131-
MaxVersion: "2.90",
132-
Supported: false,
133-
Error: false,
109+
name: "Checking too low version",
110+
version: "2.1",
111+
minVersion: "2.53",
112+
maxVersion: "2.90",
113+
supported: false,
114+
expectedError: false,
134115
},
135116
{
136-
// Invalid version
137-
Version: "2.1.53",
138-
MinVersion: "2.53",
139-
MaxVersion: "2.90",
140-
Supported: false,
141-
Error: true,
117+
name: "Invalid version",
118+
version: "2.1.53",
119+
minVersion: "2.53",
120+
maxVersion: "2.90",
121+
supported: false,
122+
expectedError: true,
142123
},
143124
}
125+
for _, tt := range tests {
126+
t.Run(tt.name, func(t *testing.T) {
127+
var err error
128+
var supportedVersions utils.SupportedMicroversions
144129

145-
for _, test := range tests {
146-
var err error
147-
var supportedVersions utils.SupportedMicroversions
148-
supportedVersions.MaxMajor, supportedVersions.MaxMinor, err = utils.ParseMicroversion(test.MaxVersion)
149-
if err != nil {
150-
t.Error("Error parsing MaxVersion!")
151-
}
152-
supportedVersions.MinMajor, supportedVersions.MinMinor, err = utils.ParseMicroversion(test.MinVersion)
153-
if err != nil {
154-
t.Error("Error parsing MinVersion!")
155-
}
156-
157-
supported, err := supportedVersions.IsSupported(test.Version)
158-
if test.Error {
159-
if err == nil {
160-
t.Error("Expected error but got none!")
130+
supportedVersions.MaxMajor, supportedVersions.MaxMinor, err = utils.ParseMicroversion(tt.maxVersion)
131+
if err != nil {
132+
t.Fatal("Error parsing MaxVersion!")
161133
}
162-
} else {
134+
135+
supportedVersions.MinMajor, supportedVersions.MinMinor, err = utils.ParseMicroversion(tt.minVersion)
163136
if err != nil {
164-
t.Errorf("Expected no error but got %s", err.Error())
137+
t.Fatal("Error parsing MinVersion!")
138+
}
139+
140+
supported, err := supportedVersions.IsSupported(tt.version)
141+
if tt.expectedError {
142+
th.AssertErr(t, err)
143+
} else {
144+
th.AssertNoErr(t, err)
145+
}
146+
147+
if tt.supported != supported {
148+
t.Fatalf("Expected supported=%t to be %t, when version=%s, min=%s and max=%s",
149+
supported, tt.supported, tt.version, tt.minVersion, tt.maxVersion)
165150
}
166-
}
167-
if test.Supported != supported {
168-
t.Errorf("Expected supported=%t to be %t, when version=%s, min=%s and max=%s",
169-
supported, test.Supported, test.Version, test.MinVersion, test.MaxVersion)
170-
}
151+
})
171152
}
172153
}

0 commit comments

Comments
 (0)