compose/pkg/compose/plugins_test.go
Nicolas De Loof 5bc5b864fc feat(provider): pull command and get-image protocol for image distribution
Providers opt into image distribution by declaring a pull block in
their metadata — the presence of the command block is the declaration
of support, generalizing the stop precedent. During its pull command a
provider can request the service image from the local daemon with a
get-image message (optionally platform-narrowed); compose answers on
stdin with one image-stream JSON line then the tar as HTTP/1.1 chunked
data (RFC 9112): length-prefixed blocks need no in-band delimiter in
binary data, the zero-length chunk marks a COMPLETE transfer, and any
stock chunked reader consumes it. There is deliberately no trailer nor
final CRLF after the zero chunk — a stock reader stops there without
consuming further bytes, so the next stdin answer starts clean for a
provider requesting several images. An export failure is announced
in-band through the error field so the provider is never left waiting;
a failure after the announce closes the answer channel so the
truncation is observable as EOF mid-chunk instead of a stream nobody
will finish. The stream is exclusive on stdin for its whole duration —
the framing invariant behind the lock.

get-image is scoped to the pull command: emitted during up, down or
stop it is a protocol error rather than served, so an image export can
never stall another lifecycle command.

Message dispatch moves out of executePlugin into handlePluginMessage,
which also keeps the function under the complexity threshold.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-10-02 17:35:20 +02:00

126 lines
3.7 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 compose
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")
}