parse_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 (
  183. trueBool = true
  184. zeroInt32 = int32(0)
  185. oneInt32 = int32(1)
  186. )
  187. var v1_result_nobuild_no_image = &porterv1.PorterApp{
  188. Name: "test-app",
  189. Services: map[string]*porterv1.Service{
  190. "example-job": {
  191. Name: "example-job",
  192. RunOptional: pointer.String("echo 'hello world'"),
  193. CpuCores: 0.1,
  194. RamMegabytes: 256,
  195. GpuCoresNvidia: 0,
  196. Config: &porterv1.Service_JobConfig{
  197. JobConfig: &porterv1.JobServiceConfig{
  198. AllowConcurrentOptional: &trueBool,
  199. Cron: "*/10 * * * *",
  200. },
  201. },
  202. Type: 3,
  203. },
  204. "example-wkr": {
  205. Name: "example-wkr",
  206. RunOptional: pointer.String("echo 'work'"),
  207. InstancesOptional: &oneInt32,
  208. Port: 80,
  209. CpuCores: 0.1,
  210. RamMegabytes: 256,
  211. GpuCoresNvidia: 0,
  212. Config: &porterv1.Service_WorkerConfig{
  213. WorkerConfig: &porterv1.WorkerServiceConfig{
  214. Autoscaling: nil,
  215. },
  216. },
  217. Type: 2,
  218. },
  219. "example-web": {
  220. Name: "example-web",
  221. RunOptional: pointer.String("node index.js"),
  222. InstancesOptional: &zeroInt32,
  223. Port: 8080,
  224. CpuCores: 0.1,
  225. GpuCoresNvidia: 0,
  226. RamMegabytes: 256,
  227. Config: &porterv1.Service_WebConfig{
  228. WebConfig: &porterv1.WebServiceConfig{
  229. Autoscaling: &porterv1.Autoscaling{
  230. Enabled: true,
  231. MinInstances: 1,
  232. MaxInstances: 3,
  233. CpuThresholdPercent: 60,
  234. MemoryThresholdPercent: 60,
  235. },
  236. Domains: []*porterv1.Domain{
  237. {
  238. Name: "test1.example.com",
  239. },
  240. {
  241. Name: "test2.example.com",
  242. },
  243. },
  244. HealthCheck: &porterv1.HealthCheck{
  245. Enabled: true,
  246. HttpPath: "/healthz",
  247. },
  248. Private: pointer.Bool(false),
  249. },
  250. },
  251. Type: 1,
  252. },
  253. },
  254. ServiceList: []*porterv1.Service{
  255. {
  256. Name: "example-job",
  257. RunOptional: pointer.String("echo 'hello world'"),
  258. CpuCores: 0.1,
  259. RamMegabytes: 256,
  260. GpuCoresNvidia: 0,
  261. Config: &porterv1.Service_JobConfig{
  262. JobConfig: &porterv1.JobServiceConfig{
  263. AllowConcurrentOptional: &trueBool,
  264. Cron: "*/10 * * * *",
  265. },
  266. },
  267. Type: 3,
  268. },
  269. {
  270. Name: "example-wkr",
  271. RunOptional: pointer.String("echo 'work'"),
  272. InstancesOptional: &oneInt32,
  273. Port: 80,
  274. CpuCores: 0.1,
  275. RamMegabytes: 256,
  276. GpuCoresNvidia: 0,
  277. Config: &porterv1.Service_WorkerConfig{
  278. WorkerConfig: &porterv1.WorkerServiceConfig{
  279. Autoscaling: nil,
  280. },
  281. },
  282. Type: 2,
  283. },
  284. {
  285. Name: "example-web",
  286. RunOptional: pointer.String("node index.js"),
  287. InstancesOptional: &zeroInt32,
  288. Port: 8080,
  289. CpuCores: 0.1,
  290. RamMegabytes: 256,
  291. GpuCoresNvidia: 0,
  292. Config: &porterv1.Service_WebConfig{
  293. WebConfig: &porterv1.WebServiceConfig{
  294. Autoscaling: &porterv1.Autoscaling{
  295. Enabled: true,
  296. MinInstances: 1,
  297. MaxInstances: 3,
  298. CpuThresholdPercent: 60,
  299. MemoryThresholdPercent: 60,
  300. },
  301. Domains: []*porterv1.Domain{
  302. {
  303. Name: "test1.example.com",
  304. },
  305. {
  306. Name: "test2.example.com",
  307. },
  308. },
  309. HealthCheck: &porterv1.HealthCheck{
  310. Enabled: true,
  311. HttpPath: "/healthz",
  312. },
  313. Private: pointer.Bool(false),
  314. },
  315. },
  316. Type: 1,
  317. },
  318. },
  319. Predeploy: &porterv1.Service{
  320. RunOptional: pointer.String("ls"),
  321. Instances: 0,
  322. Port: 0,
  323. CpuCores: 0,
  324. RamMegabytes: 0,
  325. GpuCoresNvidia: 0,
  326. Config: &porterv1.Service_JobConfig{},
  327. Type: 3,
  328. },
  329. }
  330. func diffProtoWithFailTest(t *testing.T, is *is.I, want, got *porterv1.PorterApp) {
  331. t.Helper()
  332. opts := protojson.MarshalOptions{Multiline: true}
  333. wantJson, err := opts.Marshal(want)
  334. is.NoErr(err) // no error expected marshalling want
  335. gotJson, err := opts.Marshal(got)
  336. is.NoErr(err) // no error expected marshalling got
  337. if string(wantJson) != string(gotJson) {
  338. dmp := diffmatchpatch.New()
  339. diffs := dmp.DiffMain(string(wantJson), string(gotJson), false)
  340. t.Errorf("diff between want and got: %s", dmp.DiffPrettyText(diffs))
  341. }
  342. }