Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] add inspect format for image network and volume #871

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions cli/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/alibaba/pouch/cli/inspect"

"github.com/spf13/cobra"
)

Expand All @@ -15,6 +15,7 @@ var imageInspectDescription = "Return detailed information on Pouch image"
// ImageInspectCommand use to implement 'image inspect' command.
type ImageInspectCommand struct {
baseCommand
format string
}

// Init initialize "image inspect" command.
Expand All @@ -30,20 +31,25 @@ func (i *ImageInspectCommand) Init(c *Cli) {
},
Example: i.example(),
}
i.addFlags()
}

// addFlags adds flags for specific command.
func (i *ImageInspectCommand) addFlags() {
i.cmd.Flags().StringVarP(&i.format, "format", "f", "", "Format the output using the given go template")
}

// runInpsect is used to inspect image.
func (i *ImageInspectCommand) runInspect(args []string) error {
ctx := context.Background()
apiClient := i.cli.Client()
image, err := apiClient.ImageInspect(ctx, args[0])
if err != nil {
return fmt.Errorf("failed to inspect image: %v", err)
name := args[0]

getRefFunc := func(ref string) (interface{}, error) {
return apiClient.ImageInspect(ctx, ref)
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(image)
return inspect.Inspect(os.Stdout, name, i.format, getRefFunc)
}

// example shows examples in inspect command, and is used in auto-generated cli docs.
Expand Down
13 changes: 8 additions & 5 deletions cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"context"
"fmt"
"os"
"strings"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/cli/inspect"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -252,6 +254,7 @@ var networkInspectDescription = "Inspect a network in pouchd. " +
// NetworkInspectCommand is used to implement 'network inspect' command.
type NetworkInspectCommand struct {
baseCommand
format string
}

// Init initializes NetworkInspectCommand command.
Expand All @@ -275,6 +278,7 @@ func (n *NetworkInspectCommand) Init(c *Cli) {
// addFlags adds flags for specific command.
func (n *NetworkInspectCommand) addFlags() {
//TODO add flags
n.cmd.Flags().StringVarP(&n.format, "format", "f", "", "Format the output using the given go template")
}

// runNetworkInspect is the entry of NetworkInspectCommand command.
Expand All @@ -283,13 +287,12 @@ func (n *NetworkInspectCommand) runNetworkInspect(args []string) error {

ctx := context.Background()
apiClient := n.cli.Client()
resp, err := apiClient.NetworkInspect(ctx, name)
if err != nil {
return err

getRefFunc := func(ref string) (interface{}, error) {
return apiClient.NetworkInspect(ctx, ref)
}

n.cli.Print(resp)
return nil
return inspect.Inspect(os.Stdout, name, n.format, getRefFunc)
}

// networkInspectExample shows examples in network inspect command, and is used in auto-generated cli docs.
Expand Down
15 changes: 9 additions & 6 deletions cli/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"context"
"fmt"
"os"
"strings"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/cli/inspect"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -214,6 +216,7 @@ var volumeInspectDescription = "Inspect a volume in pouchd. " +
// VolumeInspectCommand is used to implement 'volume inspect' command.
type VolumeInspectCommand struct {
baseCommand
format string
}

// Init initializes VolumeInspectCommand command.
Expand All @@ -233,7 +236,9 @@ func (v *VolumeInspectCommand) Init(c *Cli) {
}

// addFlags adds flags for specific command.
func (v *VolumeInspectCommand) addFlags() {}
func (v *VolumeInspectCommand) addFlags() {
v.cmd.Flags().StringVarP(&v.format, "format", "f", "", "Format the output using the given go template")
}

// runVolumeInspect is the entry of VolumeInspectCommand command.
func (v *VolumeInspectCommand) runVolumeInspect(args []string) error {
Expand All @@ -244,13 +249,11 @@ func (v *VolumeInspectCommand) runVolumeInspect(args []string) error {
ctx := context.Background()
apiClient := v.cli.Client()

resp, err := apiClient.VolumeInspect(ctx, name)
if err != nil {
return err
getRefFunc := func(ref string) (interface{}, error) {
return apiClient.VolumeInspect(ctx, ref)
}

v.cli.Print(resp)
return nil
return inspect.Inspect(os.Stdout, name, v.format, getRefFunc)
}

// volumeInspectExample shows examples in volume inspect command, and is used in auto-generated cli docs.
Expand Down
16 changes: 16 additions & 0 deletions test/cli_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package main

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/client"
"github.com/alibaba/pouch/pkg/utils"
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"

"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
"github.com/pkg/errors"
Expand Down Expand Up @@ -92,3 +95,16 @@ func getImageInfo(apiClient client.ImageAPIClient, name string) (types.ImageInfo
}
return types.ImageInfo{}, errors.Errorf("image %s not found", name)
}

// TestInspectImage is to verify the format flag of image inspect command.
func (suite *PouchImagesSuite) TestInspectImage(c *check.C) {
output := command.PouchRun("image", "inspect", busyboxImage).Stdout()
result := &types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}

// inspect image name
output = command.PouchRun("image", "inspect", "-f", "{{.Name}}", busyboxImage).Stdout()
c.Assert(output, check.Equals, fmt.Sprintf("%s\n", busyboxImage))
}
16 changes: 16 additions & 0 deletions test/cli_network_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"encoding/json"
"runtime"
"strings"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"

"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)
Expand All @@ -26,6 +29,19 @@ func (suite *PouchNetworkSuite) SetUpSuite(c *check.C) {
environment.PruneAllContainers(apiClient)
}

// TestNetworkInspectFormat tests the inspect format of network works.
func (suite *PouchNetworkSuite) TestNetworkInspectFormat(c *check.C) {
output := command.PouchRun("network", "inspect", "bridge").Stdout()
result := &types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}

// inspect network name
output = command.PouchRun("network", "inspect", "-f", "{{.Name}}", "bridge").Stdout()
c.Assert(output, check.Equals, "bridge\n")
}

// TestNetworkDefault tests the creation of default bridge/none/host network.
func (suite *PouchNetworkSuite) TestNetworkDefault(c *check.C) {
pc, _, _, _ := runtime.Caller(0)
Expand Down
28 changes: 28 additions & 0 deletions test/cli_volume_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package main

import (
"encoding/json"
"fmt"
"runtime"
"strings"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"

"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)
Expand Down Expand Up @@ -156,3 +160,27 @@ func (suite *PouchVolumeSuite) TestVolumeCreateWithSelector(c *check.C) {
command.PouchRun("volume", "create", "--name", funcname, "--selector", "test=foo").Assert(c, icmd.Success)
command.PouchRun("volume", "remove", funcname)
}

// TestVolumeInspectFormat tests the inspect format of volume works.
func (suite *PouchNetworkSuite) TestVolumeInspectFormat(c *check.C) {
pc, _, _, _ := runtime.Caller(0)
tmpname := strings.Split(runtime.FuncForPC(pc).Name(), ".")
var funcname string
for i := range tmpname {
funcname = tmpname[i]
}

command.PouchRun("volume", "create", "--name", funcname).Assert(c, icmd.Success)

output := command.PouchRun("volume", "inspect", funcname).Stdout()
result := &types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}

// inspect network name
output = command.PouchRun("volume", "inspect", "-f", "{{.Name}}", funcname).Stdout()
c.Assert(output, check.Equals, fmt.Sprintf("%s\n", funcname))

defer command.PouchRun("volume", "remove", funcname)
}