parse_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. Name: "example-web",
  42. RunOptional: pointer.String("node index.js"),
  43. Instances: 0,
  44. Port: 8080,
  45. CpuCores: 0.1,
  46. RamMegabytes: 256,
  47. Config: &porterv1.Service_WebConfig{
  48. WebConfig: &porterv1.WebServiceConfig{
  49. Autoscaling: &porterv1.Autoscaling{
  50. Enabled: true,
  51. MinInstances: 1,
  52. MaxInstances: 3,
  53. CpuThresholdPercent: 60,
  54. MemoryThresholdPercent: 60,
  55. },
  56. Domains: []*porterv1.Domain{
  57. {
  58. Name: "test1.example.com",
  59. },
  60. {
  61. Name: "test2.example.com",
  62. },
  63. },
  64. HealthCheck: &porterv1.HealthCheck{
  65. Enabled: true,
  66. HttpPath: "/healthz",
  67. },
  68. },
  69. },
  70. Type: 1,
  71. },
  72. "example-wkr": {
  73. Name: "example-wkr",
  74. RunOptional: pointer.String("echo 'work'"),
  75. Instances: 1,
  76. Port: 80,
  77. CpuCores: 0.1,
  78. RamMegabytes: 256,
  79. Config: &porterv1.Service_WorkerConfig{
  80. WorkerConfig: &porterv1.WorkerServiceConfig{
  81. Autoscaling: nil,
  82. },
  83. },
  84. Type: 2,
  85. },
  86. "example-job": {
  87. Name: "example-job",
  88. RunOptional: pointer.String("echo 'hello world'"),
  89. CpuCores: 0.1,
  90. RamMegabytes: 256,
  91. Config: &porterv1.Service_JobConfig{
  92. JobConfig: &porterv1.JobServiceConfig{
  93. AllowConcurrentOptional: pointer.Bool(true),
  94. Cron: "*/10 * * * *",
  95. SuspendCron: pointer.Bool(false),
  96. TimeoutSeconds: 60,
  97. },
  98. },
  99. Type: 3,
  100. },
  101. },
  102. ServiceList: []*porterv1.Service{
  103. {
  104. Name: "example-web",
  105. RunOptional: pointer.String("node index.js"),
  106. Instances: 0,
  107. Port: 8080,
  108. CpuCores: 0.1,
  109. RamMegabytes: 256,
  110. Config: &porterv1.Service_WebConfig{
  111. WebConfig: &porterv1.WebServiceConfig{
  112. Autoscaling: &porterv1.Autoscaling{
  113. Enabled: true,
  114. MinInstances: 1,
  115. MaxInstances: 3,
  116. CpuThresholdPercent: 60,
  117. MemoryThresholdPercent: 60,
  118. },
  119. Domains: []*porterv1.Domain{
  120. {
  121. Name: "test1.example.com",
  122. },
  123. {
  124. Name: "test2.example.com",
  125. },
  126. },
  127. HealthCheck: &porterv1.HealthCheck{
  128. Enabled: true,
  129. HttpPath: "/healthz",
  130. },
  131. },
  132. },
  133. Type: 1,
  134. },
  135. {
  136. Name: "example-wkr",
  137. RunOptional: pointer.String("echo 'work'"),
  138. Instances: 1,
  139. Port: 80,
  140. CpuCores: 0.1,
  141. RamMegabytes: 256,
  142. Config: &porterv1.Service_WorkerConfig{
  143. WorkerConfig: &porterv1.WorkerServiceConfig{
  144. Autoscaling: nil,
  145. },
  146. },
  147. Type: 2,
  148. },
  149. {
  150. Name: "example-job",
  151. RunOptional: pointer.String("echo 'hello world'"),
  152. CpuCores: 0.1,
  153. RamMegabytes: 256,
  154. Config: &porterv1.Service_JobConfig{
  155. JobConfig: &porterv1.JobServiceConfig{
  156. AllowConcurrentOptional: pointer.Bool(true),
  157. Cron: "*/10 * * * *",
  158. SuspendCron: pointer.Bool(false),
  159. TimeoutSeconds: 60,
  160. },
  161. },
  162. Type: 3,
  163. },
  164. },
  165. Predeploy: &porterv1.Service{
  166. RunOptional: pointer.String("ls"),
  167. Instances: 0,
  168. Port: 0,
  169. CpuCores: 0,
  170. RamMegabytes: 0,
  171. Config: &porterv1.Service_JobConfig{},
  172. Type: 3,
  173. },
  174. Image: &porterv1.AppImage{
  175. Repository: "nginx",
  176. Tag: "latest",
  177. },
  178. }
  179. var v1_result_nobuild_no_image = &porterv1.PorterApp{
  180. Name: "test-app",
  181. Services: map[string]*porterv1.Service{
  182. "example-job": {
  183. Name: "example-job",
  184. RunOptional: pointer.String("echo 'hello world'"),
  185. CpuCores: 0.1,
  186. RamMegabytes: 256,
  187. Config: &porterv1.Service_JobConfig{
  188. JobConfig: &porterv1.JobServiceConfig{
  189. AllowConcurrent: true,
  190. Cron: "*/10 * * * *",
  191. },
  192. },
  193. Type: 3,
  194. },
  195. "example-wkr": {
  196. Name: "example-wkr",
  197. RunOptional: pointer.String("echo 'work'"),
  198. Instances: 1,
  199. Port: 80,
  200. CpuCores: 0.1,
  201. RamMegabytes: 256,
  202. Config: &porterv1.Service_WorkerConfig{
  203. WorkerConfig: &porterv1.WorkerServiceConfig{
  204. Autoscaling: nil,
  205. },
  206. },
  207. Type: 2,
  208. },
  209. "example-web": {
  210. Name: "example-web",
  211. RunOptional: pointer.String("node index.js"),
  212. Instances: 0,
  213. Port: 8080,
  214. CpuCores: 0.1,
  215. RamMegabytes: 256,
  216. Config: &porterv1.Service_WebConfig{
  217. WebConfig: &porterv1.WebServiceConfig{
  218. Autoscaling: &porterv1.Autoscaling{
  219. Enabled: true,
  220. MinInstances: 1,
  221. MaxInstances: 3,
  222. CpuThresholdPercent: 60,
  223. MemoryThresholdPercent: 60,
  224. },
  225. Domains: []*porterv1.Domain{
  226. {
  227. Name: "test1.example.com",
  228. },
  229. {
  230. Name: "test2.example.com",
  231. },
  232. },
  233. HealthCheck: &porterv1.HealthCheck{
  234. Enabled: true,
  235. HttpPath: "/healthz",
  236. },
  237. Private: pointer.Bool(false),
  238. },
  239. },
  240. Type: 1,
  241. },
  242. },
  243. ServiceList: []*porterv1.Service{
  244. {
  245. Name: "example-job",
  246. RunOptional: pointer.String("echo 'hello world'"),
  247. CpuCores: 0.1,
  248. RamMegabytes: 256,
  249. Config: &porterv1.Service_JobConfig{
  250. JobConfig: &porterv1.JobServiceConfig{
  251. AllowConcurrent: true,
  252. Cron: "*/10 * * * *",
  253. },
  254. },
  255. Type: 3,
  256. },
  257. {
  258. Name: "example-wkr",
  259. RunOptional: pointer.String("echo 'work'"),
  260. Instances: 1,
  261. Port: 80,
  262. CpuCores: 0.1,
  263. RamMegabytes: 256,
  264. Config: &porterv1.Service_WorkerConfig{
  265. WorkerConfig: &porterv1.WorkerServiceConfig{
  266. Autoscaling: nil,
  267. },
  268. },
  269. Type: 2,
  270. },
  271. {
  272. Name: "example-web",
  273. RunOptional: pointer.String("node index.js"),
  274. Instances: 0,
  275. Port: 8080,
  276. CpuCores: 0.1,
  277. RamMegabytes: 256,
  278. Config: &porterv1.Service_WebConfig{
  279. WebConfig: &porterv1.WebServiceConfig{
  280. Autoscaling: &porterv1.Autoscaling{
  281. Enabled: true,
  282. MinInstances: 1,
  283. MaxInstances: 3,
  284. CpuThresholdPercent: 60,
  285. MemoryThresholdPercent: 60,
  286. },
  287. Domains: []*porterv1.Domain{
  288. {
  289. Name: "test1.example.com",
  290. },
  291. {
  292. Name: "test2.example.com",
  293. },
  294. },
  295. HealthCheck: &porterv1.HealthCheck{
  296. Enabled: true,
  297. HttpPath: "/healthz",
  298. },
  299. Private: pointer.Bool(false),
  300. },
  301. },
  302. Type: 1,
  303. },
  304. },
  305. Predeploy: &porterv1.Service{
  306. RunOptional: pointer.String("ls"),
  307. Instances: 0,
  308. Port: 0,
  309. CpuCores: 0,
  310. RamMegabytes: 0,
  311. Config: &porterv1.Service_JobConfig{},
  312. Type: 3,
  313. },
  314. }
  315. func diffProtoWithFailTest(t *testing.T, is *is.I, want, got *porterv1.PorterApp) {
  316. t.Helper()
  317. opts := protojson.MarshalOptions{Multiline: true}
  318. wantJson, err := opts.Marshal(want)
  319. is.NoErr(err) // no error expected marshalling want
  320. gotJson, err := opts.Marshal(got)
  321. is.NoErr(err) // no error expected marshalling got
  322. if string(wantJson) != string(gotJson) {
  323. dmp := diffmatchpatch.New()
  324. diffs := dmp.DiffMain(string(wantJson), string(gotJson), false)
  325. t.Errorf("diff between want and got: %s", dmp.DiffPrettyText(diffs))
  326. }
  327. }