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

     1  // Copyright (c) 2020 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package config
     7  
     8  import (
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func createPFNFile(assert *assert.Assertions, dir string) string {
    18  	pfnPath := filepath.Join(dir, "pfn")
    19  	file, err := os.Create(pfnPath)
    20  	assert.NoError(err)
    21  	defer file.Close()
    22  
    23  	l, err := file.WriteAt([]byte(pfnSignature), pfnSignatureOffset)
    24  	assert.NoError(err)
    25  	assert.Equal(len(pfnSignature), l)
    26  
    27  	return pfnPath
    28  }
    29  
    30  func TestHasPFNSignature(t *testing.T) {
    31  	assert := assert.New(t)
    32  
    33  	b := hasPFNSignature("/abc/xyz/123/sw")
    34  	assert.False(b)
    35  
    36  	f, err := ioutil.TempFile("", "pfn")
    37  	assert.NoError(err)
    38  	f.Close()
    39  	defer os.Remove(f.Name())
    40  
    41  	b = hasPFNSignature(f.Name())
    42  	assert.False(b)
    43  
    44  	pfnFile := createPFNFile(assert, os.TempDir())
    45  	defer os.Remove(pfnFile)
    46  
    47  	b = hasPFNSignature(pfnFile)
    48  	assert.True(b)
    49  }