|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/404tk/cloudtoolkit/pkg/providers/alibaba/auth" |
| 12 | +) |
| 13 | + |
| 14 | +func TestClientDescribeECSInstancesUsesLocationResolvedEndpoint(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + client := NewClient( |
| 18 | + auth.New("ak", "sk", ""), |
| 19 | + WithHTTPClient(&http.Client{ |
| 20 | + Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 21 | + switch req.URL.Host { |
| 22 | + case locationReadonlyEndpoint: |
| 23 | + if got := req.URL.Query().Get("ServiceCode"); got != "ecs" { |
| 24 | + t.Fatalf("unexpected location service code: %s", got) |
| 25 | + } |
| 26 | + if got := req.URL.Query().Get("Id"); got != "cn-guangzhou" { |
| 27 | + t.Fatalf("unexpected location region: %s", got) |
| 28 | + } |
| 29 | + if got := req.URL.Query().Get("RegionId"); got != "" { |
| 30 | + t.Fatalf("unexpected location RegionId: %s", got) |
| 31 | + } |
| 32 | + return jsonResponse(http.StatusOK, `{"Success":true,"Endpoints":{"Endpoint":[{"Endpoint":"ecs.cn-guangzhou.aliyuncs.com"}]}}`), nil |
| 33 | + case "ecs.cn-guangzhou.aliyuncs.com": |
| 34 | + if got := req.URL.Query().Get("Action"); got != "DescribeInstances" { |
| 35 | + t.Fatalf("unexpected ecs action: %s", got) |
| 36 | + } |
| 37 | + return jsonResponse(http.StatusOK, `{"PageSize":100,"PageNumber":1,"TotalCount":0,"Instances":{"Instance":[]}}`), nil |
| 38 | + default: |
| 39 | + t.Fatalf("unexpected host: %s", req.URL.Host) |
| 40 | + return nil, nil |
| 41 | + } |
| 42 | + }), |
| 43 | + }), |
| 44 | + WithClock(func() time.Time { return time.Unix(1713376800, 0).UTC() }), |
| 45 | + WithNonce(func() string { return "nonce" }), |
| 46 | + ) |
| 47 | + |
| 48 | + if _, err := client.DescribeECSInstances(context.Background(), "cn-guangzhou", 1, 100); err != nil { |
| 49 | + t.Fatalf("DescribeECSInstances() error = %v", err) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestClientDescribeECSInstancesFallsBackToGlobalEndpointWhenLocationMisses(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + client := NewClient( |
| 57 | + auth.New("ak", "sk", ""), |
| 58 | + WithHTTPClient(&http.Client{ |
| 59 | + Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 60 | + switch req.URL.Host { |
| 61 | + case locationReadonlyEndpoint: |
| 62 | + return jsonResponse(http.StatusOK, `{"Success":true,"Endpoints":{"Endpoint":[]}}`), nil |
| 63 | + case ecsGlobalEndpoint: |
| 64 | + return jsonResponse(http.StatusOK, `{"PageSize":100,"PageNumber":1,"TotalCount":0,"Instances":{"Instance":[]}}`), nil |
| 65 | + default: |
| 66 | + t.Fatalf("unexpected host: %s", req.URL.Host) |
| 67 | + return nil, nil |
| 68 | + } |
| 69 | + }), |
| 70 | + }), |
| 71 | + WithClock(func() time.Time { return time.Unix(1713376800, 0).UTC() }), |
| 72 | + WithNonce(func() string { return "nonce" }), |
| 73 | + ) |
| 74 | + |
| 75 | + if _, err := client.DescribeECSInstances(context.Background(), "cn-fuzhou", 1, 100); err != nil { |
| 76 | + t.Fatalf("DescribeECSInstances() error = %v", err) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestClientDescribeECSInstancesUsesStaticRegionalEndpoint(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + |
| 83 | + client := NewClient( |
| 84 | + auth.New("ak", "sk", ""), |
| 85 | + WithHTTPClient(&http.Client{ |
| 86 | + Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 87 | + if req.URL.Host != "ecs.ap-northeast-1.aliyuncs.com" { |
| 88 | + t.Fatalf("unexpected host: %s", req.URL.Host) |
| 89 | + } |
| 90 | + if strings.Contains(req.URL.RawQuery, "ServiceCode=ecs") { |
| 91 | + t.Fatal("location resolver should not be used for static regional endpoint") |
| 92 | + } |
| 93 | + return jsonResponse(http.StatusOK, `{"PageSize":100,"PageNumber":1,"TotalCount":0,"Instances":{"Instance":[]}}`), nil |
| 94 | + }), |
| 95 | + }), |
| 96 | + WithClock(func() time.Time { return time.Unix(1713376800, 0).UTC() }), |
| 97 | + WithNonce(func() string { return "nonce" }), |
| 98 | + ) |
| 99 | + |
| 100 | + if _, err := client.DescribeECSInstances(context.Background(), "ap-northeast-1", 1, 100); err != nil { |
| 101 | + t.Fatalf("DescribeECSInstances() error = %v", err) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestClientDescribeECSInstancesRetriesWithLocationAfterNotSupportedEndpoint(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + |
| 108 | + var initialAttempts int |
| 109 | + client := NewClient( |
| 110 | + auth.New("ak", "sk", ""), |
| 111 | + WithHTTPClient(&http.Client{ |
| 112 | + Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 113 | + switch req.URL.Host { |
| 114 | + case "ecs-cn-hangzhou.aliyuncs.com": |
| 115 | + initialAttempts++ |
| 116 | + return jsonResponse(http.StatusNotFound, `{"Code":"InvalidOperation.NotSupportedEndpoint","Message":"The specified endpoint cant operate this region.","RequestId":"req-1"}`), nil |
| 117 | + case locationReadonlyEndpoint: |
| 118 | + if got := req.URL.Query().Get("Id"); got != "ap-southeast-1" { |
| 119 | + t.Fatalf("unexpected location region: %s", got) |
| 120 | + } |
| 121 | + return jsonResponse(http.StatusOK, `{"Success":true,"Endpoints":{"Endpoint":[{"Endpoint":"ecs.ap-southeast-1.aliyuncs.com"}]}}`), nil |
| 122 | + case "ecs.ap-southeast-1.aliyuncs.com": |
| 123 | + return jsonResponse(http.StatusOK, `{"PageSize":100,"PageNumber":1,"TotalCount":0,"Instances":{"Instance":[]}}`), nil |
| 124 | + default: |
| 125 | + t.Fatalf("unexpected host: %s", req.URL.Host) |
| 126 | + return nil, nil |
| 127 | + } |
| 128 | + }), |
| 129 | + }), |
| 130 | + WithClock(func() time.Time { return time.Unix(1713376800, 0).UTC() }), |
| 131 | + WithNonce(func() string { return "nonce" }), |
| 132 | + ) |
| 133 | + |
| 134 | + if _, err := client.DescribeECSInstances(context.Background(), "ap-southeast-1", 1, 100); err != nil { |
| 135 | + t.Fatalf("DescribeECSInstances() error = %v", err) |
| 136 | + } |
| 137 | + if initialAttempts != 1 { |
| 138 | + t.Fatalf("unexpected initial attempts: %d", initialAttempts) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +type roundTripFunc func(*http.Request) (*http.Response, error) |
| 143 | + |
| 144 | +func (fn roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { |
| 145 | + return fn(req) |
| 146 | +} |
| 147 | + |
| 148 | +func jsonResponse(status int, body string) *http.Response { |
| 149 | + return &http.Response{ |
| 150 | + StatusCode: status, |
| 151 | + Status: http.StatusText(status), |
| 152 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 153 | + Body: io.NopCloser(strings.NewReader(body)), |
| 154 | + } |
| 155 | +} |
0 commit comments