func (s *MachineSuite) TestMachineAgentSymlinkJujuRunExists(c *gc.C) { if runtime.GOOS == "windows" { // Cannot make symlink to nonexistent file on windows or // create a file point a symlink to it then remove it c.Skip("Cannot test this on windows") } stm, _, _ := s.primeAgent(c, state.JobManageModel) a := s.newAgent(c, stm) defer a.Stop() // Pre-create the symlinks, but pointing to the incorrect location. links := []string{jujuRun, jujuDumpLogs} a.rootDir = c.MkDir() for _, link := range links { fullLink := utils.EnsureBaseDir(a.rootDir, link) c.Assert(os.MkdirAll(filepath.Dir(fullLink), os.FileMode(0755)), jc.ErrorIsNil) c.Assert(symlink.New("/nowhere/special", fullLink), jc.ErrorIsNil, gc.Commentf(link)) } // Start the agent and wait for it be running. _, done := s.waitForOpenState(c, a) // juju-run symlink should have been recreated. for _, link := range links { fullLink := utils.EnsureBaseDir(a.rootDir, link) linkTarget, err := symlink.Read(fullLink) c.Assert(err, jc.ErrorIsNil) c.Assert(linkTarget, gc.Not(gc.Equals), "/nowhere/special", gc.Commentf(link)) } s.waitStopped(c, state.JobManageModel, a, done) }
func (s *windowsFileSuite) TestEnsureBaseDir(c *gc.C) { c.Assert(utils.EnsureBaseDir(`C:\r`, `C:\a\b`), gc.Equals, `C:\r\a\b`) c.Assert(utils.EnsureBaseDir(`C:\r`, `D:\a\b`), gc.Equals, `C:\r\a\b`) c.Assert(utils.EnsureBaseDir(`C:`, `D:\a\b`), gc.Equals, `C:\a\b`) c.Assert(utils.EnsureBaseDir(`C:`, `\a\b`), gc.Equals, `C:\a\b`) c.Assert(utils.EnsureBaseDir(``, `C:\a\b`), gc.Equals, `C:\a\b`) }
func (a *MachineAgent) removeJujudSymlinks() (errs []error) { for _, link := range []string{jujuRun, jujuDumpLogs} { err := os.Remove(utils.EnsureBaseDir(a.rootDir, link)) if err != nil && !os.IsNotExist(err) { errs = append(errs, errors.Annotatef(err, "failed to remove %s symlink", link)) } } return }
func (s *MachineSuite) TestMachineAgentSymlinks(c *gc.C) { stm, _, _ := s.primeAgent(c, state.JobManageModel) a := s.newAgent(c, stm) defer a.Stop() _, done := s.waitForOpenState(c, a) // Symlinks should have been created for _, link := range []string{jujuRun, jujuDumpLogs} { _, err := os.Stat(utils.EnsureBaseDir(a.rootDir, link)) c.Assert(err, jc.ErrorIsNil, gc.Commentf(link)) } s.waitStopped(c, state.JobManageModel, a, done) }
func (s *MachineSuite) TestMachineAgentUninstall(c *gc.C) { m, ac, _ := s.primeAgent(c, state.JobHostUnits) err := m.EnsureDead() c.Assert(err, jc.ErrorIsNil) a := s.newAgent(c, m) err = runWithTimeout(a) c.Assert(err, jc.ErrorIsNil) // juju-run and juju-dumplogs symlinks should have been removed on // termination. for _, link := range []string{jujuRun, jujuDumpLogs} { _, err = os.Stat(utils.EnsureBaseDir(a.rootDir, link)) c.Assert(err, jc.Satisfies, os.IsNotExist) } // data-dir should have been removed on termination _, err = os.Stat(ac.DataDir()) c.Assert(err, jc.Satisfies, os.IsNotExist) }
func (a *MachineAgent) createSymlink(target, link string) error { fullLink := utils.EnsureBaseDir(a.rootDir, link) currentTarget, err := symlink.Read(fullLink) if err != nil && !os.IsNotExist(err) { return err } else if err == nil { // Link already in place - check it. if currentTarget == target { // Link already points to the right place - nothing to do. return nil } // Link points to the wrong place - delete it. if err := os.Remove(fullLink); err != nil { return err } } if err := os.MkdirAll(filepath.Dir(fullLink), os.FileMode(0755)); err != nil { return err } return symlink.New(target, fullLink) }
func (s *unixFileSuite) TestEnsureBaseDir(c *gc.C) { c.Assert(utils.EnsureBaseDir(`/a`, `/b/c`), gc.Equals, `/a/b/c`) c.Assert(utils.EnsureBaseDir(`/`, `/b/c`), gc.Equals, `/b/c`) c.Assert(utils.EnsureBaseDir(``, `/b/c`), gc.Equals, `/b/c`) }