parse_test.go 5.9 KB

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