Beispiel #1
0
func getUnitStatePage(cAPI client.API, machineID, unitName string, tok PageToken) (*schema.UnitStatePage, error) {
	states, err := cAPI.UnitStates()
	if err != nil {
		return nil, err
	}
	var filtered []*schema.UnitState
	for _, us := range states {
		if machineID != "" && machineID != us.MachineID {
			continue
		}
		if unitName != "" && unitName != us.Name {
			continue
		}
		filtered = append(filtered, us)
	}

	items, next := extractUnitStatePageData(filtered, tok)
	page := schema.UnitStatePage{
		States: items,
	}

	if next != nil {
		page.NextPageToken = next.Encode()
	}

	return &page, nil
}
func TestNullAPI(test *testing.T) {
	var api client.API = new(nullAPI)

	// all the methods return errors, all the time.

	if _, err := api.Machines(); err == nil {
		test.Error("Machines() did not return an error")
	}

	if _, err := api.Unit("foo"); err == nil {
		test.Error("Unit() did not return an error")
	}

	if _, err := api.Units(); err == nil {
		test.Error("Units() did not return an error")
	}

	if _, err := api.UnitStates(); err == nil {
		test.Error("UnitStates() did not return an error")
	}

	if err := api.SetUnitTargetState("foo", "bar"); err == nil {
		test.Error("SetUnitTargetState() did not return an error")
	}

	if err := api.CreateUnit(nil); err == nil {
		test.Error("CreateUnit() did not return an error")
	}

	if err := api.DestroyUnit("foo"); err == nil {
		test.Error("DestroyUnit() did not return an error")
	}
}
func fleetUnitHealthHandler(fleetAPIClient client.API, checker fleetUnitHealthChecker) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		checks := []fthealth.Check{}
		unitStates, err := fleetAPIClient.UnitStates()
		if err != nil {
			panic(err)
		}
		for _, unitState := range unitStates {
			checks = append(checks, newFleetUnitHealthCheck(*unitState, checker))
		}
		fthealth.HandlerParallel("Coco Fleet Unit Healthcheck", "Checks the health of all fleet units", checks...)(w, r)
	}
}
func waitForSystemdState(client client.API, unit string, allowedStates []string) error {
	seenDesiredState := 0
	for {
		states, err := client.UnitStates()
		if err != nil {
			return maskAny(err)
		}

		found := false
		seenDesired := false
		for _, state := range states {
			if state.Name == unit {
				found = true
				for _, allowedState := range allowedStates {
					if allowedState == state.SystemdActiveState {
						seenDesired = true
					}
				}
			}
		}

		if found && !seenDesired {
			seenDesiredState = 0
		} else {
			seenDesiredState++
		}

		if seenDesiredState > 5 {
			return nil
		}

		time.Sleep(500 * time.Millisecond)
	}
	return nil
}