|
1 | 1 | package common // import "github.com/docker/docker/integration/plugin/common"
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net" |
4 | 10 | "net/http"
|
| 11 | + "path" |
| 12 | + "strings" |
5 | 13 | "testing"
|
6 | 14 |
|
| 15 | + "github.com/docker/docker/api/types" |
| 16 | + "github.com/docker/docker/testutil/daemon" |
| 17 | + "github.com/docker/docker/testutil/fixtures/plugin" |
| 18 | + "github.com/docker/docker/testutil/registry" |
7 | 19 | "github.com/docker/docker/testutil/request"
|
8 | 20 | "gotest.tools/v3/assert"
|
9 | 21 | is "gotest.tools/v3/assert/cmp"
|
| 22 | + "gotest.tools/v3/skip" |
10 | 23 | )
|
11 | 24 |
|
12 | 25 | func TestPluginInvalidJSON(t *testing.T) {
|
@@ -36,3 +49,111 @@ func TestPluginInvalidJSON(t *testing.T) {
|
36 | 49 | })
|
37 | 50 | }
|
38 | 51 | }
|
| 52 | + |
| 53 | +func TestPluginInstall(t *testing.T) { |
| 54 | + skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon") |
| 55 | + skip.If(t, testEnv.OSType == "windows") |
| 56 | + skip.If(t, testEnv.IsRootless, "rootless mode has different view of localhost") |
| 57 | + |
| 58 | + ctx := context.Background() |
| 59 | + client := testEnv.APIClient() |
| 60 | + |
| 61 | + t.Run("no auth", func(t *testing.T) { |
| 62 | + defer setupTest(t)() |
| 63 | + |
| 64 | + reg := registry.NewV2(t) |
| 65 | + defer reg.Close() |
| 66 | + |
| 67 | + name := "test-" + strings.ToLower(t.Name()) |
| 68 | + repo := path.Join(registry.DefaultURL, name+":latest") |
| 69 | + assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil)) |
| 70 | + |
| 71 | + rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{Disabled: true, RemoteRef: repo}) |
| 72 | + assert.NilError(t, err) |
| 73 | + defer rdr.Close() |
| 74 | + |
| 75 | + _, err = io.Copy(ioutil.Discard, rdr) |
| 76 | + assert.NilError(t, err) |
| 77 | + |
| 78 | + _, _, err = client.PluginInspectWithRaw(ctx, repo) |
| 79 | + assert.NilError(t, err) |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("with htpasswd", func(t *testing.T) { |
| 83 | + defer setupTest(t)() |
| 84 | + |
| 85 | + reg := registry.NewV2(t, registry.Htpasswd) |
| 86 | + defer reg.Close() |
| 87 | + |
| 88 | + name := "test-" + strings.ToLower(t.Name()) |
| 89 | + repo := path.Join(registry.DefaultURL, name+":latest") |
| 90 | + auth := &types.AuthConfig{ServerAddress: registry.DefaultURL, Username: "testuser", Password: "testpassword"} |
| 91 | + assert.NilError(t, plugin.CreateInRegistry(ctx, repo, auth)) |
| 92 | + |
| 93 | + authEncoded, err := json.Marshal(auth) |
| 94 | + assert.NilError(t, err) |
| 95 | + |
| 96 | + rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{ |
| 97 | + RegistryAuth: base64.URLEncoding.EncodeToString(authEncoded), |
| 98 | + Disabled: true, |
| 99 | + RemoteRef: repo, |
| 100 | + }) |
| 101 | + assert.NilError(t, err) |
| 102 | + defer rdr.Close() |
| 103 | + |
| 104 | + _, err = io.Copy(ioutil.Discard, rdr) |
| 105 | + assert.NilError(t, err) |
| 106 | + |
| 107 | + _, _, err = client.PluginInspectWithRaw(ctx, repo) |
| 108 | + assert.NilError(t, err) |
| 109 | + }) |
| 110 | + t.Run("with insecure", func(t *testing.T) { |
| 111 | + skip.If(t, !testEnv.IsLocalDaemon()) |
| 112 | + |
| 113 | + addrs, err := net.InterfaceAddrs() |
| 114 | + assert.NilError(t, err) |
| 115 | + |
| 116 | + var bindTo string |
| 117 | + for _, addr := range addrs { |
| 118 | + ip, ok := addr.(*net.IPNet) |
| 119 | + if !ok { |
| 120 | + continue |
| 121 | + } |
| 122 | + if ip.IP.IsLoopback() || ip.IP.To4() == nil { |
| 123 | + continue |
| 124 | + } |
| 125 | + bindTo = ip.IP.String() |
| 126 | + } |
| 127 | + |
| 128 | + if bindTo == "" { |
| 129 | + t.Skip("No suitable interface to bind registry to") |
| 130 | + } |
| 131 | + |
| 132 | + regURL := bindTo + ":5000" |
| 133 | + |
| 134 | + d := daemon.New(t) |
| 135 | + defer d.Stop(t) |
| 136 | + |
| 137 | + d.Start(t, "--insecure-registry="+regURL) |
| 138 | + defer d.Stop(t) |
| 139 | + |
| 140 | + reg := registry.NewV2(t, registry.URL(regURL)) |
| 141 | + defer reg.Close() |
| 142 | + |
| 143 | + name := "test-" + strings.ToLower(t.Name()) |
| 144 | + repo := path.Join(regURL, name+":latest") |
| 145 | + assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil, plugin.WithInsecureRegistry(regURL))) |
| 146 | + |
| 147 | + client := d.NewClientT(t) |
| 148 | + rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{Disabled: true, RemoteRef: repo}) |
| 149 | + assert.NilError(t, err) |
| 150 | + defer rdr.Close() |
| 151 | + |
| 152 | + _, err = io.Copy(ioutil.Discard, rdr) |
| 153 | + assert.NilError(t, err) |
| 154 | + |
| 155 | + _, _, err = client.PluginInspectWithRaw(ctx, repo) |
| 156 | + assert.NilError(t, err) |
| 157 | + }) |
| 158 | + // TODO: test insecure registry with https |
| 159 | +} |
0 commit comments