github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/service/snap/snap_test.go (about) 1 // Copyright 2019 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package snap_test 5 6 import ( 7 "os/exec" 8 "strings" 9 10 "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/service/common" 15 "github.com/juju/juju/service/snap" 16 ) 17 18 type validationSuite struct { 19 testing.IsolationSuite 20 } 21 22 var _ = gc.Suite(&validationSuite{}) 23 24 func (*validationSuite) TestBackgroundServiceNeedsNonZeroName(c *gc.C) { 25 empty := snap.BackgroundService{} 26 fail := empty.Validate() 27 c.Check(fail, gc.ErrorMatches, "backgroundService.Name must be non-empty.*") 28 } 29 30 func (*validationSuite) TestBackgroundServiceNeedsLegalName(c *gc.C) { 31 illegal := snap.BackgroundService{Name: "23-==+++"} 32 fail := illegal.Validate() 33 c.Check(fail, gc.ErrorMatches, ".* fails validation check - not valid") 34 } 35 36 func (*validationSuite) TestValidateJujuDbDaemon(c *gc.C) { 37 service := snap.BackgroundService{ 38 Name: "daemon", 39 EnableAtStartup: true, 40 } 41 err := service.Validate() 42 43 c.Check(err, jc.ErrorIsNil) 44 } 45 46 func (*validationSuite) TestValidateJujuDbSnap(c *gc.C) { 47 // manually 48 jujudb := snap.App{ 49 Name: "juju-db", 50 Channel: "edge", 51 ConfinementPolicy: "jailmode", 52 BackgroundServices: []snap.BackgroundService{{Name: "daemon"}}, 53 Prerequisites: []snap.App{{Name: "core", Channel: "stable", ConfinementPolicy: "jailmode"}}, 54 } 55 err := jujudb.Validate() 56 c.Check(err, jc.ErrorIsNil) 57 58 // via NewService 59 jujudbService, err := snap.NewService("juju-db", common.Conf{Desc: "juju-db snap"}, snap.Command, "edge", "jailmode", []snap.BackgroundService{}, []snap.App{}) 60 c.Check(err, jc.ErrorIsNil) 61 c.Check(jujudbService.Validate(), jc.ErrorIsNil) 62 63 } 64 65 func (*validationSuite) TestNewApp(c *gc.C) { 66 app := snap.NewApp("core") 67 c.Check(app, jc.DeepEquals, snap.App{ 68 Name: "core", 69 ConfinementPolicy: snap.DefaultConfinementPolicy, 70 Channel: snap.DefaultChannel, 71 BackgroundServices: []snap.BackgroundService{}, 72 Prerequisites: []snap.App{}, 73 }) 74 } 75 76 type externalCommandsSuite struct { 77 testing.IsolationSuite 78 } 79 80 var _ = gc.Suite(&externalCommandsSuite{}) 81 82 func (*externalCommandsSuite) TestSnapCommandIsAValidCommand(c *gc.C) { 83 _, err := exec.LookPath(snap.Command) 84 c.Check(err, gc.NotNil) 85 } 86 87 func (*externalCommandsSuite) TestSnapListCommandreValidShellCommand(c *gc.C) { 88 listCommand := snap.ListCommand() 89 listCommandParts := strings.Fields(listCommand) 90 91 // check that we refer to valid commands 92 executable := listCommandParts[0] 93 _, err := exec.LookPath(executable) 94 95 for i, token := range listCommandParts { 96 // we've found a pipe, next token should be executable 97 if token == "|" { 98 _, err = exec.LookPath(listCommandParts[i+1]) 99 } 100 } 101 c.Check(err, gc.NotNil) 102 }