mirror of
https://github.com/docker/compose.git
synced 2026-11-18 06:07:53 +00:00
Running a provider plugin (finding its binary, executing it, serving the JSON messages it sends, applying the variables and endpoints it publishes, streaming it an image) needs from the engine the Docker API, the event bus, the Docker CLI that locates the plugin, the preparation of the environment of a shelled-out command, and the lock that guards the project model every concurrent provider run mutates. It now lives in internal/provider behind a Backend interface that composeService implements in pkg/compose/plugins.go, next to runPlugin, which stays as a one-line forwarder so none of its callers (up, down, stop, pull) changes. The relay it deploys goes through internal/relay, whose Backend the new one extends. The public types and constants of the protocol (JsonMessage, ProviderMetadata, the *Type message constants...) stay exported from pkg/compose as aliases of the new package's. The tests of the plugin protocol move with the code, assertions unchanged; those of the provider image phase and of the relay link network removal stay in pkg/compose. The forwarders to the relay that only the plugin code used are dropped. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
138 lines
4.2 KiB
Go
138 lines
4.2 KiB
Go
/*
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestProviderMetadata_IsEmpty(t *testing.T) {
|
|
param := []ParameterMetadata{{Name: "x"}}
|
|
|
|
tests := []struct {
|
|
name string
|
|
metadata ProviderMetadata
|
|
want bool
|
|
}{
|
|
{
|
|
name: "empty metadata",
|
|
metadata: ProviderMetadata{},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "only Description set",
|
|
metadata: ProviderMetadata{Description: "something"},
|
|
want: false,
|
|
},
|
|
{
|
|
name: "only Up.Parameters set",
|
|
metadata: ProviderMetadata{Up: CommandMetadata{Parameters: param}},
|
|
want: false,
|
|
},
|
|
{
|
|
name: "only Down.Parameters set",
|
|
metadata: ProviderMetadata{Down: CommandMetadata{Parameters: param}},
|
|
want: false,
|
|
},
|
|
{
|
|
name: "only Stop set is empty",
|
|
metadata: ProviderMetadata{Stop: &CommandMetadata{}},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "all fields set",
|
|
metadata: ProviderMetadata{
|
|
Description: "full",
|
|
Up: CommandMetadata{Parameters: param},
|
|
Down: CommandMetadata{Parameters: param},
|
|
Stop: &CommandMetadata{Parameters: param},
|
|
},
|
|
want: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
assert.Equal(t, tc.metadata.IsEmpty(), tc.want)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProviderMetadata_JSONUnmarshal(t *testing.T) {
|
|
raw := `{"description":"x","up":{"parameters":[{"name":"a"}]},"down":{"parameters":[{"name":"b"}]},"stop":{"parameters":[{"name":"c"}]}}`
|
|
|
|
var metadata ProviderMetadata
|
|
err := json.Unmarshal([]byte(raw), &metadata)
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, metadata.Description, "x")
|
|
assert.Equal(t, metadata.Up.Parameters[0].Name, "a")
|
|
assert.Equal(t, metadata.Down.Parameters[0].Name, "b")
|
|
assert.Assert(t, metadata.Stop != nil, "Stop should be non-nil when present in JSON")
|
|
assert.Equal(t, metadata.Stop.Parameters[0].Name, "c")
|
|
}
|
|
|
|
func TestProviderMetadata_StopAbsent(t *testing.T) {
|
|
raw := `{"description":"x","up":{"parameters":[]},"down":{"parameters":[]}}`
|
|
|
|
var metadata ProviderMetadata
|
|
err := json.Unmarshal([]byte(raw), &metadata)
|
|
assert.NilError(t, err)
|
|
assert.Assert(t, metadata.Stop == nil, "Stop should be nil when absent from JSON")
|
|
}
|
|
|
|
func TestProviderMetadata_PullAbsent(t *testing.T) {
|
|
raw := `{"description":"x","up":{"parameters":[]},"down":{"parameters":[]}}`
|
|
|
|
var metadata ProviderMetadata
|
|
err := json.Unmarshal([]byte(raw), &metadata)
|
|
assert.NilError(t, err)
|
|
assert.Assert(t, metadata.Pull == nil, "Pull should be nil when absent from JSON")
|
|
}
|
|
|
|
func TestProviderMetadata_PullPresent(t *testing.T) {
|
|
raw := `{"pull":{"parameters":[{"name":"registry"}]}}`
|
|
|
|
var metadata ProviderMetadata
|
|
err := json.Unmarshal([]byte(raw), &metadata)
|
|
assert.NilError(t, err)
|
|
assert.Assert(t, metadata.Pull != nil, "Pull should be non-nil when key present")
|
|
assert.Equal(t, metadata.Pull.Parameters[0].Name, "registry")
|
|
}
|
|
|
|
func TestProviderMetadata_StopAdvertisedWithoutParameters(t *testing.T) {
|
|
raw := `{"stop":{"parameters":null}}`
|
|
|
|
var metadata ProviderMetadata
|
|
err := json.Unmarshal([]byte(raw), &metadata)
|
|
assert.NilError(t, err)
|
|
assert.Assert(t, metadata.Stop != nil, "Stop should be non-nil when key present even with null parameters")
|
|
}
|
|
|
|
func TestParseEndpointMessage(t *testing.T) {
|
|
port, upstream, err := parseEndpointMessage("80=host.docker.internal:49152")
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, port, 80)
|
|
assert.Equal(t, upstream, "host.docker.internal:49152")
|
|
|
|
for _, invalid := range []string{"80", "abc=host:1", "0=host:1", "80=nohostport"} {
|
|
_, _, err := parseEndpointMessage(invalid)
|
|
assert.Assert(t, err != nil, "expected error for %q", invalid)
|
|
}
|
|
}
|