summaryrefslogtreecommitdiff
diff options
authorMichael Vogt <mvo@ubuntu.com>2017-01-30 15:53:56 +0100
committerMichael Vogt <mvo@ubuntu.com>2017-01-31 09:42:00 +0100
commitb5439031b203a24cf555316c53c352cf93b05de9 (patch)
treea1d71c825299974652afbdb2991fe96577ac2611
parent8e3faa819ae05c96e206059894eae16d2f27d6e2 (diff)
Move configstate.Transaction into a new packagetransactions-pkg
The snapstate package will need to use the configuration to record when it updated last. In order to do that we need to make it possible to import the configstate.Transaction into snapstate. Therefore the configstate.Transaction is moved into a new configstate.config.Transaction package.
-rw-r--r--daemon/api.go5
-rw-r--r--daemon/api_test.go10
-rw-r--r--overlord/configstate/config/transaction.go (renamed from overlord/configstate/transaction.go)2
-rw-r--r--overlord/configstate/config/transaction_test.go (renamed from overlord/configstate/transaction_test.go)24
-rw-r--r--overlord/configstate/handler.go23
-rw-r--r--overlord/configstate/handler_test.go8
-rw-r--r--overlord/devicestate/devicemgr.go4
-rw-r--r--overlord/hookstate/ctlcmd/get.go7
-rw-r--r--overlord/hookstate/ctlcmd/get_test.go16
-rw-r--r--overlord/hookstate/ctlcmd/set.go4
-rw-r--r--overlord/hookstate/ctlcmd/set_test.go28
11 files changed, 70 insertions, 61 deletions
diff --git a/daemon/api.go b/daemon/api.go
index dfcd5bf94f..e8a4b768b1 100644
--- a/daemon/api.go
+++ b/daemon/api.go
@@ -50,6 +50,7 @@ import (
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/configstate"
+ "github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
"github.com/snapcore/snapd/overlord/ifacestate"
@@ -1421,13 +1422,13 @@ func getSnapConf(c *Command, r *http.Request, user *auth.UserState) Response {
s := c.d.overlord.State()
s.Lock()
- transaction := configstate.NewTransaction(s)
+ tr := config.NewTransaction(s)
s.Unlock()
currentConfValues := make(map[string]interface{})
for _, key := range keys {
var value interface{}
- if err := transaction.Get(snapName, key, &value); err != nil {
+ if err := tr.Get(snapName, key, &value); err != nil {
return BadRequest("%s", err)
}
diff --git a/daemon/api_test.go b/daemon/api_test.go
index 699299fa1c..f234c32cbe 100644
--- a/daemon/api_test.go
+++ b/daemon/api_test.go
@@ -54,7 +54,7 @@ import (
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
- "github.com/snapcore/snapd/overlord/configstate"
+ "github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/ifacestate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
@@ -2179,10 +2179,10 @@ func (s *apiSuite) TestGetConfSingleKey(c *check.C) {
// Set a config that we'll get in a moment
d.overlord.State().Lock()
- transaction := configstate.NewTransaction(d.overlord.State())
- transaction.Set("test-snap", "test-key1", "test-value1")
- transaction.Set("test-snap", "test-key2", "test-value2")
- transaction.Commit()
+ tr := config.NewTransaction(d.overlord.State())
+ tr.Set("test-snap", "test-key1", "test-value1")
+ tr.Set("test-snap", "test-key2", "test-value2")
+ tr.Commit()
d.overlord.State().Unlock()
result := s.runGetConf(c, []string{"test-key1"})
diff --git a/overlord/configstate/transaction.go b/overlord/configstate/config/transaction.go
index 635535b71a..201958a2b8 100644
--- a/overlord/configstate/transaction.go
+++ b/overlord/configstate/config/transaction.go
@@ -17,7 +17,7 @@
*
*/
-package configstate
+package config
import (
"encoding/json"
diff --git a/overlord/configstate/transaction_test.go b/overlord/configstate/config/transaction_test.go
index 8462c8337e..82c28da784 100644
--- a/overlord/configstate/transaction_test.go
+++ b/overlord/configstate/config/transaction_test.go
@@ -17,7 +17,7 @@
*
*/
-package configstate_test
+package config_test
import (
"encoding/json"
@@ -26,16 +26,16 @@ import (
. "gopkg.in/check.v1"
- "github.com/snapcore/snapd/overlord/configstate"
+ "github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/state"
"strings"
)
-func TestConfigState(t *testing.T) { TestingT(t) }
+func TestT(t *testing.T) { TestingT(t) }
type transactionSuite struct {
state *state.State
- transaction *configstate.Transaction
+ transaction *config.Transaction
}
var _ = Suite(&transactionSuite{})
@@ -44,7 +44,7 @@ func (s *transactionSuite) SetUpTest(c *C) {
s.state = state.New(nil)
s.state.Lock()
defer s.state.Unlock()
- s.transaction = configstate.NewTransaction(s.state)
+ s.transaction = config.NewTransaction(s.state)
}
type setGetOp string
@@ -163,7 +163,7 @@ func (s *transactionSuite) TestSetGet(c *C) {
for _, test := range setGetTests {
c.Logf("-----")
s.state.Set("config", map[string]interface{}{})
- t := configstate.NewTransaction(s.state)
+ t := config.NewTransaction(s.state)
snap := "core"
for _, op := range test {
c.Logf("%s", op)
@@ -190,7 +190,7 @@ func (s *transactionSuite) TestSetGet(c *C) {
continue
}
if expected == "-" {
- if !configstate.IsNoOption(err) {
+ if !config.IsNoOption(err) {
c.Fatalf("Expected %q key to not exist, but it has value %v", k, obtained)
}
c.Assert(err, ErrorMatches, fmt.Sprintf("snap %q has no %q configuration option", snap, k))
@@ -269,16 +269,16 @@ func (s *transactionSuite) TestGetUnmarshalError(c *C) {
c.Check(s.transaction.Set("test-snap", "foo", "good"), IsNil)
s.transaction.Commit()
- transaction := configstate.NewTransaction(s.state)
- c.Check(transaction.Set("test-snap", "foo", "break"), IsNil)
+ tr := config.NewTransaction(s.state)
+ c.Check(tr.Set("test-snap", "foo", "break"), IsNil)
// Pristine state is good, value in the transaction breaks.
broken := brokenType{`"break"`}
- err := transaction.Get("test-snap", "foo", &broken)
+ err := tr.Get("test-snap", "foo", &broken)
c.Assert(err, ErrorMatches, ".*BAM!.*")
// Pristine state breaks, nothing in the transaction.
- transaction.Commit()
- err = transaction.Get("test-snap", "foo", &broken)
+ tr.Commit()
+ err = tr.Get("test-snap", "foo", &broken)
c.Assert(err, ErrorMatches, ".*BAM!.*")
}
diff --git a/overlord/configstate/handler.go b/overlord/configstate/handler.go
index c73c63556a..1b85bfce57 100644
--- a/overlord/configstate/handler.go
+++ b/overlord/configstate/handler.go
@@ -19,7 +19,10 @@
package configstate
-import "github.com/snapcore/snapd/overlord/hookstate"
+import (
+ "github.com/snapcore/snapd/overlord/configstate/config"
+ "github.com/snapcore/snapd/overlord/hookstate"
+)
// configureHandler is the handler for the configure hook.
type configureHandler struct {
@@ -32,23 +35,23 @@ type cachedTransaction struct{}
// ContextTransaction retrieves the transaction cached within the context (and
// creates one if it hasn't already been cached).
-func ContextTransaction(context *hookstate.Context) *Transaction {
+func ContextTransaction(context *hookstate.Context) *config.Transaction {
// Check for one already cached
- transaction, ok := context.Cached(cachedTransaction{}).(*Transaction)
+ tr, ok := context.Cached(cachedTransaction{}).(*config.Transaction)
if ok {
- return transaction
+ return tr
}
// It wasn't already cached, so create and cache a new one
- transaction = NewTransaction(context.State())
+ tr = config.NewTransaction(context.State())
context.OnDone(func() error {
- transaction.Commit()
+ tr.Commit()
return nil
})
- context.Cache(cachedTransaction{}, transaction)
- return transaction
+ context.Cache(cachedTransaction{}, tr)
+ return tr
}
func newConfigureHandler(context *hookstate.Context) hookstate.Handler {
@@ -60,14 +63,14 @@ func (h *configureHandler) Before() error {
h.context.Lock()
defer h.context.Unlock()
- transaction := ContextTransaction(h.context)
+ tr := ContextTransaction(h.context)
// Initialize the transaction if there's a patch provided in the
// context.
var patch map[string]interface{}
if err := h.context.Get("patch", &patch); err == nil {
for key, value := range patch {
- transaction.Set(h.context.SnapName(), key, value)
+ tr.Set(h.context.SnapName(), key, value)
}
}
diff --git a/overlord/configstate/handler_test.go b/overlord/configstate/handler_test.go
index 18856d8709..5f0434c3de 100644
--- a/overlord/configstate/handler_test.go
+++ b/overlord/configstate/handler_test.go
@@ -20,6 +20,8 @@
package configstate_test
import (
+ "testing"
+
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/overlord/configstate"
@@ -29,6 +31,8 @@ import (
"github.com/snapcore/snapd/snap"
)
+func TestConfigState(t *testing.T) { TestingT(t) }
+
type configureHandlerSuite struct {
context *hookstate.Context
handler hookstate.Handler
@@ -62,10 +66,10 @@ func (s *configureHandlerSuite) TestBeforeInitializesTransaction(c *C) {
c.Check(s.handler.Before(), IsNil)
s.context.Lock()
- transaction := configstate.ContextTransaction(s.context)
+ tr := configstate.ContextTransaction(s.context)
s.context.Unlock()
var value string
- c.Check(transaction.Get("test-snap", "foo", &value), IsNil)
+ c.Check(tr.Get("test-snap", "foo", &value), IsNil)
c.Check(value, Equals, "bar")
}
diff --git a/overlord/devicestate/devicemgr.go b/overlord/devicestate/devicemgr.go
index 97081e8912..c0d6365d8b 100644
--- a/overlord/devicestate/devicemgr.go
+++ b/overlord/devicestate/devicemgr.go
@@ -45,7 +45,7 @@ import (
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
- "github.com/snapcore/snapd/overlord/configstate"
+ "github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
@@ -624,7 +624,7 @@ func getSerialRequestConfig(t *state.Task) (*serialRequestConfig, error) {
}
gadgetName := gadgetInfo.Name()
- tr := configstate.NewTransaction(t.State())
+ tr := config.NewTransaction(t.State())
var svcURL string
err = tr.GetMaybe(gadgetName, "device-service.url", &svcURL)
if err != nil {
diff --git a/overlord/hookstate/ctlcmd/get.go b/overlord/hookstate/ctlcmd/get.go
index 26fb3671cf..381c300765 100644
--- a/overlord/hookstate/ctlcmd/get.go
+++ b/overlord/hookstate/ctlcmd/get.go
@@ -25,6 +25,7 @@ import (
"github.com/snapcore/snapd/i18n/dumb"
"github.com/snapcore/snapd/overlord/configstate"
+ "github.com/snapcore/snapd/overlord/configstate/config"
)
type getCommand struct {
@@ -75,15 +76,15 @@ func (c *getCommand) Execute(args []string) error {
patch := make(map[string]interface{})
context.Lock()
- transaction := configstate.ContextTransaction(context)
+ tr := configstate.ContextTransaction(context)
context.Unlock()
for _, key := range c.Positional.Keys {
var value interface{}
- err := transaction.Get(c.context().SnapName(), key, &value)
+ err := tr.Get(c.context().SnapName(), key, &value)
if err == nil {
patch[key] = value
- } else if configstate.IsNoOption(err) {
+ } else if config.IsNoOption(err) {
if !c.Typed {
value = ""
}
diff --git a/overlord/hookstate/ctlcmd/get_test.go b/overlord/hookstate/ctlcmd/get_test.go
index 8f4d38b726..d0075d4b93 100644
--- a/overlord/hookstate/ctlcmd/get_test.go
+++ b/overlord/hookstate/ctlcmd/get_test.go
@@ -20,7 +20,7 @@
package ctlcmd_test
import (
- "github.com/snapcore/snapd/overlord/configstate"
+ "github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
"github.com/snapcore/snapd/overlord/hookstate/hooktest"
@@ -53,9 +53,9 @@ func (s *getSuite) SetUpTest(c *C) {
c.Assert(err, IsNil)
// Initialize configuration
- transaction := configstate.NewTransaction(state)
- transaction.Set("test-snap", "initial-key", "initial-value")
- transaction.Commit()
+ tr := config.NewTransaction(state)
+ tr.Set("test-snap", "initial-key", "initial-value")
+ tr.Commit()
}
var getTests = []struct {
@@ -106,10 +106,10 @@ func (s *getSuite) TestGetTests(c *C) {
c.Check(err, IsNil)
// Initialize configuration
- t := configstate.NewTransaction(state)
- t.Set("test-snap", "test-key1", "test-value1")
- t.Set("test-snap", "test-key2", 2)
- t.Commit()
+ tr := config.NewTransaction(state)
+ tr.Set("test-snap", "test-key1", "test-value1")
+ tr.Set("test-snap", "test-key2", 2)
+ tr.Commit()
state.Unlock()
diff --git a/overlord/hookstate/ctlcmd/set.go b/overlord/hookstate/ctlcmd/set.go
index aff9a2922a..6377d00924 100644
--- a/overlord/hookstate/ctlcmd/set.go
+++ b/overlord/hookstate/ctlcmd/set.go
@@ -61,7 +61,7 @@ func (s *setCommand) Execute(args []string) error {
}
context.Lock()
- transaction := configstate.ContextTransaction(context)
+ tr := configstate.ContextTransaction(context)
context.Unlock()
for _, patchValue := range s.Positional.ConfValues {
@@ -77,7 +77,7 @@ func (s *setCommand) Execute(args []string) error {
value = parts[1]
}
- transaction.Set(s.context().SnapName(), key, value)
+ tr.Set(s.context().SnapName(), key, value)
}
return nil
diff --git a/overlord/hookstate/ctlcmd/set_test.go b/overlord/hookstate/ctlcmd/set_test.go
index 438b739eda..497cc87779 100644
--- a/overlord/hookstate/ctlcmd/set_test.go
+++ b/overlord/hookstate/ctlcmd/set_test.go
@@ -20,7 +20,7 @@
package ctlcmd_test
import (
- "github.com/snapcore/snapd/overlord/configstate"
+ "github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
"github.com/snapcore/snapd/overlord/hookstate/hooktest"
@@ -65,11 +65,11 @@ func (s *setSuite) TestCommand(c *C) {
// Verify that the previous set doesn't modify the global state
s.mockContext.State().Lock()
- transaction := configstate.NewTransaction(s.mockContext.State())
+ tr := config.NewTransaction(s.mockContext.State())
s.mockContext.State().Unlock()
var value string
- c.Check(transaction.Get("test-snap", "foo", &value), ErrorMatches, ".*snap.*has no.*configuration.*")
- c.Check(transaction.Get("test-snap", "baz", &value), ErrorMatches, ".*snap.*has no.*configuration.*")
+ c.Check(tr.Get("test-snap", "foo", &value), ErrorMatches, ".*snap.*has no.*configuration.*")
+ c.Check(tr.Get("test-snap", "baz", &value), ErrorMatches, ".*snap.*has no.*configuration.*")
// Notify the context that we're done. This should save the config.
s.mockContext.Lock()
@@ -77,20 +77,20 @@ func (s *setSuite) TestCommand(c *C) {
c.Check(s.mockContext.Done(), IsNil)
// Verify that the global config has been updated.
- transaction = configstate.NewTransaction(s.mockContext.State())
- c.Check(transaction.Get("test-snap", "foo", &value), IsNil)
+ tr = config.NewTransaction(s.mockContext.State())
+ c.Check(tr.Get("test-snap", "foo", &value), IsNil)
c.Check(value, Equals, "bar")
- c.Check(transaction.Get("test-snap", "baz", &value), IsNil)
+ c.Check(tr.Get("test-snap", "baz", &value), IsNil)
c.Check(value, Equals, "qux")
}
func (s *setSuite) TestCommandSavesDeltasOnly(c *C) {
// Setup an initial configuration
s.mockContext.State().Lock()
- transaction := configstate.NewTransaction(s.mockContext.State())
- transaction.Set("test-snap", "test-key1", "test-value1")
- transaction.Set("test-snap", "test-key2", "test-value2")
- transaction.Commit()
+ tr := config.NewTransaction(s.mockContext.State())
+ tr.Set("test-snap", "test-key1", "test-value1")
+ tr.Set("test-snap", "test-key2", "test-value2")
+ tr.Commit()
s.mockContext.State().Unlock()
stdout, stderr, err := ctlcmd.Run(s.mockContext, []string{"set", "test-key2=test-value3"})
@@ -104,11 +104,11 @@ func (s *setSuite) TestCommandSavesDeltasOnly(c *C) {
c.Check(s.mockContext.Done(), IsNil)
// Verify that the global config has been updated, but only test-key2
- transaction = configstate.NewTransaction(s.mockContext.State())
+ tr = config.NewTransaction(s.mockContext.State())
var value string
- c.Check(transaction.Get("test-snap", "test-key1", &value), IsNil)
+ c.Check(tr.Get("test-snap", "test-key1", &value), IsNil)
c.Check(value, Equals, "test-value1")
- c.Check(transaction.Get("test-snap", "test-key2", &value), IsNil)
+ c.Check(tr.Get("test-snap", "test-key2", &value), IsNil)
c.Check(value, Equals, "test-value3")
}