github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/utils/container/image_test.go (about) 1 package container 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestGetImageLongName(t *testing.T) { 10 var imageTags = []struct { 11 in string 12 expected string 13 }{ 14 {"domain:8080/path:1.0", "path"}, 15 {"domain:8080/path/in/artifactory:1.0", "path/in/artifactory"}, 16 {"domain:8080/path/in/artifactory", "path/in/artifactory"}, 17 {"domain/path:1.0", "path"}, 18 {"domain/path/in/artifactory:1.0", "path/in/artifactory"}, 19 {"domain/path/in/artifactory", "path/in/artifactory"}, 20 } 21 22 for _, v := range imageTags { 23 result, err := NewImage(v.in).GetImageLongName() 24 assert.NoError(t, err) 25 if result != v.expected { 26 t.Errorf("GetImageLongName(\"%s\") => '%s', want '%s'", v.in, result, v.expected) 27 } 28 } 29 // Validate failure upon missing image name 30 _, err := NewImage("domain").GetImageLongName() 31 assert.Error(t, err) 32 33 } 34 35 func TestGetImageShortName(t *testing.T) { 36 var imageTags = []struct { 37 in string 38 expected string 39 }{ 40 {"domain:8080/path:1.0", "path"}, 41 {"domain:8080/path/in/artifactory:1.0", "artifactory"}, 42 {"domain:8080/path/in/artifactory", "artifactory"}, 43 {"domain/path:1.0", "path"}, 44 {"domain/path/in/artifactory:1.0", "artifactory"}, 45 {"domain/path/in/artifactory", "artifactory"}, 46 } 47 48 for _, v := range imageTags { 49 result, err := NewImage(v.in).GetImageShortName() 50 assert.NoError(t, err) 51 if result != v.expected { 52 t.Errorf("GetImageShortName(\"%s\") => '%s', want '%s'", v.in, result, v.expected) 53 } 54 } 55 // Validate failure upon missing image name 56 _, err := NewImage("domain").GetImageShortName() 57 assert.Error(t, err) 58 59 } 60 61 func TestGetImageLongNameWithTag(t *testing.T) { 62 var imageTags = []struct { 63 in string 64 expected string 65 }{ 66 {"domain:8080/path:1.0", "path:1.0"}, 67 {"domain:8080/path/in/artifactory:1.0", "path/in/artifactory:1.0"}, 68 {"domain:8080/path/in/artifactory", "path/in/artifactory:latest"}, 69 {"domain/path:1.0", "path:1.0"}, 70 {"domain/path/in/artifactory:1.0", "path/in/artifactory:1.0"}, 71 {"domain/path/in/artifactory", "path/in/artifactory:latest"}, 72 } 73 74 for _, v := range imageTags { 75 result, err := NewImage(v.in).GetImageLongNameWithTag() 76 assert.NoError(t, err) 77 if result != v.expected { 78 t.Errorf("GetImageLongNameWithTag(\"%s\") => '%s', want '%s'", v.in, result, v.expected) 79 } 80 } 81 // Validate failure upon missing image name 82 _, err := NewImage("domain").GetImageLongNameWithTag() 83 assert.Error(t, err) 84 } 85 func TestGetImageShortNameWithTag(t *testing.T) { 86 var imageTags = []struct { 87 in string 88 expected string 89 }{ 90 {"domain:8080/path:1.0", "path:1.0"}, 91 {"domain:8080/path/in/artifactory:1.0", "artifactory:1.0"}, 92 {"domain:8080/path/in/artifactory", "artifactory:latest"}, 93 {"domain/path:1.0", "path:1.0"}, 94 {"domain/path/in/artifactory:1.0", "artifactory:1.0"}, 95 {"domain/path/in/artifactory", "artifactory:latest"}, 96 } 97 98 for _, v := range imageTags { 99 result, err := NewImage(v.in).GetImageShortNameWithTag() 100 assert.NoError(t, err) 101 if result != v.expected { 102 t.Errorf("GetImageShortNameWithTag(\"%s\") => '%s', want '%s'", v.in, result, v.expected) 103 } 104 } 105 // Validate failure upon missing image name 106 _, err := NewImage("domain").GetImageLongNameWithTag() 107 assert.Error(t, err) 108 } 109 110 func TestResolveRegistryFromTag(t *testing.T) { 111 var imageTags = []struct { 112 in string 113 expected string 114 expectingError bool 115 }{ 116 {"domain:8080/path:1.0", "domain:8080", false}, 117 {"domain:8080/path/in/artifactory:1.0", "domain:8080", false}, 118 {"domain:8080/path/in/artifactory", "domain:8080", false}, 119 {"domain/path:1.0", "domain", false}, 120 {"domain/path/in/artifactory:1.0", "domain", false}, 121 {"domain/path/in/artifactory", "domain", false}, 122 {"domain:8081", "", true}, 123 } 124 125 for _, v := range imageTags { 126 result, err := NewImage(v.in).GetRegistry() 127 if err != nil && !v.expectingError { 128 t.Error(err.Error()) 129 } 130 if result != v.expected { 131 t.Errorf("ResolveRegistryFromTag(\"%s\") => '%s', expected '%s'", v.in, result, v.expected) 132 } 133 } 134 } 135 136 func TestDockerClientApiVersionRegex(t *testing.T) { 137 var versionStrings = []struct { 138 in string 139 expected bool 140 }{ 141 {"1", false}, 142 {"1.1", true}, 143 {"1.11", true}, 144 {"12.12", true}, 145 {"1.1.11", false}, 146 {"1.illegal", false}, 147 {"1 11", false}, 148 } 149 150 for _, v := range versionStrings { 151 result := ApiVersionRegex.Match([]byte(v.in)) 152 if result != v.expected { 153 t.Errorf("Version(\"%s\") => '%v', want '%v'", v.in, result, v.expected) 154 } 155 } 156 } 157 158 func TestBuildRemoteRepoUrl(t *testing.T) { 159 var data = []struct { 160 image string 161 isSecure bool 162 expectedRepo string 163 }{ 164 {"localhost:8082/docker-local/hello-world:123", true, "https://localhost:8082/v2/docker-local/hello-world/manifests/123"}, 165 {"localhost:8082/docker-local/hello-world:latest", true, "https://localhost:8082/v2/docker-local/hello-world/manifests/latest"}, 166 {"localhost:8082/docker-local/hello-world:latest", false, "http://localhost:8082/v2/docker-local/hello-world/manifests/latest"}, 167 // With proxy 168 {"jfrog-docker-local.jfrog.io/hello-world:123", true, "https://jfrog-docker-local.jfrog.io/v2/hello-world/manifests/123"}, 169 {"jfrog-docker-local.jfrog.io/hello-world:latest", true, "https://jfrog-docker-local.jfrog.io/v2/hello-world/manifests/latest"}, 170 {"jfrog-docker-local.jfrog.io/hello-world:123", false, "http://jfrog-docker-local.jfrog.io/v2/hello-world/manifests/123"}, 171 } 172 for _, v := range data { 173 testImae := NewImage(v.image) 174 containerRegistryUrl, err := testImae.GetRegistry() 175 assert.NoError(t, err) 176 177 longImageName, err := testImae.GetImageLongName() 178 assert.NoError(t, err) 179 180 imageTag, err := testImae.GetImageTag() 181 assert.NoError(t, err) 182 183 actualRepo := buildRequestUrl(longImageName, imageTag, containerRegistryUrl, v.isSecure) 184 assert.Equal(t, v.expectedRepo, actualRepo) 185 } 186 }