parsers_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package parsers
  14. import (
  15. "testing"
  16. )
  17. // Based on Docker test case removed in:
  18. // https://github.com/docker/docker/commit/4352da7803d182a6013a5238ce20a7c749db979a
  19. func TestParseImageName(t *testing.T) {
  20. testCases := []struct {
  21. Input string
  22. Repo string
  23. Tag string
  24. Digest string
  25. }{
  26. {Input: "root", Repo: "root", Tag: "latest"},
  27. {Input: "root:tag", Repo: "root", Tag: "tag"},
  28. {Input: "root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "root", Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
  29. {Input: "user/repo", Repo: "user/repo", Tag: "latest"},
  30. {Input: "user/repo:tag", Repo: "user/repo", Tag: "tag"},
  31. {Input: "user/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "user/repo", Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
  32. {Input: "url:5000/repo", Repo: "url:5000/repo", Tag: "latest"},
  33. {Input: "url:5000/repo:tag", Repo: "url:5000/repo", Tag: "tag"},
  34. {Input: "url:5000/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "url:5000/repo", Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
  35. }
  36. for _, testCase := range testCases {
  37. repo, tag, digest, err := ParseImageName(testCase.Input)
  38. if err != nil {
  39. t.Errorf("ParseImageName(%s) failed: %v", testCase.Input, err)
  40. } else if repo != testCase.Repo || tag != testCase.Tag || digest != testCase.Digest {
  41. t.Errorf("Expected repo: %q, tag: %q and digest: %q, got %q, %q and %q", testCase.Repo, testCase.Tag, testCase.Digest,
  42. repo, tag, digest)
  43. }
  44. }
  45. }