Skip to content

Commit

Permalink
Quote further, add test
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Aug 7, 2024
1 parent a75aaf2 commit 2dcd432
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion os/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (c Linux) CleanupServiceEnvironment(h Host, s string) error {

// CommandExist returns true if the command exists
func (c Linux) CommandExist(h Host, cmd string) bool {
return h.Execf(`/bin/sh -c 'command -v -- "%s"' 2> /dev/null`, cmd, exec.Sudo(h)) == nil
return h.Execf("/bin/sh -c %s 2> /dev/null", shellescape.Quote(fmt.Sprintf("command -v -- %s", shellescape.Quote(cmd)))) == nil
}

// Reboot executes the reboot command
Expand Down
46 changes: 30 additions & 16 deletions test/rig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type configurer interface {
Touch(rigos.Host, string, time.Time, ...exec.Option) error
MkDir(rigos.Host, string, ...exec.Option) error
Sha256sum(rigos.Host, string, ...exec.Option) (string, error)
CommandExist(rigos.Host, string) bool
}

// Host is a host that utilizes rig for connections
Expand Down Expand Up @@ -244,15 +245,19 @@ type SuiteLogger struct {
func (s *SuiteLogger) Tracef(msg string, args ...interface{}) {
s.t.Log(fmt.Sprintf("%s TRACE %s", time.Now(), fmt.Sprintf(msg, args...)))
}

func (s *SuiteLogger) Debugf(msg string, args ...interface{}) {
s.t.Log(fmt.Sprintf("%s DEBUG %s", time.Now(), fmt.Sprintf(msg, args...)))
}

func (s *SuiteLogger) Infof(msg string, args ...interface{}) {
s.t.Log(fmt.Sprintf("%s INFO %s", time.Now(), fmt.Sprintf(msg, args...)))
}

func (s *SuiteLogger) Warnf(msg string, args ...interface{}) {
s.t.Log(fmt.Sprintf("%s WARN %s", time.Now(), fmt.Sprintf(msg, args...)))
}

func (s *SuiteLogger) Errorf(msg string, args ...interface{}) {
s.t.Log(fmt.Sprintf("%s ERROR %s", time.Now(), fmt.Sprintf(msg, args...)))
}
Expand All @@ -270,7 +275,7 @@ func (s *ConnectedSuite) SetupSuite() {
s.Require().NoError(err)
s.Require().NoError(s.Host.LoadOS())
s.tempDir = "tmp.rig-test." + time.Now().Format("20060102150405")
s.Require().NoError(s.Host.Fsys().MkDirAll(s.tempDir, 0755))
s.Require().NoError(s.Host.Fsys().MkDirAll(s.tempDir, 0o755))
}

func (s *ConnectedSuite) TearDownSuite() {
Expand All @@ -294,6 +299,16 @@ type ConfigurerSuite struct {
ConnectedSuite
}

func (s *ConfigurerSuite) TestCommandExist() {
s.Run("Command exists", func() {
s.True(s.Host.Configurer.CommandExist(s.Host, "ls"))
})

s.Run("Command does not exist", func() {
s.False(s.Host.Configurer.CommandExist(s.Host, "doesnotexist"))
})
}

func (s *ConfigurerSuite) TestStat() {
s.Run("File does not exist", func() {
stat, err := s.Host.Configurer.Stat(s.Host, s.TempPath("doesnotexist"))
Expand Down Expand Up @@ -416,7 +431,6 @@ func (s *ConfigurerSuite) TestUpload() {
s.Require().NoError(err)
s.Equal(hex.EncodeToString(sha.Sum(nil)), sum)
})

})
}
}
Expand Down Expand Up @@ -448,7 +462,7 @@ func (s *FsysSuite) TestMkdir() {
}()
s.Run("Create directory", func() {
s.T().Log("mkdirall")
s.Require().NoError(s.fsys.MkDirAll(testPath, 0755))
s.Require().NoError(s.fsys.MkDirAll(testPath, 0o755))
})
s.Run("Verify directory exists", func() {
s.T().Log("stat")
Expand All @@ -458,18 +472,18 @@ func (s *FsysSuite) TestMkdir() {
if s.Host.IsWindows() {
s.T().Skip("Windows does not support chmod permissions")
}
s.Equal(os.FileMode(0755), stat.Mode().Perm())
s.Equal(os.FileMode(0o755), stat.Mode().Perm())
parent, err := s.fsys.Stat(s.TempPath("test"))
s.Require().NoError(err)
s.Equal(os.FileMode(0755), parent.Mode().Perm())
s.Equal(os.FileMode(0o755), parent.Mode().Perm())
})
})
}

func (s *FsysSuite) TestRemove() {
testPath := s.TempPath("test", "subdir")
s.Run("Create directory", func() {
s.Require().NoError(s.fsys.MkDirAll(testPath, 0755))
s.Require().NoError(s.fsys.MkDirAll(testPath, 0o755))
})
s.Run("Remove directory", func() {
s.Require().NoError(s.fsys.RemoveAll(testPath))
Expand Down Expand Up @@ -509,7 +523,7 @@ func (s *FsysSuite) TestReadWriteFile() {
_ = s.fsys.Remove(fn)
}()
s.Run("Write file", func() {
f, err := s.fsys.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0644)
f, err := s.fsys.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0o644)
s.Require().NoError(err)
n, err := io.Copy(f, reader)
s.Require().NoError(err)
Expand Down Expand Up @@ -564,7 +578,7 @@ func (s *FsysSuite) TestReadWriteFileCopy() {
_ = s.fsys.Remove(fn)
}()
s.Run("Write file", func() {
f, err := s.fsys.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0644)
f, err := s.fsys.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0o644)
s.Require().NoError(err)
n, err := f.CopyFrom(reader)
s.Require().NoError(err)
Expand Down Expand Up @@ -620,7 +634,7 @@ func (s *FsysSuite) TestSeek() {
defer func() {
_ = s.fsys.Remove(fn)
}()
f, err := s.fsys.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0644)
f, err := s.fsys.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0o644)
s.Require().NoError(err)
n, err := io.Copy(f, bytes.NewReader(bytes.Repeat([]byte{'a'}, 1024)))
s.Require().NoError(err)
Expand All @@ -637,7 +651,7 @@ func (s *FsysSuite) TestSeek() {
s.Equal(reference, b)
})
s.Run("Alter file beginning", func() {
f, err := s.fsys.OpenFile(fn, os.O_WRONLY, 0644)
f, err := s.fsys.OpenFile(fn, os.O_WRONLY, 0o644)
s.Require().NoError(err)
np, err := f.Seek(0, io.SeekStart)
s.Require().NoError(err)
Expand All @@ -658,7 +672,7 @@ func (s *FsysSuite) TestSeek() {
s.Equal(reference, b)
})
s.Run("Alter file ending", func() {
f, err := s.fsys.OpenFile(fn, os.O_WRONLY, 0644)
f, err := s.fsys.OpenFile(fn, os.O_WRONLY, 0o644)
s.Require().NoError(err)
np, err := f.Seek(-256, io.SeekEnd)
s.Require().NoError(err)
Expand All @@ -679,7 +693,7 @@ func (s *FsysSuite) TestSeek() {
s.Equal(reference, b)
})
s.Run("Alter file middle", func() {
f, err := s.fsys.OpenFile(fn, os.O_WRONLY, 0644)
f, err := s.fsys.OpenFile(fn, os.O_WRONLY, 0o644)
s.Require().NoError(err)
np, err := f.Seek(256, io.SeekStart)
s.Require().NoError(err)
Expand All @@ -706,12 +720,12 @@ func (s *FsysSuite) TestReadDir() {
_ = s.fsys.RemoveAll(s.TempPath("test"))
}()
s.Run("Create directory", func() {
s.Require().NoError(s.fsys.MkDirAll(s.TempPath("test"), 0755))
s.Require().NoError(s.fsys.MkDirAll(s.TempPath("test"), 0o755))
})
s.Run("Create files", func() {
for _, fn := range []string{s.TempPath("test", "subdir", "nestedfile"), s.TempPath("test", "file")} {
s.Require().NoError(s.fsys.MkDirAll(pathDir(fn), 0755))
f, err := s.fsys.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0644)
s.Require().NoError(s.fsys.MkDirAll(pathDir(fn), 0o755))
f, err := s.fsys.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0o644)
s.Require().NoError(err)
n, err := f.Write([]byte("test"))
s.Require().NoError(err)
Expand All @@ -721,7 +735,7 @@ func (s *FsysSuite) TestReadDir() {
})

s.Run("Read directory", func() {
dir, err := s.fsys.OpenFile(s.TempPath("test"), os.O_RDONLY, 0644)
dir, err := s.fsys.OpenFile(s.TempPath("test"), os.O_RDONLY, 0o644)
s.Require().NoError(err)
s.Require().NotNil(dir)
readDirFile, ok := dir.(fs.ReadDirFile)
Expand Down

0 comments on commit 2dcd432

Please sign in to comment.