• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang testing.PatchValue函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/juju/testing.PatchValue函数的典型用法代码示例。如果您正苦于以下问题:Golang PatchValue函数的具体用法?Golang PatchValue怎么用?Golang PatchValue使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了PatchValue函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: SetUpTest

func (s *commonMachineSuite) SetUpTest(c *gc.C) {
	s.agentSuite.SetUpTest(c)
	s.TestSuite.SetUpTest(c)

	os.Remove(jujuRun) // ignore error; may not exist
	// Fake $HOME, and ssh user to avoid touching ~ubuntu/.ssh/authorized_keys.
	fakeHome := coretesting.MakeEmptyFakeHomeWithoutJuju(c)
	s.AddCleanup(func(*gc.C) { fakeHome.Restore() })
	s.PatchValue(&authenticationworker.SSHUser, "")

	testpath := c.MkDir()
	s.PatchEnvPathPrepend(testpath)
	// mock out the start method so we can fake install services without sudo
	fakeCmd(filepath.Join(testpath, "start"))
	fakeCmd(filepath.Join(testpath, "stop"))

	s.PatchValue(&upstart.InitDir, c.MkDir())

	s.singularRecord = &singularRunnerRecord{}
	testing.PatchValue(&newSingularRunner, s.singularRecord.newSingularRunner)
	testing.PatchValue(&peergrouperNew, func(st *state.State) (worker.Worker, error) {
		return newDummyWorker(), nil
	})

	s.fakeEnsureMongo = fakeEnsure{}
	s.PatchValue(&ensureMongoServer, s.fakeEnsureMongo.fakeEnsureMongo)
	s.PatchValue(&maybeInitiateMongoServer, s.fakeEnsureMongo.fakeInitiateMongo)
}
开发者ID:jameinel,项目名称:core,代码行数:28,代码来源:machine_test.go


示例2: PatchHostAndCertPool

func PatchHostAndCertPool(host string, certPool *x509.CertPool) func() {
	restoreHost := testing.PatchValue(&metricsHost, host)
	restoreCertsPool := testing.PatchValue(&metricsCertsPool, certPool)
	return func() {
		restoreHost()
		restoreCertsPool()
	}
}
开发者ID:imoapps,项目名称:juju,代码行数:8,代码来源:export_test.go


示例3: SetUpTest

func (s *agreeSuite) SetUpTest(c *gc.C) {
	s.client = &mockClient{}

	jujutesting.PatchValue(agree.ClientNew, func(...terms.ClientOption) (terms.Client, error) {
		return s.client, nil
	})
}
开发者ID:tasdomas,项目名称:romulus-1,代码行数:7,代码来源:agree_test.go


示例4: TestDeployCharmsEndpointNotImplemented

func (s *DeploySuite) TestDeployCharmsEndpointNotImplemented(c *gc.C) {
	setter := &testMetricCredentialsSetter{
		assert: func(serviceName string, data []byte) {},
		err: &params.Error{
			Message: "IsMetered",
			Code:    params.CodeNotImplemented,
		},
	}
	cleanup := jujutesting.PatchValue(&getMetricCredentialsAPI, func(_ api.Connection) (metricCredentialsAPI, error) {
		return setter, nil
	})
	defer cleanup()

	stub := &jujutesting.Stub{}
	handler := &testMetricsRegistrationHandler{Stub: stub}
	server := httptest.NewServer(handler)
	defer server.Close()

	testcharms.Repo.ClonedDirPath(s.SeriesPath, "metered")
	deploy := &DeployCommand{Steps: []DeployStep{&RegisterMeteredCharm{RegisterURL: server.URL, QueryURL: server.URL}}}
	_, err := coretesting.RunCommand(c, envcmd.Wrap(deploy), "local:quantal/metered-1", "--plan", "someplan")

	c.Assert(err, jc.ErrorIsNil)
	c.Check(c.GetTestLog(), jc.Contains, "current state server version does not support charm metering")
}
开发者ID:imoapps,项目名称:juju,代码行数:25,代码来源:deploy_test.go


示例5: TestAddMetricCredentialsHttp

func (s *DeploySuite) TestAddMetricCredentialsHttp(c *gc.C) {
	handler := &testMetricsRegistrationHandler{}
	server := httptest.NewServer(handler)
	defer server.Close()

	var called bool
	setter := &testMetricCredentialsSetter{
		assert: func(serviceName string, data []byte) {
			called = true
			c.Assert(serviceName, gc.DeepEquals, "metered")
			var b []byte
			err := json.Unmarshal(data, &b)
			c.Assert(err, gc.IsNil)
			c.Assert(string(b), gc.Equals, "hello registration")
		},
	}

	cleanup := jujutesting.PatchValue(&getMetricCredentialsAPI, func(_ api.Connection) (metricCredentialsAPI, error) {
		return setter, nil
	})
	defer cleanup()

	testcharms.Repo.ClonedDirPath(s.SeriesPath, "metered")
	_, err := coretesting.RunCommand(c, envcmd.Wrap(&deployCommand{RegisterURL: server.URL}), "local:quantal/metered-1")
	c.Assert(err, jc.ErrorIsNil)
	curl := charm.MustParseURL("local:quantal/metered-1")
	s.AssertService(c, "metered", curl, 1, 0)
	c.Assert(called, jc.IsTrue)

	c.Assert(handler.registrationCalls, gc.HasLen, 1)
	c.Assert(handler.registrationCalls[0].CharmURL, gc.DeepEquals, "local:quantal/metered-1")
	c.Assert(handler.registrationCalls[0].ServiceName, gc.DeepEquals, "metered")
}
开发者ID:snailwalker,项目名称:juju,代码行数:33,代码来源:deploy_test.go


示例6: TestSetInt

func (*PatchValueSuite) TestSetInt(c *gc.C) {
	i := 99
	restore := testing.PatchValue(&i, 88)
	c.Assert(i, gc.Equals, 88)
	restore()
	c.Assert(i, gc.Equals, 99)
}
开发者ID:howbazaar,项目名称:testing,代码行数:7,代码来源:patch_test.go


示例7: SetUpSuite

func (s *storageSuite) SetUpSuite(c *gc.C) {
	s.LoggingSuite.SetUpSuite(c)

	var err error
	flockBin, err = exec.LookPath("flock")
	c.Assert(err, gc.IsNil)

	s.bin = c.MkDir()
	s.PatchEnvPathPrepend(s.bin)

	// Create a "sudo" command which shifts away the "-n", sets
	// SUDO_UID/SUDO_GID, and executes the remaining args.
	err = ioutil.WriteFile(filepath.Join(s.bin, "sudo"), []byte(
		"#!/bin/sh\nshift; export SUDO_UID=`id -u` SUDO_GID=`id -g`; exec \"[email protected]\"",
	), 0755)
	c.Assert(err, gc.IsNil)
	restoreSshCommand := testing.PatchValue(&sshCommand, func(host string, command ...string) *ssh.Cmd {
		return s.sshCommand(c, host, command...)
	})
	s.AddSuiteCleanup(func(*gc.C) { restoreSshCommand() })

	// Create a new "flock" which calls the original, but in non-blocking mode.
	data := []byte(fmt.Sprintf("#!/bin/sh\nexec %s --nonblock \"[email protected]\"", flockBin))
	err = ioutil.WriteFile(filepath.Join(s.bin, "flock"), data, 0755)
	c.Assert(err, gc.IsNil)
}
开发者ID:jameinel,项目名称:core,代码行数:26,代码来源:storage_test.go


示例8: checkArgs

func checkArgs(c *gc.C, args *CheckArgs, facade string, version int, id, method string, inArgs, outResults interface{}) {
	if args == nil {
		c.Logf("checkArgs: args is nil!")
		return
	} else {
		if args.Facade != "" {
			c.Check(facade, gc.Equals, args.Facade)
		}
		if args.Version != 0 {
			c.Check(version, gc.Equals, args.Version)
		} else if args.VersionIsZero {
			c.Check(version, gc.Equals, 0)
		}
		if args.Id != "" {
			c.Check(id, gc.Equals, args.Id)
		} else if args.IdIsEmpty {
			c.Check(id, gc.Equals, "")
		}
		if args.Method != "" {
			c.Check(method, gc.Equals, args.Method)
		}
		if args.Args != nil {
			c.Check(inArgs, jc.DeepEquals, args.Args)
		}
		if args.Results != nil {
			c.Check(outResults, gc.NotNil)
			testing.PatchValue(outResults, args.Results)
		}
	}
}
开发者ID:felicianotech,项目名称:juju,代码行数:30,代码来源:apicaller.go


示例9: patchAPIClient

func (s *BaseActionSuite) patchAPIClient(client *fakeAPIClient) func() {
	return jujutesting.PatchValue(action.NewActionAPIClient,
		func(c *action.ActionCommandBase) (action.APIClient, error) {
			return client, nil
		},
	)
}
开发者ID:pmatulis,项目名称:juju,代码行数:7,代码来源:package_test.go


示例10: SetUpTest

func (s *senderSuite) SetUpTest(c *gc.C) {
	s.spoolDir = c.MkDir()
	s.socketDir = c.MkDir()

	s.metricfactory = &stubMetricFactory{
		&testing.Stub{},
		s.spoolDir,
	}

	declaredMetrics := map[string]corecharm.Metric{
		"pings": corecharm.Metric{Description: "test pings", Type: corecharm.MetricTypeAbsolute},
	}
	recorder, err := s.metricfactory.Recorder(declaredMetrics, "local:trusty/testcharm", "testcharm/0")
	c.Assert(err, jc.ErrorIsNil)

	err = recorder.AddMetric("pings", "50", time.Now())
	c.Assert(err, jc.ErrorIsNil)

	err = recorder.Close()
	c.Assert(err, jc.ErrorIsNil)

	reader, err := s.metricfactory.Reader()
	c.Assert(err, jc.ErrorIsNil)
	batches, err := reader.Read()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(batches, gc.HasLen, 1)

	testing.PatchValue(sender.SocketName, func(_, _ string) string {
		return sockPath(c)
	})
}
开发者ID:exekias,项目名称:juju,代码行数:31,代码来源:sender_test.go


示例11: mockAPICaller

func mockAPICaller(c *gc.C, callNumber *int32, apiCalls ...apiCall) apitesting.APICallerFunc {
	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
		switch objType {
		case "NotifyWatcher":
			return nil
		case "Uniter":
			index := int(atomic.AddInt32(callNumber, 1)) - 1
			c.Check(index < len(apiCalls), jc.IsTrue)
			call := apiCalls[index]
			c.Logf("request %d, %s", index, request)
			c.Check(version, gc.Equals, 4)
			c.Check(id, gc.Equals, "")
			c.Check(request, gc.Equals, call.request)
			c.Check(arg, jc.DeepEquals, call.args)
			if call.err != nil {
				return common.ServerError(call.err)
			}
			testing.PatchValue(result, call.result)
		default:
			c.Fail()
		}
		return nil
	})
	return apiCaller
}
开发者ID:kat-co,项目名称:juju,代码行数:25,代码来源:relations_test.go


示例12: DisableFinishBootstrap

// DisableFinishBootstrap disables common.FinishBootstrap so that tests
// do not attempt to SSH to non-existent machines. The result is a function
// that restores finishBootstrap.
func DisableFinishBootstrap() func() {
	f := func(environs.BootstrapContext, ssh.Client, instance.Instance, *cloudinit.MachineConfig) error {
		logger.Warningf("provider/common.FinishBootstrap is disabled")
		return nil
	}
	return testing.PatchValue(&common.FinishBootstrap, f)
}
开发者ID:kapilt,项目名称:juju,代码行数:10,代码来源:bootstrap.go


示例13: SetUpTest

func (s *listAgreementsSuite) SetUpTest(c *gc.C) {
	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
	s.client = &mockClient{}

	jujutesting.PatchValue(listagreements.NewClient, func(_ *httpbakery.Client) (listagreements.TermsServiceClient, error) {
		return s.client, nil
	})
}
开发者ID:alesstimec,项目名称:romulus-1,代码行数:8,代码来源:listagreements_test.go


示例14: TestSetErrorToNil

func (*PatchValueSuite) TestSetErrorToNil(c *gc.C) {
	oldErr := errors.New("foo")
	err := oldErr
	restore := testing.PatchValue(&err, nil)
	c.Assert(err, gc.Equals, nil)
	restore()
	c.Assert(err, gc.Equals, oldErr)
}
开发者ID:howbazaar,项目名称:testing,代码行数:8,代码来源:patch_test.go


示例15: TestSetMapToNil

func (*PatchValueSuite) TestSetMapToNil(c *gc.C) {
	oldMap := map[string]int{"foo": 1234}
	m := oldMap
	restore := testing.PatchValue(&m, nil)
	c.Assert(m, gc.IsNil)
	restore()
	c.Assert(m, gc.DeepEquals, oldMap)
}
开发者ID:howbazaar,项目名称:testing,代码行数:8,代码来源:patch_test.go


示例16: SetUpTest

func (s *agreeSuite) SetUpTest(c *gc.C) {
	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
	s.client = &mockClient{}

	jujutesting.PatchValue(agree.ClientNew, func(...api.ClientOption) (api.Client, error) {
		return s.client, nil
	})
}
开发者ID:bac,项目名称:juju,代码行数:8,代码来源:agree_test.go


示例17: lifeTest

func lifeTest(c *gc.C, stub *testing.Stub, life apiagent.Life, test func() (api.Connection, error)) (api.Connection, error) {
	newFacade := func(apiCaller base.APICaller) (apiagent.ConnFacade, error) {
		c.Check(apiCaller, gc.FitsTypeOf, (*mockConn)(nil))
		return newMockConnFacade(stub, life), nil
	}
	unpatch := testing.PatchValue(apicaller.NewConnFacade, newFacade)
	defer unpatch()
	return test()
}
开发者ID:bac,项目名称:juju,代码行数:9,代码来源:util_test.go


示例18: SetUpSuite

func (s *RsyslogSuite) SetUpSuite(c *gc.C) {
	s.JujuConnSuite.SetUpSuite(c)
	// TODO(waigani) 2014-03-19 bug 1294462
	// Add patch for suite functions
	restore := testing.PatchValue(rsyslog.LookupUser, func(username string) (uid, gid int, err error) {
		// worker will not attempt to chown files if uid/gid is 0
		return 0, 0, nil
	})
	s.AddSuiteCleanup(func(*gc.C) { restore() })
}
开发者ID:ktsakalozos,项目名称:juju,代码行数:10,代码来源:rsyslog_common_test.go


示例19: TestHookCommandNotPowerShellScripts

func (s *WindowsHookSuite) TestHookCommandNotPowerShellScripts(c *gc.C) {
	restorer := envtesting.PatchValue(&version.Current.OS, version.Windows)
	defer restorer()

	cmdhook := "somehook.cmd"
	c.Assert(runner.HookCommand(cmdhook), gc.DeepEquals, []string{cmdhook})

	bathook := "somehook.bat"
	c.Assert(runner.HookCommand(bathook), gc.DeepEquals, []string{bathook})
}
开发者ID:Pankov404,项目名称:juju,代码行数:10,代码来源:args_test.go


示例20: TestWatchActionsNoResults

func (s *unitSuite) TestWatchActionsNoResults(c *gc.C) {
	restore := testing.PatchValue(uniter.Call, func(st *uniter.State, method string, params, results interface{}) error {
		return nil
	})

	defer restore()

	_, err := s.apiUnit.WatchActions()
	c.Assert(err.Error(), gc.Equals, "expected 1 result, got 0")
}
开发者ID:jiasir,项目名称:juju,代码行数:10,代码来源:unit_test.go



注:本文中的github.com/juju/testing.PatchValue函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang testing.CleanupSuite类代码示例发布时间:2022-05-23
下一篇:
Golang testing.PatchExecutableThrowError函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap