parse_test.go 6.1 KB

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