func refreshConfig(c *agent.Conf) error { nc, err := agent.ReadConf(c.DataDir, c.StateInfo.EntityName) if err != nil { return err } *c = *nc return nil }
func (s *agentSuite) testAgentPasswordChanging(c *C, ent entity, newAgent func() runner) { conf, err := agent.ReadConf(s.DataDir(), ent.EntityName()) c.Assert(err, IsNil) // Check that it starts initially and changes the password err = ent.SetMongoPassword("initial") c.Assert(err, IsNil) setOldPassword := func(password string) { conf.OldPassword = password err = conf.Write() c.Assert(err, IsNil) } setOldPassword("initial") err = runStop(newAgent()) c.Assert(err, IsNil) // Check that we can no longer gain access with the initial password. info := s.StateInfo(c) info.EntityName = ent.EntityName() info.Password = "******" testOpenState(c, info, state.ErrUnauthorized) // Read the configuration and check that we can connect with it. c.Assert(refreshConfig(conf), IsNil) newPassword := conf.StateInfo.Password testOpenState(c, conf.StateInfo, nil) // Check that it starts again ok err = runStop(newAgent()) c.Assert(err, IsNil) // Change the password in the configuration and check // that it falls back to using the initial password c.Assert(refreshConfig(conf), IsNil) conf.StateInfo.Password = "******" conf.OldPassword = newPassword c.Assert(conf.Write(), IsNil) err = runStop(newAgent()) c.Assert(err, IsNil) // Check that it's changed the password again c.Assert(refreshConfig(conf), IsNil) c.Assert(conf.StateInfo.Password, Not(Equals), "spurious") c.Assert(conf.StateInfo.Password, Not(Equals), newPassword) info.Password = conf.StateInfo.Password testOpenState(c, info, nil) }
func (fix *SimpleToolsFixture) checkUnitInstalled(c *C, name, xName, password string) { entityName := state.UnitEntityName(name) uconfPath, _, toolsDir := fix.paths(entityName, xName) uconfData, err := ioutil.ReadFile(uconfPath) c.Assert(err, IsNil) uconf := string(uconfData) var execLine string for _, line := range strings.Split(uconf, "\n") { if strings.HasPrefix(line, "exec ") { execLine = line break } } if execLine == "" { c.Fatalf("no command found in %s:\n%s", uconfPath, uconf) } logPath := filepath.Join(fix.logDir, entityName+".log") jujudPath := filepath.Join(toolsDir, "jujud") for _, pat := range []string{ "^exec " + jujudPath + " unit ", " --unit-name " + name + " ", " >> " + logPath + " 2>&1$", } { match, err := regexp.MatchString(pat, execLine) c.Assert(err, IsNil) if !match { c.Fatalf("failed to match:\n%s\nin:\n%s", pat, execLine) } } conf, err := agent.ReadConf(fix.dataDir, entityName) c.Assert(err, IsNil) c.Assert(conf, DeepEquals, &agent.Conf{ DataDir: fix.dataDir, OldPassword: password, StateInfo: &state.Info{ Addrs: []string{"s1:123", "s2:123"}, CACert: []byte("test-cert"), EntityName: entityName, }, }) jujudData, err := ioutil.ReadFile(jujudPath) c.Assert(err, IsNil) c.Assert(string(jujudData), Equals, fakeJujud) }
func (c *AgentConf) read(entityName string) error { var err error c.Conf, err = agent.ReadConf(c.dataDir, entityName) return err }
func (suite) TestConfReadWriteCheck(c *C) { d := c.MkDir() dataDir := filepath.Join(d, "data") for i, test := range confTests { c.Logf("test %d; %s", i, test.about) conf := test.conf conf.DataDir = dataDir err := conf.Check() if test.checkErr != "" { c.Assert(err, ErrorMatches, test.checkErr) c.Assert(conf.Write(), ErrorMatches, test.checkErr) cmds, err := conf.WriteCommands() c.Assert(cmds, IsNil) c.Assert(err, ErrorMatches, test.checkErr) continue } c.Assert(err, IsNil) err = os.Mkdir(dataDir, 0777) c.Assert(err, IsNil) err = conf.Write() c.Assert(err, IsNil) info, err := os.Stat(conf.File("agent.conf")) c.Assert(err, IsNil) c.Assert(info.Mode()&os.ModePerm, Equals, os.FileMode(0600)) // Move the configuration file to a different directory // to check that the entity name gets set correctly when // reading. newDir := filepath.Join(dataDir, "agents", "another") err = os.Mkdir(newDir, 0777) c.Assert(err, IsNil) err = os.Rename(conf.File("agent.conf"), filepath.Join(newDir, "agent.conf")) c.Assert(err, IsNil) rconf, err := agent.ReadConf(dataDir, "another") c.Assert(err, IsNil) c.Assert(rconf.StateInfo.EntityName, Equals, "another") if rconf.StateInfo != nil { rconf.StateInfo.EntityName = conf.EntityName() } if rconf.APIInfo != nil { rconf.APIInfo.EntityName = conf.EntityName() } c.Assert(rconf, DeepEquals, &conf) err = os.RemoveAll(dataDir) c.Assert(err, IsNil) // Try the equivalent shell commands. cmds, err := conf.WriteCommands() c.Assert(err, IsNil) for _, cmd := range cmds { out, err := exec.Command("sh", "-c", cmd).CombinedOutput() c.Assert(err, IsNil, Commentf("command %q; output %q", cmd, out)) } info, err = os.Stat(conf.File("agent.conf")) c.Assert(err, IsNil) c.Assert(info.Mode()&os.ModePerm, Equals, os.FileMode(0600)) rconf, err = agent.ReadConf(dataDir, conf.StateInfo.EntityName) c.Assert(err, IsNil) c.Assert(rconf, DeepEquals, &conf) err = os.RemoveAll(dataDir) c.Assert(err, IsNil) } }