-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1391 from fuweid/feature_add_image_load
feature: add pouch load functionality
- Loading branch information
Showing
20 changed files
with
830 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
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,71 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// loadDescription is used to describe load command in detail and auto generate command doc. | ||
var loadDescription = "load a set of images by tar stream" | ||
|
||
// LoadCommand use to implement 'load' command. | ||
type LoadCommand struct { | ||
baseCommand | ||
input string | ||
} | ||
|
||
// Init initialize load command. | ||
func (l *LoadCommand) Init(c *Cli) { | ||
l.cli = c | ||
l.cmd = &cobra.Command{ | ||
Use: "load [OPTIONS] [IMAGE_NAME]", | ||
Short: "load a set of images from a tar archive or STDIN", | ||
Long: loadDescription, | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
return l.runLoad(args) | ||
}, | ||
Example: loadExample(), | ||
} | ||
l.addFlags() | ||
} | ||
|
||
// addFlags adds flags for specific command. | ||
func (l *LoadCommand) addFlags() { | ||
flagSet := l.cmd.Flags() | ||
flagSet.StringVarP(&l.input, "input", "i", "", "Read from tar archive file, instead of STDIN") | ||
} | ||
|
||
// runLoad is the entry of load command. | ||
func (l *LoadCommand) runLoad(args []string) error { | ||
ctx := context.Background() | ||
apiClient := l.cli.Client() | ||
|
||
var ( | ||
in io.Reader = os.Stdin | ||
imageName = "" | ||
) | ||
|
||
if l.input != "" { | ||
file, err := os.Open(l.input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer file.Close() | ||
in = file | ||
} | ||
|
||
if len(args) > 0 { | ||
imageName = args[0] | ||
} | ||
return apiClient.ImageLoad(ctx, imageName, in) | ||
} | ||
|
||
// loadExample shows examples in load command, and is used in auto-generated cli docs. | ||
func loadExample() string { | ||
return `$ pouch load -i busybox.tar busybox` | ||
} |
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
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,26 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/url" | ||
) | ||
|
||
// ImageLoad requests daemon to load an image from tarstream. | ||
func (client *APIClient) ImageLoad(ctx context.Context, imageName string, reader io.Reader) error { | ||
q := url.Values{} | ||
if imageName != "" { | ||
q.Set("name", imageName) | ||
} | ||
|
||
headers := map[string][]string{} | ||
headers["Content-Type"] = []string{"application/x-tar"} | ||
|
||
resp, err := client.postRawData(ctx, "/images/load", q, reader, headers) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ensureCloseReader(resp) | ||
return nil | ||
} |
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,57 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestImageLoadServerError(t *testing.T) { | ||
expectedError := "Server error" | ||
|
||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, expectedError)), | ||
} | ||
|
||
err := client.ImageLoad(context.Background(), "test_image_load_500", nil) | ||
if err == nil || !strings.Contains(err.Error(), expectedError) { | ||
t.Fatalf("expected (%v), got (%v)", expectedError, err) | ||
} | ||
} | ||
|
||
func TestImageLoadOK(t *testing.T) { | ||
expectedURL := "/images/load" | ||
expectedImageName := "test_image_load_ok" | ||
|
||
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) { | ||
if !strings.HasPrefix(req.URL.Path, expectedURL) { | ||
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) | ||
} | ||
|
||
if req.Method != "POST" { | ||
return nil, fmt.Errorf("expected POST method, got %s", req.Method) | ||
} | ||
|
||
if got := req.FormValue("name"); got != expectedImageName { | ||
return nil, fmt.Errorf("expected (%s), got %s", expectedImageName, got) | ||
} | ||
|
||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), | ||
}, nil | ||
}) | ||
|
||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
|
||
if err := client.ImageLoad(context.Background(), expectedImageName, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
} |
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
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
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
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
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
Oops, something went wrong.