gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/kmod/kmod_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package kmod_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"gitee.com/mysnapcore/mysnapd/interfaces"
    26  	"gitee.com/mysnapcore/mysnapd/interfaces/ifacetest"
    27  	"gitee.com/mysnapcore/mysnapd/interfaces/kmod"
    28  )
    29  
    30  type kmodSuite struct {
    31  	ifacetest.BackendSuite
    32  }
    33  
    34  var _ = Suite(&kmodSuite{})
    35  
    36  func (s *kmodSuite) SetUpTest(c *C) {
    37  	s.Backend = &kmod.Backend{}
    38  	s.BackendSuite.SetUpTest(c)
    39  }
    40  
    41  func (s *kmodSuite) TearDownTest(c *C) {
    42  	s.BackendSuite.TearDownTest(c)
    43  }
    44  
    45  func (s *kmodSuite) TestModprobeCall(c *C) {
    46  	type CallRecord struct {
    47  		module  string
    48  		options []string
    49  	}
    50  	var calls []CallRecord
    51  	restore := kmod.MockLoadModule(func(module string, options []string) error {
    52  		calls = append(calls, CallRecord{module, options})
    53  		return nil
    54  	})
    55  	defer restore()
    56  
    57  	b, ok := s.Backend.(*kmod.Backend)
    58  	c.Assert(ok, Equals, true)
    59  	b.LoadModules([]string{
    60  		"module1",
    61  		"module2",
    62  	})
    63  	c.Assert(calls, DeepEquals, []CallRecord{
    64  		{"module1", []string{}},
    65  		{"module2", []string{}},
    66  	})
    67  }
    68  
    69  func (s *kmodSuite) TestNoModprobeCallWhenPreseeding(c *C) {
    70  	loadModuleCalls := 0
    71  	restore := kmod.MockLoadModule(func(module string, options []string) error {
    72  		loadModuleCalls++
    73  		return nil
    74  	})
    75  	defer restore()
    76  
    77  	b := kmod.Backend{}
    78  	opts := &interfaces.SecurityBackendOptions{
    79  		Preseed: true,
    80  	}
    81  	c.Assert(b.Initialize(opts), IsNil)
    82  
    83  	b.LoadModules([]string{"module1"})
    84  	c.Assert(loadModuleCalls, Equals, 0)
    85  }