Ejemplo n.º 1
0
func (s *cmdRegistrationSuite) TestAddUserAndRegister(c *gc.C) {
	// First, add user "bob", and record the "juju register" command
	// that is printed out.
	context := s.run(c, nil, "add-user", "bob", "Bob Dobbs")
	c.Check(testing.Stderr(context), gc.Equals, "")
	stdout := testing.Stdout(context)
	c.Check(stdout, gc.Matches, `
User "Bob Dobbs \(bob\)" added
Please send this command to bob:
    juju register .*

"Bob Dobbs \(bob\)" has not been granted access to any models(.|\n)*
`[1:])
	jujuRegisterCommand := strings.Fields(strings.TrimSpace(
		strings.SplitN(stdout[strings.Index(stdout, "juju register"):], "\n", 2)[0],
	))
	c.Logf("%q", jujuRegisterCommand)

	// Now run the "juju register" command. We need to pass the
	// controller name and password to set, and we need a different
	// file store to mimic a different local OS user.
	userHomeParams := jujutesting.UserHomeParams{Username: "******"}
	s.CreateUserHome(c, &userHomeParams)
	stdin := strings.NewReader("bob-controller\nhunter2\nhunter2\n")
	args := jujuRegisterCommand[1:] // drop the "juju"

	// The expected prompt does not include a warning about the controller
	// name, as this new local user does not have a controller named
	// "kontroll" registered.
	expectedPrompt := `
Enter a name for this controller [kontroll]: 
Enter a new password: 
Confirm password: 

Welcome, bob. You are now logged into "bob-controller".

There are no models available. You can add models with
"juju add-model", or you can ask an administrator or owner
of a model to grant access to that model with "juju grant".

`[1:]

	context = s.run(c, stdin, args...)
	c.Check(testing.Stdout(context), gc.Equals, "")
	c.Check(testing.Stderr(context), gc.Equals, expectedPrompt)

	// Make sure that the saved server details are sufficient to connect
	// to the api server.
	jar, err := cookiejar.New(&cookiejar.Options{
		Filename: cookiejar.DefaultCookieFile(),
	})
	c.Assert(err, jc.ErrorIsNil)
	dialOpts := api.DefaultDialOpts()
	dialOpts.BakeryClient = httpbakery.NewClient()
	dialOpts.BakeryClient.Jar = jar
	accountDetails, err := s.ControllerStore.AccountDetails("bob-controller")
	c.Assert(err, jc.ErrorIsNil)
	api, err := juju.NewAPIConnection(juju.NewAPIConnectionParams{
		Store:          s.ControllerStore,
		ControllerName: "bob-controller",
		AccountDetails: accountDetails,
		DialOpts:       dialOpts,
		OpenAPI:        api.Open,
	})
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(api.Close(), jc.ErrorIsNil)
}
Ejemplo n.º 2
0
func (s *MigrateSuite) SetUpTest(c *gc.C) {
	s.SetInitialFeatureFlags(feature.Migration)
	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)

	s.store = jujuclienttesting.NewMemStore()

	// Define the source controller in the config and set it as the default.
	err := s.store.AddController("source", jujuclient.ControllerDetails{
		ControllerUUID: "eeeeeeee-0bad-400d-8000-4b1d0d06f00d",
		CACert:         "somecert",
	})
	c.Assert(err, jc.ErrorIsNil)
	err = s.store.SetCurrentController("source")
	c.Assert(err, jc.ErrorIsNil)

	// Define an account for the model in the source controller in the config.
	err = s.store.UpdateAccount("source", jujuclient.AccountDetails{
		User: "******",
	})
	c.Assert(err, jc.ErrorIsNil)

	// Define the model to migrate in the config.
	err = s.store.UpdateModel("source", "source@local/model", jujuclient.ModelDetails{
		ModelUUID: modelUUID,
	})
	c.Assert(err, jc.ErrorIsNil)

	// Define the account for the target controller.
	err = s.store.UpdateAccount("target", jujuclient.AccountDetails{
		User:     "******",
		Password: "******",
	})
	c.Assert(err, jc.ErrorIsNil)

	// Define the target controller in the config.
	err = s.store.AddController("target", jujuclient.ControllerDetails{
		ControllerUUID: targetControllerUUID,
		APIEndpoints:   []string{"1.2.3.4:5"},
		CACert:         "cert",
	})
	c.Assert(err, jc.ErrorIsNil)

	s.api = &fakeMigrateAPI{}

	mac0, err := macaroon.New([]byte("secret0"), "id0", "location0")
	c.Assert(err, jc.ErrorIsNil)
	mac1, err := macaroon.New([]byte("secret1"), "id1", "location1")
	c.Assert(err, jc.ErrorIsNil)

	jar, err := cookiejar.New(&cookiejar.Options{
		Filename: cookiejar.DefaultCookieFile(),
	})
	c.Assert(err, jc.ErrorIsNil)

	s.targetControllerAPI = &fakeTargetControllerAPI{
		cookieURL: &url.URL{
			Scheme: "https",
			Host:   "testing.invalid",
			Path:   "/",
		},
		macaroons: []macaroon.Slice{{mac0}},
	}
	addCookie(c, jar, mac0, s.targetControllerAPI.cookieURL)
	addCookie(c, jar, mac1, &url.URL{
		Scheme: "https",
		Host:   "tasting.invalid",
		Path:   "/",
	})

	err = jar.Save()
	c.Assert(err, jc.ErrorIsNil)
}
Ejemplo n.º 3
0
func (s *cmdRegistrationSuite) TestAddUserAndRegister(c *gc.C) {
	// First, add user "bob", and record the "juju register" command
	// that is printed out.

	context := run(c, nil, "add-user", "bob", "Bob Dobbs")
	c.Check(testing.Stderr(context), gc.Equals, "")
	stdout := testing.Stdout(context)
	expectPat := `
User "Bob Dobbs \(bob\)" added
Please send this command to bob:
    juju register (.+)

"Bob Dobbs \(bob\)" has not been granted access to any models(.|\n)*
`[1:]
	c.Assert(stdout, gc.Matches, expectPat)

	arg := regexp.MustCompile("^" + expectPat + "$").FindStringSubmatch(stdout)[1]
	c.Logf("juju register %q", arg)

	// Now run the "juju register" command. We need to pass the
	// controller name and password to set, and we need a different
	// file store to mimic a different local OS user.
	s.CreateUserHome(c, &jujutesting.UserHomeParams{
		Username: "******",
	})

	// The expected prompt does not include a warning about the controller
	// name, as this new local user does not have a controller named
	// "kontroll" registered.
	prompter := cmdtesting.NewSeqPrompter(c, "»", `
Enter a new password: »hunter2

Confirm password: »hunter2

Initial password successfully set for bob.
Enter a name for this controller \[kontroll\]: »bob-controller

Welcome, bob. You are now logged into "bob-controller".

There are no models available. (.|\n)*
`[1:])

	context = run(c, prompter, "register", arg)
	prompter.AssertDone()

	// Make sure that the saved server details are sufficient to connect
	// to the api server.
	jar, err := cookiejar.New(&cookiejar.Options{
		Filename: cookiejar.DefaultCookieFile(),
	})
	c.Assert(err, jc.ErrorIsNil)
	dialOpts := api.DefaultDialOpts()
	dialOpts.BakeryClient = httpbakery.NewClient()
	dialOpts.BakeryClient.Jar = jar
	accountDetails, err := s.ControllerStore.AccountDetails("bob-controller")
	c.Assert(err, jc.ErrorIsNil)
	api, err := juju.NewAPIConnection(juju.NewAPIConnectionParams{
		Store:          s.ControllerStore,
		ControllerName: "bob-controller",
		AccountDetails: accountDetails,
		DialOpts:       dialOpts,
		OpenAPI:        api.Open,
	})
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(api.Close(), jc.ErrorIsNil)
}