parse_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, env, 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. is.Equal(env, map[string]string{
  29. "PORT": "8080",
  30. "NODE_ENV": "production",
  31. })
  32. })
  33. }
  34. }
  35. var result_nobuild = &porterv1.PorterApp{
  36. Name: "js-test-app",
  37. Services: map[string]*porterv1.Service{
  38. "example-job": {
  39. Run: "echo 'hello world'",
  40. CpuCores: 0.1,
  41. RamMegabytes: 256,
  42. Config: &porterv1.Service_JobConfig{
  43. JobConfig: &porterv1.JobServiceConfig{
  44. AllowConcurrent: true,
  45. Cron: "*/10 * * * *",
  46. },
  47. },
  48. Type: 3,
  49. },
  50. "example-wkr": {
  51. Run: "echo 'work'",
  52. Instances: 1,
  53. Port: 80,
  54. CpuCores: 0.1,
  55. RamMegabytes: 256,
  56. Config: &porterv1.Service_WorkerConfig{
  57. WorkerConfig: &porterv1.WorkerServiceConfig{
  58. Autoscaling: nil,
  59. },
  60. },
  61. Type: 2,
  62. },
  63. "example-web": {
  64. Run: "node index.js",
  65. Instances: 0,
  66. Port: 8080,
  67. CpuCores: 0.1,
  68. RamMegabytes: 256,
  69. Config: &porterv1.Service_WebConfig{
  70. WebConfig: &porterv1.WebServiceConfig{
  71. Autoscaling: &porterv1.Autoscaling{
  72. Enabled: true,
  73. MinInstances: 1,
  74. MaxInstances: 3,
  75. CpuThresholdPercent: 60,
  76. MemoryThresholdPercent: 60,
  77. },
  78. Domains: []*porterv1.Domain{
  79. {
  80. Name: "test1.example.com",
  81. },
  82. {
  83. Name: "test2.example.com",
  84. },
  85. },
  86. HealthCheck: &porterv1.HealthCheck{
  87. Enabled: true,
  88. HttpPath: "/healthz",
  89. },
  90. },
  91. },
  92. Type: 1,
  93. },
  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. Predeploy: &porterv1.Service{
  170. Run: "ls",
  171. Instances: 0,
  172. Port: 0,
  173. CpuCores: 0,
  174. RamMegabytes: 0,
  175. Config: &porterv1.Service_JobConfig{},
  176. Type: 3,
  177. },
  178. }
  179. func diffProtoWithFailTest(t *testing.T, is *is.I, want, got *porterv1.PorterApp) {
  180. t.Helper()
  181. opts := protojson.MarshalOptions{Multiline: true}
  182. wantJson, err := opts.Marshal(want)
  183. is.NoErr(err) // no error expected marshalling want
  184. gotJson, err := opts.Marshal(got)
  185. is.NoErr(err) // no error expected marshalling got
  186. if string(wantJson) != string(gotJson) {
  187. dmp := diffmatchpatch.New()
  188. diffs := dmp.DiffMain(string(wantJson), string(gotJson), false)
  189. t.Errorf("diff between want and got: %s", dmp.DiffPrettyText(diffs))
  190. }
  191. }