parse_test.go 5.1 KB

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