@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- package capacityclient
17+ package compute
1818
1919import (
2020 "context"
@@ -25,13 +25,16 @@ import (
2525 "strings"
2626 "testing"
2727
28+ "github.com/google/go-cmp/cmp"
2829 "google.golang.org/protobuf/encoding/protojson"
2930 "google.golang.org/protobuf/proto"
3031
31- computev1 "go.goms.io/fleet/pkg/protos/azure/compute/v1"
32+ computev1 "go.goms.io/fleet/apis/protos/azure/compute/v1"
33+ "go.goms.io/fleet/pkg/clients/consts"
3234)
3335
34- func TestNewClient (t * testing.T ) {
36+ func TestNewAttributeBasedVMSizeRecommenderClient (t * testing.T ) {
37+ defaultClient := & http.Client {Timeout : consts .HTTPTimeoutAzure }
3538 tests := []struct {
3639 name string
3740 endpoint string
@@ -51,51 +54,46 @@ func TestNewClient(t *testing.T) {
5154 endpoint : "https://example.com" ,
5255 httpClient : nil ,
5356 wantBaseURL : "https://example.com" ,
54- wantHTTPClient : http . DefaultClient ,
57+ wantHTTPClient : defaultClient ,
5558 },
5659 {
5760 name : "removes trailing slash from endpoint" ,
5861 endpoint : "https://example.com/" ,
5962 httpClient : nil ,
6063 wantBaseURL : "https://example.com" ,
61- wantHTTPClient : http . DefaultClient ,
64+ wantHTTPClient : defaultClient ,
6265 },
6366 {
6467 name : "adds http scheme to endpoint without scheme" ,
6568 endpoint : "localhost:8080" ,
6669 httpClient : nil ,
6770 wantBaseURL : "http://localhost:8080" ,
68- wantHTTPClient : http . DefaultClient ,
71+ wantHTTPClient : defaultClient ,
6972 },
7073 {
7174 name : "adds http scheme and removes trailing slash" ,
7275 endpoint : "example.com:8080/" ,
7376 httpClient : nil ,
7477 wantBaseURL : "http://example.com:8080" ,
75- wantHTTPClient : http . DefaultClient ,
78+ wantHTTPClient : defaultClient ,
7679 },
7780 {
7881 name : "preserves existing http scheme" ,
7982 endpoint : "http://localhost:8080" ,
8083 httpClient : nil ,
8184 wantBaseURL : "http://localhost:8080" ,
82- wantHTTPClient : http . DefaultClient ,
85+ wantHTTPClient : defaultClient ,
8386 },
8487 }
8588
8689 for _ , tt := range tests {
8790 t .Run (tt .name , func (t * testing.T ) {
88- got := NewClient (tt .endpoint , tt .httpClient ).(* client )
91+ got := NewAttributeBasedVMSizeRecommenderClient (tt .endpoint , tt .httpClient ).(* attributeBasedVMSizeRecommenderClient )
8992 if got .baseURL != tt .wantBaseURL {
9093 t .Errorf ("NewClient() baseURL = %v, want %v" , got .baseURL , tt .wantBaseURL )
9194 }
92- // For custom HTTP client, just verify it's not nil
93- if tt .httpClient != nil && got .httpClient == nil {
94- t .Errorf ("NewClient() httpClient is nil, want non-nil" )
95- }
96- // For nil HTTP client, verify it uses default client
97- if tt .httpClient == nil && got .httpClient != http .DefaultClient {
98- t .Errorf ("NewClient() httpClient = %v, want http.DefaultClient" , got .httpClient )
95+ if ! cmp .Equal (got .httpClient , tt .wantHTTPClient ) {
96+ t .Errorf ("NewClient() httpClient = %v, want %v" , got .httpClient , tt .wantHTTPClient )
9997 }
10098 })
10199 }
@@ -240,22 +238,22 @@ func TestClient_GenerateAttributeBasedRecommendations(t *testing.T) {
240238 server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
241239 // Verify request method
242240 if r .Method != http .MethodPost {
243- t .Errorf ("expected POST request, got %s" , r .Method )
241+ t .Errorf ("got %s, want POST request " , r .Method )
244242 }
245243
246244 // Verify headers
247245 if r .Header .Get ("Content-Type" ) != "application/json" {
248- t .Errorf ("expected Content-Type: application/json, got %s " , r .Header .Get ("Content-Type" ))
246+ t .Errorf ("got %s, want Content-Type: application/json" , r .Header .Get ("Content-Type" ))
249247 }
250248 if r .Header .Get ("Accept" ) != "application/json" {
251- t .Errorf ("expected Accept: application/json, got %s " , r .Header .Get ("Accept" ))
249+ t .Errorf ("got %s, want Accept: application/json" , r .Header .Get ("Accept" ))
252250 }
253251
254252 // Verify URL path if request is not nil
255253 if tt .request != nil && tt .request .SubscriptionId != "" && tt .request .Location != "" {
256- expectedPath := fmt .Sprintf (recommendationsPathTemplate , tt .request .SubscriptionId , tt .request .Location )
257- if r .URL .Path != expectedPath {
258- t .Errorf ("expected path %s, got %s" , expectedPath , r .URL .Path )
254+ wantPath := fmt .Sprintf (recommendationsPathTemplate , tt .request .SubscriptionId , tt .request .Location )
255+ if r .URL .Path != wantPath {
256+ t .Errorf ("got %s, want path %s" , r .URL .Path , wantPath )
259257 }
260258
261259 // Verify request body using protojson for proper proto3 oneof support
@@ -284,7 +282,7 @@ func TestClient_GenerateAttributeBasedRecommendations(t *testing.T) {
284282 defer server .Close ()
285283
286284 // Create client
287- client := NewClient (server .URL , nil )
285+ client := NewAttributeBasedVMSizeRecommenderClient (server .URL , nil )
288286
289287 // Execute request
290288 got , err := client .GenerateAttributeBasedRecommendations (context .Background (), tt .request )
@@ -307,31 +305,3 @@ func TestClient_GenerateAttributeBasedRecommendations(t *testing.T) {
307305 })
308306 }
309307}
310-
311- func TestClient_GenerateAttributeBasedRecommendations_ContextCancellation (t * testing.T ) {
312- // Create a server that delays response
313- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
314- // This handler will never respond in time
315- <- r .Context ().Done ()
316- }))
317- defer server .Close ()
318-
319- client := NewClient (server .URL , nil )
320-
321- // Create a context that is already cancelled
322- ctx , cancel := context .WithCancel (context .Background ())
323- cancel ()
324-
325- req := & computev1.GenerateAttributeBasedRecommendationsRequest {
326- SubscriptionId : "sub-123" ,
327- Location : "eastus" ,
328- PriorityProfile : & computev1.GenerateAttributeBasedRecommendationsRequest_RegularPriorityProfile {
329- RegularPriorityProfile : & computev1.RegularPriorityProfile {},
330- },
331- }
332-
333- _ , err := client .GenerateAttributeBasedRecommendations (ctx , req )
334- if err == nil {
335- t .Error ("GenerateAttributeBasedRecommendations() expected error for cancelled context, got nil" )
336- }
337- }
0 commit comments