parse_test.go 8.6 KB

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