當前位置: 首頁>>代碼示例>>Golang>>正文


Golang mockclient.NewMockClient函數代碼示例

本文整理匯總了Golang中github.com/samalba/dockerclient/mockclient.NewMockClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewMockClient函數的具體用法?Golang NewMockClient怎麽用?Golang NewMockClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewMockClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestEngineCpusMemory

func TestEngineCpusMemory(t *testing.T) {
	engine := NewEngine("test", 0, engOpts)
	engine.setState(stateUnhealthy)
	assert.False(t, engine.isConnected())

	client := mockclient.NewMockClient()
	apiClient := engineapimock.NewMockClient()
	apiClient.On("Info", mock.Anything).Return(mockInfo, nil)
	apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil)
	apiClient.On("NetworkList", mock.Anything,
		mock.AnythingOfType("NetworkListOptions"),
	).Return([]types.NetworkResource{}, nil)
	apiClient.On("VolumeList", mock.Anything,
		mock.AnythingOfType("Args"),
	).Return(types.VolumesListResponse{}, nil)
	apiClient.On("ImageList", mock.Anything, mock.AnythingOfType("ImageListOptions")).Return([]types.Image{}, nil)
	apiClient.On("ContainerList", mock.Anything, types.ContainerListOptions{All: true, Size: false}).Return([]types.Container{}, nil)
	apiClient.On("Events", mock.Anything, mock.AnythingOfType("EventsOptions")).Return(&nopCloser{infiniteRead{}}, nil)

	assert.NoError(t, engine.ConnectWithClient(client, apiClient))
	assert.True(t, engine.isConnected())
	assert.True(t, engine.IsHealthy())

	assert.Equal(t, engine.UsedCpus(), int64(0))
	assert.Equal(t, engine.UsedMemory(), int64(0))

	client.Mock.AssertExpectations(t)
	apiClient.Mock.AssertExpectations(t)
}
開發者ID:cbbs,項目名稱:swarm,代碼行數:29,代碼來源:engine_test.go

示例2: TestEngineSpecs

func TestEngineSpecs(t *testing.T) {
	engine := NewEngine("test", 0)
	assert.False(t, engine.isConnected())

	client := mockclient.NewMockClient()
	client.On("Info").Return(mockInfo, nil)
	client.On("Version").Return(mockVersion, nil)
	client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil)
	client.On("ListImages").Return([]*dockerclient.Image{}, nil)
	client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()

	assert.NoError(t, engine.ConnectWithClient(client))
	assert.True(t, engine.isConnected())
	assert.True(t, engine.IsHealthy())

	assert.Equal(t, engine.Cpus, mockInfo.NCPU)
	assert.Equal(t, engine.Memory, mockInfo.MemTotal)
	assert.Equal(t, engine.Labels["storagedriver"], mockInfo.Driver)
	assert.Equal(t, engine.Labels["executiondriver"], mockInfo.ExecutionDriver)
	assert.Equal(t, engine.Labels["kernelversion"], mockInfo.KernelVersion)
	assert.Equal(t, engine.Labels["operatingsystem"], mockInfo.OperatingSystem)
	assert.Equal(t, engine.Labels["foo"], "bar")

	client.Mock.AssertExpectations(t)
}
開發者ID:ealang,項目名稱:swarm,代碼行數:25,代碼來源:engine_test.go

示例3: TestStopContainer_DefaultSuccess

func TestStopContainer_DefaultSuccess(t *testing.T) {
	c := Container{
		containerInfo: &dockerclient.ContainerInfo{
			Name:   "foo",
			Id:     "abc123",
			Config: &dockerclient.ContainerConfig{},
		},
	}

	ci := &dockerclient.ContainerInfo{
		State: &dockerclient.State{
			Running: false,
		},
	}

	api := mockclient.NewMockClient()
	api.On("KillContainer", "abc123", "SIGTERM").Return(nil)
	api.On("InspectContainer", "abc123").Return(ci, nil).Once()
	api.On("RemoveContainer", "abc123", true, false).Return(nil)
	api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("Not Found"))

	client := dockerClient{api: api}
	err := client.StopContainer(c, time.Second)

	assert.NoError(t, err)
	api.AssertExpectations(t)
}
開發者ID:ChrisMcKenzie,項目名稱:watchtower,代碼行數:27,代碼來源:client_test.go

示例4: TestNodeContainerLookup

func TestNodeContainerLookup(t *testing.T) {
	node := NewNode("test-node", 0)
	assert.False(t, node.isConnected())

	client := mockclient.NewMockClient()
	client.On("Info").Return(mockInfo, nil)
	client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()

	client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "container-id", Names: []string{"/container-name1", "/container-name2"}}}, nil).Once()
	client.On("ListImages").Return([]*dockerclient.Image{}, nil).Once()
	client.On("InspectContainer", "container-id").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: 100}}, nil).Once()

	assert.NoError(t, node.connectClient(client))
	assert.True(t, node.isConnected())

	// Invalid lookup
	assert.Nil(t, node.Container("invalid-id"))
	assert.Nil(t, node.Container(""))
	// Container ID lookup.
	assert.NotNil(t, node.Container("container-id"))
	// Container ID prefix lookup.
	assert.NotNil(t, node.Container("container-"))
	// Container name lookup.
	assert.NotNil(t, node.Container("container-name1"))
	assert.NotNil(t, node.Container("container-name2"))
	// Container node/name matching.
	assert.NotNil(t, node.Container("id/container-name1"))
	assert.NotNil(t, node.Container("id/container-name2"))

	client.Mock.AssertExpectations(t)
}
開發者ID:josephwinston,項目名稱:libswarm,代碼行數:31,代碼來源:node_test.go

示例5: TestUsedCpus

func TestUsedCpus(t *testing.T) {
	var (
		containerNcpu = []int64{1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
		hostNcpu      = []int64{1, 2, 4, 8, 10, 12, 16, 20, 32, 36, 40, 48}
	)

	engine := NewEngine("test", 0)
	client := mockclient.NewMockClient()

	for _, hn := range hostNcpu {
		for _, cn := range containerNcpu {
			if cn <= hn {
				mockInfo.NCPU = hn
				cpuShares := int64(math.Ceil(float64(cn*1024) / float64(mockInfo.NCPU)))

				client.On("Info").Return(mockInfo, nil)
				client.On("Version").Return(mockVersion, nil)
				client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
				client.On("ListImages").Return([]*dockerclient.Image{}, nil).Once()
				client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "test"}}, nil).Once()
				client.On("InspectContainer", "test").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: cpuShares}}, nil).Once()
				engine.ConnectWithClient(client)

				assert.Equal(t, engine.UsedCpus(), cn)
			}
		}
	}
}
開發者ID:ealang,項目名稱:swarm,代碼行數:28,代碼來源:engine_test.go

示例6: TestContainerRemovedDuringRefresh

func TestContainerRemovedDuringRefresh(t *testing.T) {
	var (
		container1 = dockerclient.Container{Id: "c1"}
		container2 = dockerclient.Container{Id: "c2"}
		info1      *dockerclient.ContainerInfo
		info2      = &dockerclient.ContainerInfo{Id: "c2", Config: &dockerclient.ContainerConfig{}}
	)

	engine := NewEngine("test", 0)
	assert.False(t, engine.isConnected())

	// A container is removed before it can be inspected.
	client := mockclient.NewMockClient()

	client.On("Info").Return(mockInfo, nil)
	client.On("Version").Return(mockVersion, nil)
	client.On("ListImages").Return([]*dockerclient.Image{}, nil)
	client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
	client.On("ListContainers", true, false, "").Return([]dockerclient.Container{container1, container2}, nil)
	client.On("InspectContainer", "c1").Return(info1, errors.New("Not found"))
	client.On("InspectContainer", "c2").Return(info2, nil)

	assert.NoError(t, engine.ConnectWithClient(client))
	assert.Nil(t, engine.RefreshContainers(true))

	// List of containers is still valid
	containers := engine.Containers()
	assert.Len(t, containers, 1)
	assert.Equal(t, containers[0].Id, "c2")

	client.Mock.AssertExpectations(t)
}
開發者ID:ealang,項目名稱:swarm,代碼行數:32,代碼來源:engine_test.go

示例7: TestEngineSpecs

func TestEngineSpecs(t *testing.T) {
	engine := NewEngine("test", 0, engOpts)
	engine.setState(stateUnhealthy)
	assert.False(t, engine.isConnected())

	client := mockclient.NewMockClient()
	apiClient := engineapimock.NewMockClient()
	apiClient.On("Info", mock.Anything).Return(mockInfo, nil)
	apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil)
	apiClient.On("NetworkList", mock.Anything,
		mock.AnythingOfType("NetworkListOptions"),
	).Return([]types.NetworkResource{}, nil)
	apiClient.On("VolumeList", mock.Anything,
		mock.AnythingOfType("Args"),
	).Return(types.VolumesListResponse{}, nil)
	apiClient.On("ImageList", mock.Anything, mock.AnythingOfType("ImageListOptions")).Return([]types.Image{}, nil)
	apiClient.On("ContainerList", mock.Anything, types.ContainerListOptions{All: true, Size: false}).Return([]types.Container{}, nil)
	apiClient.On("Events", mock.Anything, mock.AnythingOfType("EventsOptions")).Return(&nopCloser{infiniteRead{}}, nil)

	assert.NoError(t, engine.ConnectWithClient(client, apiClient))
	assert.True(t, engine.isConnected())
	assert.True(t, engine.IsHealthy())

	assert.Equal(t, engine.Cpus, int64(mockInfo.NCPU))
	assert.Equal(t, engine.Memory, mockInfo.MemTotal)
	assert.Equal(t, engine.Labels["storagedriver"], mockInfo.Driver)
	assert.Equal(t, engine.Labels["executiondriver"], mockInfo.ExecutionDriver)
	assert.Equal(t, engine.Labels["kernelversion"], mockInfo.KernelVersion)
	assert.Equal(t, engine.Labels["operatingsystem"], mockInfo.OperatingSystem)
	assert.Equal(t, engine.Labels["foo"], "bar")

	client.Mock.AssertExpectations(t)
	apiClient.Mock.AssertExpectations(t)
}
開發者ID:cbbs,項目名稱:swarm,代碼行數:34,代碼來源:engine_test.go

示例8: TestStopContainer_CustomSignalSuccess

func TestStopContainer_CustomSignalSuccess(t *testing.T) {
	c := Container{
		containerInfo: &dockerclient.ContainerInfo{
			Name: "foo",
			Id:   "abc123",
			Config: &dockerclient.ContainerConfig{
				Labels: map[string]string{"com.centurylinklabs.watchtower.stop-signal": "SIGUSR1"}},
		},
	}

	ci := &dockerclient.ContainerInfo{
		State: &dockerclient.State{
			Running: false,
		},
	}

	api := mockclient.NewMockClient()
	api.On("KillContainer", "abc123", "SIGUSR1").Return(nil)
	api.On("InspectContainer", "abc123").Return(ci, nil).Once()
	api.On("RemoveContainer", "abc123", true, false).Return(nil)
	api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("Not Found"))

	client := dockerClient{api: api}
	err := client.StopContainer(c, time.Second)

	assert.NoError(t, err)
	api.AssertExpectations(t)
}
開發者ID:ChrisMcKenzie,項目名稱:watchtower,代碼行數:28,代碼來源:client_test.go

示例9: TestCreateContainer

func TestCreateContainer(t *testing.T) {
	var (
		config = &ContainerConfig{dockerclient.ContainerConfig{
			Image:     "busybox",
			CpuShares: 1,
			Cmd:       []string{"date"},
			Tty:       false,
		}}
		engine = NewEngine("test", 0)
		client = mockclient.NewMockClient()
	)

	client.On("Info").Return(mockInfo, nil)
	client.On("Version").Return(mockVersion, nil)
	client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
	client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once()
	client.On("ListImages").Return([]*dockerclient.Image{}, nil).Once()
	assert.NoError(t, engine.ConnectWithClient(client))
	assert.True(t, engine.isConnected())

	mockConfig := config.ContainerConfig
	mockConfig.CpuShares = int64(math.Ceil(float64(config.CpuShares*1024) / float64(mockInfo.NCPU)))
	mockConfig.HostConfig.CpuShares = mockConfig.CpuShares

	// Everything is ok
	name := "test1"
	id := "id1"
	client.On("CreateContainer", &mockConfig, name).Return(id, nil).Once()
	client.On("ListContainers", true, false, fmt.Sprintf(`{"id":[%q]}`, id)).Return([]dockerclient.Container{{Id: id}}, nil).Once()
	client.On("ListImages").Return([]*dockerclient.Image{}, nil).Once()
	client.On("InspectContainer", id).Return(&dockerclient.ContainerInfo{Config: &config.ContainerConfig}, nil).Once()
	container, err := engine.Create(config, name, false)
	assert.Nil(t, err)
	assert.Equal(t, container.Id, id)
	assert.Len(t, engine.Containers(), 1)

	// Image not found, pullImage == false
	name = "test2"
	mockConfig.CpuShares = int64(math.Ceil(float64(config.CpuShares*1024) / float64(mockInfo.NCPU)))
	client.On("CreateContainer", &mockConfig, name).Return("", dockerclient.ErrNotFound).Once()
	container, err = engine.Create(config, name, false)
	assert.Equal(t, err, dockerclient.ErrNotFound)
	assert.Nil(t, container)

	// Image not found, pullImage == true, and the image can be pulled successfully
	name = "test3"
	id = "id3"
	mockConfig.CpuShares = int64(math.Ceil(float64(config.CpuShares*1024) / float64(mockInfo.NCPU)))
	client.On("PullImage", config.Image+":latest", mock.Anything).Return(nil).Once()
	client.On("CreateContainer", &mockConfig, name).Return("", dockerclient.ErrNotFound).Once()
	client.On("CreateContainer", &mockConfig, name).Return(id, nil).Once()
	client.On("ListContainers", true, false, fmt.Sprintf(`{"id":[%q]}`, id)).Return([]dockerclient.Container{{Id: id}}, nil).Once()
	client.On("ListImages").Return([]*dockerclient.Image{}, nil).Once()
	client.On("InspectContainer", id).Return(&dockerclient.ContainerInfo{Config: &config.ContainerConfig}, nil).Once()
	container, err = engine.Create(config, name, true)
	assert.Nil(t, err)
	assert.Equal(t, container.Id, id)
	assert.Len(t, engine.Containers(), 2)
}
開發者ID:ealang,項目名稱:swarm,代碼行數:59,代碼來源:engine_test.go

示例10: TestResolveImage_WhenImageExisting

func TestResolveImage_WhenImageExisting(t *testing.T) {
	c := mockclient.NewMockClient()
	c.On("InspectImage", "Foo").Return(&dockerclient.ImageInfo{Id: "ytu678"}, nil)
	e := DockerEndpoint{client: c}
	imageID, err := e.ResolveImage("Foo")

	assert.NoError(t, err)
	assert.Equal(t, "ytu678", imageID)
}
開發者ID:rupakg,項目名稱:zodiac,代碼行數:9,代碼來源:endpoint_test.go

示例11: TestDockerEndpointVersion_ErroredClient

func TestDockerEndpointVersion_ErroredClient(t *testing.T) {
	c := mockclient.NewMockClient()
	c.On("Version").Return(&dockerclient.Version{Version: ""}, errors.New("test error"))
	e := DockerEndpoint{client: c}

	v, err := e.Version()
	assert.Empty(t, "", v)
	assert.EqualError(t, err, "test error")
}
開發者ID:rupakg,項目名稱:zodiac,代碼行數:9,代碼來源:endpoint_test.go

示例12: TestOutdatedNode

func TestOutdatedNode(t *testing.T) {
	node := NewNode("test", 0)
	client := mockclient.NewMockClient()
	client.On("Info").Return(&dockerclient.Info{}, nil)

	assert.Error(t, node.connectClient(client))
	assert.False(t, node.IsConnected())

	client.Mock.AssertExpectations(t)
}
開發者ID:SivagnanamCiena,項目名稱:swarm,代碼行數:10,代碼來源:node_test.go

示例13: TestDockerEndpointVersion_Successful

func TestDockerEndpointVersion_Successful(t *testing.T) {
	c := mockclient.NewMockClient()
	c.On("Version").Return(&dockerclient.Version{Version: "1.6.1"}, nil).Once()
	e := DockerEndpoint{client: c}

	v, err := e.Version()
	assert.NoError(t, err)
	assert.Equal(t, "1.6.1", v)
	c.AssertExpectations(t)
}
開發者ID:rupakg,項目名稱:zodiac,代碼行數:10,代碼來源:endpoint_test.go

示例14: TestRemoveContainer_Success

func TestRemoveContainer_Success(t *testing.T) {
	c := mockclient.NewMockClient()
	c.On("RemoveContainer", "foo", true, false).Return(nil)

	e := DockerEndpoint{client: c}
	err := e.RemoveContainer("foo")

	assert.NoError(t, err)
	c.AssertExpectations(t)
}
開發者ID:rupakg,項目名稱:zodiac,代碼行數:10,代碼來源:endpoint_test.go

示例15: TestOutdatedEngine

func TestOutdatedEngine(t *testing.T) {
	engine := NewEngine("test", 0)
	client := mockclient.NewMockClient()
	client.On("Info").Return(&dockerclient.Info{}, nil)

	assert.Error(t, engine.ConnectWithClient(client))
	assert.False(t, engine.isConnected())

	client.Mock.AssertExpectations(t)
}
開發者ID:ealang,項目名稱:swarm,代碼行數:10,代碼來源:engine_test.go


注:本文中的github.com/samalba/dockerclient/mockclient.NewMockClient函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。