parse_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package porter_app
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  9. "github.com/sergi/go-diff/diffmatchpatch"
  10. "github.com/matryer/is"
  11. )
  12. func TestParseYAML(t *testing.T) {
  13. tests := []struct {
  14. porterYamlFileName string
  15. want *porterv1.PorterApp
  16. }{
  17. {"v2_input_nobuild", result_nobuild},
  18. }
  19. for _, tt := range tests {
  20. t.Run(tt.porterYamlFileName, func(t *testing.T) {
  21. is := is.New(t)
  22. want, err := os.ReadFile(fmt.Sprintf("testdata/%s.yaml", tt.porterYamlFileName))
  23. is.NoErr(err) // no error expected reading test file
  24. got, err := ParseYAML(context.Background(), want)
  25. is.NoErr(err) // umbrella chart values should convert to map[string]any without issues
  26. diffProtoWithFailTest(t, is, tt.want, got)
  27. })
  28. }
  29. }
  30. var result_nobuild = &porterv1.PorterApp{
  31. Name: "js-test-app",
  32. Services: map[string]*porterv1.Service{
  33. "example-job": {
  34. Run: "echo 'hello world'",
  35. CpuCores: 0.1,
  36. RamMegabytes: 256,
  37. Config: &porterv1.Service_JobConfig{
  38. JobConfig: &porterv1.JobServiceConfig{
  39. AllowConcurrent: true,
  40. Cron: "*/10 * * * *",
  41. },
  42. },
  43. Type: 3,
  44. },
  45. "example-wkr": {
  46. Run: "echo 'work'",
  47. Instances: 1,
  48. Port: 80,
  49. CpuCores: 0.1,
  50. RamMegabytes: 256,
  51. Config: &porterv1.Service_WorkerConfig{
  52. WorkerConfig: &porterv1.WorkerServiceConfig{
  53. Autoscaling: &porterv1.Autoscaling{
  54. Enabled: false,
  55. MinInstances: 0,
  56. MaxInstances: 0,
  57. CpuThresholdPercent: 0,
  58. MemoryThresholdPercent: 0,
  59. },
  60. },
  61. },
  62. Type: 2,
  63. },
  64. "example-web": {
  65. Run: "node index.js",
  66. Instances: 0,
  67. Port: 8080,
  68. CpuCores: 0.1,
  69. RamMegabytes: 256,
  70. Config: &porterv1.Service_WebConfig{
  71. WebConfig: &porterv1.WebServiceConfig{
  72. Autoscaling: &porterv1.Autoscaling{
  73. Enabled: true,
  74. MinInstances: 1,
  75. MaxInstances: 3,
  76. CpuThresholdPercent: 60,
  77. MemoryThresholdPercent: 60,
  78. },
  79. Domains: []*porterv1.Domain{
  80. {
  81. Name: "test1.example.com",
  82. },
  83. {
  84. Name: "test2.example.com",
  85. },
  86. },
  87. HealthCheck: &porterv1.HealthCheck{
  88. Enabled: true,
  89. HttpPath: "/healthz",
  90. },
  91. },
  92. },
  93. Type: 1,
  94. },
  95. },
  96. Env: map[string]string{
  97. "PORT": "8080",
  98. "NODE_ENV": "production",
  99. },
  100. Predeploy: &porterv1.Service{
  101. Run: "ls",
  102. Instances: 0,
  103. Port: 0,
  104. CpuCores: 0,
  105. RamMegabytes: 0,
  106. Config: &porterv1.Service_JobConfig{},
  107. Type: 3,
  108. },
  109. Image: &porterv1.AppImage{
  110. Repository: "nginx",
  111. Tag: "latest",
  112. },
  113. }
  114. func diffProtoWithFailTest(t *testing.T, is *is.I, want, got *porterv1.PorterApp) {
  115. t.Helper()
  116. opts := protojson.MarshalOptions{Multiline: true}
  117. wantJson, err := opts.Marshal(want)
  118. is.NoErr(err) // no error expected marshalling want
  119. gotJson, err := opts.Marshal(got)
  120. is.NoErr(err) // no error expected marshalling got
  121. if string(wantJson) != string(gotJson) {
  122. dmp := diffmatchpatch.New()
  123. diffs := dmp.DiffMain(string(wantJson), string(gotJson), false)
  124. t.Errorf("diff between want and got: %s", dmp.DiffPrettyText(diffs))
  125. }
  126. }