func newFakeRegistryForSsh() registry.Registry { machines := []machine.MachineState{ {"c31e44e1-f858-436e-933e-59c642517860", "1.2.3.4", map[string]string{"ping": "pong"}, "", resource.ResourceTuple{}}, {"595989bb-cbb7-49ce-8726-722d6e157b4e", "5.6.7.8", map[string]string{"foo": "bar"}, "", resource.ResourceTuple{}}, {"hello.service", "8.7.6.5", map[string]string{"foo": "bar"}, "", resource.ResourceTuple{}}, } jobs := []job.Job{ *job.NewJob("j1.service", unit.Unit{}), *job.NewJob("j2.service", unit.Unit{}), *job.NewJob("hello.service", unit.Unit{}), } states := map[string]*unit.UnitState{ "j1.service": unit.NewUnitState("loaded", "active", "listening", &machines[0]), "j2.service": unit.NewUnitState("loaded", "inactive", "dead", &machines[1]), "hello.service": unit.NewUnitState("loaded", "inactive", "dead", &machines[2]), } reg := registry.NewFakeRegistry() reg.SetMachines(machines) reg.SetUnitStates(states) reg.SetJobs(jobs) return reg }
func setupRegistryForStart(echoAttempts int) { m1 := machine.MachineState{ ID: "c31e44e1-f858-436e-933e-59c642517860", PublicIP: "1.2.3.4", Metadata: map[string]string{"ping": "pong"}, } m2 := machine.MachineState{ ID: "595989bb-cbb7-49ce-8726-722d6e157b4e", PublicIP: "5.6.7.8", Metadata: map[string]string{"foo": "bar"}, } m3 := machine.MachineState{ ID: "520983A8-FB9C-4A68-B49C-CED5BB2E9D08", Metadata: map[string]string{"foo": "bar"}, } js := unit.NewUnitState("loaded", "active", "listening", m1.ID) js2 := unit.NewUnitState("loaded", "inactive", "dead", m2.ID) js3 := unit.NewUnitState("loaded", "inactive", "dead", m2.ID) js4 := unit.NewUnitState("loaded", "inactive", "dead", m3.ID) states := map[string]*unit.UnitState{"pong.service": js, "hello.service": js2, "echo.service": js3, "private.service": js4} machines := []machine.MachineState{m1, m2, m3} reg := registry.NewFakeRegistry() reg.SetMachines(machines) reg.SetUnitStates(states) cAPI = &BlockedFakeRegistry{echoAttempts, *reg} }
func newFakeRegistryForSsh() registry.Registry { // clear machineStates for every invocation machineStates = nil machines := []machine.MachineState{ newMachineState("c31e44e1-f858-436e-933e-59c642517860", "1.2.3.4", map[string]string{"ping": "pong"}), newMachineState("595989bb-cbb7-49ce-8726-722d6e157b4e", "5.6.7.8", map[string]string{"foo": "bar"}), newMachineState("hello.service", "8.7.6.5", map[string]string{"foo": "bar"}), } jobs := []job.Job{ *job.NewJob("j1.service", unit.Unit{}), *job.NewJob("j2.service", unit.Unit{}), *job.NewJob("hello.service", unit.Unit{}), } states := map[string]*unit.UnitState{ "j1.service": unit.NewUnitState("loaded", "active", "listening", machines[0].ID), "j2.service": unit.NewUnitState("loaded", "inactive", "dead", machines[1].ID), "hello.service": unit.NewUnitState("loaded", "inactive", "dead", machines[2].ID), } reg := registry.NewFakeRegistry() reg.SetMachines(machines) reg.SetUnitStates(states) reg.SetJobs(jobs) return reg }
func setupRegistryForStart(echoAttempts int) { m1 := machine.MachineState{"c31e44e1-f858-436e-933e-59c642517860", "1.2.3.4", map[string]string{"ping": "pong"}, ""} m2 := machine.MachineState{"595989bb-cbb7-49ce-8726-722d6e157b4e", "5.6.7.8", map[string]string{"foo": "bar"}, ""} m3 := machine.MachineState{"520983A8-FB9C-4A68-B49C-CED5BB2E9D08", "", map[string]string{"foo": "bar"}, ""} js := unit.NewUnitState("loaded", "active", "listening", &m1) js2 := unit.NewUnitState("loaded", "inactive", "dead", &m2) js3 := unit.NewUnitState("loaded", "inactive", "dead", &m2) js4 := unit.NewUnitState("loaded", "inactive", "dead", &m3) states := map[string]*unit.UnitState{"pong.service": js, "hello.service": js2, "echo.service": js3, "private.service": js4} machines := []machine.MachineState{m1, m2, m3} registryCtl = BlockedTestRegistry{echoAttempts, TestRegistry{jobStates: states, machines: machines}} }
// GetUnitState generates a UnitState object representing the // current state of a Unit func (m *SystemdUnitManager) GetUnitState(name string) (*unit.UnitState, error) { loadState, activeState, subState, err := m.getUnitStates(name) if err != nil { return nil, err } return unit.NewUnitState(loadState, activeState, subState, nil), nil }
// GetUnitState generates a UnitState object representing the // current state of a Job's unit func (m *SystemdManager) GetUnitState(jobName string) (*unit.UnitState, error) { loadState, activeState, subState, err := m.getUnitStates(jobName) if err != nil { return nil, err } ms := m.Machine.State() return unit.NewUnitState(loadState, activeState, subState, &ms), nil }
func translateUnitStatusEvents(changes map[string]*dbus.UnitStatus) []event.Event { events := make([]event.Event, 0) for name, status := range changes { var state *unit.UnitState if status != nil { state = unit.NewUnitState(status.LoadState, status.ActiveState, status.SubState, nil) } ev := event.Event{"EventUnitStateUpdated", state, name} events = append(events, ev) } return events }
func TestListUnitsFieldsToStrings(t *testing.T) { j := newTestJobFromUnitContents(t, "") for _, tt := range []string{"state", "load", "active", "sub", "desc", "machine"} { f := listUnitsFields[tt](j, false) assertEqual(t, tt, "-", f) } f := listUnitsFields["unit"](j, false) assertEqual(t, "unit", j.Name, f) j = newTestJobFromUnitContents(t, `[Unit] Description=some description`) d := listUnitsFields["desc"](j, false) assertEqual(t, "desc", "some description", d) for _, state := range []job.JobState{job.JobStateLoaded, job.JobStateInactive, job.JobStateLaunched} { j.State = &state f := listUnitsFields["state"](j, false) assertEqual(t, "state", string(state), f) } j.UnitState = unit.NewUnitState("foo", "bar", "baz", "") for k, want := range map[string]string{ "load": "foo", "active": "bar", "sub": "baz", "machine": "-", } { got := listUnitsFields[k](j, false) assertEqual(t, k, want, got) } j.UnitState.MachineID = "some-id" ms := listUnitsFields["machine"](j, true) assertEqual(t, "machine", "some-id", ms) j.UnitState.MachineID = "other-id" machineStates = map[string]*machine.MachineState{ "other-id": &machine.MachineState{ ID: "other-id", PublicIP: "1.2.3.4", }, } ms = listUnitsFields["machine"](j, true) assertEqual(t, "machine", "other-id/1.2.3.4", ms) uh := "f035b2f14edc4d23572e5f3d3d4cb4f78d0e53c3" j.UnitState.UnitHash = uh fuh := listUnitsFields["hash"](j, true) suh := listUnitsFields["hash"](j, false) assertEqual(t, "hash", uh, fuh) assertEqual(t, "hash", uh[:7], suh) }
func newTestRegistryForSsh() registry.Registry { machines := []machine.MachineState{ machine.MachineState{"c31e44e1-f858-436e-933e-59c642517860", "1.2.3.4", map[string]string{"ping": "pong"}, ""}, machine.MachineState{"595989bb-cbb7-49ce-8726-722d6e157b4e", "5.6.7.8", map[string]string{"foo": "bar"}, ""}, machine.MachineState{"hello.service", "8.7.6.5", map[string]string{"foo": "bar"}, ""}, } jobs := []job.Job{ *job.NewJob("j1.service", unit.Unit{}), *job.NewJob("j2.service", unit.Unit{}), *job.NewJob("hello.service", unit.Unit{}), } states := map[string]*unit.UnitState{ "j1.service": unit.NewUnitState("loaded", "active", "listening", &machines[0]), "j2.service": unit.NewUnitState("loaded", "inactive", "dead", &machines[1]), "hello.service": unit.NewUnitState("loaded", "inactive", "dead", &machines[2]), } return TestRegistry{machines: machines, jobStates: states, jobs: jobs} }
func TestSaveUnitState(t *testing.T) { e := &testEtcdClient{} r := &EtcdRegistry{etcd: e, keyPrefix: "/fleet/"} j := "foo.service" mID := "mymachine" us := unit.NewUnitState("abc", "def", "ghi", mID) // Saving nil unit state should fail r.SaveUnitState(j, nil, time.Second) if e.sets != nil || e.deletes != nil { t.Logf("sets: %#v", e.sets) t.Logf("deletes: %#v", e.deletes) t.Fatalf("SaveUnitState of nil state should fail but acted unexpectedly!") } // Saving unit state with no hash should succeed for now, but should fail // in the future. See https://github.com/coreos/fleet/issues/720. //r.SaveUnitState(j, us, time.Second) //if len(e.sets) != 1 || e.deletes == nil { // t.Logf("sets: %#v", e.sets) // t.Logf("deletes: %#v", e.deletes) // t.Fatalf("SaveUnitState on UnitState with no hash acted unexpectedly!") //} us.UnitHash = "quickbrownfox" r.SaveUnitState(j, us, time.Second) json := `{"loadState":"abc","activeState":"def","subState":"ghi","machineState":{"ID":"mymachine","PublicIP":"","Metadata":null,"Version":""},"unitHash":"quickbrownfox"}` p1 := "/fleet/state/foo.service" p2 := "/fleet/states/foo.service/mymachine" want := []action{ action{key: p1, val: json}, action{key: p2, val: json}, } got := e.sets if !reflect.DeepEqual(got, want) { t.Errorf("bad result from SaveUnitState: \ngot\n%#v\nwant\n%#v", got, want) } if e.deletes != nil { t.Errorf("unexpected deletes during SaveUnitState: %#v", e.deletes) } if e.gets != nil { t.Errorf("unexpected gets during SaveUnitState: %#v", e.gets) } }
func TestListUnitsFieldsToStrings(t *testing.T) { j := job.NewJob("test", *unit.NewUnit("")) for _, tt := range []string{"state", "load", "active", "sub", "desc", "machine"} { f := listUnitsFields[tt](j, false) assertEqual(t, tt, "-", f) } f := listUnitsFields["unit"](j, false) assertEqual(t, "unit", "test", f) j = job.NewJob("test", *unit.NewUnit(`[Unit] Description=some description`)) d := listUnitsFields["desc"](j, false) assertEqual(t, "desc", "some description", d) for _, state := range []job.JobState{job.JobStateLoaded, job.JobStateInactive, job.JobStateLaunched} { j.State = &state f := listUnitsFields["state"](j, false) assertEqual(t, "state", string(state), f) } j.UnitState = unit.NewUnitState("foo", "bar", "baz", nil) for k, want := range map[string]string{ "load": "foo", "active": "bar", "sub": "baz", "machine": "-", } { got := listUnitsFields[k](j, false) assertEqual(t, k, want, got) } j.UnitState.MachineState = &machine.MachineState{"some-id", "1.2.3.4", nil, "", resource.ResourceTuple{}} ms := listUnitsFields["machine"](j, true) assertEqual(t, "machine", "some-id/1.2.3.4", ms) uh := "f035b2f14edc4d23572e5f3d3d4cb4f78d0e53c3" fuh := listUnitsFields["hash"](j, true) suh := listUnitsFields["hash"](j, false) assertEqual(t, "hash", uh, fuh) assertEqual(t, "hash", uh[:7], suh) }
func TestGetUnitState(t *testing.T) { for i, tt := range []struct { res *etcd.Result // result returned from etcd err error // error returned from etcd us *unit.UnitState }{ { // Unit state with no UnitHash should be OK res: makeResult(`{"loadState":"abc","activeState":"def","subState":"ghi","machineState":{"ID":"mymachine","PublicIP":"","Metadata":null,"Version":"","TotalResources":{"Cores":0,"Memory":0,"Disk":0},"FreeResources":{"Cores":0,"Memory":0,"Disk":0}}}`), err: nil, us: unit.NewUnitState("abc", "def", "ghi", "mymachine"), }, { // Unit state with UnitHash should be OK res: makeResult(`{"loadState":"abc","activeState":"def","subState":"ghi","machineState":{"ID":"mymachine","PublicIP":"","Metadata":null,"Version":"","TotalResources":{"Cores":0,"Memory":0,"Disk":0},"FreeResources":{"Cores":0,"Memory":0,"Disk":0}},"unitHash":"quickbrownfox"}`), err: nil, us: &unit.UnitState{"abc", "def", "ghi", "mymachine", "quickbrownfox"}, }, { // Unit state with no MachineState should be OK res: makeResult(`{"loadState":"abc","activeState":"def","subState":"ghi"}`), err: nil, us: &unit.UnitState{"abc", "def", "ghi", "", ""}, }, { // Bad unit state object should simply result in nil returned res: makeResult(`garbage, not good proper json`), err: nil, us: nil, }, { // Unknown errors should result in nil returned res: nil, err: errors.New("some random error from etcd"), us: nil, }, { // KeyNotFound should result in nil returned res: nil, err: etcd.Error{ErrorCode: etcd.ErrorKeyNotFound}, us: nil, }, } { e := &testEtcdClient{ res: tt.res, err: tt.err, } r := &EtcdRegistry{e, "/fleet/"} j := "foo.service" us := r.getUnitState(j) want := []action{ action{key: "/fleet/state/foo.service", rec: true}, } got := e.gets if !reflect.DeepEqual(got, want) { t.Errorf("case %d: bad result from GetUnitState:\ngot\n%#v\nwant\n%#v", i, got, want) } if !reflect.DeepEqual(us, tt.us) { t.Errorf("case %d: bad UnitState:\ngot\n%#v\nwant\n%#v", i, us, tt.us) } } }