provider_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package ovh
  2. import (
  3. "math"
  4. "net/http"
  5. "net/http/httptest"
  6. "os"
  7. "strconv"
  8. "testing"
  9. v1 "k8s.io/api/core/v1"
  10. )
  11. func newTestProvider(t *testing.T, filename string) *OVH {
  12. t.Helper()
  13. data, err := os.ReadFile(filename)
  14. if err != nil {
  15. t.Fatalf("failed to read test fixture %s: %v", filename, err)
  16. }
  17. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. w.Header().Set("Content-Type", "application/json")
  19. w.Write(data)
  20. }))
  21. t.Cleanup(srv.Close)
  22. provider := &OVH{
  23. catalogURL: srv.URL,
  24. }
  25. if err := provider.DownloadPricingData(); err != nil {
  26. t.Fatalf("DownloadPricingData failed: %v", err)
  27. }
  28. return provider
  29. }
  30. func assertIntEqual(t *testing.T, name string, got, want int) {
  31. t.Helper()
  32. if got != want {
  33. t.Errorf("%s: got %d, want %d", name, got, want)
  34. }
  35. }
  36. func assertFloatClose(t *testing.T, name string, got, want, tolerance float64) {
  37. t.Helper()
  38. if math.Abs(got-want) > tolerance {
  39. t.Errorf("%s: got %f, want %f (tolerance %f)", name, got, want, tolerance)
  40. }
  41. }
  42. func parseFloat(t *testing.T, s string) float64 {
  43. t.Helper()
  44. v, err := strconv.ParseFloat(s, 64)
  45. if err != nil {
  46. t.Fatalf("parseFloat(%q) failed: %v", s, err)
  47. }
  48. return v
  49. }
  50. func TestParseCatalog(t *testing.T) {
  51. data, err := os.ReadFile("testdata/ovh_catalog.json")
  52. if err != nil {
  53. t.Fatalf("failed to read catalog fixture: %v", err)
  54. }
  55. pricing, volumePricing, err := parseCatalog(data)
  56. if err != nil {
  57. t.Fatalf("parseCatalog failed: %v", err)
  58. }
  59. // b2-7 instance: hourly and monthly
  60. b2, ok := pricing["b2-7"]
  61. if !ok {
  62. t.Fatal("b2-7 flavor not found")
  63. }
  64. // Hourly: 6810000 microcents / 100_000_000 = 0.0681
  65. assertFloatClose(t, "b2-7 hourly", b2.HourlyPrice, 0.0681, 0.0001)
  66. // Monthly: 2420000000 / 100_000_000 / 730 = 24.2 / 730
  67. assertFloatClose(t, "b2-7 monthly", b2.MonthlyPrice, 24.2/730.0, 0.0001)
  68. assertIntEqual(t, "b2-7 VCPU", b2.VCPU, 2)
  69. assertIntEqual(t, "b2-7 RAM", b2.RAM, 7)
  70. assertIntEqual(t, "b2-7 Disk", b2.Disk, 50)
  71. assertIntEqual(t, "b2-7 GPU", b2.GPU, 0)
  72. // t2-45 GPU instance
  73. t2, ok := pricing["t2-45"]
  74. if !ok {
  75. t.Fatal("t2-45 flavor not found")
  76. }
  77. // Hourly: 180000000 / 100_000_000 = 1.8
  78. assertFloatClose(t, "t2-45 hourly", t2.HourlyPrice, 1.8, 0.0001)
  79. // Monthly: 63800000000 / 100_000_000 / 730 = 638 / 730
  80. assertFloatClose(t, "t2-45 monthly", t2.MonthlyPrice, 638.0/730.0, 0.0001)
  81. assertIntEqual(t, "t2-45 VCPU", t2.VCPU, 15)
  82. assertIntEqual(t, "t2-45 RAM", t2.RAM, 45)
  83. assertIntEqual(t, "t2-45 Disk", t2.Disk, 400)
  84. assertIntEqual(t, "t2-45 GPU", t2.GPU, 1)
  85. if t2.GPUName != "Tesla V100S" {
  86. t.Errorf("t2-45 GPUName: got %q, want %q", t2.GPUName, "Tesla V100S")
  87. }
  88. // Volume pricing
  89. // high-speed-gen2: 11900 / 100_000_000 = 0.000119
  90. hsGen2, ok := volumePricing["high-speed-gen2"]
  91. if !ok {
  92. t.Fatal("high-speed-gen2 volume type not found")
  93. }
  94. assertFloatClose(t, "high-speed-gen2", hsGen2, 0.000119, 0.000001)
  95. hs, ok := volumePricing["high-speed"]
  96. if !ok {
  97. t.Fatal("high-speed volume type not found")
  98. }
  99. assertFloatClose(t, "high-speed", hs, 0.000119, 0.000001)
  100. // classic: 5900 / 100_000_000 = 0.000059
  101. classic, ok := volumePricing["classic"]
  102. if !ok {
  103. t.Fatal("classic volume type not found")
  104. }
  105. assertFloatClose(t, "classic", classic, 0.000059, 0.000001)
  106. }
  107. func TestOVHKey(t *testing.T) {
  108. key := &ovhKey{
  109. Labels: map[string]string{
  110. v1.LabelTopologyRegion: "GRA7",
  111. v1.LabelInstanceTypeStable: "b2-7",
  112. },
  113. }
  114. if got := key.Features(); got != "GRA7,b2-7" {
  115. t.Errorf("Features(): got %q, want %q", got, "GRA7,b2-7")
  116. }
  117. if got := key.GPUType(); got != "" {
  118. t.Errorf("GPUType(): got %q, want empty", got)
  119. }
  120. if got := key.GPUCount(); got != 0 {
  121. t.Errorf("GPUCount(): got %d, want 0", got)
  122. }
  123. if got := key.ID(); got != "" {
  124. t.Errorf("ID(): got %q, want empty", got)
  125. }
  126. }
  127. func TestOVHKeyGPU(t *testing.T) {
  128. tests := []struct {
  129. instanceType string
  130. wantGPU string
  131. }{
  132. {"t2-45", "t2-45"},
  133. {"l4-24", "l4-24"},
  134. {"l40s-48", "l40s-48"},
  135. {"a10-96", "a10-96"},
  136. {"a100-180", "a100-180"},
  137. {"b2-7", ""},
  138. {"d2-4", ""},
  139. }
  140. for _, tc := range tests {
  141. t.Run(tc.instanceType, func(t *testing.T) {
  142. key := &ovhKey{
  143. Labels: map[string]string{
  144. v1.LabelInstanceTypeStable: tc.instanceType,
  145. },
  146. }
  147. if got := key.GPUType(); got != tc.wantGPU {
  148. t.Errorf("GPUType(%s): got %q, want %q", tc.instanceType, got, tc.wantGPU)
  149. }
  150. })
  151. }
  152. }
  153. func TestOVHPVKey(t *testing.T) {
  154. tests := []struct {
  155. name string
  156. storageClass string
  157. zone string
  158. wantFeatures string
  159. }{
  160. {"high-speed-gen2", "csi-cinder-high-speed-gen2", "GRA7", "GRA7,high-speed-gen2"},
  161. {"high-speed", "csi-cinder-high-speed", "GRA9", "GRA9,high-speed"},
  162. {"classic", "csi-cinder-classic", "BHS5", "BHS5,classic"},
  163. {"unknown", "unknown-class", "GRA7", "GRA7,"},
  164. }
  165. for _, tc := range tests {
  166. t.Run(tc.name, func(t *testing.T) {
  167. key := &ovhPVKey{
  168. StorageClassName: tc.storageClass,
  169. Zone: tc.zone,
  170. }
  171. if got := key.Features(); got != tc.wantFeatures {
  172. t.Errorf("Features(): got %q, want %q", got, tc.wantFeatures)
  173. }
  174. if got := key.GetStorageClass(); got != tc.storageClass {
  175. t.Errorf("GetStorageClass(): got %q, want %q", got, tc.storageClass)
  176. }
  177. if got := key.ID(); got != "" {
  178. t.Errorf("ID(): got %q, want empty", got)
  179. }
  180. })
  181. }
  182. }
  183. func TestIsMonthlyBilling(t *testing.T) {
  184. tests := []struct {
  185. name string
  186. labels map[string]string
  187. monthlyPools []string
  188. want bool
  189. }{
  190. {
  191. name: "default hourly",
  192. labels: map[string]string{},
  193. want: false,
  194. },
  195. {
  196. name: "label monthly",
  197. labels: map[string]string{BillingLabel: "monthly"},
  198. want: true,
  199. },
  200. {
  201. name: "label hourly",
  202. labels: map[string]string{BillingLabel: "hourly"},
  203. want: false,
  204. },
  205. {
  206. name: "env monthly",
  207. labels: map[string]string{NodepoolLabel: "pool-monthly"},
  208. monthlyPools: []string{"pool-monthly", "other-pool"},
  209. want: true,
  210. },
  211. {
  212. name: "env miss",
  213. labels: map[string]string{NodepoolLabel: "pool-hourly"},
  214. monthlyPools: []string{"pool-monthly"},
  215. want: false,
  216. },
  217. {
  218. name: "label overrides env",
  219. labels: map[string]string{BillingLabel: "hourly", NodepoolLabel: "pool-monthly"},
  220. monthlyPools: []string{"pool-monthly"},
  221. want: false,
  222. },
  223. }
  224. for _, tc := range tests {
  225. t.Run(tc.name, func(t *testing.T) {
  226. got := isMonthlyBilling(tc.labels, tc.monthlyPools)
  227. if got != tc.want {
  228. t.Errorf("isMonthlyBilling(): got %v, want %v", got, tc.want)
  229. }
  230. })
  231. }
  232. }
  233. func TestNodePricing_Hourly(t *testing.T) {
  234. provider := newTestProvider(t, "testdata/ovh_catalog.json")
  235. key := &ovhKey{
  236. Labels: map[string]string{
  237. v1.LabelTopologyRegion: "GRA7",
  238. v1.LabelInstanceTypeStable: "b2-7",
  239. },
  240. }
  241. node, meta, err := provider.NodePricing(key)
  242. if err != nil {
  243. t.Fatalf("NodePricing failed: %v", err)
  244. }
  245. if meta.Source != "ovh" {
  246. t.Errorf("Source: got %q, want %q", meta.Source, "ovh")
  247. }
  248. assertFloatClose(t, "cost", parseFloat(t, node.Cost), 0.0681, 0.0001)
  249. assertIntEqual(t, "VCPU", int(parseFloat(t, node.VCPU)), 2)
  250. assertIntEqual(t, "RAM", int(parseFloat(t, node.RAM)), 7)
  251. assertIntEqual(t, "Storage", int(parseFloat(t, node.Storage)), 50)
  252. if node.Region != "GRA7" {
  253. t.Errorf("Region: got %q, want %q", node.Region, "GRA7")
  254. }
  255. if node.InstanceType != "b2-7" {
  256. t.Errorf("InstanceType: got %q, want %q", node.InstanceType, "b2-7")
  257. }
  258. }
  259. func TestNodePricing_Monthly(t *testing.T) {
  260. provider := newTestProvider(t, "testdata/ovh_catalog.json")
  261. key := &ovhKey{
  262. Labels: map[string]string{
  263. v1.LabelTopologyRegion: "GRA7",
  264. v1.LabelInstanceTypeStable: "b2-7",
  265. BillingLabel: "monthly",
  266. },
  267. }
  268. node, _, err := provider.NodePricing(key)
  269. if err != nil {
  270. t.Fatalf("NodePricing failed: %v", err)
  271. }
  272. // Monthly price: 24.2 / 730
  273. assertFloatClose(t, "cost", parseFloat(t, node.Cost), 24.2/730.0, 0.0001)
  274. }
  275. func TestNodePricing_MonthlyViaEnv(t *testing.T) {
  276. provider := newTestProvider(t, "testdata/ovh_catalog.json")
  277. provider.monthlyNodepools = []string{"my-monthly-pool"}
  278. key := &ovhKey{
  279. Labels: map[string]string{
  280. v1.LabelTopologyRegion: "GRA7",
  281. v1.LabelInstanceTypeStable: "b2-7",
  282. NodepoolLabel: "my-monthly-pool",
  283. },
  284. }
  285. node, _, err := provider.NodePricing(key)
  286. if err != nil {
  287. t.Fatalf("NodePricing failed: %v", err)
  288. }
  289. assertFloatClose(t, "cost", parseFloat(t, node.Cost), 24.2/730.0, 0.0001)
  290. }
  291. func TestNodePricing_GPU(t *testing.T) {
  292. provider := newTestProvider(t, "testdata/ovh_catalog.json")
  293. key := &ovhKey{
  294. Labels: map[string]string{
  295. v1.LabelTopologyRegion: "GRA7",
  296. v1.LabelInstanceTypeStable: "t2-45",
  297. },
  298. }
  299. node, _, err := provider.NodePricing(key)
  300. if err != nil {
  301. t.Fatalf("NodePricing failed: %v", err)
  302. }
  303. assertFloatClose(t, "cost", parseFloat(t, node.Cost), 1.8, 0.0001)
  304. assertIntEqual(t, "GPU", int(parseFloat(t, node.GPU)), 1)
  305. if node.GPUName != "Tesla V100S" {
  306. t.Errorf("GPUName: got %q, want %q", node.GPUName, "Tesla V100S")
  307. }
  308. assertIntEqual(t, "VCPU", int(parseFloat(t, node.VCPU)), 15)
  309. assertIntEqual(t, "RAM", int(parseFloat(t, node.RAM)), 45)
  310. }
  311. func TestNodePricing_NotFound(t *testing.T) {
  312. provider := newTestProvider(t, "testdata/ovh_catalog.json")
  313. key := &ovhKey{
  314. Labels: map[string]string{
  315. v1.LabelTopologyRegion: "GRA7",
  316. v1.LabelInstanceTypeStable: "unknown-flavor",
  317. },
  318. }
  319. _, _, err := provider.NodePricing(key)
  320. if err == nil {
  321. t.Fatal("expected error for unknown flavor, got nil")
  322. }
  323. }
  324. func TestPVPricing(t *testing.T) {
  325. provider := newTestProvider(t, "testdata/ovh_catalog.json")
  326. key := &ovhPVKey{
  327. StorageClassName: "csi-cinder-high-speed-gen2",
  328. Zone: "GRA7",
  329. }
  330. pv, err := provider.PVPricing(key)
  331. if err != nil {
  332. t.Fatalf("PVPricing failed: %v", err)
  333. }
  334. assertFloatClose(t, "cost", parseFloat(t, pv.Cost), 0.000119, 0.000001)
  335. if pv.Class != "csi-cinder-high-speed-gen2" {
  336. t.Errorf("Class: got %q, want %q", pv.Class, "csi-cinder-high-speed-gen2")
  337. }
  338. }
  339. func TestNetworkPricing(t *testing.T) {
  340. provider := &OVH{}
  341. net, err := provider.NetworkPricing()
  342. if err != nil {
  343. t.Fatalf("NetworkPricing failed: %v", err)
  344. }
  345. if net.ZoneNetworkEgressCost != 0 {
  346. t.Errorf("ZoneNetworkEgressCost: got %f, want 0", net.ZoneNetworkEgressCost)
  347. }
  348. if net.RegionNetworkEgressCost != 0 {
  349. t.Errorf("RegionNetworkEgressCost: got %f, want 0", net.RegionNetworkEgressCost)
  350. }
  351. assertFloatClose(t, "InternetNetworkEgressCost", net.InternetNetworkEgressCost, 0.01, 0.0001)
  352. if net.NatGatewayEgressCost != 0 {
  353. t.Errorf("NatGatewayEgressCost: got %f, want 0", net.NatGatewayEgressCost)
  354. }
  355. if net.NatGatewayIngressCost != 0 {
  356. t.Errorf("NatGatewayIngressCost: got %f, want 0", net.NatGatewayIngressCost)
  357. }
  358. }
  359. func TestLoadBalancerPricing(t *testing.T) {
  360. provider := &OVH{}
  361. lb, err := provider.LoadBalancerPricing()
  362. if err != nil {
  363. t.Fatalf("LoadBalancerPricing failed: %v", err)
  364. }
  365. assertFloatClose(t, "LB cost", lb.Cost, 0.012, 0.0001)
  366. }