From 51fc8827f54436864924ea8d4dc96111da81e5bf Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Mon, 20 Nov 2017 15:43:56 -0500 Subject: 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 --- libpod/sql_state.go | 55 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 18 deletions(-) (limited to 'libpod/sql_state.go') 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()) -- cgit v1.2.3-54-g00ecf