parse_test.go 8.6 KB

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