staticpath_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package pathing
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestNewStaticFileStoragePathFormatter(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. rootDir string
  10. pipeline string
  11. wantErr bool
  12. }{
  13. {"empty rootDir returns error", "", "pricingmodel", true},
  14. {"empty pipeline returns error", "cloud-agent", "", true},
  15. {"valid args returns formatter", "cloud-agent", "pricingmodel", false},
  16. }
  17. for _, tt := range tests {
  18. t.Run(tt.name, func(t *testing.T) {
  19. f, err := NewStaticFileStoragePathFormatter(tt.rootDir, tt.pipeline)
  20. if tt.wantErr {
  21. require.Error(t, err)
  22. require.Nil(t, f)
  23. } else {
  24. require.NoError(t, err)
  25. require.NotNil(t, f)
  26. }
  27. })
  28. }
  29. }
  30. func TestStaticFileStoragePathFormatter_Dir(t *testing.T) {
  31. tests := []struct {
  32. rootDir string
  33. pipeline string
  34. expected string
  35. }{
  36. {"cloud-agent", "pricingmodel", "cloud-agent/pricingmodel"},
  37. {"/var/data", "kubemodel", "/var/data/kubemodel"},
  38. {"root", "a/b/c", "root/a/b/c"},
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.expected, func(t *testing.T) {
  42. f, err := NewStaticFileStoragePathFormatter(tt.rootDir, tt.pipeline)
  43. require.NoError(t, err)
  44. require.Equal(t, tt.expected, f.Dir())
  45. })
  46. }
  47. }
  48. func TestStaticFilePathFormatter(t *testing.T) {
  49. type testCase struct {
  50. name string
  51. rootDir string
  52. pipeline string
  53. prefix string
  54. in string
  55. fileExt string
  56. expected string
  57. }
  58. testCases := []testCase{
  59. {
  60. name: "no prefix no ext",
  61. rootDir: "cloud-agent",
  62. pipeline: "pricingmodel",
  63. prefix: "",
  64. in: "aws_list_pricing_api",
  65. fileExt: "",
  66. expected: "cloud-agent/pricingmodel/aws_list_pricing_api",
  67. },
  68. {
  69. name: "no prefix with ext",
  70. rootDir: "cloud-agent",
  71. pipeline: "pricingmodel",
  72. prefix: "",
  73. in: "aws_list_pricing_api",
  74. fileExt: "bin",
  75. expected: "cloud-agent/pricingmodel/aws_list_pricing_api.bin",
  76. },
  77. {
  78. name: "with prefix with ext",
  79. rootDir: "cloud-agent",
  80. pipeline: "pricingmodel",
  81. prefix: "v1",
  82. in: "aws_list_pricing_api",
  83. fileExt: "bin",
  84. expected: "cloud-agent/pricingmodel/v1.aws_list_pricing_api.bin",
  85. },
  86. {
  87. name: "with prefix no ext",
  88. rootDir: "cloud-agent",
  89. pipeline: "pricingmodel",
  90. prefix: "v1",
  91. in: "aws_list_pricing_api",
  92. fileExt: "",
  93. expected: "cloud-agent/pricingmodel/v1.aws_list_pricing_api",
  94. },
  95. {
  96. name: "in with subdir no prefix no ext",
  97. rootDir: "cloud-agent",
  98. pipeline: "pricingmodel",
  99. prefix: "",
  100. in: "us-east-1/aws_list_pricing_api",
  101. fileExt: "",
  102. expected: "cloud-agent/pricingmodel/us-east-1/aws_list_pricing_api",
  103. },
  104. {
  105. name: "in with subdir with prefix and ext",
  106. rootDir: "cloud-agent",
  107. pipeline: "pricingmodel",
  108. prefix: "v1",
  109. in: "us-east-1/aws_list_pricing_api",
  110. fileExt: "bin",
  111. expected: "cloud-agent/pricingmodel/us-east-1/v1.aws_list_pricing_api.bin",
  112. },
  113. {
  114. name: "in with nested subdir with ext",
  115. rootDir: "/var/data",
  116. pipeline: "kubemodel",
  117. prefix: "",
  118. in: "2024/01/nodes",
  119. fileExt: "bin",
  120. expected: "/var/data/kubemodel/2024/01/nodes.bin",
  121. },
  122. }
  123. for _, tc := range testCases {
  124. t.Run(tc.name, func(t *testing.T) {
  125. pathing, err := NewStaticFileStoragePathFormatter(tc.rootDir, tc.pipeline)
  126. if err != nil {
  127. t.Fatalf("Unexpected error: %v", err)
  128. }
  129. result := pathing.ToFullPath(tc.prefix, tc.in, tc.fileExt)
  130. require.Equal(t, tc.expected, result)
  131. })
  132. }
  133. }