Skip to content

Commit

Permalink
smoke: add mount api test case
Browse files Browse the repository at this point in the history
Signed-off-by: fappy1234567 <[email protected]>
  • Loading branch information
fappy1234567 authored and imeoer committed Aug 30, 2024
1 parent 3eb5c7b commit 114ec88
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
42 changes: 42 additions & 0 deletions smoke/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,48 @@ func (a *APIV1TestSuite) TestPrefetch(t *testing.T) {
require.NoError(t, err)
}

func (a *APIV1TestSuite) TestMount(t *testing.T) {

ctx := tool.DefaultContext(t)

ctx.PrepareWorkDir(t)
defer ctx.Destroy(t)

rootFs := texture.MakeLowerLayer(t, filepath.Join(ctx.Env.WorkDir, "rootfs"))

rafs := a.buildLayer(t, ctx, rootFs)

config := tool.NydusdConfig{
NydusdPath: ctx.Binary.Nydusd,
MountPath: ctx.Env.MountDir,
APISockPath: filepath.Join(ctx.Env.WorkDir, "nydusd-api.sock"),
ConfigPath: filepath.Join(ctx.Env.WorkDir, "nydusd-config.fusedev.json"),
}
nydusd, err := tool.NewNydusd(config)
require.NoError(t, err)

err = nydusd.Mount()
require.NoError(t, err)

config.BootstrapPath = rafs
config.MountPath = "/mount"
config.BackendType = "localfs"
config.BackendConfig = fmt.Sprintf(`{"dir": "%s"}`, ctx.Env.BlobDir)
config.BlobCacheDir = ctx.Env.CacheDir
config.CacheType = ctx.Runtime.CacheType
config.CacheCompressed = ctx.Runtime.CacheCompressed
config.RafsMode = ctx.Runtime.RafsMode
config.EnablePrefetch = ctx.Runtime.EnablePrefetch
config.DigestValidate = false
config.AmplifyIO = ctx.Runtime.AmplifyIO
err = nydusd.MountByAPI(config)
require.NoError(t, err)

defer nydusd.Umount()
defer nydusd.UmountByAPI(config.MountPath)
nydusd.VerifyByPath(t, rootFs.FileTree, config.MountPath)
}

func (a *APIV1TestSuite) buildLayer(t *testing.T, ctx *tool.Context, rootFs *tool.Layer) string {
digest := rootFs.Pack(t,
converter.PackOption{
Expand Down
48 changes: 30 additions & 18 deletions smoke/tests/tool/nydusd.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,13 @@ func (nydusd *Nydusd) Mount() error {
}

func (nydusd *Nydusd) MountByAPI(config NydusdConfig) error {
err := makeConfig(NydusdConfigTpl, config)
if err != nil {
return err
}
f, err := os.Open(config.ConfigPath)
if err != nil {
return err
}
defer f.Close()
rafsConfig, err := io.ReadAll(f)
if err != nil {
return err
tpl := template.Must(template.New("").Parse(configTpl))

var ret bytes.Buffer
if err := tpl.Execute(&ret, config); err != nil {
return errors.New("prepare config template for Nydusd")
}
rafsConfig := ret.String()

nydusdConfig := struct {
Bootstrap string `json:"source"`
Expand All @@ -336,7 +330,7 @@ func (nydusd *Nydusd) MountByAPI(config NydusdConfig) error {
PrefetchFiles []string `json:"prefetch_files"`
}{
Bootstrap: config.BootstrapPath,
RafsConfig: string(rafsConfig),
RafsConfig: rafsConfig,
FsType: "rafs",
PrefetchFiles: config.PrefetchFiles,
}
Expand All @@ -352,6 +346,7 @@ func (nydusd *Nydusd) MountByAPI(config NydusdConfig) error {
)

return err

}

func (nydusd *Nydusd) Umount() error {
Expand All @@ -365,6 +360,21 @@ func (nydusd *Nydusd) Umount() error {
return nil
}

func (nydusd *Nydusd) UmountByAPI(subPath string) error {
url := fmt.Sprintf("http://unix/api/v1/mount?mountpoint=%s", subPath)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
resp, err := nydusd.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

return nil
}

func (nydusd *Nydusd) WaitStatus(states ...string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
Expand Down Expand Up @@ -622,17 +632,19 @@ func (nydusd *Nydusd) GetInflightMetrics() (*InflightMetrics, error) {
}

func (nydusd *Nydusd) Verify(t *testing.T, expectedFileTree map[string]*File) {
nydusd.VerifyByPath(t, expectedFileTree, "")
}

func (nydusd *Nydusd) VerifyByPath(t *testing.T, expectedFileTree map[string]*File, subPath string) {
actualFiles := map[string]*File{}
err := filepath.WalkDir(nydusd.MountPath, func(path string, _ fs.DirEntry, err error) error {
mountPath := filepath.Join(nydusd.MountPath, subPath)
err := filepath.WalkDir(mountPath, func(path string, _ fs.DirEntry, err error) error {
require.Nil(t, err)

targetPath, err := filepath.Rel(nydusd.MountPath, path)
targetPath, err := filepath.Rel(mountPath, path)
require.NoError(t, err)

if targetPath == "." || targetPath == ".." {
return nil
}

file := NewFile(t, path, targetPath)
actualFiles[targetPath] = file
if expectedFileTree[targetPath] != nil {
Expand Down

0 comments on commit 114ec88

Please sign in to comment.