forked from fsouza/go-dockerclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Container operations divided separate files (fsouza#832)
* container operations divided into separate files * import errors fixed * container_test.go file divided into separate files * formatting the go codes * formatting the go codes Co-authored-by: Umut Çömlekçioğlu <umut.comlekcioglu@rasyotek.com.tr>
- Loading branch information
1 parent
60a2c34
commit 23318e6
Showing
50 changed files
with
4,226 additions
and
3,854 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package docker | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// UploadToContainerOptions is the set of options that can be used when | ||
// uploading an archive into a container. | ||
// | ||
// See https://goo.gl/g25o7u for more details. | ||
type UploadToContainerOptions struct { | ||
InputStream io.Reader `json:"-" qs:"-"` | ||
Path string `qs:"path"` | ||
NoOverwriteDirNonDir bool `qs:"noOverwriteDirNonDir"` | ||
Context context.Context | ||
} | ||
|
||
// UploadToContainer uploads a tar archive to be extracted to a path in the | ||
// filesystem of the container. | ||
// | ||
// See https://goo.gl/g25o7u for more details. | ||
func (c *Client) UploadToContainer(id string, opts UploadToContainerOptions) error { | ||
url := fmt.Sprintf("/containers/%s/archive?", id) + queryString(opts) | ||
|
||
return c.stream(http.MethodPut, url, streamOptions{ | ||
in: opts.InputStream, | ||
context: opts.Context, | ||
}) | ||
} | ||
|
||
// DownloadFromContainerOptions is the set of options that can be used when | ||
// downloading resources from a container. | ||
// | ||
// See https://goo.gl/W49jxK for more details. | ||
type DownloadFromContainerOptions struct { | ||
OutputStream io.Writer `json:"-" qs:"-"` | ||
Path string `qs:"path"` | ||
InactivityTimeout time.Duration `qs:"-"` | ||
Context context.Context | ||
} | ||
|
||
// DownloadFromContainer downloads a tar archive of files or folders in a container. | ||
// | ||
// See https://goo.gl/W49jxK for more details. | ||
func (c *Client) DownloadFromContainer(id string, opts DownloadFromContainerOptions) error { | ||
url := fmt.Sprintf("/containers/%s/archive?", id) + queryString(opts) | ||
|
||
return c.stream(http.MethodGet, url, streamOptions{ | ||
setRawTerminal: true, | ||
stdout: opts.OutputStream, | ||
inactivityTimeout: opts.InactivityTimeout, | ||
context: opts.Context, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package docker | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestUploadToContainer(t *testing.T) { | ||
t.Parallel() | ||
content := "File content" | ||
in := stdinMock{bytes.NewBufferString(content)} | ||
fakeRT := &FakeRoundTripper{status: http.StatusOK} | ||
client := newTestClient(fakeRT) | ||
opts := UploadToContainerOptions{ | ||
Path: "abc", | ||
InputStream: in, | ||
} | ||
err := client.UploadToContainer("a123456", opts) | ||
if err != nil { | ||
t.Errorf("UploadToContainer: caught error %#v while uploading archive to container, expected nil", err) | ||
} | ||
|
||
req := fakeRT.requests[0] | ||
|
||
if req.Method != http.MethodPut { | ||
t.Errorf("UploadToContainer{Path:abc}: Wrong HTTP method. Want PUT. Got %s", req.Method) | ||
} | ||
|
||
if pathParam := req.URL.Query().Get("path"); pathParam != "abc" { | ||
t.Errorf("ListImages({Path:abc}): Wrong parameter. Want path=abc. Got path=%s", pathParam) | ||
} | ||
} | ||
|
||
func TestDownloadFromContainer(t *testing.T) { | ||
t.Parallel() | ||
filecontent := "File content" | ||
client := newTestClient(&FakeRoundTripper{message: filecontent, status: http.StatusOK}) | ||
|
||
var out bytes.Buffer | ||
opts := DownloadFromContainerOptions{ | ||
OutputStream: &out, | ||
} | ||
err := client.DownloadFromContainer("a123456", opts) | ||
if err != nil { | ||
t.Errorf("DownloadFromContainer: caught error %#v while downloading from container, expected nil", err.Error()) | ||
} | ||
if out.String() != filecontent { | ||
t.Errorf("DownloadFromContainer: wrong stdout. Want %#v. Got %#v.", filecontent, out.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package docker | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// AttachToContainerOptions is the set of options that can be used when | ||
// attaching to a container. | ||
// | ||
// See https://goo.gl/JF10Zk for more details. | ||
type AttachToContainerOptions struct { | ||
Container string `qs:"-"` | ||
InputStream io.Reader `qs:"-"` | ||
OutputStream io.Writer `qs:"-"` | ||
ErrorStream io.Writer `qs:"-"` | ||
|
||
// If set, after a successful connect, a sentinel will be sent and then the | ||
// client will block on receive before continuing. | ||
// | ||
// It must be an unbuffered channel. Using a buffered channel can lead | ||
// to unexpected behavior. | ||
Success chan struct{} | ||
|
||
// Override the key sequence for detaching a container. | ||
DetachKeys string | ||
|
||
// Use raw terminal? Usually true when the container contains a TTY. | ||
RawTerminal bool `qs:"-"` | ||
|
||
// Get container logs, sending it to OutputStream. | ||
Logs bool | ||
|
||
// Stream the response? | ||
Stream bool | ||
|
||
// Attach to stdin, and use InputStream. | ||
Stdin bool | ||
|
||
// Attach to stdout, and use OutputStream. | ||
Stdout bool | ||
|
||
// Attach to stderr, and use ErrorStream. | ||
Stderr bool | ||
} | ||
|
||
// AttachToContainer attaches to a container, using the given options. | ||
// | ||
// See https://goo.gl/JF10Zk for more details. | ||
func (c *Client) AttachToContainer(opts AttachToContainerOptions) error { | ||
cw, err := c.AttachToContainerNonBlocking(opts) | ||
if err != nil { | ||
return err | ||
} | ||
return cw.Wait() | ||
} | ||
|
||
// AttachToContainerNonBlocking attaches to a container, using the given options. | ||
// This function does not block. | ||
// | ||
// See https://goo.gl/NKpkFk for more details. | ||
func (c *Client) AttachToContainerNonBlocking(opts AttachToContainerOptions) (CloseWaiter, error) { | ||
if opts.Container == "" { | ||
return nil, &NoSuchContainer{ID: opts.Container} | ||
} | ||
path := "/containers/" + opts.Container + "/attach?" + queryString(opts) | ||
return c.hijack(http.MethodPost, path, hijackOptions{ | ||
success: opts.Success, | ||
setRawTerminal: opts.RawTerminal, | ||
in: opts.InputStream, | ||
stdout: opts.OutputStream, | ||
stderr: opts.ErrorStream, | ||
}) | ||
} |
Oops, something went wrong.