parse_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package porter_app
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  9. "github.com/sergi/go-diff/diffmatchpatch"
  10. "github.com/matryer/is"
  11. )
  12. func TestParseYAML(t *testing.T) {
  13. tests := []struct {
  14. porterYamlFileName string
  15. want *porterv1.PorterApp
  16. }{
  17. {"v2_input_nobuild", result_nobuild},
  18. {"v1_input_no_build_no_image", v1_result_nobuild_no_image},
  19. }
  20. for _, tt := range tests {
  21. t.Run(tt.porterYamlFileName, func(t *testing.T) {
  22. is := is.New(t)
  23. want, err := os.ReadFile(fmt.Sprintf("testdata/%s.yaml", tt.porterYamlFileName))
  24. is.NoErr(err) // no error expected reading test file
  25. got, err := ParseYAML(context.Background(), want, "test-app")
  26. is.NoErr(err) // umbrella chart values should convert to map[string]any without issues
  27. diffProtoWithFailTest(t, is, tt.want, got)
  28. })
  29. }
  30. }
  31. var result_nobuild = &porterv1.PorterApp{
  32. Name: "js-test-app",
  33. Services: map[string]*porterv1.Service{
  34. "example-job": {
  35. Run: "echo 'hello world'",
  36. CpuCores: 0.1,
  37. RamMegabytes: 256,
  38. Config: &porterv1.Service_JobConfig{
  39. JobConfig: &porterv1.JobServiceConfig{
  40. AllowConcurrent: true,
  41. Cron: "*/10 * * * *",
  42. },
  43. },
  44. Type: 3,
  45. },
  46. "example-wkr": {
  47. Run: "echo 'work'",
  48. Instances: 1,
  49. Port: 80,
  50. CpuCores: 0.1,
  51. RamMegabytes: 256,
  52. Config: &porterv1.Service_WorkerConfig{
  53. WorkerConfig: &porterv1.WorkerServiceConfig{
  54. Autoscaling: nil,
  55. },
  56. },
  57. Type: 2,
  58. },
  59. "example-web": {
  60. Run: "node index.js",
  61. Instances: 0,
  62. Port: 8080,
  63. CpuCores: 0.1,
  64. RamMegabytes: 256,
  65. Config: &porterv1.Service_WebConfig{
  66. WebConfig: &porterv1.WebServiceConfig{
  67. Autoscaling: &porterv1.Autoscaling{
  68. Enabled: true,
  69. MinInstances: 1,
  70. MaxInstances: 3,
  71. CpuThresholdPercent: 60,
  72. MemoryThresholdPercent: 60,
  73. },
  74. Domains: []*porterv1.Domain{
  75. {
  76. Name: "test1.example.com",
  77. },
  78. {
  79. Name: "test2.example.com",
  80. },
  81. },
  82. HealthCheck: &porterv1.HealthCheck{
  83. Enabled: true,
  84. HttpPath: "/healthz",
  85. },
  86. },
  87. },
  88. Type: 1,
  89. },
  90. },
  91. Env: map[string]string{
  92. "PORT": "8080",
  93. "NODE_ENV": "production",
  94. },
  95. Predeploy: &porterv1.Service{
  96. Run: "ls",
  97. Instances: 0,
  98. Port: 0,
  99. CpuCores: 0,
  100. RamMegabytes: 0,
  101. Config: &porterv1.Service_JobConfig{},
  102. Type: 3,
  103. },
  104. Image: &porterv1.AppImage{
  105. Repository: "nginx",
  106. Tag: "latest",
  107. },
  108. }
  109. var v1_result_nobuild_no_image = &porterv1.PorterApp{
  110. Name: "test-app",
  111. Services: map[string]*porterv1.Service{
  112. "example-job": {
  113. Run: "echo 'hello world'",
  114. CpuCores: 0.1,
  115. RamMegabytes: 256,
  116. Config: &porterv1.Service_JobConfig{
  117. JobConfig: &porterv1.JobServiceConfig{
  118. AllowConcurrent: true,
  119. Cron: "*/10 * * * *",
  120. },
  121. },
  122. Type: 3,
  123. },
  124. "example-wkr": {
  125. Run: "echo 'work'",
  126. Instances: 1,
  127. Port: 80,
  128. CpuCores: 0.1,
  129. RamMegabytes: 256,
  130. Config: &porterv1.Service_WorkerConfig{
  131. WorkerConfig: &porterv1.WorkerServiceConfig{
  132. Autoscaling: nil,
  133. },
  134. },
  135. Type: 2,
  136. },
  137. "example-web": {
  138. Run: "node index.js",
  139. Instances: 0,
  140. Port: 8080,
  141. CpuCores: 0.1,
  142. RamMegabytes: 256,
  143. Config: &porterv1.Service_WebConfig{
  144. WebConfig: &porterv1.WebServiceConfig{
  145. Autoscaling: &porterv1.Autoscaling{
  146. Enabled: true,
  147. MinInstances: 1,
  148. MaxInstances: 3,
  149. CpuThresholdPercent: 60,
  150. MemoryThresholdPercent: 60,
  151. },
  152. Domains: []*porterv1.Domain{
  153. {
  154. Name: "test1.example.com",
  155. },
  156. {
  157. Name: "test2.example.com",
  158. },
  159. },
  160. HealthCheck: &porterv1.HealthCheck{
  161. Enabled: true,
  162. HttpPath: "/healthz",
  163. },
  164. },
  165. },
  166. Type: 1,
  167. },
  168. },
  169. Env: map[string]string{
  170. "PORT": "8080",
  171. "NODE_ENV": "production",
  172. },
  173. Predeploy: &porterv1.Service{
  174. Run: "ls",
  175. Instances: 0,
  176. Port: 0,
  177. CpuCores: 0,
  178. RamMegabytes: 0,
  179. Config: &porterv1.Service_JobConfig{},
  180. Type: 3,
  181. },
  182. }
  183. func diffProtoWithFailTest(t *testing.T, is *is.I, want, got *porterv1.PorterApp) {
  184. t.Helper()
  185. opts := protojson.MarshalOptions{Multiline: true}
  186. wantJson, err := opts.Marshal(want)
  187. is.NoErr(err) // no error expected marshalling want
  188. gotJson, err := opts.Marshal(got)
  189. is.NoErr(err) // no error expected marshalling got
  190. if string(wantJson) != string(gotJson) {
  191. dmp := diffmatchpatch.New()
  192. diffs := dmp.DiffMain(string(wantJson), string(gotJson), false)
  193. t.Errorf("diff between want and got: %s", dmp.DiffPrettyText(diffs))
  194. }
  195. }