gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/hypervisor_amd64_test.go (about)

     1  // Copyright (c) 2019 ARM Limited
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  var dataFlagsFieldWithoutHypervisor = []byte(`
    17  fpu_exception   : yes
    18  cpuid level     : 20
    19  wp              : yes
    20  flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms rtm rdseed adx smap xsaveopt
    21  bugs            :
    22  bogomips        : 4589.35
    23  `)
    24  
    25  var dataFlagsFieldWithHypervisor = []byte(`
    26  fpu_exception   : yes
    27  cpuid level     : 20
    28  wp              : yes
    29  flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms rtm rdseed adx smap xsaveopt
    30  bugs            :
    31  bogomips        : 4589.35
    32  `)
    33  
    34  var dataWithoutFlagsField = []byte(`
    35  fpu_exception   : yes
    36  cpuid level     : 20
    37  wp              : yes
    38  bugs            :
    39  bogomips        : 4589.35
    40  `)
    41  
    42  func TestRunningOnVMM(t *testing.T) {
    43  	var data []testNestedVMMData
    44  
    45  	//file cpuinfo doesn't contain 'hypervisor' flag
    46  	dataNestedVMMFalseSuccessful := testNestedVMMData{
    47  		content:     dataFlagsFieldWithoutHypervisor,
    48  		expectedErr: false,
    49  		expected:    false,
    50  	}
    51  	data = append(data, dataNestedVMMFalseSuccessful)
    52  
    53  	//file cpuinfo contains 'hypervisor' flag
    54  	dataNestedVMMTrueSuccessful := testNestedVMMData{
    55  		content:     dataFlagsFieldWithHypervisor,
    56  		expectedErr: false,
    57  		expected:    true,
    58  	}
    59  	data = append(data, dataNestedVMMTrueSuccessful)
    60  
    61  	//file cpuinfo  doesn't contain field flags
    62  	dataNestedVMMWithoutFlagsField := testNestedVMMData{
    63  		content:     dataWithoutFlagsField,
    64  		expectedErr: true,
    65  		expected:    false,
    66  	}
    67  	data = append(data, dataNestedVMMWithoutFlagsField)
    68  
    69  	genericTestRunningOnVMM(t, data)
    70  }
    71  
    72  func TestRunningOnVMMNotExistingCPUInfoPathFailure(t *testing.T) {
    73  	f, err := ioutil.TempFile("", "cpuinfo")
    74  	assert.NoError(t, err)
    75  
    76  	filePath := f.Name()
    77  
    78  	f.Close()
    79  	os.Remove(filePath)
    80  	_, err = RunningOnVMM(filePath)
    81  	assert.Error(t, err)
    82  }