diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2017-11-20 15:43:56 -0500 |
---|---|---|
committer | Matthew Heon <matthew.heon@gmail.com> | 2017-11-20 16:24:28 -0500 |
commit | 51fc8827f54436864924ea8d4dc96111da81e5bf (patch) | |
tree | 312ed45344f42d0c16ef7b0cb5fecd1316d20f0b /libpod/sql_state.go | |
parent | 5d52f74d2136786c2874898ad373808e25609545 (diff) | |
download | podman-51fc8827f54436864924ea8d4dc96111da81e5bf.tar.gz podman-51fc8827f54436864924ea8d4dc96111da81e5bf.tar.bz2 podman-51fc8827f54436864924ea8d4dc96111da81e5bf.zip |
Add tests for SQL-backed state impl
Minor changes to container.go and sql_state.go to fix issues
identified by the tests
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Diffstat (limited to 'libpod/sql_state.go')
-rw-r--r-- | libpod/sql_state.go | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/libpod/sql_state.go b/libpod/sql_state.go index 034fc03e1..466ad66a5 100644 --- a/libpod/sql_state.go +++ b/libpod/sql_state.go @@ -109,6 +109,10 @@ func (s *SQLState) Container(id string) (*Container, error) { containerState ON containers.Id = containerState.Id WHERE containers.Id=?;` + if id == "" { + return nil, ErrEmptyID + } + if !s.valid { return nil, ErrDBClosed } @@ -138,6 +142,10 @@ func (s *SQLState) LookupContainer(idOrName string) (*Container, error) { containerState ON containers.Id = containerState.Id WHERE (containers.Id LIKE ?) OR containers.Name=?;` + if idOrName == "" { + return nil, ErrEmptyID + } + if !s.valid { return nil, ErrDBClosed } @@ -178,6 +186,10 @@ func (s *SQLState) LookupContainer(idOrName string) (*Container, error) { func (s *SQLState) HasContainer(id string) (bool, error) { const query = "SELECT 1 FROM containers WHERE Id=?;" + if id == "" { + return false, ErrEmptyID + } + if !s.valid { return false, ErrDBClosed } @@ -225,23 +237,6 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) { return errors.Wrapf(err, "error marshaling container %s labels to JSON", ctr.ID()) } - // Save the container's runtime spec to disk - specJSON, err := json.Marshal(ctr.config.Spec) - if err != nil { - return errors.Wrapf(err, "error marshalling container %s spec to JSON", ctr.ID()) - } - specPath := getSpecPath(s.specsDir, ctr.ID()) - if err := ioutil.WriteFile(specPath, specJSON, 0750); err != nil { - return errors.Wrapf(err, "error saving container %s spec JSON to disk", ctr.ID()) - } - defer func() { - if err != nil { - if err2 := os.Remove(specPath); err2 != nil { - logrus.Errorf("Error removing container %s JSON spec from state: %v", ctr.ID(), err2) - } - } - }() - s.lock.Lock() defer s.lock.Unlock() @@ -288,6 +283,23 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) { return errors.Wrapf(err, "error adding container %s state to database", ctr.ID()) } + // Save the container's runtime spec to disk + specJSON, err := json.Marshal(ctr.config.Spec) + if err != nil { + return errors.Wrapf(err, "error marshalling container %s spec to JSON", ctr.ID()) + } + specPath := getSpecPath(s.specsDir, ctr.ID()) + if err := ioutil.WriteFile(specPath, specJSON, 0750); err != nil { + return errors.Wrapf(err, "error saving container %s spec JSON to disk", ctr.ID()) + } + defer func() { + if err != nil { + if err2 := os.Remove(specPath); err2 != nil { + logrus.Errorf("Error removing container %s JSON spec from state: %v", ctr.ID(), err2) + } + } + }() + if err := tx.Commit(); err != nil { return errors.Wrapf(err, "error committing transaction to add container %s", ctr.ID()) } @@ -411,7 +423,7 @@ func (s *SQLState) SaveContainer(ctr *Container) error { }() // Add container state to the database - _, err = tx.Exec(update, + result, err := tx.Exec(update, ctr.state.State, ctr.state.ConfigPath, ctr.state.RunDir, @@ -423,6 +435,13 @@ func (s *SQLState) SaveContainer(ctr *Container) error { if err != nil { return errors.Wrapf(err, "error updating container %s state in database", ctr.ID()) } + rows, err := result.RowsAffected() + if err != nil { + return errors.Wrapf(err, "error retrieving number of rows modified by update of container %s", ctr.ID()) + } + if rows == 0 { + return ErrNoSuchCtr + } if err := tx.Commit(); err != nil { return errors.Wrapf(err, "error committing transaction to update container %s", ctr.ID()) |