Replace find with native Go code

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2021-12-07 10:46:25 +00:00
parent 22dc60e059
commit 77a321a073
3 changed files with 44 additions and 32 deletions

View File

@ -18,13 +18,15 @@ package integration
import ( import (
"fmt" "fmt"
"io/ioutil"
"os"
"path/filepath"
goruntime "runtime" goruntime "runtime"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
exec "golang.org/x/sys/execabs"
) )
const ( const (
@ -69,12 +71,14 @@ func TestVolumeCopyUp(t *testing.T) {
assert.Equal(t, "test_content\n", string(stdout)) assert.Equal(t, "test_content\n", string(stdout))
t.Logf("Check host path of the volume") t.Logf("Check host path of the volume")
// Windows paths might have spaces in them (e.g.: Program Files), which would volumePaths, err := getHostPathForVolumes(*criRoot, cn)
// cause issues for this command. This will allow us to bypass them.
hostCmd := fmt.Sprintf("find '%s/containers/%s/volumes/' -type f -print0 | xargs -0 cat", *criRoot, cn)
output, err := exec.Command("sh", "-c", hostCmd).CombinedOutput()
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "test_content\n", string(output)) assert.Equal(t, len(volumePaths), 1, "expected exactly 1 volume")
testFilePath := filepath.Join(volumePaths[0], "test_file")
contents, err := ioutil.ReadFile(testFilePath)
require.NoError(t, err)
assert.Equal(t, "test_content\n", string(contents))
t.Logf("Update volume from inside the container") t.Logf("Update volume from inside the container")
_, _, err = runtimeService.ExecSync(cn, []string{ _, _, err = runtimeService.ExecSync(cn, []string{
@ -85,9 +89,9 @@ func TestVolumeCopyUp(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
t.Logf("Check whether host path of the volume is updated") t.Logf("Check whether host path of the volume is updated")
output, err = exec.Command("sh", "-c", hostCmd).CombinedOutput() contents, err = ioutil.ReadFile(testFilePath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "new_content\n", string(output)) assert.Equal(t, "new_content\n", string(contents))
} }
func TestVolumeOwnership(t *testing.T) { func TestVolumeOwnership(t *testing.T) {
@ -140,7 +144,34 @@ func TestVolumeOwnership(t *testing.T) {
assert.Equal(t, expectedContainerOutput, string(stdout)) assert.Equal(t, expectedContainerOutput, string(stdout))
t.Logf("Check ownership of test directory on the host") t.Logf("Check ownership of test directory on the host")
output, err := getVolumeHostPathOwnership(*criRoot, cn) volumePaths, err := getHostPathForVolumes(*criRoot, cn)
require.NoError(t, err)
assert.Equal(t, len(volumePaths), 1, "expected exactly 1 volume")
output, err := getOwnership(volumePaths[0])
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, expectedHostOutput, output) assert.Equal(t, expectedHostOutput, output)
} }
func getHostPathForVolumes(criRoot, containerID string) ([]string, error) {
hostPath := filepath.Join(criRoot, "containers", containerID, "volumes")
if _, err := os.Stat(hostPath); err != nil {
return nil, err
}
volumes, err := ioutil.ReadDir(hostPath)
if err != nil {
return nil, err
}
if len(volumes) == 0 {
return []string{}, nil
}
volumePaths := make([]string, len(volumes))
for idx, volume := range volumes {
volumePaths[idx] = filepath.Join(hostPath, volume.Name())
}
return volumePaths, nil
}

View File

@ -25,8 +25,8 @@ import (
exec "golang.org/x/sys/execabs" exec "golang.org/x/sys/execabs"
) )
func getVolumeHostPathOwnership(criRoot, containerID string) (string, error) { func getOwnership(path string) (string, error) {
hostCmd := fmt.Sprintf("find %s/containers/%s/volumes/* | xargs stat -c %%U:%%G", criRoot, containerID) hostCmd := fmt.Sprintf("stat -c %%U:%%G '%s'", path)
output, err := exec.Command("sh", "-c", hostCmd).CombinedOutput() output, err := exec.Command("sh", "-c", hostCmd).CombinedOutput()
if err != nil { if err != nil {
return "", err return "", err

View File

@ -20,31 +20,12 @@
package integration package integration
import ( import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
func getVolumeHostPathOwnership(criRoot, containerID string) (string, error) { func getOwnership(path string) (string, error) {
hostPath := fmt.Sprintf("%s/containers/%s/volumes/", criRoot, containerID)
if _, err := os.Stat(hostPath); err != nil {
return "", err
}
volumes, err := ioutil.ReadDir(hostPath)
if err != nil {
return "", err
}
if len(volumes) != 1 {
return "", fmt.Errorf("expected to find exactly 1 volume (got %d)", len(volumes))
}
secInfo, err := windows.GetNamedSecurityInfo( secInfo, err := windows.GetNamedSecurityInfo(
filepath.Join(hostPath, volumes[0].Name()), windows.SE_FILE_OBJECT, path, windows.SE_FILE_OBJECT,
windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION) windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION)
if err != nil { if err != nil {