package system import ( "context" "errors" "os/exec" "testing" ) // A user that does not exist yet is "absent", in the words the host's own runner uses // ("getent exited 2"), not only Go's ("exit status 2"). Matched as text, the runner's wording read as // a user database that did not answer, and the controller's account was never created. func TestAMissingUserIsAbsentInTheRunnersWords(t *testing.T) { for _, words := range []string{"getent exited 2: ", "exit status 2"} { run := func(context.Context, string, ...string) (string, error) { return "", errors.New(words) } _, found, err := LookUpUser(context.Background(), run, "nobody-here") if err != nil || found { t.Errorf("%q: found %v, err %v; want absent", words, found, err) } } run := func(context.Context, string, ...string) (string, error) { return "", errors.New("getent exited 1: ") } if _, _, err := LookUpUser(context.Background(), run, "x"); err == nil { t.Error("a database that failed read as an answer") } } // The real command, through a real exit: getent's code for a key not found. func TestAMissingUserIsAbsentFromTheRealGetent(t *testing.T) { if _, err := exec.LookPath("getent"); err != nil { t.Skip("no getent here") } run := func(ctx context.Context, name string, args ...string) (string, error) { out, err := exec.CommandContext(ctx, name, args...).Output() return string(out), err } _, found, err := LookUpUser(context.Background(), run, "mesh-no-such-user-0b1f") if err != nil || found { t.Errorf("found %v, err %v; want absent", found, err) } }