modelpath_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package server
  2. import "testing"
  3. func TestParseModelPath(t *testing.T) {
  4. type input struct {
  5. name string
  6. allowInsecure bool
  7. }
  8. tests := []struct {
  9. name string
  10. args input
  11. want ModelPath
  12. wantErr error
  13. }{
  14. {
  15. "full path https",
  16. input{"https://example.com/ns/repo:tag", false},
  17. ModelPath{
  18. ProtocolScheme: "https",
  19. Registry: "example.com",
  20. Namespace: "ns",
  21. Repository: "repo",
  22. Tag: "tag",
  23. },
  24. nil,
  25. },
  26. {
  27. "full path http without insecure",
  28. input{"http://example.com/ns/repo:tag", false},
  29. ModelPath{},
  30. ErrInsecureProtocol,
  31. },
  32. {
  33. "full path http with insecure",
  34. input{"http://example.com/ns/repo:tag", true},
  35. ModelPath{
  36. ProtocolScheme: "http",
  37. Registry: "example.com",
  38. Namespace: "ns",
  39. Repository: "repo",
  40. Tag: "tag",
  41. },
  42. nil,
  43. },
  44. {
  45. "full path invalid protocol",
  46. input{"file://example.com/ns/repo:tag", false},
  47. ModelPath{},
  48. ErrInvalidProtocol,
  49. },
  50. {
  51. "no protocol",
  52. input{"example.com/ns/repo:tag", false},
  53. ModelPath{
  54. ProtocolScheme: "https",
  55. Registry: "example.com",
  56. Namespace: "ns",
  57. Repository: "repo",
  58. Tag: "tag",
  59. },
  60. nil,
  61. },
  62. {
  63. "no registry",
  64. input{"ns/repo:tag", false},
  65. ModelPath{
  66. ProtocolScheme: "https",
  67. Registry: DefaultRegistry,
  68. Namespace: "ns",
  69. Repository: "repo",
  70. Tag: "tag",
  71. },
  72. nil,
  73. },
  74. {
  75. "no namespace",
  76. input{"repo:tag", false},
  77. ModelPath{
  78. ProtocolScheme: "https",
  79. Registry: DefaultRegistry,
  80. Namespace: DefaultNamespace,
  81. Repository: "repo",
  82. Tag: "tag",
  83. },
  84. nil,
  85. },
  86. {
  87. "no tag",
  88. input{"repo", false},
  89. ModelPath{
  90. ProtocolScheme: "https",
  91. Registry: DefaultRegistry,
  92. Namespace: DefaultNamespace,
  93. Repository: "repo",
  94. Tag: DefaultTag,
  95. },
  96. nil,
  97. },
  98. {
  99. "invalid image format",
  100. input{"example.com/a/b/c", false},
  101. ModelPath{},
  102. ErrInvalidImageFormat,
  103. },
  104. }
  105. for _, tc := range tests {
  106. t.Run(tc.name, func(t *testing.T) {
  107. got, err := ParseModelPath(tc.args.name, tc.args.allowInsecure)
  108. if err != tc.wantErr {
  109. t.Errorf("got: %q want: %q", err, tc.wantErr)
  110. }
  111. if got != tc.want {
  112. t.Errorf("got: %q want: %q", got, tc.want)
  113. }
  114. })
  115. }
  116. }