github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/servicestate/servicestate_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2015-2020 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 servicestate_test 21 22 import ( 23 "fmt" 24 "os" 25 "path/filepath" 26 "strings" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/client" 31 "github.com/snapcore/snapd/dirs" 32 "github.com/snapcore/snapd/overlord/servicestate" 33 "github.com/snapcore/snapd/snap" 34 "github.com/snapcore/snapd/systemd" 35 ) 36 37 type statusDecoratorSuite struct{} 38 39 var _ = Suite(&statusDecoratorSuite{}) 40 41 func (s *statusDecoratorSuite) TestDecorateWithStatus(c *C) { 42 dirs.SetRootDir(c.MkDir()) 43 defer dirs.SetRootDir("") 44 snp := &snap.Info{ 45 SideInfo: snap.SideInfo{ 46 RealName: "foo", 47 Revision: snap.R(1), 48 }, 49 } 50 err := os.MkdirAll(snp.MountDir(), 0755) 51 c.Assert(err, IsNil) 52 err = os.Symlink(snp.Revision.String(), filepath.Join(filepath.Dir(snp.MountDir()), "current")) 53 c.Assert(err, IsNil) 54 55 disabled := false 56 r := systemd.MockSystemctl(func(args ...string) (buf []byte, err error) { 57 c.Assert(args[0], Equals, "show") 58 unit := args[2] 59 activeState, unitState := "active", "enabled" 60 if disabled { 61 activeState = "inactive" 62 unitState = "disabled" 63 } 64 if strings.HasSuffix(unit, ".timer") || strings.HasSuffix(unit, ".socket") { 65 return []byte(fmt.Sprintf(`Id=%s 66 ActiveState=%s 67 UnitFileState=%s 68 `, args[2], activeState, unitState)), nil 69 } else { 70 return []byte(fmt.Sprintf(`Id=%s 71 Type=simple 72 ActiveState=%s 73 UnitFileState=%s 74 `, args[2], activeState, unitState)), nil 75 } 76 }) 77 defer r() 78 79 sd := servicestate.NewStatusDecorator(nil) 80 81 // not a service 82 app := &client.AppInfo{ 83 Snap: "foo", 84 Name: "app", 85 } 86 snapApp := &snap.AppInfo{Snap: snp, Name: "app"} 87 88 err = sd.DecorateWithStatus(app, snapApp) 89 c.Assert(err, IsNil) 90 91 for _, enabled := range []bool{true, false} { 92 disabled = !enabled 93 94 // service only 95 app = &client.AppInfo{ 96 Snap: snp.InstanceName(), 97 Name: "svc", 98 Daemon: "simple", 99 } 100 snapApp = &snap.AppInfo{ 101 Snap: snp, 102 Name: "svc", 103 Daemon: "simple", 104 } 105 106 err = sd.DecorateWithStatus(app, snapApp) 107 c.Assert(err, IsNil) 108 c.Check(app.Active, Equals, enabled) 109 c.Check(app.Enabled, Equals, enabled) 110 111 // service + timer 112 app = &client.AppInfo{ 113 Snap: snp.InstanceName(), 114 Name: "svc", 115 Daemon: "simple", 116 } 117 snapApp = &snap.AppInfo{ 118 Snap: snp, 119 Name: "svc", 120 Daemon: "simple", 121 DaemonScope: snap.SystemDaemon, 122 } 123 snapApp.Timer = &snap.TimerInfo{ 124 App: snapApp, 125 Timer: "10:00", 126 } 127 128 err = sd.DecorateWithStatus(app, snapApp) 129 c.Assert(err, IsNil) 130 c.Check(app.Active, Equals, enabled) 131 c.Check(app.Enabled, Equals, enabled) 132 c.Check(app.Activators, DeepEquals, []client.AppActivator{ 133 {Name: "svc", Type: "timer", Active: enabled, Enabled: enabled}, 134 }) 135 136 // service with socket 137 app = &client.AppInfo{ 138 Snap: snp.InstanceName(), 139 Name: "svc", 140 Daemon: "simple", 141 } 142 snapApp = &snap.AppInfo{ 143 Snap: snp, 144 Name: "svc", 145 Daemon: "simple", 146 DaemonScope: snap.SystemDaemon, 147 } 148 snapApp.Sockets = map[string]*snap.SocketInfo{ 149 "socket1": { 150 App: snapApp, 151 Name: "socket1", 152 ListenStream: "a.socket", 153 }, 154 } 155 156 err = sd.DecorateWithStatus(app, snapApp) 157 c.Assert(err, IsNil) 158 c.Check(app.Active, Equals, enabled) 159 c.Check(app.Enabled, Equals, enabled) 160 c.Check(app.Activators, DeepEquals, []client.AppActivator{ 161 {Name: "socket1", Type: "socket", Active: enabled, Enabled: enabled}, 162 }) 163 164 } 165 }