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

     1  // Copyright (c) 2016 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func testSetAgentType(t *testing.T, value string, expected AgentType) {
    15  	var agentType AgentType
    16  	assert := assert.New(t)
    17  
    18  	err := (&agentType).Set(value)
    19  	assert.NoError(err)
    20  	assert.Equal(agentType, expected)
    21  }
    22  
    23  func TestSetNoopAgentType(t *testing.T) {
    24  	testSetAgentType(t, "noop", NoopAgentType)
    25  }
    26  
    27  func TestSetKataAgentType(t *testing.T) {
    28  	testSetAgentType(t, "kata", KataContainersAgent)
    29  }
    30  
    31  func TestSetUnknownAgentType(t *testing.T) {
    32  	var agentType AgentType
    33  	assert := assert.New(t)
    34  
    35  	err := (&agentType).Set("unknown")
    36  	assert.Error(err)
    37  	assert.NotEqual(agentType, NoopAgentType)
    38  }
    39  
    40  func testStringFromAgentType(t *testing.T, agentType AgentType, expected string) {
    41  	agentTypeStr := (&agentType).String()
    42  	assert.Equal(t, agentTypeStr, expected)
    43  }
    44  
    45  func TestStringFromNoopAgentType(t *testing.T) {
    46  	testStringFromAgentType(t, NoopAgentType, "noop")
    47  }
    48  
    49  func TestStringFromKataAgentType(t *testing.T) {
    50  	testStringFromAgentType(t, KataContainersAgent, "kata")
    51  }
    52  
    53  func TestStringFromUnknownAgentType(t *testing.T) {
    54  	var agentType AgentType
    55  	testStringFromAgentType(t, agentType, "")
    56  }
    57  
    58  func testNewAgentFromAgentType(t *testing.T, agentType AgentType, expected agent) {
    59  	ag := newAgent(agentType)
    60  	assert.Exactly(t, ag, expected)
    61  }
    62  
    63  func TestNewAgentFromNoopAgentType(t *testing.T) {
    64  	testNewAgentFromAgentType(t, NoopAgentType, &noopAgent{})
    65  }
    66  
    67  func TestNewAgentFromKataAgentType(t *testing.T) {
    68  	testNewAgentFromAgentType(t, KataContainersAgent, &kataAgent{})
    69  }
    70  
    71  func TestNewAgentFromUnknownAgentType(t *testing.T) {
    72  	var agentType AgentType
    73  	testNewAgentFromAgentType(t, agentType, &noopAgent{})
    74  }
    75  
    76  func testNewAgentConfig(t *testing.T, config SandboxConfig, expected interface{}) {
    77  	agentConfig, err := newAgentConfig(config.AgentType, config.AgentConfig)
    78  	assert.NoError(t, err)
    79  	assert.Exactly(t, agentConfig, expected)
    80  }
    81  
    82  func TestNewAgentConfigFromNoopAgentType(t *testing.T) {
    83  	var agentConfig interface{}
    84  
    85  	sandboxConfig := SandboxConfig{
    86  		AgentType:   NoopAgentType,
    87  		AgentConfig: agentConfig,
    88  	}
    89  
    90  	testNewAgentConfig(t, sandboxConfig, agentConfig)
    91  }
    92  
    93  func TestNewAgentConfigFromKataAgentType(t *testing.T) {
    94  	agentConfig := KataAgentConfig{UseVSock: true}
    95  
    96  	sandboxConfig := SandboxConfig{
    97  		AgentType:   KataContainersAgent,
    98  		AgentConfig: agentConfig,
    99  	}
   100  
   101  	testNewAgentConfig(t, sandboxConfig, agentConfig)
   102  }
   103  
   104  func TestNewAgentConfigFromUnknownAgentType(t *testing.T) {
   105  	var agentConfig interface{}
   106  
   107  	testNewAgentConfig(t, SandboxConfig{}, agentConfig)
   108  }