2
0

keytuple_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package costmodel_test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/opencost/opencost/pkg/costmodel"
  6. )
  7. func TestKeyTupleSplit(t *testing.T) {
  8. const (
  9. ns = "kubecost"
  10. key = "my-pod"
  11. clusterID = "cluster-one"
  12. fullKey = "kubecost,my-pod,cluster-one"
  13. )
  14. kt, err := costmodel.NewKeyTuple(fullKey)
  15. if err != nil {
  16. t.Errorf("Error: %s\n", err)
  17. return
  18. }
  19. t.Logf("Namespace: %s, Key: %s, ClusterID: %s\n", kt.Namespace(), kt.Key(), kt.ClusterID())
  20. if !strings.EqualFold(kt.Namespace(), ns) {
  21. t.Errorf("Namespace: \"%s\" != \"%s\"", kt.Namespace(), ns)
  22. return
  23. }
  24. if !strings.EqualFold(kt.Key(), key) {
  25. t.Errorf("Key: \"%s\" != \"%s\"\n", kt.Key(), key)
  26. return
  27. }
  28. if !strings.EqualFold(kt.ClusterID(), clusterID) {
  29. t.Errorf("ClusterID: \"%s\" != \"%s\"\n", kt.ClusterID(), clusterID)
  30. return
  31. }
  32. }
  33. func TestKeyTupleSingleFail(t *testing.T) {
  34. _, err := costmodel.NewKeyTuple("foo")
  35. if err == nil {
  36. t.Errorf("Error was non-nil for single element!")
  37. return
  38. }
  39. }
  40. func TestKeyTupleDoubleFail(t *testing.T) {
  41. _, err := costmodel.NewKeyTuple("foo,bar")
  42. if err == nil {
  43. t.Errorf("Error was non-nil for two elements!")
  44. return
  45. }
  46. }
  47. func TestKeyTupleMoreThanThreeFail(t *testing.T) {
  48. _, err := costmodel.NewKeyTuple("foo,bar,fizz,buzz")
  49. if err == nil {
  50. t.Errorf("Error was non-nil for two elements!")
  51. return
  52. }
  53. }
  54. func TestOnlyCommas(t *testing.T) {
  55. kt, err := costmodel.NewKeyTuple(",,")
  56. if err != nil {
  57. t.Errorf("Error: %s\n", err)
  58. return
  59. }
  60. t.Logf("Namespace: \"%s\", Key: \"%s\", ClusterID: \"%s\"\n", kt.Namespace(), kt.Key(), kt.ClusterID())
  61. if !strings.EqualFold(kt.Namespace(), "") {
  62. t.Errorf("Namespace: \"%s\" != \"%s\"", kt.Namespace(), "")
  63. return
  64. }
  65. if !strings.EqualFold(kt.Key(), "") {
  66. t.Errorf("Key: \"%s\" != \"%s\"\n", kt.Key(), "")
  67. return
  68. }
  69. if !strings.EqualFold(kt.ClusterID(), "") {
  70. t.Errorf("ClusterID: \"%s\" != \"%s\"\n", kt.ClusterID(), "")
  71. return
  72. }
  73. }
  74. func TestManyEntrys(t *testing.T) {
  75. _, err := costmodel.NewKeyTuple("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p")
  76. if err == nil {
  77. t.Errorf("Error was non-nil for single element!")
  78. return
  79. }
  80. }