github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/configstate/configcore/refresh_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017-2018 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 configcore_test 21 22 import ( 23 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/overlord/configstate/configcore" 26 ) 27 28 type refreshSuite struct { 29 configcoreSuite 30 } 31 32 var _ = Suite(&refreshSuite{}) 33 34 func (s *refreshSuite) TestConfigureRefreshTimerHappy(c *C) { 35 err := configcore.Run(&mockConf{ 36 state: s.state, 37 conf: map[string]interface{}{ 38 "refresh.timer": "8:00~12:00/2", 39 }, 40 }) 41 c.Assert(err, IsNil) 42 } 43 44 func (s *refreshSuite) TestConfigureRefreshTimerRejected(c *C) { 45 err := configcore.Run(&mockConf{ 46 state: s.state, 47 conf: map[string]interface{}{ 48 "refresh.timer": "invalid", 49 }, 50 }) 51 c.Assert(err, ErrorMatches, `cannot parse "invalid": "invalid" is not a valid weekday`) 52 } 53 54 func (s *refreshSuite) TestConfigureLegacyRefreshScheduleHappy(c *C) { 55 err := configcore.Run(&mockConf{ 56 state: s.state, 57 conf: map[string]interface{}{ 58 "refresh.schedule": "8:00-12:00", 59 }, 60 }) 61 c.Assert(err, IsNil) 62 } 63 64 func (s *refreshSuite) TestConfigureLegacyRefreshScheduleRejected(c *C) { 65 err := configcore.Run(&mockConf{ 66 state: s.state, 67 conf: map[string]interface{}{ 68 "refresh.schedule": "invalid", 69 }, 70 }) 71 c.Assert(err, ErrorMatches, `cannot parse "invalid": not a valid interval`) 72 73 // check that refresh.schedule is verified against legacy parser 74 err = configcore.Run(&mockConf{ 75 state: s.state, 76 conf: map[string]interface{}{ 77 "refresh.schedule": "8:00~12:00/2", 78 }, 79 }) 80 c.Assert(err, ErrorMatches, `cannot parse "8:00~12:00": not a valid interval`) 81 } 82 83 func (s *refreshSuite) TestConfigureRefreshHoldHappy(c *C) { 84 err := configcore.Run(&mockConf{ 85 state: s.state, 86 conf: map[string]interface{}{ 87 "refresh.hold": "2018-08-18T15:00:00Z", 88 }, 89 }) 90 c.Assert(err, IsNil) 91 } 92 93 func (s *refreshSuite) TestConfigureRefreshHoldInvalid(c *C) { 94 err := configcore.Run(&mockConf{ 95 state: s.state, 96 conf: map[string]interface{}{ 97 "refresh.hold": "invalid", 98 }, 99 }) 100 c.Assert(err, ErrorMatches, `refresh\.hold cannot be parsed:.*`) 101 } 102 103 func (s *refreshSuite) TestConfigureRefreshHoldOnMeteredInvalid(c *C) { 104 err := configcore.Run(&mockConf{ 105 state: s.state, 106 conf: map[string]interface{}{ 107 "refresh.metered": "invalid", 108 }, 109 }) 110 c.Assert(err, ErrorMatches, `refresh\.metered value "invalid" is invalid`) 111 } 112 113 func (s *refreshSuite) TestConfigureRefreshHoldOnMeteredHappy(c *C) { 114 err := configcore.Run(&mockConf{ 115 state: s.state, 116 conf: map[string]interface{}{ 117 "refresh.metered": "hold", 118 }, 119 }) 120 c.Assert(err, IsNil) 121 122 err = configcore.Run(&mockConf{ 123 state: s.state, 124 conf: map[string]interface{}{ 125 "refresh.metered": "", 126 }, 127 }) 128 c.Assert(err, IsNil) 129 } 130 131 func (s *refreshSuite) TestConfigureRefreshRetainHappy(c *C) { 132 err := configcore.Run(&mockConf{ 133 state: s.state, 134 conf: map[string]interface{}{ 135 "refresh.retain": "4", 136 }, 137 }) 138 c.Assert(err, IsNil) 139 } 140 141 func (s *refreshSuite) TestConfigureRefreshRetainUnderRange(c *C) { 142 err := configcore.Run(&mockConf{ 143 state: s.state, 144 conf: map[string]interface{}{ 145 "refresh.retain": "1", 146 }, 147 }) 148 c.Assert(err, ErrorMatches, `retain must be a number between 2 and 20, not "1"`) 149 } 150 151 func (s *refreshSuite) TestConfigureRefreshRetainOverRange(c *C) { 152 err := configcore.Run(&mockConf{ 153 state: s.state, 154 conf: map[string]interface{}{ 155 "refresh.retain": "100", 156 }, 157 }) 158 c.Assert(err, ErrorMatches, `retain must be a number between 2 and 20, not "100"`) 159 } 160 161 func (s *refreshSuite) TestConfigureRefreshRetainInvalid(c *C) { 162 err := configcore.Run(&mockConf{ 163 state: s.state, 164 conf: map[string]interface{}{ 165 "refresh.retain": "invalid", 166 }, 167 }) 168 c.Assert(err, ErrorMatches, `retain must be a number between 2 and 20, not "invalid"`) 169 }