本文整理匯總了Golang中github.com/docker/docker/cli/internal/test.NewFakeCli函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewFakeCli函數的具體用法?Golang NewFakeCli怎麽用?Golang NewFakeCli使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewFakeCli函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestNodeRemoveErrors
func TestNodeRemoveErrors(t *testing.T) {
testCases := []struct {
args []string
nodeRemoveFunc func() error
expectedError string
}{
{
expectedError: "requires at least 1 argument",
},
{
args: []string{"nodeID"},
nodeRemoveFunc: func() error {
return fmt.Errorf("error removing the node")
},
expectedError: "error removing the node",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newRemoveCommand(
test.NewFakeCli(&fakeClient{
nodeRemoveFunc: tc.nodeRemoveFunc,
}, buf))
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
示例2: TestSwarmLeaveErrors
func TestSwarmLeaveErrors(t *testing.T) {
testCases := []struct {
name string
args []string
swarmLeaveFunc func() error
expectedError string
}{
{
name: "too-many-args",
args: []string{"foo"},
expectedError: "accepts no argument(s)",
},
{
name: "leave-failed",
swarmLeaveFunc: func() error {
return fmt.Errorf("error leaving the swarm")
},
expectedError: "error leaving the swarm",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newLeaveCommand(
test.NewFakeCli(&fakeClient{
swarmLeaveFunc: tc.swarmLeaveFunc,
}, buf))
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
示例3: TestSwarmLeave
func TestSwarmLeave(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newLeaveCommand(
test.NewFakeCli(&fakeClient{}, buf))
assert.NilError(t, cmd.Execute())
assert.Equal(t, strings.TrimSpace(buf.String()), "Node left the swarm.")
}
示例4: TestNodeUpdateErrors
func TestNodeUpdateErrors(t *testing.T) {
testCases := []struct {
args []string
flags map[string]string
nodeInspectFunc func() (swarm.Node, []byte, error)
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
expectedError string
}{
{
expectedError: "requires exactly 1 argument",
},
{
args: []string{"node1", "node2"},
expectedError: "requires exactly 1 argument",
},
{
args: []string{"nodeID"},
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return swarm.Node{}, []byte{}, fmt.Errorf("error inspecting the node")
},
expectedError: "error inspecting the node",
},
{
args: []string{"nodeID"},
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
return fmt.Errorf("error updating the node")
},
expectedError: "error updating the node",
},
{
args: []string{"nodeID"},
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(NodeLabels(map[string]string{
"key": "value",
})), []byte{}, nil
},
flags: map[string]string{
"label-rm": "notpresent",
},
expectedError: "key notpresent doesn't exist in node's labels",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newUpdateCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc,
}, buf))
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
cmd.SetOutput(ioutil.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
示例5: TestNodeListQuietShouldOnlyPrintIDs
func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newListCommand(
test.NewFakeCli(&fakeClient{
nodeListFunc: func() ([]swarm.Node, error) {
return []swarm.Node{
*Node(),
}, nil
},
}, buf))
cmd.Flags().Set("quiet", "true")
assert.NilError(t, cmd.Execute())
assert.Contains(t, buf.String(), "nodeID")
}
示例6: TestNodePsErrors
func TestNodePsErrors(t *testing.T) {
testCases := []struct {
args []string
flags map[string]string
infoFunc func() (types.Info, error)
nodeInspectFunc func() (swarm.Node, []byte, error)
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
expectedError string
}{
{
infoFunc: func() (types.Info, error) {
return types.Info{}, fmt.Errorf("error asking for node info")
},
expectedError: "error asking for node info",
},
{
args: []string{"nodeID"},
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return swarm.Node{}, []byte{}, fmt.Errorf("error inspecting the node")
},
expectedError: "error inspecting the node",
},
{
args: []string{"nodeID"},
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{}, fmt.Errorf("error returning the task list")
},
expectedError: "error returning the task list",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newPsCommand(
test.NewFakeCli(&fakeClient{
infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc,
taskInspectFunc: tc.taskInspectFunc,
taskListFunc: tc.taskListFunc,
}, buf))
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
cmd.SetOutput(ioutil.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
示例7: TestSwarmInit
func TestSwarmInit(t *testing.T) {
testCases := []struct {
name string
flags map[string]string
swarmInitFunc func() (string, error)
swarmInspectFunc func() (swarm.Swarm, error)
swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error)
nodeInspectFunc func() (swarm.Node, []byte, error)
}{
{
name: "init",
swarmInitFunc: func() (string, error) {
return "nodeID", nil
},
},
{
name: "init-autolock",
flags: map[string]string{
flagAutolock: "true",
},
swarmInitFunc: func() (string, error) {
return "nodeID", nil
},
swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
return types.SwarmUnlockKeyResponse{
UnlockKey: "unlock-key",
}, nil
},
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newInitCommand(
test.NewFakeCli(&fakeClient{
swarmInitFunc: tc.swarmInitFunc,
swarmInspectFunc: tc.swarmInspectFunc,
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
nodeInspectFunc: tc.nodeInspectFunc,
}, buf))
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
assert.NilError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("init-%s.golden", tc.name))
assert.EqualNormalizedString(t, assert.RemoveSpace, actual, string(expected))
}
}
示例8: TestSwarmJoinErrors
func TestSwarmJoinErrors(t *testing.T) {
testCases := []struct {
name string
args []string
swarmJoinFunc func() error
infoFunc func() (types.Info, error)
expectedError string
}{
{
name: "not-enough-args",
expectedError: "requires exactly 1 argument",
},
{
name: "too-many-args",
args: []string{"remote1", "remote2"},
expectedError: "requires exactly 1 argument",
},
{
name: "join-failed",
args: []string{"remote"},
swarmJoinFunc: func() error {
return fmt.Errorf("error joining the swarm")
},
expectedError: "error joining the swarm",
},
{
name: "join-failed-on-init",
args: []string{"remote"},
infoFunc: func() (types.Info, error) {
return types.Info{}, fmt.Errorf("error asking for node info")
},
expectedError: "error asking for node info",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newJoinCommand(
test.NewFakeCli(&fakeClient{
swarmJoinFunc: tc.swarmJoinFunc,
infoFunc: tc.infoFunc,
}, buf))
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
示例9: TestNodeDemoteMultipleNode
func TestNodeDemoteMultipleNode(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newDemoteCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(Manager()), []byte{}, nil
},
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
if node.Role != swarm.NodeRoleWorker {
return fmt.Errorf("expected role worker, got %s", node.Role)
}
return nil
},
}, buf))
cmd.SetArgs([]string{"nodeID1", "nodeID2"})
assert.NilError(t, cmd.Execute())
}
示例10: TestSwarmJoin
func TestSwarmJoin(t *testing.T) {
testCases := []struct {
name string
infoFunc func() (types.Info, error)
expected string
}{
{
name: "join-as-manager",
infoFunc: func() (types.Info, error) {
return types.Info{
Swarm: swarm.Info{
ControlAvailable: true,
},
}, nil
},
expected: "This node joined a swarm as a manager.",
},
{
name: "join-as-worker",
infoFunc: func() (types.Info, error) {
return types.Info{
Swarm: swarm.Info{
ControlAvailable: false,
},
}, nil
},
expected: "This node joined a swarm as a worker.",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newJoinCommand(
test.NewFakeCli(&fakeClient{
infoFunc: tc.infoFunc,
}, buf))
cmd.SetArgs([]string{"remote"})
assert.NilError(t, cmd.Execute())
assert.Equal(t, strings.TrimSpace(buf.String()), tc.expected)
}
}
示例11: TestNodeInspectPretty
func TestNodeInspectPretty(t *testing.T) {
testCases := []struct {
name string
nodeInspectFunc func() (swarm.Node, []byte, error)
}{
{
name: "simple",
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(NodeLabels(map[string]string{
"lbl1": "value1",
})), []byte{}, nil
},
},
{
name: "manager",
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(Manager()), []byte{}, nil
},
},
{
name: "manager-leader",
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(Manager(Leader())), []byte{}, nil
},
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newInspectCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc,
}, buf))
cmd.SetArgs([]string{"nodeID"})
cmd.Flags().Set("pretty", "true")
assert.NilError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("node-inspect-pretty.%s.golden", tc.name))
assert.EqualNormalizedString(t, assert.RemoveSpace, actual, string(expected))
}
}
示例12: TestSwarmUnlock
func TestSwarmUnlock(t *testing.T) {
input := "unlockKey"
buf := new(bytes.Buffer)
dockerCli := test.NewFakeCli(&fakeClient{
infoFunc: func() (types.Info, error) {
return types.Info{
Swarm: swarm.Info{
LocalNodeState: swarm.LocalNodeStateLocked,
},
}, nil
},
swarmUnlockFunc: func(req swarm.UnlockRequest) error {
if req.UnlockKey != input {
return fmt.Errorf("Invalid unlock key")
}
return nil
},
}, buf)
dockerCli.SetIn(ioutil.NopCloser(strings.NewReader(input)))
cmd := newUnlockCommand(dockerCli)
assert.NilError(t, cmd.Execute())
}
示例13: TestNodeListErrorOnAPIFailure
func TestNodeListErrorOnAPIFailure(t *testing.T) {
testCases := []struct {
nodeListFunc func() ([]swarm.Node, error)
infoFunc func() (types.Info, error)
expectedError string
}{
{
nodeListFunc: func() ([]swarm.Node, error) {
return []swarm.Node{}, fmt.Errorf("error listing nodes")
},
expectedError: "error listing nodes",
},
{
nodeListFunc: func() ([]swarm.Node, error) {
return []swarm.Node{
{
ID: "nodeID",
},
}, nil
},
infoFunc: func() (types.Info, error) {
return types.Info{}, fmt.Errorf("error asking for node info")
},
expectedError: "error asking for node info",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newListCommand(
test.NewFakeCli(&fakeClient{
nodeListFunc: tc.nodeListFunc,
infoFunc: tc.infoFunc,
}, buf))
cmd.SetOutput(ioutil.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
示例14: TestNodeDemoteErrors
func TestNodeDemoteErrors(t *testing.T) {
testCases := []struct {
args []string
nodeInspectFunc func() (swarm.Node, []byte, error)
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
expectedError string
}{
{
expectedError: "requires at least 1 argument",
},
{
args: []string{"nodeID"},
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return swarm.Node{}, []byte{}, fmt.Errorf("error inspecting the node")
},
expectedError: "error inspecting the node",
},
{
args: []string{"nodeID"},
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
return fmt.Errorf("error updating the node")
},
expectedError: "error updating the node",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newDemoteCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc,
}, buf))
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
示例15: TestNodeList
func TestNodeList(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newListCommand(
test.NewFakeCli(&fakeClient{
nodeListFunc: func() ([]swarm.Node, error) {
return []swarm.Node{
*Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())),
*Node(NodeID("nodeID2"), Hostname("nodeHostname2"), Manager()),
*Node(NodeID("nodeID3"), Hostname("nodeHostname3")),
}, nil
},
infoFunc: func() (types.Info, error) {
return types.Info{
Swarm: swarm.Info{
NodeID: "nodeID1",
},
}, nil
},
}, buf))
assert.NilError(t, cmd.Execute())
assert.Contains(t, buf.String(), `nodeID1 * nodeHostname1 Ready Active Leader`)
assert.Contains(t, buf.String(), `nodeID2 nodeHostname2 Ready Active Reachable`)
assert.Contains(t, buf.String(), `nodeID3 nodeHostname3 Ready Active`)
}