parse_test.go 4.9 KB

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