github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/virtiofsd_test.go (about)

     1  // Copyright (c) 2019 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"context"
    10  	"io"
    11  	"io/ioutil"
    12  	"os"
    13  	"os/exec"
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestVirtiofsdStart(t *testing.T) {
    20  	assert := assert.New(t)
    21  	type fields struct {
    22  		path       string
    23  		socketPath string
    24  		cache      string
    25  		extraArgs  []string
    26  		sourcePath string
    27  		debug      bool
    28  		PID        int
    29  		ctx        context.Context
    30  	}
    31  
    32  	sourcePath, err := ioutil.TempDir("", "")
    33  	assert.NoError(err)
    34  	defer os.RemoveAll(sourcePath)
    35  
    36  	socketDir, err := ioutil.TempDir("", "")
    37  	assert.NoError(err)
    38  	defer os.RemoveAll(socketDir)
    39  
    40  	socketPath := socketDir + "socket.s"
    41  
    42  	validConfig := fields{
    43  		path:       "/usr/bin/virtiofsd-path",
    44  		socketPath: socketPath,
    45  		sourcePath: sourcePath,
    46  	}
    47  	NoDirectorySocket := validConfig
    48  	NoDirectorySocket.socketPath = "/tmp/path/to/virtiofsd/socket.sock"
    49  
    50  	tests := []struct {
    51  		name    string
    52  		fields  fields
    53  		wantErr bool
    54  	}{
    55  		{"empty config", fields{}, true},
    56  		{"Directory socket does not exist", NoDirectorySocket, true},
    57  		{"valid config", validConfig, false},
    58  	}
    59  	for _, tt := range tests {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			v := &virtiofsd{
    62  				path:       tt.fields.path,
    63  				socketPath: tt.fields.socketPath,
    64  				cache:      tt.fields.cache,
    65  				extraArgs:  tt.fields.extraArgs,
    66  				sourcePath: tt.fields.sourcePath,
    67  				debug:      tt.fields.debug,
    68  				PID:        tt.fields.PID,
    69  				ctx:        tt.fields.ctx,
    70  				//Mock  wait function
    71  				wait: func(runningCmd *exec.Cmd, stderr io.ReadCloser, debug bool) error {
    72  					return nil
    73  				},
    74  			}
    75  			var ctx context.Context
    76  			_, err := v.Start(ctx)
    77  			if (err != nil) != tt.wantErr {
    78  				t.Errorf("virtiofsd.Start() error = %v, wantErr %v", err, tt.wantErr)
    79  				return
    80  			}
    81  		})
    82  	}
    83  }