This commit is contained in:
@@ -21,6 +21,24 @@ func testClient(t *testing.T, handler http.HandlerFunc) (*Client, *httptest.Serv
|
||||
return client, server
|
||||
}
|
||||
|
||||
func TestCreateOptionsDecodeDetailedImageAndInstanceTypeMetadata(t *testing.T) {
|
||||
var options CreateOptions
|
||||
raw := `{
|
||||
"region":"ap-northeast-1",
|
||||
"images":[{"catalog_id":"image-row","region":"ap-northeast-1","category_id":"linux","image_id":"ami-1","source_scope":"quick-start","source_name":"al2023","display_name":"Amazon Linux","availability_status":"available","description":"Linux image","architecture":"x86_64","platform_details":"Linux/UNIX","os_family":"linux","recommended_user":"ec2-user","root_volume_size_gib":8,"root_volume_type":"gp3","virtualization_type":"hvm","ena_support":true,"public":true}],
|
||||
"instance_types":[{"id":"type-row","region":"ap-northeast-1","category_id":"general","instance_type":"g4dn.xlarge","vcpus":4,"memory_mib":16384,"architectures":["x86_64"],"network_performance":"Up to 25 Gigabit","network_baseline_gbps":5,"network_peak_gbps":25,"gpu_count":1,"gpu_memory_mib":16384,"gpus":[{"manufacturer":"NVIDIA","name":"T4","count":1,"memory_mib":16384}],"current_generation":true}]
|
||||
}`
|
||||
if err := json.Unmarshal([]byte(raw), &options); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(options.Images) != 1 || options.Images[0].Architecture != "x86_64" || options.Images[0].RecommendedUser != "ec2-user" || options.Images[0].RootVolumeSizeGiB != 8 || !options.Images[0].ENASupport {
|
||||
t.Fatalf("detailed image metadata was not decoded: %#v", options.Images)
|
||||
}
|
||||
if len(options.InstanceTypes) != 1 || options.InstanceTypes[0].NetworkPeakGbps != 25 || options.InstanceTypes[0].GPUMemoryMiB != 16384 || len(options.InstanceTypes[0].GPUs) != 1 || options.InstanceTypes[0].GPUs[0].Name != "T4" {
|
||||
t.Fatalf("detailed instance type metadata was not decoded: %#v", options.InstanceTypes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientSendsAuthAndStableIdempotencyOnRetry(t *testing.T) {
|
||||
var mutex sync.Mutex
|
||||
attempts := 0
|
||||
@@ -138,3 +156,50 @@ func TestRuntimePricingAndInstanceEIPOperations(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceChangePricingAndAsyncTraffic(t *testing.T) {
|
||||
paths := []string{}
|
||||
client, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
paths = append(paths, r.Method+" "+r.URL.Path)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.URL.Path == "/api/sdk/v1/regions/us-east-1/instances/local-1/change-price-estimate" {
|
||||
var body InstanceChangePriceRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Kind != InstanceChangeTrafficLimit || body.LimitGiB == nil || *body.LimitGiB != 200 {
|
||||
t.Fatalf("unexpected quote body %#v err=%v", body, err)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"code":200,"message":"Success","data":{"kind":"traffic_limit","currency":"USD","charge_price":5.5,"configuration_fingerprint":"fp"}}`))
|
||||
return
|
||||
}
|
||||
if r.Header.Get("Idempotency-Key") != "traffic-order-1" {
|
||||
t.Fatalf("missing stable idempotency key")
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"code":202,"message":"Accepted","data":{"task_id":"task-traffic","status":"queued"}}`))
|
||||
})
|
||||
limit := 200.0
|
||||
estimate, err := client.Pricing.EstimateInstanceChange(context.Background(), "us-east-1", "local-1", InstanceChangePriceRequest{Kind: InstanceChangeTrafficLimit, PeriodEnd: time.Now().AddDate(0, 1, 0), LimitGiB: &limit})
|
||||
if err != nil || estimate.ConfigurationFingerprint != "fp" || estimate.ChargePrice != 5.5 {
|
||||
t.Fatalf("estimate=%#v err=%v", estimate, err)
|
||||
}
|
||||
operation, err := client.Traffic.UpdateAsync(context.Background(), "us-east-1", "local-1", TrafficLimitRequest{LimitGiB: &limit, ExpectedConfigurationFingerprint: "fp"}, WithIdempotencyKey("traffic-order-1"))
|
||||
if err != nil || operation.TaskID != "task-traffic" {
|
||||
t.Fatalf("operation=%#v err=%v", operation, err)
|
||||
}
|
||||
want := []string{"POST /api/sdk/v1/regions/us-east-1/instances/local-1/change-price-estimate", "POST /api/sdk/v1/regions/us-east-1/instances/local-1/traffic-limit/update"}
|
||||
if len(paths) != len(want) || paths[0] != want[0] || paths[1] != want[1] {
|
||||
t.Fatalf("paths=%v want=%v", paths, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceDetailsExposeCatalogMetadata(t *testing.T) {
|
||||
client, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"code":200,"message":"Success","data":{"instance":{"instance_id":"i-1"},"instance_type_details":{"instance_type":"m7i.large","vcpus":2,"catalog_match":true},"image_details":{"image_id":"ami-1","display_name":"Ubuntu 24.04","catalog_match":true}}}`))
|
||||
})
|
||||
details, err := client.Instances.GetDetails(context.Background(), "us-east-1", "local-1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if details.InstanceTypeDetails()["instance_type"] != "m7i.large" || details.ImageDetails()["display_name"] != "Ubuntu 24.04" {
|
||||
t.Fatalf("catalog details were not decoded: %#v", details)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user