parse_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. ServiceList: []*porterv1.Service{
  40. {
  41. Name: "example-web",
  42. RunOptional: pointer.String("node index.js"),
  43. Port: 8080,
  44. CpuCores: 0.1,
  45. RamMegabytes: 256,
  46. GpuCoresNvidia: 0,
  47. Gpu: &porterv1.GPU{
  48. Enabled: false,
  49. GpuCoresNvidia: 0,
  50. },
  51. Config: &porterv1.Service_WebConfig{
  52. WebConfig: &porterv1.WebServiceConfig{
  53. Autoscaling: &porterv1.Autoscaling{
  54. Enabled: true,
  55. MinInstances: 1,
  56. MaxInstances: 3,
  57. CpuThresholdPercent: 60,
  58. MemoryThresholdPercent: 60,
  59. },
  60. Domains: []*porterv1.Domain{
  61. {
  62. Name: "test1.example.com",
  63. },
  64. {
  65. Name: "test2.example.com",
  66. },
  67. },
  68. HealthCheck: &porterv1.HealthCheck{
  69. Enabled: true,
  70. HttpPath: "/healthz",
  71. },
  72. },
  73. },
  74. Type: 1,
  75. },
  76. {
  77. Name: "example-wkr",
  78. RunOptional: pointer.String("echo 'work'"),
  79. InstancesOptional: pointer.Int32(1),
  80. Port: 80,
  81. CpuCores: 0.1,
  82. RamMegabytes: 256,
  83. GpuCoresNvidia: 0,
  84. Gpu: &porterv1.GPU{
  85. Enabled: false,
  86. GpuCoresNvidia: 0,
  87. },
  88. Config: &porterv1.Service_WorkerConfig{
  89. WorkerConfig: &porterv1.WorkerServiceConfig{
  90. Autoscaling: nil,
  91. },
  92. },
  93. Type: 2,
  94. },
  95. {
  96. Name: "example-job",
  97. RunOptional: pointer.String("echo 'hello world'"),
  98. CpuCores: 0.1,
  99. RamMegabytes: 256,
  100. GpuCoresNvidia: 0,
  101. Gpu: &porterv1.GPU{
  102. Enabled: false,
  103. GpuCoresNvidia: 0,
  104. },
  105. Config: &porterv1.Service_JobConfig{
  106. JobConfig: &porterv1.JobServiceConfig{
  107. AllowConcurrentOptional: pointer.Bool(true),
  108. Cron: "*/10 * * * *",
  109. SuspendCron: pointer.Bool(false),
  110. TimeoutSeconds: 60,
  111. },
  112. },
  113. Type: 3,
  114. },
  115. },
  116. Predeploy: &porterv1.Service{
  117. RunOptional: pointer.String("ls"),
  118. Port: 0,
  119. CpuCores: 0,
  120. RamMegabytes: 0,
  121. GpuCoresNvidia: 0,
  122. Gpu: &porterv1.GPU{
  123. Enabled: false,
  124. GpuCoresNvidia: 0,
  125. },
  126. Config: &porterv1.Service_JobConfig{},
  127. Type: 3,
  128. },
  129. Image: &porterv1.AppImage{
  130. Repository: "nginx",
  131. Tag: "latest",
  132. },
  133. }
  134. var (
  135. trueBool = true
  136. zeroInt32 = int32(0)
  137. oneInt32 = int32(1)
  138. )
  139. var v1_result_nobuild_no_image = &porterv1.PorterApp{
  140. Name: "test-app",
  141. ServiceList: []*porterv1.Service{
  142. {
  143. Name: "example-web",
  144. RunOptional: pointer.String("node index.js"),
  145. InstancesOptional: &zeroInt32,
  146. Port: 8080,
  147. CpuCores: 0.1,
  148. RamMegabytes: 256,
  149. GpuCoresNvidia: 0,
  150. Config: &porterv1.Service_WebConfig{
  151. WebConfig: &porterv1.WebServiceConfig{
  152. Autoscaling: &porterv1.Autoscaling{
  153. Enabled: true,
  154. MinInstances: 1,
  155. MaxInstances: 3,
  156. CpuThresholdPercent: 60,
  157. MemoryThresholdPercent: 60,
  158. },
  159. Domains: []*porterv1.Domain{
  160. {
  161. Name: "test1.example.com",
  162. },
  163. {
  164. Name: "test2.example.com",
  165. },
  166. },
  167. HealthCheck: &porterv1.HealthCheck{
  168. Enabled: true,
  169. HttpPath: "/healthz",
  170. },
  171. Private: pointer.Bool(false),
  172. },
  173. },
  174. Type: 1,
  175. },
  176. {
  177. Name: "example-wkr",
  178. RunOptional: pointer.String("echo 'work'"),
  179. InstancesOptional: &oneInt32,
  180. Port: 80,
  181. CpuCores: 0.1,
  182. RamMegabytes: 256,
  183. GpuCoresNvidia: 0,
  184. Config: &porterv1.Service_WorkerConfig{
  185. WorkerConfig: &porterv1.WorkerServiceConfig{
  186. Autoscaling: nil,
  187. },
  188. },
  189. Type: 2,
  190. },
  191. {
  192. Name: "example-job",
  193. RunOptional: pointer.String("echo 'hello world'"),
  194. CpuCores: 0.1,
  195. RamMegabytes: 256,
  196. GpuCoresNvidia: 0,
  197. Config: &porterv1.Service_JobConfig{
  198. JobConfig: &porterv1.JobServiceConfig{
  199. AllowConcurrentOptional: &trueBool,
  200. Cron: "*/10 * * * *",
  201. },
  202. },
  203. Type: 3,
  204. },
  205. },
  206. Predeploy: &porterv1.Service{
  207. RunOptional: pointer.String("ls"),
  208. Instances: 0,
  209. Port: 0,
  210. CpuCores: 0,
  211. RamMegabytes: 0,
  212. GpuCoresNvidia: 0,
  213. Config: &porterv1.Service_JobConfig{},
  214. Type: 3,
  215. },
  216. }
  217. func diffProtoWithFailTest(t *testing.T, is *is.I, want, got *porterv1.PorterApp) {
  218. t.Helper()
  219. opts := protojson.MarshalOptions{Multiline: true}
  220. wantJson, err := opts.Marshal(want)
  221. is.NoErr(err) // no error expected marshalling want
  222. gotJson, err := opts.Marshal(got)
  223. is.NoErr(err) // no error expected marshalling got
  224. if string(wantJson) != string(gotJson) {
  225. dmp := diffmatchpatch.New()
  226. diffs := dmp.DiffMain(string(wantJson), string(gotJson), false)
  227. t.Errorf("diff between want and got: %s", dmp.DiffPrettyText(diffs))
  228. }
  229. }