github.com/bshelton229/agent@v3.5.4+incompatible/agent/plugin/plugin_test.go (about) 1 package plugin 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/buildkite/agent/env" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestCreateFromJSON(t *testing.T) { 13 t.Parallel() 14 15 var plugins []*Plugin 16 var err error 17 18 plugins, err = CreateFromJSON(`[{"http://github.com/buildkite/plugins/docker-compose#a34fa34":{"container":"app"}}, "github.com/buildkite/plugins/ping#master"]`) 19 assert.Equal(t, len(plugins), 2) 20 assert.Nil(t, err) 21 22 assert.Equal(t, plugins[0].Location, "github.com/buildkite/plugins/docker-compose") 23 assert.Equal(t, plugins[0].Version, "a34fa34") 24 assert.Equal(t, plugins[0].Scheme, "http") 25 assert.Equal(t, plugins[0].Configuration, map[string]interface{}{"container": "app"}) 26 27 assert.Equal(t, plugins[1].Location, "github.com/buildkite/plugins/ping") 28 assert.Equal(t, plugins[1].Version, "master") 29 assert.Equal(t, plugins[1].Scheme, "") 30 assert.Equal(t, plugins[1].Configuration, map[string]interface{}{}) 31 32 plugins, err = CreateFromJSON(`["ssh://git:foo@github.com/buildkite/plugins/docker-compose#a34fa34"]`) 33 assert.Equal(t, len(plugins), 1) 34 assert.Nil(t, err) 35 36 assert.Equal(t, plugins[0].Location, "github.com/buildkite/plugins/docker-compose") 37 assert.Equal(t, plugins[0].Version, "a34fa34") 38 assert.Equal(t, plugins[0].Scheme, "ssh") 39 assert.Equal(t, plugins[0].Authentication, "git:foo") 40 41 plugins, err = CreateFromJSON(`blah`) 42 assert.Equal(t, len(plugins), 0) 43 assert.NotNil(t, err) 44 assert.Equal(t, err.Error(), "invalid character 'b' looking for beginning of value") 45 46 plugins, err = CreateFromJSON(`{"foo": "bar"}`) 47 assert.Equal(t, len(plugins), 0) 48 assert.NotNil(t, err) 49 assert.Equal(t, err.Error(), "JSON structure was not an array") 50 51 plugins, err = CreateFromJSON(`["github.com/buildkite/plugins/ping#master#lololo"]`) 52 assert.Equal(t, len(plugins), 0) 53 assert.NotNil(t, err) 54 assert.Equal(t, err.Error(), "Too many #'s in \"github.com/buildkite/plugins/ping#master#lololo\"") 55 } 56 57 func TestPluginNameParsedFromLocation(t *testing.T) { 58 t.Parallel() 59 60 for _, tc := range []struct { 61 location string 62 expectedName string 63 }{ 64 {"github.com/buildkite-plugins/docker-compose-buildkite-plugin.git", "docker-compose"}, 65 {"github.com/buildkite-plugins/docker-compose-buildkite-plugin", "docker-compose"}, 66 {"github.com/my-org/docker-compose-buildkite-plugin", "docker-compose"}, 67 {"github.com/buildkite/plugins/docker-compose", "docker-compose"}, 68 {"~/Development/plugins/test", "test"}, 69 {"~/Development/plugins/UPPER CASE_party", "upper-case-party"}, 70 {"vendor/src/vendored with a space", "vendored-with-a-space"}, 71 {"", ""}, 72 } { 73 tc := tc 74 t.Run(tc.location, func(tt *testing.T) { 75 tt.Parallel() 76 plugin := &Plugin{Location: tc.location} 77 assert.Equal(tt, tc.expectedName, plugin.Name()) 78 }) 79 } 80 } 81 82 func TestIdentifier(t *testing.T) { 83 t.Parallel() 84 85 var plugin *Plugin 86 var id string 87 var err error 88 89 plugin = &Plugin{Location: "github.com/buildkite/plugins/docker-compose/beta#master"} 90 id, err = plugin.Identifier() 91 assert.Equal(t, id, "github-com-buildkite-plugins-docker-compose-beta-master") 92 assert.Nil(t, err) 93 94 plugin = &Plugin{Location: "github.com/buildkite/plugins/docker-compose/beta"} 95 id, err = plugin.Identifier() 96 assert.Equal(t, id, "github-com-buildkite-plugins-docker-compose-beta") 97 assert.Nil(t, err) 98 99 plugin = &Plugin{Location: "192.168.0.1/foo.git#12341234"} 100 id, err = plugin.Identifier() 101 assert.Equal(t, id, "192-168-0-1-foo-git-12341234") 102 assert.Nil(t, err) 103 104 plugin = &Plugin{Location: "/foo/bar/"} 105 id, err = plugin.Identifier() 106 assert.Equal(t, id, "foo-bar") 107 assert.Nil(t, err) 108 } 109 110 func TestRepositoryAndSubdirectory(t *testing.T) { 111 t.Parallel() 112 113 var plugin *Plugin 114 var repo string 115 var sub string 116 var err error 117 118 plugin = &Plugin{Location: "github.com/buildkite/plugins/docker-compose/beta"} 119 repo, err = plugin.Repository() 120 assert.Equal(t, repo, "https://github.com/buildkite/plugins") 121 assert.Nil(t, err) 122 sub, err = plugin.RepositorySubdirectory() 123 assert.Equal(t, sub, "docker-compose/beta") 124 assert.Nil(t, err) 125 126 plugin = &Plugin{Location: "github.com/buildkite/test-plugin"} 127 repo, err = plugin.Repository() 128 assert.Equal(t, repo, "https://github.com/buildkite/test-plugin") 129 assert.Nil(t, err) 130 sub, err = plugin.RepositorySubdirectory() 131 assert.Equal(t, sub, "") 132 assert.Nil(t, err) 133 134 plugin = &Plugin{Location: "github.com/buildkite"} 135 repo, err = plugin.Repository() 136 assert.Equal(t, repo, "") 137 assert.NotNil(t, err) 138 assert.Equal(t, err.Error(), `Incomplete github.com path "github.com/buildkite"`) 139 sub, err = plugin.RepositorySubdirectory() 140 assert.Equal(t, sub, "") 141 assert.NotNil(t, err) 142 assert.Equal(t, err.Error(), `Incomplete github.com path "github.com/buildkite"`) 143 144 plugin = &Plugin{Location: "bitbucket.org/buildkite"} 145 repo, err = plugin.Repository() 146 assert.Equal(t, repo, "") 147 assert.NotNil(t, err) 148 assert.Equal(t, err.Error(), `Incomplete bitbucket.org path "bitbucket.org/buildkite"`) 149 sub, err = plugin.RepositorySubdirectory() 150 assert.Equal(t, sub, "") 151 assert.NotNil(t, err) 152 assert.Equal(t, err.Error(), `Incomplete bitbucket.org path "bitbucket.org/buildkite"`) 153 154 plugin = &Plugin{Location: "bitbucket.org/user/project/sub/directory"} 155 repo, err = plugin.Repository() 156 assert.Equal(t, repo, "https://bitbucket.org/user/project") 157 assert.Nil(t, err) 158 sub, err = plugin.RepositorySubdirectory() 159 assert.Equal(t, sub, "sub/directory") 160 assert.Nil(t, err) 161 162 plugin = &Plugin{Location: "bitbucket.org/user/project/sub/directory", Scheme: "http", Authentication: "foo:bar"} 163 repo, err = plugin.Repository() 164 assert.Equal(t, repo, "http://foo:bar@bitbucket.org/user/project") 165 assert.Nil(t, err) 166 sub, err = plugin.RepositorySubdirectory() 167 assert.Equal(t, sub, "sub/directory") 168 assert.Nil(t, err) 169 170 plugin = &Plugin{Location: "114.135.234.212/foo.git"} 171 repo, err = plugin.Repository() 172 assert.Equal(t, repo, "https://114.135.234.212/foo.git") 173 assert.Nil(t, err) 174 sub, err = plugin.RepositorySubdirectory() 175 assert.Equal(t, sub, "") 176 assert.Nil(t, err) 177 178 plugin = &Plugin{Location: "github.com/buildkite/plugins/docker-compose/beta"} 179 repo, err = plugin.Repository() 180 assert.Equal(t, repo, "https://github.com/buildkite/plugins") 181 assert.Nil(t, err) 182 sub, err = plugin.RepositorySubdirectory() 183 assert.Equal(t, sub, "docker-compose/beta") 184 assert.Nil(t, err) 185 186 plugin = &Plugin{Location: "/Users/keithpitt/Development/plugins.git/test-plugin"} 187 repo, err = plugin.Repository() 188 assert.Equal(t, repo, "/Users/keithpitt/Development/plugins.git") 189 assert.Nil(t, err) 190 sub, err = plugin.RepositorySubdirectory() 191 assert.Equal(t, sub, "test-plugin") 192 assert.Nil(t, err) 193 194 plugin = &Plugin{Location: ""} 195 repo, err = plugin.Repository() 196 assert.Equal(t, repo, "") 197 assert.NotNil(t, err) 198 assert.Equal(t, err.Error(), "Missing plugin location") 199 } 200 201 func TestConfigurationToEnvironment(t *testing.T) { 202 t.Parallel() 203 204 var envMap *env.Environment 205 var err error 206 207 envMap, err = pluginEnvFromConfig(t, `{ "config-key": 42 }`) 208 assert.NoError(t, err) 209 assert.Equal(t, []string{"BUILDKITE_PLUGIN_DOCKER_COMPOSE_CONFIG_KEY=42"}, envMap.ToSlice()) 210 211 envMap, err = pluginEnvFromConfig(t, `{ "container": "app", "some-other-setting": "else right here" }`) 212 assert.NoError(t, err) 213 assert.Equal(t, []string{ 214 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_CONTAINER=app", 215 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_SOME_OTHER_SETTING=else right here"}, 216 envMap.ToSlice()) 217 218 envMap, err = pluginEnvFromConfig(t, `{ "and _ with a - number": 12 }`) 219 assert.NoError(t, err) 220 assert.Equal(t, []string{"BUILDKITE_PLUGIN_DOCKER_COMPOSE_AND_WITH_A_NUMBER=12"}, envMap.ToSlice()) 221 222 envMap, err = pluginEnvFromConfig(t, `{ "bool-true-key": true, "bool-false-key": false }`) 223 assert.NoError(t, err) 224 assert.Equal(t, []string{ 225 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_BOOL_FALSE_KEY=false", 226 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_BOOL_TRUE_KEY=true"}, 227 envMap.ToSlice()) 228 229 envMap, err = pluginEnvFromConfig(t, `{ "array-key": [ "array-val-1", "array-val-2" ] }`) 230 assert.NoError(t, err) 231 assert.Equal(t, []string{ 232 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_0=array-val-1", 233 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_1=array-val-2"}, 234 envMap.ToSlice()) 235 236 envMap, err = pluginEnvFromConfig(t, `{ "array-key": [ 42, 43, 44 ] }`) 237 assert.NoError(t, err) 238 assert.Equal(t, []string{ 239 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_0=42", 240 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_1=43", 241 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_2=44"}, 242 envMap.ToSlice()) 243 244 envMap, err = pluginEnvFromConfig(t, `{ "array-key": [ 42, 43, "foo" ] }`) 245 assert.NoError(t, err) 246 assert.Equal(t, []string{ 247 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_0=42", 248 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_1=43", 249 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_2=foo"}, 250 envMap.ToSlice()) 251 252 envMap, err = pluginEnvFromConfig(t, `{ "array-key": [ { "subkey": "subval" } ] }`) 253 assert.NoError(t, err) 254 assert.Equal(t, []string{ 255 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_0_SUBKEY=subval"}, 256 envMap.ToSlice()) 257 258 envMap, err = pluginEnvFromConfig(t, `{ "array-key": [ { "subkey": [1, 2, "llamas"] } ] }`) 259 assert.NoError(t, err) 260 assert.Equal(t, []string{ 261 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_0_SUBKEY_0=1", 262 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_0_SUBKEY_1=2", 263 "BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARRAY_KEY_0_SUBKEY_2=llamas", 264 }, envMap.ToSlice()) 265 } 266 267 func pluginEnvFromConfig(t *testing.T, configJson string) (*env.Environment, error) { 268 var config map[string]interface{} 269 270 json.Unmarshal([]byte(configJson), &config) 271 272 jsonString := fmt.Sprintf(`[ { "%s": %s } ]`, "github.com/buildkite-plugins/docker-compose-buildkite-plugin", configJson) 273 274 plugins, err := CreateFromJSON(jsonString) 275 276 assert.NoError(t, err) 277 assert.Equal(t, 1, len(plugins)) 278 279 return plugins[0].ConfigurationToEnvironment() 280 }