2018-03-06 05:15:18 -05:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/e2e/internal/fixtures"
|
2018-05-17 07:11:59 -04:00
|
|
|
"github.com/docker/cli/internal/test/environment"
|
2018-03-06 05:15:18 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/fs"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-03-06 05:15:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const registryPrefix = "registry:5000"
|
|
|
|
|
|
|
|
func TestInstallWithContentTrust(t *testing.T) {
|
2018-05-17 07:11:59 -04:00
|
|
|
skip.If(t, environment.SkipPluginTests())
|
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
pluginName := fmt.Sprintf("%s/plugin-content-trust", registryPrefix)
|
|
|
|
|
|
|
|
dir := fixtures.SetupConfigFile(t)
|
|
|
|
defer dir.Remove()
|
|
|
|
|
|
|
|
pluginDir := preparePluginDir(t)
|
|
|
|
defer pluginDir.Remove()
|
|
|
|
|
|
|
|
icmd.RunCommand("docker", "plugin", "create", pluginName, pluginDir.Path()).Assert(t, icmd.Success)
|
|
|
|
result := icmd.RunCmd(icmd.Command("docker", "plugin", "push", pluginName),
|
|
|
|
fixtures.WithConfig(dir.Path()),
|
|
|
|
fixtures.WithTrust,
|
|
|
|
fixtures.WithNotary,
|
|
|
|
fixtures.WithPassphrase("foo", "bar"),
|
|
|
|
)
|
|
|
|
result.Assert(t, icmd.Expected{
|
|
|
|
Out: "Signing and pushing trust metadata",
|
|
|
|
})
|
|
|
|
|
|
|
|
icmd.RunCommand("docker", "plugin", "rm", "-f", pluginName).Assert(t, icmd.Success)
|
|
|
|
|
|
|
|
result = icmd.RunCmd(icmd.Command("docker", "plugin", "install", "--grant-all-permissions", pluginName),
|
|
|
|
fixtures.WithConfig(dir.Path()),
|
|
|
|
fixtures.WithTrust,
|
|
|
|
fixtures.WithNotary,
|
|
|
|
)
|
|
|
|
result.Assert(t, icmd.Expected{
|
|
|
|
Out: fmt.Sprintf("Status: Downloaded newer image for %s@sha", pluginName),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInstallWithContentTrustUntrusted(t *testing.T) {
|
2018-05-17 07:11:59 -04:00
|
|
|
skip.If(t, environment.SkipPluginTests())
|
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
dir := fixtures.SetupConfigFile(t)
|
|
|
|
defer dir.Remove()
|
|
|
|
|
|
|
|
result := icmd.RunCmd(icmd.Command("docker", "plugin", "install", "--grant-all-permissions", "tiborvass/sample-volume-plugin:latest"),
|
|
|
|
fixtures.WithConfig(dir.Path()),
|
|
|
|
fixtures.WithTrust,
|
|
|
|
fixtures.WithNotary,
|
|
|
|
)
|
|
|
|
result.Assert(t, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "Error: remote trust data does not exist",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func preparePluginDir(t *testing.T) *fs.Dir {
|
2023-11-20 05:10:29 -05:00
|
|
|
t.Helper()
|
2018-03-06 05:15:18 -05:00
|
|
|
p := &types.PluginConfig{
|
|
|
|
Interface: types.PluginConfigInterface{
|
|
|
|
Socket: "basic.sock",
|
|
|
|
Types: []types.PluginInterfaceType{{Capability: "docker.dummy/1.0"}},
|
|
|
|
},
|
|
|
|
Entrypoint: []string{"/basic"},
|
|
|
|
}
|
|
|
|
configJSON, err := json.Marshal(p)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
binPath, err := ensureBasicPluginBin()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
dir := fs.NewDir(t, "plugin_test",
|
2022-09-30 13:13:22 -04:00
|
|
|
fs.WithFile("config.json", string(configJSON), fs.WithMode(0o644)),
|
|
|
|
fs.WithDir("rootfs", fs.WithMode(0o755)),
|
2018-03-06 05:15:18 -05:00
|
|
|
)
|
|
|
|
icmd.RunCommand("/bin/cp", binPath, dir.Join("rootfs", p.Entrypoint[0])).Assert(t, icmd.Success)
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureBasicPluginBin() (string, error) {
|
|
|
|
name := "docker-basic-plugin"
|
|
|
|
p, err := exec.LookPath(name)
|
|
|
|
if err == nil {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
goBin, err := exec.LookPath("/usr/local/go/bin/go")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
installPath := filepath.Join(os.Getenv("GOPATH"), "bin", name)
|
|
|
|
cmd := exec.Command(goBin, "build", "-o", installPath, "./basic")
|
2019-03-04 14:09:34 -05:00
|
|
|
cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
|
2018-03-06 05:15:18 -05:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
return "", errors.Wrapf(err, "error building basic plugin bin: %s", string(out))
|
|
|
|
}
|
|
|
|
return installPath, nil
|
|
|
|
}
|