github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/daemon/api_console_conf_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 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 daemon 21 22 import ( 23 "bytes" 24 "net/http" 25 "sort" 26 "time" 27 28 "github.com/snapcore/snapd/overlord/configstate/config" 29 "github.com/snapcore/snapd/overlord/snapstate" 30 "github.com/snapcore/snapd/overlord/state" 31 "gopkg.in/check.v1" 32 . "gopkg.in/check.v1" 33 ) 34 35 var _ = Suite(&consoleConfSuite{}) 36 37 type consoleConfSuite struct { 38 APIBaseSuite 39 } 40 41 func (s *consoleConfSuite) TestPostConsoleConfStartRoutine(c *C) { 42 t0 := time.Now() 43 d := s.daemonWithOverlordMock(c) 44 snapMgr, err := snapstate.Manager(d.overlord.State(), d.overlord.TaskRunner()) 45 c.Assert(err, check.IsNil) 46 d.overlord.AddManager(snapMgr) 47 48 st := d.overlord.State() 49 50 body := bytes.NewBuffer(nil) 51 req, err := http.NewRequest("POST", "/v2/internal/console-conf-start", body) 52 c.Assert(err, IsNil) 53 54 // no changes in state, no changes in response 55 rsp := consoleConfStartRoutine(routineConsoleConfStartCmd, req, nil).(*resp) 56 c.Check(rsp.Type, Equals, ResponseTypeSync) 57 c.Assert(rsp.Result, DeepEquals, &ConsoleConfStartRoutineResult{}) 58 59 // we did set the refresh.hold time back 20 minutes though 60 st.Lock() 61 defer st.Unlock() 62 63 tr := config.NewTransaction(st) 64 var t1 time.Time 65 err = tr.Get("core", "refresh.hold", &t1) 66 c.Assert(err, IsNil) 67 68 c.Assert(t0.Add(20*time.Minute).After(t1), Equals, false) 69 70 // if we add some changes to state that are in progress, then they are 71 // returned 72 73 // now make some auto-refresh changes to make sure we get those figured out 74 chg0 := st.NewChange("auto-refresh", "auto-refresh-the-things") 75 chg0.AddTask(st.NewTask("nop", "do nothing")) 76 77 // make it in doing state 78 chg0.SetStatus(state.DoingStatus) 79 chg0.Set("snap-names", []string{"doing-snap"}) 80 81 // this one will be picked up too 82 chg1 := st.NewChange("auto-refresh", "auto-refresh-the-things") 83 chg1.AddTask(st.NewTask("nop", "do nothing")) 84 chg1.SetStatus(state.DoStatus) 85 chg1.Set("snap-names", []string{"do-snap"}) 86 87 // this one won't, it's Done 88 chg2 := st.NewChange("auto-refresh", "auto-refresh-the-things") 89 chg2.AddTask(st.NewTask("nop", "do nothing")) 90 chg2.SetStatus(state.DoneStatus) 91 chg2.Set("snap-names", []string{"done-snap"}) 92 93 // nor this one, it's Undone 94 chg3 := st.NewChange("auto-refresh", "auto-refresh-the-things") 95 chg3.AddTask(st.NewTask("nop", "do nothing")) 96 chg3.SetStatus(state.UndoneStatus) 97 chg3.Set("snap-names", []string{"undone-snap"}) 98 99 st.Unlock() 100 defer st.Lock() 101 102 req2, err := http.NewRequest("POST", "/v2/internal/console-conf-start", body) 103 c.Assert(err, IsNil) 104 rsp2 := consoleConfStartRoutine(routineConsoleConfStartCmd, req2, nil).(*resp) 105 c.Check(rsp2.Type, Equals, ResponseTypeSync) 106 c.Assert(rsp2.Result, FitsTypeOf, &ConsoleConfStartRoutineResult{}) 107 res := rsp2.Result.(*ConsoleConfStartRoutineResult) 108 sort.Strings(res.ActiveAutoRefreshChanges) 109 sort.Strings(res.ActiveAutoRefreshSnaps) 110 expChgs := []string{chg0.ID(), chg1.ID()} 111 sort.Strings(expChgs) 112 c.Assert(res, DeepEquals, &ConsoleConfStartRoutineResult{ 113 ActiveAutoRefreshChanges: expChgs, 114 ActiveAutoRefreshSnaps: []string{"do-snap", "doing-snap"}, 115 }) 116 }