当前位置: 首页>>代码示例>>Golang>>正文


Golang unit.NewUnit函数代码示例

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


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


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