github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/vm/disk/disk_test.go (about)

     1  package disk_test
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/devseccon/trivy/pkg/fanal/vm"
    12  	"github.com/devseccon/trivy/pkg/fanal/vm/disk"
    13  )
    14  
    15  func TestNew(t *testing.T) {
    16  	type args struct {
    17  		rs    io.ReadSeeker
    18  		cache vm.Cache[string, []byte]
    19  	}
    20  	tests := []struct {
    21  		name     string
    22  		fileName string
    23  		wantErr  string
    24  	}{
    25  		{
    26  			name:     "invalid vm file",
    27  			fileName: "testdata/invalid.vmdk",
    28  			wantErr:  "virtual machine can not be detected",
    29  		},
    30  	}
    31  	for _, tt := range tests {
    32  		t.Run(tt.name, func(t *testing.T) {
    33  			f, err := os.Open(tt.fileName)
    34  			require.NoError(t, err)
    35  
    36  			_, err = disk.New(f, nil)
    37  			if err == nil {
    38  				assert.Fail(t, "required error test")
    39  			}
    40  			assert.ErrorContains(t, err, tt.wantErr)
    41  		})
    42  	}
    43  }