@@ -23,92 +23,138 @@ func TestGetVMEndpoint(t *testing.T) {
23
23
name string // Test case name
24
24
vmID string // Virtual machine ID to test
25
25
endpointType string // Endpoint type (ssh, scp, or invalid)
26
+ username string // Custom username (empty to use GitHub username)
26
27
mockVM * types.VM // Mock VM response from API
27
28
mockGithubUsername string // Mock GitHub username response
29
+ githubAPIError bool // Should GitHub username API return an error
28
30
expectedOutput string // Expected output to stdout
29
31
expectedError string // Expected error string (empty for no error)
30
32
}{
31
33
{
32
34
name : "Success - Get SSH endpoint for running VM" ,
33
35
vmID : "vm-123" ,
34
36
endpointType : "ssh" ,
37
+ username : "" ,
35
38
mockVM : & types.VM {
36
39
ID : "vm-123" ,
37
40
DirectSSHEndpoint : "test-vm.example.com" ,
38
41
DirectSSHPort : 22 ,
39
42
Status : types .VMStatusRunning ,
40
43
},
41
44
mockGithubUsername : "testuser" ,
45
+ githubAPIError : false ,
42
46
expectedOutput : "ssh://testuser@test-vm.example.com:22\n " ,
43
47
expectedError : "" ,
44
48
},
45
49
{
46
50
name : "Success - Get SCP endpoint for running VM" ,
47
51
vmID : "vm-456" ,
48
52
endpointType : "scp" ,
53
+ username : "" ,
49
54
mockVM : & types.VM {
50
55
ID : "vm-456" ,
51
56
DirectSSHEndpoint : "test-vm.example.com" ,
52
57
DirectSSHPort : 22 ,
53
58
Status : types .VMStatusRunning ,
54
59
},
55
60
mockGithubUsername : "testuser" ,
61
+ githubAPIError : false ,
56
62
expectedOutput : "scp://testuser@test-vm.example.com:22\n " ,
57
63
expectedError : "" ,
58
64
},
65
+ {
66
+ name : "Success - Custom username provided" ,
67
+ vmID : "vm-123" ,
68
+ endpointType : "ssh" ,
69
+ username : "customuser" ,
70
+ mockVM : & types.VM {
71
+ ID : "vm-123" ,
72
+ DirectSSHEndpoint : "test-vm.example.com" ,
73
+ DirectSSHPort : 22 ,
74
+ Status : types .VMStatusRunning ,
75
+ },
76
+ mockGithubUsername : "testuser" , // Should be ignored
77
+ githubAPIError : false , // Should be ignored
78
+ expectedOutput : "ssh://customuser@test-vm.example.com:22\n " ,
79
+ expectedError : "" ,
80
+ },
59
81
{
60
82
name : "Error - Missing SSH endpoint configuration" ,
61
83
vmID : "vm-789" ,
62
84
endpointType : "ssh" ,
85
+ username : "" ,
63
86
mockVM : & types.VM {
64
87
ID : "vm-789" ,
65
88
DirectSSHEndpoint : "" ,
66
89
DirectSSHPort : 0 ,
67
90
Status : types .VMStatusRunning ,
68
91
},
69
92
mockGithubUsername : "testuser" ,
93
+ githubAPIError : false ,
70
94
expectedOutput : "" ,
71
95
expectedError : "VM vm-789 does not have ssh endpoint configured" ,
72
96
},
73
97
{
74
98
name : "Error - Missing GitHub username" ,
75
99
vmID : "vm-123" ,
76
100
endpointType : "ssh" ,
101
+ username : "" ,
77
102
mockVM : & types.VM {
78
103
ID : "vm-123" ,
79
104
DirectSSHEndpoint : "test-vm.example.com" ,
80
105
DirectSSHPort : 22 ,
81
106
Status : types .VMStatusRunning ,
82
107
},
83
108
mockGithubUsername : "" ,
109
+ githubAPIError : false ,
84
110
expectedOutput : "" ,
85
- expectedError : "no github account associated with vendor portal user" ,
111
+ expectedError : "no GitHub account associated with Vendor Portal user" ,
112
+ },
113
+ {
114
+ name : "Error - GitHub username API error" ,
115
+ vmID : "vm-123" ,
116
+ endpointType : "ssh" ,
117
+ username : "" ,
118
+ mockVM : & types.VM {
119
+ ID : "vm-123" ,
120
+ DirectSSHEndpoint : "test-vm.example.com" ,
121
+ DirectSSHPort : 22 ,
122
+ Status : types .VMStatusRunning ,
123
+ },
124
+ mockGithubUsername : "" , // Won't be used due to API error
125
+ githubAPIError : true ,
126
+ expectedOutput : "" ,
127
+ expectedError : "--username flag to specify a custom username" , // Check for new error message
86
128
},
87
129
{
88
130
name : "Error - Invalid endpoint type" ,
89
131
vmID : "vm-123" ,
90
132
endpointType : "invalid" ,
133
+ username : "" ,
91
134
mockVM : & types.VM {
92
135
ID : "vm-123" ,
93
136
DirectSSHEndpoint : "test-vm.example.com" ,
94
137
DirectSSHPort : 22 ,
95
138
Status : types .VMStatusRunning ,
96
139
},
97
140
mockGithubUsername : "testuser" ,
141
+ githubAPIError : false ,
98
142
expectedOutput : "" ,
99
143
expectedError : "invalid endpoint type: invalid" ,
100
144
},
101
145
{
102
146
name : "Error - VM not in running state" ,
103
147
vmID : "vm-123" ,
104
148
endpointType : "ssh" ,
149
+ username : "" ,
105
150
mockVM : & types.VM {
106
151
ID : "vm-123" ,
107
152
DirectSSHEndpoint : "test-vm.example.com" ,
108
153
DirectSSHPort : 22 ,
109
154
Status : types .VMStatusProvisioning ,
110
155
},
111
156
mockGithubUsername : "testuser" ,
157
+ githubAPIError : false ,
112
158
expectedOutput : "" ,
113
159
expectedError : "VM vm-123 is not in running state (current state: provisioning). SSH is only available for running VMs" ,
114
160
},
@@ -117,7 +163,7 @@ func TestGetVMEndpoint(t *testing.T) {
117
163
for _ , tc := range tests {
118
164
t .Run (tc .name , func (t * testing.T ) {
119
165
// Create a mock server and client for API testing
120
- server , apiClient := createMockServerAndClient (tc .vmID , tc .mockVM , tc .mockGithubUsername )
166
+ server , apiClient := createMockServerAndClient (tc .vmID , tc .mockVM , tc .mockGithubUsername , tc . githubAPIError )
121
167
defer server .Close ()
122
168
123
169
// Capture stdout directly
@@ -131,7 +177,7 @@ func TestGetVMEndpoint(t *testing.T) {
131
177
}
132
178
133
179
// Execute the function under test
134
- err := runner .getVMEndpoint (tc .vmID , tc .endpointType )
180
+ err := runner .getVMEndpoint (tc .vmID , tc .endpointType , tc . username )
135
181
136
182
// Get captured output
137
183
w .Close ()
@@ -153,7 +199,7 @@ func TestGetVMEndpoint(t *testing.T) {
153
199
}
154
200
155
201
// createMockServerAndClient sets up a mock HTTP server and returns both kotsAPI.GetVM and kotsAPI.GetGitHubUsername
156
- func createMockServerAndClient (vmID string , mockVM * types.VM , mockGithubUsername string ) (* httptest.Server , * kotsclient.VendorV3Client ) {
202
+ func createMockServerAndClient (vmID string , mockVM * types.VM , mockGithubUsername string , githubAPIError bool ) (* httptest.Server , * kotsclient.VendorV3Client ) {
157
203
// Create a mock HTTP server to handle API requests
158
204
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
159
205
w .Header ().Set ("Content-Type" , "application/json" )
@@ -168,6 +214,11 @@ func createMockServerAndClient(vmID string, mockVM *types.VM, mockGithubUsername
168
214
169
215
// Handle GetGitHubUsername request
170
216
case r .URL .Path == "/v1/user" && r .Method == "GET" :
217
+ if githubAPIError {
218
+ w .WriteHeader (http .StatusInternalServerError )
219
+ json .NewEncoder (w ).Encode (map [string ]string {"error" : "GitHub API error" })
220
+ return
221
+ }
171
222
response := kotsclient.GetUserResponse {
172
223
GitHubUsername : mockGithubUsername ,
173
224
}
0 commit comments