@@ -11,17 +11,15 @@ import (
1111
1212func TestGetInstanceTypesConvertsProducts (t * testing.T ) {
1313 server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
14- if r .URL .Path != "/vserver/v2/getServerProductList" {
14+ w .Header ().Set ("Content-Type" , "application/json" )
15+ switch r .URL .Path {
16+ case "/vserver/v2/getServerProductList" :
17+ writeServerProductList (t , w , r )
18+ case "/billing/v1/product/getProductPriceList" :
19+ writeProductPriceList (t , w , r )
20+ default :
1521 t .Fatalf ("path = %q" , r .URL .Path )
1622 }
17- if got := r .URL .Query ().Get ("regionCode" ); got != "KR" {
18- t .Fatalf ("regionCode = %q" , got )
19- }
20- if got := r .URL .Query ().Get ("serverImageProductCode" ); got != defaultServerImageProductCode {
21- t .Fatalf ("serverImageProductCode = %q" , got )
22- }
23- w .Header ().Set ("Content-Type" , "application/json" )
24- _ , _ = w .Write ([]byte (`{"getServerProductListResponse":{"returnCode":"0","returnMessage":"success","totalRows":2,"productList":[{"productCode":"SVR.VSVR.GPU.T4.C004.M016.NET.SSD.B050.G002","productName":"vCPU 4EA, Memory 16GB, NVIDIA T4 1EA, [SSD]Disk 50GB","productType":{"code":"GPU","codeName":"GPU"},"productDescription":"vCPU 4EA, Memory 16GB, NVIDIA T4 1EA, [SSD]Disk 50GB","cpuCount":4,"memorySize":17179869184,"baseBlockStorageSize":53687091200,"diskType":{"code":"NET","codeName":"Network storage"},"generationCode":"G2"},{"productCode":"SVR.VSVR.STAND.C002.M008.NET.SSD.B050.G002","productName":"vCPU 2EA, Memory 8GB, [SSD]Disk 50GB","productType":{"code":"STAND","codeName":"Standard"},"productDescription":"vCPU 2EA, Memory 8GB, [SSD]Disk 50GB","cpuCount":2,"memorySize":8589934592,"baseBlockStorageSize":53687091200,"diskType":{"code":"NET","codeName":"Network storage"},"generationCode":"G2"}]}}` ))
2523 }))
2624 defer server .Close ()
2725
@@ -36,7 +34,11 @@ func TestGetInstanceTypesConvertsProducts(t *testing.T) {
3634 if len (types ) != 1 {
3735 t .Fatalf ("instance types len = %d, want 1" , len (types ))
3836 }
39- it := types [0 ]
37+ assertPricedGPUInstanceType (t , types [0 ])
38+ }
39+
40+ func assertPricedGPUInstanceType (t * testing.T , it cloud.InstanceType ) {
41+ t .Helper ()
4042 if it .Provider != CloudProviderID || it .Cloud != CloudProviderID || it .Location != "KR" {
4143 t .Fatalf ("unexpected provider fields: %+v" , it )
4244 }
@@ -46,7 +48,64 @@ func TestGetInstanceTypesConvertsProducts(t *testing.T) {
4648 if len (it .SupportedGPUs ) != 1 || it .SupportedGPUs [0 ].Name != "T4" || it .SupportedGPUs [0 ].Count != 1 {
4749 t .Fatalf ("unexpected GPU conversion: %+v" , it .SupportedGPUs )
4850 }
51+ if it .BasePrice == nil || it .BasePrice .CurrencyCode () != "USD" || it .BasePrice .Number () != "0.5" {
52+ t .Fatalf ("unexpected base price: %v" , it .BasePrice )
53+ }
4954 if it .ID == "" {
5055 t .Fatal ("instance type ID is empty" )
5156 }
5257}
58+
59+ func writeServerProductList (t * testing.T , w http.ResponseWriter , r * http.Request ) {
60+ t .Helper ()
61+ if got := r .URL .Query ().Get ("regionCode" ); got != "KR" {
62+ t .Fatalf ("regionCode = %q" , got )
63+ }
64+ if got := r .URL .Query ().Get ("serverImageProductCode" ); got != defaultServerImageProductCode {
65+ t .Fatalf ("serverImageProductCode = %q" , got )
66+ }
67+ _ , _ = w .Write ([]byte (`{"getServerProductListResponse":{"returnCode":"0","returnMessage":"success","totalRows":2,"productList":[{"productCode":"SVR.VSVR.GPU.T4.C004.M016.NET.SSD.B050.G002","productName":"vCPU 4EA, Memory 16GB, NVIDIA T4 1EA, [SSD]Disk 50GB","productType":{"code":"GPU","codeName":"GPU"},"productDescription":"vCPU 4EA, Memory 16GB, NVIDIA T4 1EA, [SSD]Disk 50GB","cpuCount":4,"memorySize":17179869184,"baseBlockStorageSize":53687091200,"diskType":{"code":"NET","codeName":"Network storage"},"generationCode":"G2"},{"productCode":"SVR.VSVR.STAND.C002.M008.NET.SSD.B050.G002","productName":"vCPU 2EA, Memory 8GB, [SSD]Disk 50GB","productType":{"code":"STAND","codeName":"Standard"},"productDescription":"vCPU 2EA, Memory 8GB, [SSD]Disk 50GB","cpuCount":2,"memorySize":8589934592,"baseBlockStorageSize":53687091200,"diskType":{"code":"NET","codeName":"Network storage"},"generationCode":"G2"}]}}` ))
68+ }
69+
70+ func writeProductPriceList (t * testing.T , w http.ResponseWriter , r * http.Request ) {
71+ t .Helper ()
72+ if got := r .URL .Query ().Get ("regionCode" ); got != "KR" {
73+ t .Fatalf ("price regionCode = %q" , got )
74+ }
75+ if got := r .URL .Query ().Get ("productItemKindCode" ); got != ncloudServerProductKindVPC {
76+ t .Fatalf ("productItemKindCode = %q" , got )
77+ }
78+ if got := r .URL .Query ().Get ("payCurrencyCode" ); got != defaultPayCurrencyCode {
79+ t .Fatalf ("payCurrencyCode = %q" , got )
80+ }
81+ _ , _ = w .Write ([]byte (`{"getProductPriceListResponse":{"returnCode":"0","returnMessage":"success","totalRows":1,"productPriceList":[{"productCode":"SVR.VSVR.GPU.T4.C004.M016.NET.SSD.B050.G002","priceList":[{"priceType":{"code":"FXSUM","codeName":"Monthly flat rate"},"unit":{"code":"USAGE_TIME","codeName":"Usage time"},"price":1500,"payCurrency":{"code":"USD","codeName":"US Dollar"}},{"priceType":{"code":"MTRAT","codeName":"Meter rate"},"unit":{"code":"USAGE_HH","codeName":"Usage time (per hour)"},"price":0.5,"payCurrency":{"code":"USD","codeName":"US Dollar"}}]}]}}` ))
82+ }
83+
84+ func TestGetInstanceTypesIgnoresPriceLookupFailure (t * testing.T ) {
85+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
86+ w .Header ().Set ("Content-Type" , "application/json" )
87+ switch r .URL .Path {
88+ case "/vserver/v2/getServerProductList" :
89+ _ , _ = w .Write ([]byte (`{"getServerProductListResponse":{"returnCode":"0","returnMessage":"success","totalRows":1,"productList":[{"productCode":"SVR.VSVR.STAND.C002.M008.NET.SSD.B050.G002","productName":"vCPU 2EA, Memory 8GB, [SSD]Disk 50GB","productType":{"code":"STAND","codeName":"Standard"},"productDescription":"vCPU 2EA, Memory 8GB, [SSD]Disk 50GB","cpuCount":2,"memorySize":8589934592,"baseBlockStorageSize":53687091200,"diskType":{"code":"NET","codeName":"Network storage"},"generationCode":"G2"}]}}` ))
90+ case "/billing/v1/product/getProductPriceList" :
91+ http .Error (w , "billing unavailable" , http .StatusInternalServerError )
92+ default :
93+ t .Fatalf ("path = %q" , r .URL .Path )
94+ }
95+ }))
96+ defer server .Close ()
97+
98+ client := newTestNaverClient (t , server .URL )
99+ types , err := client .GetInstanceTypes (context .Background (), cloud.GetInstanceTypeArgs {
100+ Locations : cloud.LocationsFilter {"KR" },
101+ })
102+ if err != nil {
103+ t .Fatalf ("GetInstanceTypes() error = %v" , err )
104+ }
105+ if len (types ) != 1 {
106+ t .Fatalf ("instance types len = %d, want 1" , len (types ))
107+ }
108+ if types [0 ].BasePrice != nil {
109+ t .Fatalf ("BasePrice = %v, want nil after price lookup failure" , types [0 ].BasePrice )
110+ }
111+ }
0 commit comments