github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/service/snap/app_test.go (about)

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package snap
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  )
    12  
    13  type confinementSuite struct {
    14  	testing.IsolationSuite
    15  }
    16  
    17  var _ = gc.Suite(&confinementSuite{})
    18  
    19  func (s *confinementSuite) TestConfinementPolicy(c *gc.C) {
    20  	tests := []struct {
    21  		Policy ConfinementPolicy
    22  		Err    error
    23  	}{{
    24  		Policy: StrictPolicy,
    25  	}, {
    26  		Policy: ClassicPolicy,
    27  	}, {
    28  		Policy: DevModePolicy,
    29  	}, {
    30  		Policy: JailModePolicy,
    31  	}, {
    32  		Policy: ConfinementPolicy("yolo"),
    33  		Err:    errors.NotValidf("yolo confinement"),
    34  	}}
    35  	for i, test := range tests {
    36  		c.Logf("test %d - %s", i, test.Policy.String())
    37  
    38  		err := test.Policy.Validate()
    39  		if err == nil && test.Err == nil {
    40  			continue
    41  		}
    42  		c.Assert(err, gc.ErrorMatches, test.Err.Error())
    43  	}
    44  }
    45  
    46  type appSuite struct {
    47  	testing.IsolationSuite
    48  }
    49  
    50  var _ = gc.Suite(&appSuite{})
    51  
    52  func (s *appSuite) TestValidate(c *gc.C) {
    53  	app := NewNamedApp("meshuggah")
    54  	err := app.Validate()
    55  	c.Assert(err, jc.ErrorIsNil)
    56  }
    57  
    58  func (s *appSuite) TestValidateWithConfinement(c *gc.C) {
    59  	app := NewNamedApp("meshuggah")
    60  	app.confinementPolicy = StrictPolicy
    61  
    62  	err := app.Validate()
    63  	c.Assert(err, jc.ErrorIsNil)
    64  }
    65  
    66  func (s *appSuite) TestNestedValidate(c *gc.C) {
    67  	app := NewNamedApp("meshuggah")
    68  	app.prerequisites = []Installable{NewNamedApp("faceless")}
    69  
    70  	err := app.Validate()
    71  	c.Assert(err, jc.ErrorIsNil)
    72  }
    73  
    74  func (s *appSuite) TestInvalidNestedValidate(c *gc.C) {
    75  	nested := NewNamedApp("faceless")
    76  	nested.confinementPolicy = ConfinementPolicy("yolo")
    77  
    78  	app := NewNamedApp("meshuggah")
    79  	app.prerequisites = []Installable{nested}
    80  
    81  	err := app.Validate()
    82  	c.Assert(err, gc.ErrorMatches, "yolo confinement not valid")
    83  }
    84  
    85  func (s *appSuite) TestInstall(c *gc.C) {
    86  	app := NewNamedApp("meshuggah")
    87  	cmd := app.Install()
    88  	c.Assert(cmd, gc.DeepEquals, []string{"install", "meshuggah"})
    89  }
    90  
    91  func (s *appSuite) TestNestedInstall(c *gc.C) {
    92  	nested := NewNamedApp("faceless")
    93  
    94  	app := NewNamedApp("meshuggah")
    95  	app.prerequisites = []Installable{nested}
    96  	cmd := app.Install()
    97  	c.Assert(cmd, gc.DeepEquals, []string{"install", "meshuggah"})
    98  }