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

     1  // Copyright (c) 2017 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package vcmock
     7  
     8  import (
     9  	"context"
    10  	"reflect"
    11  	"syscall"
    12  	"testing"
    13  
    14  	vc "github.com/kata-containers/runtime/virtcontainers"
    15  	"github.com/kata-containers/runtime/virtcontainers/factory"
    16  	vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types"
    17  	"github.com/kata-containers/runtime/virtcontainers/types"
    18  	"github.com/sirupsen/logrus"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  const (
    23  	testSandboxID   = "testSandboxID"
    24  	testContainerID = "testContainerID"
    25  )
    26  
    27  var (
    28  	loggerTriggered  = 0
    29  	factoryTriggered = 0
    30  )
    31  
    32  func TestVCImplementations(t *testing.T) {
    33  	// official implementation
    34  	mainImpl := &vc.VCImpl{}
    35  
    36  	// test implementation
    37  	testImpl := &VCMock{}
    38  
    39  	var interfaceType vc.VC
    40  
    41  	// check that the official implementation implements the
    42  	// interface
    43  	mainImplType := reflect.TypeOf(mainImpl)
    44  	mainImplementsIF := mainImplType.Implements(reflect.TypeOf(&interfaceType).Elem())
    45  	assert.True(t, mainImplementsIF)
    46  
    47  	// check that the test implementation implements the
    48  	// interface
    49  	testImplType := reflect.TypeOf(testImpl)
    50  	testImplementsIF := testImplType.Implements(reflect.TypeOf(&interfaceType).Elem())
    51  	assert.True(t, testImplementsIF)
    52  }
    53  
    54  func TestVCSandboxImplementations(t *testing.T) {
    55  	// official implementation
    56  	mainImpl := &vc.Sandbox{}
    57  
    58  	// test implementation
    59  	testImpl := &Sandbox{}
    60  
    61  	var interfaceType vc.VCSandbox
    62  
    63  	// check that the official implementation implements the
    64  	// interface
    65  	mainImplType := reflect.TypeOf(mainImpl)
    66  	mainImplementsIF := mainImplType.Implements(reflect.TypeOf(&interfaceType).Elem())
    67  	assert.True(t, mainImplementsIF)
    68  
    69  	// check that the test implementation implements the
    70  	// interface
    71  	testImplType := reflect.TypeOf(testImpl)
    72  	testImplementsIF := testImplType.Implements(reflect.TypeOf(&interfaceType).Elem())
    73  	assert.True(t, testImplementsIF)
    74  }
    75  
    76  func TestVCContainerImplementations(t *testing.T) {
    77  	// official implementation
    78  	mainImpl := &vc.Container{}
    79  
    80  	// test implementation
    81  	testImpl := &Container{}
    82  
    83  	var interfaceType vc.VCContainer
    84  
    85  	// check that the official implementation implements the
    86  	// interface
    87  	mainImplType := reflect.TypeOf(mainImpl)
    88  	mainImplementsIF := mainImplType.Implements(reflect.TypeOf(&interfaceType).Elem())
    89  	assert.True(t, mainImplementsIF)
    90  
    91  	// check that the test implementation implements the
    92  	// interface
    93  	testImplType := reflect.TypeOf(testImpl)
    94  	testImplementsIF := testImplType.Implements(reflect.TypeOf(&interfaceType).Elem())
    95  	assert.True(t, testImplementsIF)
    96  }
    97  
    98  func TestVCMockSetLogger(t *testing.T) {
    99  	assert := assert.New(t)
   100  
   101  	m := &VCMock{}
   102  	assert.Nil(m.SetLoggerFunc)
   103  
   104  	logger := logrus.NewEntry(logrus.New())
   105  
   106  	assert.Equal(loggerTriggered, 0)
   107  	ctx := context.Background()
   108  	m.SetLogger(ctx, logger)
   109  	assert.Equal(loggerTriggered, 0)
   110  
   111  	m.SetLoggerFunc = func(ctx context.Context, logger *logrus.Entry) {
   112  		loggerTriggered = 1
   113  	}
   114  
   115  	m.SetLogger(ctx, logger)
   116  	assert.Equal(loggerTriggered, 1)
   117  }
   118  
   119  func TestVCMockCreateSandbox(t *testing.T) {
   120  	assert := assert.New(t)
   121  
   122  	m := &VCMock{}
   123  	assert.Nil(m.CreateSandboxFunc)
   124  
   125  	ctx := context.Background()
   126  	_, err := m.CreateSandbox(ctx, vc.SandboxConfig{})
   127  	assert.Error(err)
   128  	assert.True(IsMockError(err))
   129  
   130  	m.CreateSandboxFunc = func(ctx context.Context, sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) {
   131  		return &Sandbox{}, nil
   132  	}
   133  
   134  	sandbox, err := m.CreateSandbox(ctx, vc.SandboxConfig{})
   135  	assert.NoError(err)
   136  	assert.Equal(sandbox, &Sandbox{})
   137  
   138  	// reset
   139  	m.CreateSandboxFunc = nil
   140  
   141  	_, err = m.CreateSandbox(ctx, vc.SandboxConfig{})
   142  	assert.Error(err)
   143  	assert.True(IsMockError(err))
   144  }
   145  
   146  func TestVCMockDeleteSandbox(t *testing.T) {
   147  	assert := assert.New(t)
   148  
   149  	m := &VCMock{}
   150  	assert.Nil(m.DeleteSandboxFunc)
   151  
   152  	ctx := context.Background()
   153  	_, err := m.DeleteSandbox(ctx, testSandboxID)
   154  	assert.Error(err)
   155  	assert.True(IsMockError(err))
   156  
   157  	m.DeleteSandboxFunc = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
   158  		return &Sandbox{}, nil
   159  	}
   160  
   161  	sandbox, err := m.DeleteSandbox(ctx, testSandboxID)
   162  	assert.NoError(err)
   163  	assert.Equal(sandbox, &Sandbox{})
   164  
   165  	// reset
   166  	m.DeleteSandboxFunc = nil
   167  
   168  	_, err = m.DeleteSandbox(ctx, testSandboxID)
   169  	assert.Error(err)
   170  	assert.True(IsMockError(err))
   171  }
   172  
   173  func TestVCMockListSandbox(t *testing.T) {
   174  	assert := assert.New(t)
   175  
   176  	m := &VCMock{}
   177  	assert.Nil(m.ListSandboxFunc)
   178  
   179  	ctx := context.Background()
   180  	_, err := m.ListSandbox(ctx)
   181  	assert.Error(err)
   182  	assert.True(IsMockError(err))
   183  
   184  	m.ListSandboxFunc = func(ctx context.Context) ([]vc.SandboxStatus, error) {
   185  		return []vc.SandboxStatus{}, nil
   186  	}
   187  
   188  	sandboxes, err := m.ListSandbox(ctx)
   189  	assert.NoError(err)
   190  	assert.Equal(sandboxes, []vc.SandboxStatus{})
   191  
   192  	// reset
   193  	m.ListSandboxFunc = nil
   194  
   195  	_, err = m.ListSandbox(ctx)
   196  	assert.Error(err)
   197  	assert.True(IsMockError(err))
   198  }
   199  
   200  func TestVCMockRunSandbox(t *testing.T) {
   201  	assert := assert.New(t)
   202  
   203  	m := &VCMock{}
   204  	assert.Nil(m.RunSandboxFunc)
   205  
   206  	ctx := context.Background()
   207  	_, err := m.RunSandbox(ctx, vc.SandboxConfig{})
   208  	assert.Error(err)
   209  	assert.True(IsMockError(err))
   210  
   211  	m.RunSandboxFunc = func(ctx context.Context, sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) {
   212  		return &Sandbox{}, nil
   213  	}
   214  
   215  	sandbox, err := m.RunSandbox(ctx, vc.SandboxConfig{})
   216  	assert.NoError(err)
   217  	assert.Equal(sandbox, &Sandbox{})
   218  
   219  	// reset
   220  	m.RunSandboxFunc = nil
   221  
   222  	_, err = m.RunSandbox(ctx, vc.SandboxConfig{})
   223  	assert.Error(err)
   224  	assert.True(IsMockError(err))
   225  }
   226  
   227  func TestVCMockStartSandbox(t *testing.T) {
   228  	assert := assert.New(t)
   229  
   230  	m := &VCMock{}
   231  	assert.Nil(m.StartSandboxFunc)
   232  
   233  	ctx := context.Background()
   234  	_, err := m.StartSandbox(ctx, testSandboxID)
   235  	assert.Error(err)
   236  	assert.True(IsMockError(err))
   237  
   238  	m.StartSandboxFunc = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
   239  		return &Sandbox{}, nil
   240  	}
   241  
   242  	sandbox, err := m.StartSandbox(ctx, testSandboxID)
   243  	assert.NoError(err)
   244  	assert.Equal(sandbox, &Sandbox{})
   245  
   246  	// reset
   247  	m.StartSandboxFunc = nil
   248  
   249  	_, err = m.StartSandbox(ctx, testSandboxID)
   250  	assert.Error(err)
   251  	assert.True(IsMockError(err))
   252  }
   253  
   254  func TestVCMockStatusSandbox(t *testing.T) {
   255  	assert := assert.New(t)
   256  
   257  	m := &VCMock{}
   258  	assert.Nil(m.StatusSandboxFunc)
   259  
   260  	ctx := context.Background()
   261  	_, err := m.StatusSandbox(ctx, testSandboxID)
   262  	assert.Error(err)
   263  	assert.True(IsMockError(err))
   264  
   265  	m.StatusSandboxFunc = func(ctx context.Context, sandboxID string) (vc.SandboxStatus, error) {
   266  		return vc.SandboxStatus{}, nil
   267  	}
   268  
   269  	sandbox, err := m.StatusSandbox(ctx, testSandboxID)
   270  	assert.NoError(err)
   271  	assert.Equal(sandbox, vc.SandboxStatus{})
   272  
   273  	// reset
   274  	m.StatusSandboxFunc = nil
   275  
   276  	_, err = m.StatusSandbox(ctx, testSandboxID)
   277  	assert.Error(err)
   278  	assert.True(IsMockError(err))
   279  }
   280  
   281  func TestVCMockStopSandbox(t *testing.T) {
   282  	assert := assert.New(t)
   283  
   284  	m := &VCMock{}
   285  	assert.Nil(m.StopSandboxFunc)
   286  
   287  	ctx := context.Background()
   288  	_, err := m.StopSandbox(ctx, testSandboxID, false)
   289  	assert.Error(err)
   290  	assert.True(IsMockError(err))
   291  
   292  	m.StopSandboxFunc = func(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) {
   293  		return &Sandbox{}, nil
   294  	}
   295  
   296  	sandbox, err := m.StopSandbox(ctx, testSandboxID, false)
   297  	assert.NoError(err)
   298  	assert.Equal(sandbox, &Sandbox{})
   299  
   300  	// reset
   301  	m.StopSandboxFunc = nil
   302  
   303  	_, err = m.StopSandbox(ctx, testSandboxID, false)
   304  	assert.Error(err)
   305  	assert.True(IsMockError(err))
   306  }
   307  
   308  func TestVCMockCreateContainer(t *testing.T) {
   309  	assert := assert.New(t)
   310  
   311  	m := &VCMock{}
   312  	assert.Nil(m.CreateContainerFunc)
   313  
   314  	ctx := context.Background()
   315  	config := vc.ContainerConfig{}
   316  	_, _, err := m.CreateContainer(ctx, testSandboxID, config)
   317  	assert.Error(err)
   318  	assert.True(IsMockError(err))
   319  
   320  	m.CreateContainerFunc = func(ctx context.Context, sandboxID string, containerConfig vc.ContainerConfig) (vc.VCSandbox, vc.VCContainer, error) {
   321  		return &Sandbox{}, &Container{}, nil
   322  	}
   323  
   324  	sandbox, container, err := m.CreateContainer(ctx, testSandboxID, config)
   325  	assert.NoError(err)
   326  	assert.Equal(sandbox, &Sandbox{})
   327  	assert.Equal(container, &Container{})
   328  
   329  	// reset
   330  	m.CreateContainerFunc = nil
   331  
   332  	_, _, err = m.CreateContainer(ctx, testSandboxID, config)
   333  	assert.Error(err)
   334  	assert.True(IsMockError(err))
   335  }
   336  
   337  func TestVCMockDeleteContainer(t *testing.T) {
   338  	assert := assert.New(t)
   339  
   340  	m := &VCMock{}
   341  	assert.Nil(m.DeleteContainerFunc)
   342  
   343  	ctx := context.Background()
   344  	_, err := m.DeleteContainer(ctx, testSandboxID, testContainerID)
   345  	assert.Error(err)
   346  	assert.True(IsMockError(err))
   347  
   348  	m.DeleteContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) {
   349  		return &Container{}, nil
   350  	}
   351  
   352  	container, err := m.DeleteContainer(ctx, testSandboxID, testContainerID)
   353  	assert.NoError(err)
   354  	assert.Equal(container, &Container{})
   355  
   356  	// reset
   357  	m.DeleteContainerFunc = nil
   358  
   359  	_, err = m.DeleteContainer(ctx, testSandboxID, testContainerID)
   360  	assert.Error(err)
   361  	assert.True(IsMockError(err))
   362  }
   363  
   364  func TestVCMockEnterContainer(t *testing.T) {
   365  	assert := assert.New(t)
   366  
   367  	m := &VCMock{}
   368  	assert.Nil(m.EnterContainerFunc)
   369  
   370  	ctx := context.Background()
   371  	cmd := types.Cmd{}
   372  	_, _, _, err := m.EnterContainer(ctx, testSandboxID, testContainerID, cmd)
   373  	assert.Error(err)
   374  	assert.True(IsMockError(err))
   375  
   376  	m.EnterContainerFunc = func(ctx context.Context, sandboxID, containerID string, cmd types.Cmd) (vc.VCSandbox, vc.VCContainer, *vc.Process, error) {
   377  		return &Sandbox{}, &Container{}, &vc.Process{}, nil
   378  	}
   379  
   380  	sandbox, container, process, err := m.EnterContainer(ctx, testSandboxID, testContainerID, cmd)
   381  	assert.NoError(err)
   382  	assert.Equal(sandbox, &Sandbox{})
   383  	assert.Equal(container, &Container{})
   384  	assert.Equal(process, &vc.Process{})
   385  
   386  	// reset
   387  	m.EnterContainerFunc = nil
   388  
   389  	_, _, _, err = m.EnterContainer(ctx, testSandboxID, testContainerID, cmd)
   390  	assert.Error(err)
   391  	assert.True(IsMockError(err))
   392  }
   393  
   394  func TestVCMockKillContainer(t *testing.T) {
   395  	assert := assert.New(t)
   396  
   397  	m := &VCMock{}
   398  	assert.Nil(m.KillContainerFunc)
   399  
   400  	ctx := context.Background()
   401  	sig := syscall.SIGTERM
   402  
   403  	for _, all := range []bool{true, false} {
   404  		err := m.KillContainer(ctx, testSandboxID, testContainerID, sig, all)
   405  		assert.Error(err)
   406  		assert.True(IsMockError(err))
   407  	}
   408  
   409  	m.KillContainerFunc = func(ctx context.Context, sandboxID, containerID string, signal syscall.Signal, all bool) error {
   410  		return nil
   411  	}
   412  
   413  	for _, all := range []bool{true, false} {
   414  		err := m.KillContainer(ctx, testSandboxID, testContainerID, sig, all)
   415  		assert.NoError(err)
   416  	}
   417  
   418  	// reset
   419  	m.KillContainerFunc = nil
   420  
   421  	for _, all := range []bool{true, false} {
   422  		err := m.KillContainer(ctx, testSandboxID, testContainerID, sig, all)
   423  		assert.Error(err)
   424  		assert.True(IsMockError(err))
   425  	}
   426  }
   427  
   428  func TestVCMockStartContainer(t *testing.T) {
   429  	assert := assert.New(t)
   430  
   431  	m := &VCMock{}
   432  	assert.Nil(m.StartContainerFunc)
   433  
   434  	ctx := context.Background()
   435  	_, err := m.StartContainer(ctx, testSandboxID, testContainerID)
   436  	assert.Error(err)
   437  	assert.True(IsMockError(err))
   438  
   439  	m.StartContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) {
   440  		return &Container{}, nil
   441  	}
   442  
   443  	container, err := m.StartContainer(ctx, testSandboxID, testContainerID)
   444  	assert.NoError(err)
   445  	assert.Equal(container, &Container{})
   446  
   447  	// reset
   448  	m.StartContainerFunc = nil
   449  
   450  	_, err = m.StartContainer(ctx, testSandboxID, testContainerID)
   451  	assert.Error(err)
   452  	assert.True(IsMockError(err))
   453  }
   454  
   455  func TestVCMockStatusContainer(t *testing.T) {
   456  	assert := assert.New(t)
   457  
   458  	m := &VCMock{}
   459  	assert.Nil(m.StatusContainerFunc)
   460  
   461  	ctx := context.Background()
   462  	_, err := m.StatusContainer(ctx, testSandboxID, testContainerID)
   463  	assert.Error(err)
   464  	assert.True(IsMockError(err))
   465  
   466  	m.StatusContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) {
   467  		return vc.ContainerStatus{}, nil
   468  	}
   469  
   470  	status, err := m.StatusContainer(ctx, testSandboxID, testContainerID)
   471  	assert.NoError(err)
   472  	assert.Equal(status, vc.ContainerStatus{})
   473  
   474  	// reset
   475  	m.StatusContainerFunc = nil
   476  
   477  	_, err = m.StatusContainer(ctx, testSandboxID, testContainerID)
   478  	assert.Error(err)
   479  	assert.True(IsMockError(err))
   480  }
   481  
   482  func TestVCMockStatsContainer(t *testing.T) {
   483  	assert := assert.New(t)
   484  
   485  	m := &VCMock{}
   486  	assert.Nil(m.StatsContainerFunc)
   487  
   488  	ctx := context.Background()
   489  	_, err := m.StatsContainer(ctx, testSandboxID, testContainerID)
   490  
   491  	assert.Error(err)
   492  	assert.True(IsMockError(err))
   493  
   494  	m.StatsContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStats, error) {
   495  		return vc.ContainerStats{}, nil
   496  	}
   497  
   498  	stats, err := m.StatsContainer(ctx, testSandboxID, testContainerID)
   499  	assert.NoError(err)
   500  	assert.Equal(stats, vc.ContainerStats{})
   501  
   502  	// reset
   503  	m.StatsContainerFunc = nil
   504  
   505  	_, err = m.StatsContainer(ctx, testSandboxID, testContainerID)
   506  	assert.Error(err)
   507  	assert.True(IsMockError(err))
   508  }
   509  
   510  func TestVCMockStopContainer(t *testing.T) {
   511  	assert := assert.New(t)
   512  
   513  	m := &VCMock{}
   514  	assert.Nil(m.StopContainerFunc)
   515  
   516  	ctx := context.Background()
   517  	_, err := m.StopContainer(ctx, testSandboxID, testContainerID)
   518  	assert.Error(err)
   519  	assert.True(IsMockError(err))
   520  
   521  	m.StopContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) {
   522  		return &Container{}, nil
   523  	}
   524  
   525  	container, err := m.StopContainer(ctx, testSandboxID, testContainerID)
   526  	assert.NoError(err)
   527  	assert.Equal(container, &Container{})
   528  
   529  	// reset
   530  	m.StopContainerFunc = nil
   531  
   532  	_, err = m.StopContainer(ctx, testSandboxID, testContainerID)
   533  	assert.Error(err)
   534  	assert.True(IsMockError(err))
   535  }
   536  
   537  func TestVCMockProcessListContainer(t *testing.T) {
   538  	assert := assert.New(t)
   539  
   540  	m := &VCMock{}
   541  	assert.Nil(m.ProcessListContainerFunc)
   542  
   543  	options := vc.ProcessListOptions{
   544  		Format: "json",
   545  		Args:   []string{"-ef"},
   546  	}
   547  
   548  	ctx := context.Background()
   549  	_, err := m.ProcessListContainer(ctx, testSandboxID, testContainerID, options)
   550  	assert.Error(err)
   551  	assert.True(IsMockError(err))
   552  
   553  	processList := vc.ProcessList("hi")
   554  
   555  	m.ProcessListContainerFunc = func(ctx context.Context, sandboxID, containerID string, options vc.ProcessListOptions) (vc.ProcessList, error) {
   556  		return processList, nil
   557  	}
   558  
   559  	pList, err := m.ProcessListContainer(ctx, testSandboxID, testContainerID, options)
   560  	assert.NoError(err)
   561  	assert.Equal(pList, processList)
   562  
   563  	// reset
   564  	m.ProcessListContainerFunc = nil
   565  
   566  	_, err = m.ProcessListContainer(ctx, testSandboxID, testContainerID, options)
   567  	assert.Error(err)
   568  	assert.True(IsMockError(err))
   569  }
   570  
   571  func TestVCMockFetchSandbox(t *testing.T) {
   572  	assert := assert.New(t)
   573  
   574  	m := &VCMock{}
   575  	config := &vc.SandboxConfig{}
   576  	assert.Nil(m.FetchSandboxFunc)
   577  
   578  	ctx := context.Background()
   579  	_, err := m.FetchSandbox(ctx, config.ID)
   580  	assert.Error(err)
   581  	assert.True(IsMockError(err))
   582  
   583  	m.FetchSandboxFunc = func(ctx context.Context, id string) (vc.VCSandbox, error) {
   584  		return &Sandbox{}, nil
   585  	}
   586  
   587  	sandbox, err := m.FetchSandbox(ctx, config.ID)
   588  	assert.NoError(err)
   589  	assert.Equal(sandbox, &Sandbox{})
   590  
   591  	// reset
   592  	m.FetchSandboxFunc = nil
   593  
   594  	_, err = m.FetchSandbox(ctx, config.ID)
   595  	assert.Error(err)
   596  	assert.True(IsMockError(err))
   597  
   598  }
   599  
   600  func TestVCMockPauseContainer(t *testing.T) {
   601  	assert := assert.New(t)
   602  
   603  	m := &VCMock{}
   604  	config := &vc.SandboxConfig{}
   605  	assert.Nil(m.PauseContainerFunc)
   606  
   607  	ctx := context.Background()
   608  	err := m.PauseContainer(ctx, config.ID, config.ID)
   609  	assert.Error(err)
   610  	assert.True(IsMockError(err))
   611  
   612  	m.PauseContainerFunc = func(ctx context.Context, sid, cid string) error {
   613  		return nil
   614  	}
   615  
   616  	err = m.PauseContainer(ctx, config.ID, config.ID)
   617  	assert.NoError(err)
   618  
   619  	// reset
   620  	m.PauseContainerFunc = nil
   621  
   622  	err = m.PauseContainer(ctx, config.ID, config.ID)
   623  	assert.Error(err)
   624  	assert.True(IsMockError(err))
   625  }
   626  
   627  func TestVCMockResumeContainer(t *testing.T) {
   628  	assert := assert.New(t)
   629  
   630  	m := &VCMock{}
   631  	config := &vc.SandboxConfig{}
   632  	assert.Nil(m.ResumeContainerFunc)
   633  
   634  	ctx := context.Background()
   635  	err := m.ResumeContainer(ctx, config.ID, config.ID)
   636  	assert.Error(err)
   637  	assert.True(IsMockError(err))
   638  
   639  	m.ResumeContainerFunc = func(ctx context.Context, sid, cid string) error {
   640  		return nil
   641  	}
   642  
   643  	err = m.ResumeContainer(ctx, config.ID, config.ID)
   644  	assert.NoError(err)
   645  
   646  	// reset
   647  	m.ResumeContainerFunc = nil
   648  
   649  	err = m.ResumeContainer(ctx, config.ID, config.ID)
   650  	assert.Error(err)
   651  	assert.True(IsMockError(err))
   652  }
   653  
   654  func TestVCMockSetVMFactory(t *testing.T) {
   655  	assert := assert.New(t)
   656  
   657  	m := &VCMock{}
   658  	assert.Nil(m.SetFactoryFunc)
   659  
   660  	hyperConfig := vc.HypervisorConfig{
   661  		KernelPath: "foobar",
   662  		ImagePath:  "foobar",
   663  	}
   664  	vmConfig := vc.VMConfig{
   665  		HypervisorType:   vc.MockHypervisor,
   666  		AgentType:        vc.NoopAgentType,
   667  		HypervisorConfig: hyperConfig,
   668  	}
   669  
   670  	ctx := context.Background()
   671  	f, err := factory.NewFactory(ctx, factory.Config{VMConfig: vmConfig}, false)
   672  	assert.Nil(err)
   673  
   674  	assert.Equal(factoryTriggered, 0)
   675  	m.SetFactory(ctx, f)
   676  	assert.Equal(factoryTriggered, 0)
   677  
   678  	m.SetFactoryFunc = func(ctx context.Context, factory vc.Factory) {
   679  		factoryTriggered = 1
   680  	}
   681  
   682  	m.SetFactory(ctx, f)
   683  	assert.Equal(factoryTriggered, 1)
   684  }
   685  
   686  func TestVCMockAddInterface(t *testing.T) {
   687  	assert := assert.New(t)
   688  
   689  	m := &VCMock{}
   690  	config := &vc.SandboxConfig{}
   691  	assert.Nil(m.AddInterfaceFunc)
   692  
   693  	ctx := context.Background()
   694  	_, err := m.AddInterface(ctx, config.ID, nil)
   695  	assert.Error(err)
   696  	assert.True(IsMockError(err))
   697  
   698  	m.AddInterfaceFunc = func(ctx context.Context, sid string, inf *vcTypes.Interface) (*vcTypes.Interface, error) {
   699  		return nil, nil
   700  	}
   701  
   702  	_, err = m.AddInterface(ctx, config.ID, nil)
   703  	assert.NoError(err)
   704  
   705  	// reset
   706  	m.AddInterfaceFunc = nil
   707  
   708  	_, err = m.AddInterface(ctx, config.ID, nil)
   709  	assert.Error(err)
   710  	assert.True(IsMockError(err))
   711  }
   712  
   713  func TestVCMockRemoveInterface(t *testing.T) {
   714  	assert := assert.New(t)
   715  
   716  	m := &VCMock{}
   717  	config := &vc.SandboxConfig{}
   718  	assert.Nil(m.RemoveInterfaceFunc)
   719  
   720  	ctx := context.Background()
   721  	_, err := m.RemoveInterface(ctx, config.ID, nil)
   722  	assert.Error(err)
   723  	assert.True(IsMockError(err))
   724  
   725  	m.RemoveInterfaceFunc = func(ctx context.Context, sid string, inf *vcTypes.Interface) (*vcTypes.Interface, error) {
   726  		return nil, nil
   727  	}
   728  
   729  	_, err = m.RemoveInterface(ctx, config.ID, nil)
   730  	assert.NoError(err)
   731  
   732  	// reset
   733  	m.RemoveInterfaceFunc = nil
   734  
   735  	_, err = m.RemoveInterface(ctx, config.ID, nil)
   736  	assert.Error(err)
   737  	assert.True(IsMockError(err))
   738  }
   739  
   740  func TestVCMockListInterfaces(t *testing.T) {
   741  	assert := assert.New(t)
   742  
   743  	m := &VCMock{}
   744  	config := &vc.SandboxConfig{}
   745  	assert.Nil(m.ListInterfacesFunc)
   746  
   747  	ctx := context.Background()
   748  	_, err := m.ListInterfaces(ctx, config.ID)
   749  	assert.Error(err)
   750  	assert.True(IsMockError(err))
   751  
   752  	m.ListInterfacesFunc = func(ctx context.Context, sid string) ([]*vcTypes.Interface, error) {
   753  		return nil, nil
   754  	}
   755  
   756  	_, err = m.ListInterfaces(ctx, config.ID)
   757  	assert.NoError(err)
   758  
   759  	// reset
   760  	m.ListInterfacesFunc = nil
   761  
   762  	_, err = m.ListInterfaces(ctx, config.ID)
   763  	assert.Error(err)
   764  	assert.True(IsMockError(err))
   765  }
   766  
   767  func TestVCMockUpdateRoutes(t *testing.T) {
   768  	assert := assert.New(t)
   769  
   770  	m := &VCMock{}
   771  	config := &vc.SandboxConfig{}
   772  	assert.Nil(m.UpdateRoutesFunc)
   773  
   774  	ctx := context.Background()
   775  	_, err := m.UpdateRoutes(ctx, config.ID, nil)
   776  	assert.Error(err)
   777  	assert.True(IsMockError(err))
   778  
   779  	m.UpdateRoutesFunc = func(ctx context.Context, sid string, routes []*vcTypes.Route) ([]*vcTypes.Route, error) {
   780  		return nil, nil
   781  	}
   782  
   783  	_, err = m.UpdateRoutes(ctx, config.ID, nil)
   784  	assert.NoError(err)
   785  
   786  	// reset
   787  	m.UpdateRoutesFunc = nil
   788  
   789  	_, err = m.UpdateRoutes(ctx, config.ID, nil)
   790  	assert.Error(err)
   791  	assert.True(IsMockError(err))
   792  }
   793  
   794  func TestVCMockListRoutes(t *testing.T) {
   795  	assert := assert.New(t)
   796  
   797  	m := &VCMock{}
   798  	config := &vc.SandboxConfig{}
   799  	assert.Nil(m.ListRoutesFunc)
   800  
   801  	ctx := context.Background()
   802  	_, err := m.ListRoutes(ctx, config.ID)
   803  	assert.Error(err)
   804  	assert.True(IsMockError(err))
   805  
   806  	m.ListRoutesFunc = func(ctx context.Context, sid string) ([]*vcTypes.Route, error) {
   807  		return nil, nil
   808  	}
   809  
   810  	_, err = m.ListRoutes(ctx, config.ID)
   811  	assert.NoError(err)
   812  
   813  	// reset
   814  	m.ListRoutesFunc = nil
   815  
   816  	_, err = m.ListRoutes(ctx, config.ID)
   817  	assert.Error(err)
   818  	assert.True(IsMockError(err))
   819  }