@@ -18,14 +18,28 @@ limitations under the License.
18
18
package gcecloudprovider
19
19
20
20
import (
21
+ "context"
21
22
"errors"
22
23
"fmt"
23
24
"net/http"
24
25
"testing"
26
+ "time"
27
+
28
+ "golang.org/x/oauth2"
25
29
26
30
"google.golang.org/api/googleapi"
27
31
)
28
32
33
+ type mockTokenSource struct {}
34
+
35
+ func (* mockTokenSource ) Token () (* oauth2.Token , error ) {
36
+ return & oauth2.Token {
37
+ AccessToken : "access" ,
38
+ TokenType : "Bearer" ,
39
+ RefreshToken : "refresh" ,
40
+ Expiry : time .Now ().Add (1 * time .Hour ),
41
+ }, nil
42
+ }
29
43
func TestIsGCEError (t * testing.T ) {
30
44
testCases := []struct {
31
45
name string
@@ -84,3 +98,55 @@ func TestIsGCEError(t *testing.T) {
84
98
}
85
99
}
86
100
}
101
+
102
+ func TestGetComputeVersion (t * testing.T ) {
103
+ testCases := []struct {
104
+ name string
105
+ computeEndpoint string
106
+ computeEnvironment Environment
107
+ computeVersion Version
108
+ expectedEndpoint string
109
+ expectError bool
110
+ }{
111
+
112
+ {
113
+ name : "check for production environment" ,
114
+ computeEndpoint : "https://compute.googleapis.com" ,
115
+ computeEnvironment : "production" ,
116
+ computeVersion : "v1" ,
117
+ expectedEndpoint : "https://compute.googleapis.com/compute/v1/" ,
118
+ expectError : false ,
119
+ },
120
+ {
121
+ name : "check for incorrect endpoint" ,
122
+ computeEndpoint : "https://compute.googleapis" ,
123
+ computeEnvironment : "prod" ,
124
+ computeVersion : "v1" ,
125
+ expectError : true ,
126
+ },
127
+ {
128
+ name : "check for staging environment" ,
129
+ computeEndpoint : "https://compute.googleapis.com" ,
130
+ computeEnvironment : environmentStaging ,
131
+ computeVersion : "v1" ,
132
+ expectedEndpoint : "compute/staging_v1/" ,
133
+ expectError : false ,
134
+ },
135
+ {
136
+ name : "check for random string as endpoint" ,
137
+ computeEndpoint : "compute-googleapis" ,
138
+ computeEnvironment : "prod" ,
139
+ computeVersion : "v1" ,
140
+ expectedEndpoint : "compute/v1/" ,
141
+ expectError : true ,
142
+ },
143
+ }
144
+ for _ , tc := range testCases {
145
+ ctx := context .Background ()
146
+ _ , err := getComputeVersion (ctx , & mockTokenSource {}, tc .computeEndpoint , tc .computeEnvironment , tc .computeVersion )
147
+ if err != nil && ! tc .expectError {
148
+ t .Fatalf ("Got error %v, expected endpoint %s" , err , tc .expectedEndpoint )
149
+ }
150
+ }
151
+
152
+ }
0 commit comments