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

Golang unit.NewUnit函数代码示例

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

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



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

示例1: TestGetJobsByPeer

// Assert that jobs and their peers are properly indexed
func TestGetJobsByPeer(t *testing.T) {
	state := NewState()

	u1 := unit.NewUnit(`[X-Fleet]
X-ConditionMachineOf=b
X-ConditionMachineOf=c
`)
	j1 := job.NewJob("a", *u1)
	state.TrackJob(j1)

	u2 := unit.NewUnit(`[X-Fleet]
X-ConditionMachineOf=c
`)
	j2 := job.NewJob("d", *u2)
	state.TrackJob(j2)

	peers := state.GetJobsByPeer("b")
	if len(peers) != 1 || peers[0] != "a" {
		t.Fatalf("Unexpected index of job peers %v", peers)
	}

	peers = state.GetJobsByPeer("c")
	if len(peers) != 2 || peers[0] != "a" || peers[1] != "d" {
		t.Fatalf("Unexpected index of job peers %v", peers)
	}
}
开发者ID:rayleyva,项目名称:fleet,代码行数:27,代码来源:state_test.go


示例2: TestSignJob

func TestSignJob(t *testing.T) {
	c, _ := initSign(t)

	u := unit.NewUnit("Echo")
	j := job.NewJob("echo.service", *u)

	data, err := marshal(u)
	if err != nil {
		t.Fatal("marshal error:", err)
	}

	expectedSig, err := c.keyring.Sign(testPublicKeys["rsa"], data)
	if err != nil {
		t.Fatal("sign error:", err)
	}

	s, err := c.SignJob(j)
	if err != nil {
		t.Fatal("sign payload error:", err)
	}
	if s.Tag != TagForJob("echo.service") {
		t.Fatal("sign tag error:", err)
	}

	if len(s.Signatures) != 1 {
		t.Fatal("expect 1 signature instead of", len(s.Signatures))
	}
	if bytes.Compare(s.Signatures[0].Blob, expectedSig.Blob) != 0 {
		t.Fatal("wrong signature")
	}
}
开发者ID:johnmontero,项目名称:fleet,代码行数:31,代码来源:job_test.go


示例3: mapUnitToJob

func mapUnitToJob(entity *schema.Unit, mm map[string]*machine.MachineState) (*job.Job, error) {
	contents, err := base64.StdEncoding.DecodeString(entity.FileContents)
	if err != nil {
		return nil, err
	}
	u, err := unit.NewUnit(string(contents))
	if err != nil {
		return nil, err
	}

	js := job.JobState(entity.CurrentState)
	j := job.Job{
		Name:  entity.Name,
		State: &js,
		Unit:  *u,
	}

	// populate a UnitState object only if the entity
	// is actually reporting relevant data
	if entity.Systemd != nil {
		j.UnitState = &unit.UnitState{
			LoadState:   entity.Systemd.LoadState,
			ActiveState: entity.Systemd.ActiveState,
			SubState:    entity.Systemd.SubState,
		}
		if len(entity.Systemd.MachineID) > 0 {
			j.UnitState.MachineID = entity.Systemd.MachineID
		}
	}

	return &j, nil
}
开发者ID:BillTheBest,项目名称:fleet,代码行数:32,代码来源:http.go


示例4: getUnitByHash

// getUnitByHash retrieves from the Registry the Unit associated with the given Hash
func (r *EtcdRegistry) getUnitByHash(hash unit.Hash) *unit.Unit {
	req := etcd.Get{
		Key:       r.hashedUnitPath(hash),
		Recursive: true,
	}
	resp, err := r.etcd.Do(&req)
	if err != nil {
		if isKeyNotFound(err) {
			err = nil
		}
		return nil
	}
	var um unitModel
	if err := unmarshal(resp.Node.Value, &um); err != nil {
		log.Errorf("error unmarshaling Unit(%s): %v", hash, err)
		return nil
	}

	u, err := unit.NewUnit(um.Raw)
	if err != nil {
		log.Errorf("error parsing Unit(%s): %v", hash, err)
		return nil
	}

	return u
}
开发者ID:BillTheBest,项目名称:fleet,代码行数:27,代码来源:unit.go


示例5: TestMarshaling

func TestMarshaling(t *testing.T) {
	units := []string{
		``,

		`[Service]
		ExecStart=/bin/sleep 1`,

		`[Unit]
		Description=Foo

		[Service]
		ExecStart=echo "foo"`,

		`[Path]
		PathExists=/foo`,
	}

	for _, contents := range units {
		u := unit.NewUnit(contents)
		json, err := marshal(u)
		if err != nil {
			t.Error("Error marshaling unit:", err)
		}
		var um unit.Unit
		err = unmarshal(json, &um)
		if err != nil {
			t.Error("Error unmarshaling unit:", err)
		}
		if !reflect.DeepEqual(*u, um) {
			t.Errorf("Unmarshaled unit does not match original!\nOriginal:\n%s\nUnmarshaled:\n%s", *u, um)
		}
	}

}
开发者ID:johnmontero,项目名称:fleet,代码行数:34,代码来源:unit_test.go


示例6: newUnit

func newUnit(t *testing.T, str string) *unit.Unit {
	u, err := unit.NewUnit(str)
	if err != nil {
		t.Fatalf("Unexpected error creating unit from %q: %v", str, err)
	}
	return u
}
开发者ID:JuanCarlosM,项目名称:fleet,代码行数:7,代码来源:job_test.go


示例7: decodeUnitContents

func decodeUnitContents(c string) (*unit.Unit, error) {
	dec, err := base64.StdEncoding.DecodeString(c)
	if err != nil {
		return nil, err
	}

	return unit.NewUnit(string(dec))
}
开发者ID:JuanCarlosM,项目名称:fleet,代码行数:8,代码来源:units.go


示例8: TestFakeRegistryJobLifecycle

func TestFakeRegistryJobLifecycle(t *testing.T) {
	reg := NewFakeRegistry()

	jobs, err := reg.Jobs()
	if err != nil {
		t.Fatalf("Received error while calling Jobs: %v", err)
	}
	if !reflect.DeepEqual([]job.Job{}, jobs) {
		t.Fatalf("Expected no jobs, got %v", jobs)
	}

	u, _ := unit.NewUnit("")
	j1 := job.NewJob("job1.service", *u)
	err = reg.CreateJob(j1)
	if err != nil {
		t.Fatalf("Received error while calling CreateJob: %v", err)
	}

	jobs, err = reg.Jobs()
	if err != nil {
		t.Fatalf("Received error while calling Jobs: %v", err)
	}
	if len(jobs) != 1 {
		t.Fatalf("Expected 1 Job, got %v", jobs)
	}
	if jobs[0].Name != "job1.service" {
		t.Fatalf("Expected Job with name \"job1.service\", got %q", jobs[0].Name)
	}

	err = reg.ScheduleJob("job1.service", "XXX")
	if err != nil {
		t.Fatalf("Received error while calling ScheduleJob: %v", err)
	}

	j, err := reg.Job("job1.service")
	if err != nil {
		t.Fatalf("Received error while calling JobTarget: %v", err)
	}
	if j.TargetMachineID != "XXX" {
		t.Fatalf("Job should be scheduled to XXX, got %v", j.TargetMachineID)
	}

	err = reg.DestroyJob("job1.service")
	if err != nil {
		t.Fatalf("Received error while calling DestroyJob: %v", err)
	}

	jobs, err = reg.Jobs()
	if err != nil {
		t.Fatalf("Received error while calling Jobs: %v", err)
	}
	if !reflect.DeepEqual([]job.Job{}, jobs) {
		t.Fatalf("Expected no jobs, got %v", jobs)
	}
}
开发者ID:Jitendrakry,项目名称:fleet,代码行数:55,代码来源:fake_test.go


示例9: newNamedTestJobFromUnitContents

func newNamedTestJobFromUnitContents(t *testing.T, name, contents string) *job.Job {
	u, err := unit.NewUnit(contents)
	if err != nil {
		t.Fatalf("error creating Unit from %q: %v", contents, err)
	}
	j := job.NewJob(name, *u)
	if j == nil {
		t.Fatalf("error creating Job %q from %q", name, u)
	}
	return j
}
开发者ID:BillTheBest,项目名称:fleet,代码行数:11,代码来源:list_units_test.go


示例10: getUnitFromFile

// getUnitFromFile attempts to load a Job from a given filename
// It returns the Job or nil, and any error encountered
func getUnitFromFile(file string) (*unit.Unit, error) {
	out, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}

	unitName := path.Base(file)
	log.V(1).Infof("Unit(%s) found in local filesystem", unitName)

	return unit.NewUnit(string(out)), nil
}
开发者ID:paulczar,项目名称:fleet,代码行数:13,代码来源:fleetctl.go


示例11: TestAbleToRunConditionMachineIDMatch

func TestAbleToRunConditionMachineIDMatch(t *testing.T) {
	u := unit.NewUnit(`[X-Fleet]
X-ConditionMachineID=XYZ
`)
	job := job.NewJob("example.service", *u)

	mach := &machine.FakeMachine{machine.MachineState{ID: "XYZ"}}
	agent := Agent{Machine: mach, state: NewState()}
	if !agent.ableToRun(job) {
		t.Fatalf("Agent should be able to run job")
	}
}
开发者ID:paulczar,项目名称:fleet,代码行数:12,代码来源:agent_test.go


示例12: TestAbleToRunConditionMachineIDMismatch

func TestAbleToRunConditionMachineIDMismatch(t *testing.T) {
	u := unit.NewUnit(`[X-Fleet]
X-ConditionMachineID=XYZ
`)
	job := job.NewJob("example.service", *u)

	mach := machine.New(machine.MachineState{ID: "123"})
	agent := Agent{machine: mach, state: NewState()}
	if agent.AbleToRun(job) {
		t.Fatalf("Agent should not be able to run job")
	}
}
开发者ID:rswart,项目名称:fleet,代码行数:12,代码来源:agent_test.go


示例13: fleetUnit

func fleetUnit(t *testing.T, opts ...string) unit.Unit {
	contents := "[X-Fleet]"
	for _, v := range opts {
		contents = fmt.Sprintf("%s\n%s", contents, v)
	}

	u, err := unit.NewUnit(contents)
	if u == nil || err != nil {
		t.Fatalf("Failed creating test unit: unit=%v, err=%v", u, err)
	}

	return *u
}
开发者ID:JuanCarlosM,项目名称:fleet,代码行数:13,代码来源:reconcile_test.go


示例14: TestGetJobsByPeerUnknown

// Assert that no jobs are returned for unknown peers
func TestGetJobsByPeerUnknown(t *testing.T) {
	u := unit.NewUnit(`[X-Fleet]
X-ConditionMachineOf=b
`)
	j := job.NewJob("a", *u)

	state := NewState()
	state.TrackJob(j)

	peers := state.GetJobsByPeer("c")
	if len(peers) != 0 {
		t.Fatalf("Unexpected index of job peers %v", peers)
	}
}
开发者ID:rayleyva,项目名称:fleet,代码行数:15,代码来源:state_test.go


示例15: TestHasConflictNoMatch

// Assert that existing jobs and potential jobs that do not conflict do not
// trigger a match
func TestHasConflictNoMatch(t *testing.T) {
	state := NewState()

	u := unit.NewUnit(`[X-Fleet]`)
	j := job.NewJob("example.service", *u)
	state.TrackJob(j)
	state.SetTargetState(j.Name, job.JobStateLoaded)

	agent := Agent{state: state}

	matched, name := agent.HasConflict("other.service", []string{})
	if matched {
		t.Errorf("Expected no match, but got conflict with %s", name)
	}
}
开发者ID:rswart,项目名称:fleet,代码行数:17,代码来源:agent_test.go


示例16: TestLegacyPayload

func TestLegacyPayload(t *testing.T) {
	unitContents := `
[Service]
ExecStart=/bin/sleep 30000
`[1:]
	legacyPayloadContents := `{"Name":"sleep.service","Unit":{"Contents":{"Service":{"ExecStart":"/bin/sleep 30000"}},"Raw":"[Service]\nExecStart=/bin/sleep 30000\n"}}`
	want := unit.NewUnit(unitContents)
	var ljp LegacyJobPayload
	err := unmarshal(legacyPayloadContents, &ljp)
	if err != nil {
		t.Error("Error unmarshaling legacy payload:", err)
	}
	got := ljp.Unit
	if !reflect.DeepEqual(*want, got) {
		t.Errorf("Unit from legacy payload does not match expected!\nwant:\n%s\ngot:\n%s", *want, got)
	}
}
开发者ID:johnmontero,项目名称:fleet,代码行数:17,代码来源:unit_test.go


示例17: TestHasConflictPotentialMatch

// Assert that a potential conflict is triggered against the existing job name
func TestHasConflictPotentialMatch(t *testing.T) {
	state := NewState()

	u := unit.NewUnit(`[X-Fleet]`)
	j := job.NewJob("example.service", *u)
	state.TrackJob(j)
	state.SetTargetState(j.Name, job.JobStateLoaded)

	agent := Agent{state: state}

	matched, name := agent.HasConflict("other.service", []string{"example.service"})
	if !matched {
		t.Errorf("Expected conflict with 'example.service', no conflict reported")
	} else if name != "example.service" {
		t.Errorf("Expected conflict with 'example.service', but conflict found with %s", name)
	}
}
开发者ID:rswart,项目名称:fleet,代码行数:18,代码来源:agent_test.go


示例18: TestHasConflictIgnoresBids

func TestHasConflictIgnoresBids(t *testing.T) {
	state := NewState()

	u := unit.NewUnit(`[X-Fleet]
X-Conflicts=other.service
`)
	j := job.NewJob("example.service", *u)
	state.TrackJob(j)
	state.TrackBid(j.Name)

	agent := Agent{state: state}

	matched, name := agent.HasConflict("other.service", []string{})
	if matched {
		t.Errorf("Expected no conflict, but got conflict with %s", name)
	}
}
开发者ID:rswart,项目名称:fleet,代码行数:17,代码来源:agent_test.go


示例19: TestVerifyJob

func TestVerifyJob(t *testing.T) {
	c, v := initSign(t)

	u, err := unit.NewUnit("Echo")
	if err != nil {
		t.Fatalf("unexpected error creating new unit: %v", err)
	}
	j := job.NewJob("echo.service", *u)

	data, err := marshal(u)
	if err != nil {
		t.Fatal("marshal error:", err)
	}

	v.pubkeys = append(v.pubkeys, testPublicKeys["rsa"])
	signature, err := c.keyring.Sign(testPublicKeys["rsa"], data)
	if err != nil {
		t.Fatal("sign error:", err)
	}

	ss := &SignatureSet{TagForJob("echo.service"), []*gossh.Signature{signature}}

	ok, err := v.VerifyJob(j, ss)
	if err != nil {
		t.Fatal("error verifying job:", err)
	}
	if !ok {
		t.Fatal("job verification failed")
	}

	ss.Tag = ""
	ok, err = v.VerifyJob(j, ss)
	if err == nil || ok == true {
		t.Fatal("should fail on job verification")
	}

	ok, err = v.VerifyJob(j, nil)
	if err == nil || ok == true {
		t.Fatal("should fail on job verification")
	}
}
开发者ID:BillTheBest,项目名称:fleet,代码行数:41,代码来源:job_test.go


示例20: UnmarshalJSON

func (ljp *LegacyJobPayload) UnmarshalJSON(data []byte) error {
	var ljpm legacyJobPayloadModel
	err := json.Unmarshal(data, &ljpm)
	if err != nil {
		return fmt.Errorf("unable to JSON-deserialize object: %s", err)
	}

	var u *unit.Unit
	if len(ljpm.Unit.Raw) > 0 {
		u, err = unit.NewUnit(ljpm.Unit.Raw)
	} else {
		u, err = unit.NewUnitFromLegacyContents(ljpm.Unit.Contents)
	}
	if err != nil {
		return err
	}

	ljp.Unit = *u
	ljp.Name = ljpm.Name

	return nil
}
开发者ID:BillTheBest,项目名称:fleet,代码行数:22,代码来源:unit.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang unit.NewUnitFile函数代码示例发布时间:2022-05-23
下一篇:
Golang glog.V函数代码示例发布时间: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