github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/helpers_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 interfaces_test
    21  
    22  import (
    23  	"fmt"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/dirs"
    28  	"github.com/snapcore/snapd/interfaces"
    29  	"github.com/snapcore/snapd/interfaces/ifacetest"
    30  	"github.com/snapcore/snapd/snap"
    31  	"github.com/snapcore/snapd/snap/snaptest"
    32  	"github.com/snapcore/snapd/testutil"
    33  	"github.com/snapcore/snapd/timings"
    34  )
    35  
    36  type HelpersSuite struct {
    37  	testutil.BaseTest
    38  
    39  	repo  *interfaces.Repository
    40  	snap1 *snap.Info
    41  	snap2 *snap.Info
    42  	tm    timings.Measurer
    43  }
    44  
    45  var _ = Suite(&HelpersSuite{})
    46  
    47  const snapYaml1 = `
    48  name: some-snap
    49  version: 1
    50  `
    51  
    52  const snapYaml2 = `
    53  name: other-snap
    54  version: 2
    55  `
    56  
    57  func (s *HelpersSuite) SetUpTest(c *C) {
    58  	s.BaseTest.SetUpTest(c)
    59  	tmp := c.MkDir()
    60  	dirs.SetRootDir(tmp)
    61  
    62  	s.repo = interfaces.NewRepository()
    63  	s.tm = timings.New(nil)
    64  	s.snap1 = snaptest.MockSnap(c, snapYaml1, &snap.SideInfo{Revision: snap.R(1)})
    65  	s.snap2 = snaptest.MockSnap(c, snapYaml2, &snap.SideInfo{Revision: snap.R(1)})
    66  }
    67  
    68  func (s *HelpersSuite) TearDownTest(c *C) {
    69  	s.BaseTest.TearDownTest(c)
    70  	dirs.SetRootDir("/")
    71  }
    72  
    73  func (s *HelpersSuite) TestSetupManyRunsSetupManyIfImplemented(c *C) {
    74  	confinementOpts := func(snapName string) interfaces.ConfinementOptions {
    75  		return interfaces.ConfinementOptions{}
    76  	}
    77  
    78  	setupCalls := 0
    79  	setupManyCalls := 0
    80  
    81  	backend := &ifacetest.TestSecurityBackendSetupMany{
    82  		TestSecurityBackend: ifacetest.TestSecurityBackend{BackendName: "fake",
    83  			SetupCallback: func(snap *snap.Info, opts interfaces.ConfinementOptions, repo *interfaces.Repository) error {
    84  				setupCalls++
    85  				return nil
    86  			},
    87  		},
    88  		SetupManyCallback: func(snaps []*snap.Info, confinement func(snapName string) interfaces.ConfinementOptions, repo *interfaces.Repository, tm timings.Measurer) []error {
    89  			c.Assert(snaps, HasLen, 2)
    90  			c.Check(snaps[0].SnapName(), Equals, "some-snap")
    91  			c.Check(snaps[1].SnapName(), Equals, "other-snap")
    92  			setupManyCalls++
    93  			return nil
    94  		},
    95  	}
    96  
    97  	errs := interfaces.SetupMany(s.repo, backend, []*snap.Info{s.snap1, s.snap2}, confinementOpts, s.tm)
    98  	c.Check(errs, HasLen, 0)
    99  	c.Check(setupManyCalls, Equals, 1)
   100  	c.Check(setupCalls, Equals, 0)
   101  }
   102  
   103  func (s *HelpersSuite) TestSetupManyRunsSetupIfSetupManyNotImplemented(c *C) {
   104  	setupCalls := 0
   105  	confinementOptsCalls := 0
   106  
   107  	backend := &ifacetest.TestSecurityBackend{
   108  		BackendName: "fake",
   109  		SetupCallback: func(snap *snap.Info, opts interfaces.ConfinementOptions, repo *interfaces.Repository) error {
   110  			setupCalls++
   111  			return nil
   112  		},
   113  	}
   114  
   115  	confinementOpts := func(snapName string) interfaces.ConfinementOptions {
   116  		confinementOptsCalls++
   117  		return interfaces.ConfinementOptions{}
   118  	}
   119  
   120  	errs := interfaces.SetupMany(s.repo, backend, []*snap.Info{s.snap1, s.snap2}, confinementOpts, s.tm)
   121  	c.Check(errs, HasLen, 0)
   122  	c.Check(setupCalls, Equals, 2)
   123  	c.Check(confinementOptsCalls, Equals, 2)
   124  }
   125  
   126  func (s *HelpersSuite) TestSetupManySetupManyNotOK(c *C) {
   127  	confinementOpts := func(snapName string) interfaces.ConfinementOptions {
   128  		return interfaces.ConfinementOptions{}
   129  	}
   130  
   131  	setupCalls := 0
   132  	setupManyCalls := 0
   133  
   134  	backend := &ifacetest.TestSecurityBackendSetupMany{
   135  		TestSecurityBackend: ifacetest.TestSecurityBackend{
   136  			BackendName: "fake",
   137  			SetupCallback: func(snap *snap.Info, opts interfaces.ConfinementOptions, repo *interfaces.Repository) error {
   138  				setupCalls++
   139  				return nil
   140  			},
   141  		},
   142  		SetupManyCallback: func(snaps []*snap.Info, confinement func(snapName string) interfaces.ConfinementOptions, repo *interfaces.Repository, tm timings.Measurer) []error {
   143  			c.Check(snaps, HasLen, 2)
   144  			setupManyCalls++
   145  			return []error{fmt.Errorf("error1"), fmt.Errorf("error2")}
   146  		},
   147  	}
   148  
   149  	errs := interfaces.SetupMany(s.repo, backend, []*snap.Info{s.snap1, s.snap2}, confinementOpts, s.tm)
   150  	c.Check(errs, HasLen, 2)
   151  	c.Check(setupManyCalls, Equals, 1)
   152  	c.Check(setupCalls, Equals, 0)
   153  }
   154  
   155  func (s *HelpersSuite) TestSetupManySetupNotOK(c *C) {
   156  	confinementOpts := func(snapName string) interfaces.ConfinementOptions {
   157  		return interfaces.ConfinementOptions{}
   158  	}
   159  
   160  	setupCalls := 0
   161  	backend := &ifacetest.TestSecurityBackend{
   162  		BackendName: "fake",
   163  		SetupCallback: func(snap *snap.Info, opts interfaces.ConfinementOptions, repo *interfaces.Repository) error {
   164  			setupCalls++
   165  			return fmt.Errorf("error %d", setupCalls)
   166  		},
   167  	}
   168  
   169  	errs := interfaces.SetupMany(s.repo, backend, []*snap.Info{s.snap1, s.snap2}, confinementOpts, s.tm)
   170  	c.Check(errs, HasLen, 2)
   171  	c.Check(setupCalls, Equals, 2)
   172  }