parse_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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: nil,
  54. },
  55. },
  56. Type: 2,
  57. },
  58. "example-web": {
  59. Run: "node index.js",
  60. Instances: 0,
  61. Port: 8080,
  62. CpuCores: 0.1,
  63. RamMegabytes: 256,
  64. Config: &porterv1.Service_WebConfig{
  65. WebConfig: &porterv1.WebServiceConfig{
  66. Autoscaling: &porterv1.Autoscaling{
  67. Enabled: true,
  68. MinInstances: 1,
  69. MaxInstances: 3,
  70. CpuThresholdPercent: 60,
  71. MemoryThresholdPercent: 60,
  72. },
  73. Domains: []*porterv1.Domain{
  74. {
  75. Name: "test1.example.com",
  76. },
  77. {
  78. Name: "test2.example.com",
  79. },
  80. },
  81. HealthCheck: &porterv1.HealthCheck{
  82. Enabled: true,
  83. HttpPath: "/healthz",
  84. },
  85. },
  86. },
  87. Type: 1,
  88. },
  89. },
  90. Env: map[string]string{
  91. "PORT": "8080",
  92. "NODE_ENV": "production",
  93. },
  94. Predeploy: &porterv1.Service{
  95. Run: "ls",
  96. Instances: 0,
  97. Port: 0,
  98. CpuCores: 0,
  99. RamMegabytes: 0,
  100. Config: &porterv1.Service_JobConfig{},
  101. Type: 3,
  102. },
  103. Image: &porterv1.AppImage{
  104. Repository: "nginx",
  105. Tag: "latest",
  106. },
  107. }
  108. func diffProtoWithFailTest(t *testing.T, is *is.I, want, got *porterv1.PorterApp) {
  109. t.Helper()
  110. opts := protojson.MarshalOptions{Multiline: true}
  111. wantJson, err := opts.Marshal(want)
  112. is.NoErr(err) // no error expected marshalling want
  113. gotJson, err := opts.Marshal(got)
  114. is.NoErr(err) // no error expected marshalling got
  115. if string(wantJson) != string(gotJson) {
  116. dmp := diffmatchpatch.New()
  117. diffs := dmp.DiffMain(string(wantJson), string(gotJson), false)
  118. t.Errorf("diff between want and got: %s", dmp.DiffPrettyText(diffs))
  119. }
  120. }