From 34ba0cb8a922347364afaa14f199409ad7dc2451 Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Wed, 22 Nov 2017 12:21:53 -0500 Subject: Order containers returned from state and make container config public Signed-off-by: Matthew Heon Closes: #63 Approved by: baude --- libpod/container.go | 18 +++++++++++++----- libpod/sql_state.go | 3 ++- libpod/sql_state_internal.go | 2 +- libpod/sql_state_test.go | 13 ++++++++++++- 4 files changed, 28 insertions(+), 8 deletions(-) (limited to 'libpod') diff --git a/libpod/container.go b/libpod/container.go index 9b48d2ca1..8ad0bb10d 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -46,7 +46,7 @@ const ( // Container is a single OCI container type Container struct { - config *containerConfig + config *ContainerConfig pod *Pod runningSpec *spec.Spec @@ -59,7 +59,7 @@ type Container struct { runtime *Runtime } -// containerState contains the current state of the container +// containerRuntimeInfo contains the current state of the container // It is stored on disk in a tmpfs and recreated on reboot type containerRuntimeInfo struct { // The current state of the running container @@ -88,10 +88,10 @@ type containerRuntimeInfo struct { // TODO: Save information about image used in container if one is used } -// containerConfig contains all information that was used to create the +// ContainerConfig contains all information that was used to create the // container. It may not be changed once created. // It is stored, read-only, on disk -type containerConfig struct { +type ContainerConfig struct { Spec *spec.Spec `json:"spec"` ID string `json:"id"` Name string `json:"name"` @@ -153,6 +153,14 @@ func (c *Container) Labels() map[string]string { return labels } +// Config returns the configuration used to create the container +func (c *Container) Config() *ContainerConfig { + returnConfig := new(ContainerConfig) + deepcopier.Copy(c.config).To(returnConfig) + + return returnConfig +} + // LogPath returns the path to the container's log file // This file will only be present after Init() is called to create the container // in runc @@ -235,7 +243,7 @@ func newContainer(rspec *spec.Spec) (*Container, error) { } ctr := new(Container) - ctr.config = new(containerConfig) + ctr.config = new(ContainerConfig) ctr.state = new(containerRuntimeInfo) ctr.config.ID = stringid.GenerateNonCryptoID() diff --git a/libpod/sql_state.go b/libpod/sql_state.go index 893223914..8b18a8959 100644 --- a/libpod/sql_state.go +++ b/libpod/sql_state.go @@ -544,7 +544,8 @@ func (s *SQLState) AllContainers() ([]*Container, error) { containerState.Pid FROM containers INNER JOIN - containerState ON containers.Id = containerState.Id;` + containerState ON containers.Id = containerState.Id + ORDER BY containers.CreatedTime DESC;` if !s.valid { return nil, ErrDBClosed diff --git a/libpod/sql_state_internal.go b/libpod/sql_state_internal.go index b6816e008..58a6daa58 100644 --- a/libpod/sql_state_internal.go +++ b/libpod/sql_state_internal.go @@ -186,7 +186,7 @@ func ctrFromScannable(row scannable, runtime *Runtime, specsDir string) (*Contai } ctr := new(Container) - ctr.config = new(containerConfig) + ctr.config = new(ContainerConfig) ctr.state = new(containerRuntimeInfo) ctr.config.ID = id diff --git a/libpod/sql_state_test.go b/libpod/sql_state_test.go index 5ccd069d3..9f6b5d078 100644 --- a/libpod/sql_state_test.go +++ b/libpod/sql_state_test.go @@ -15,7 +15,7 @@ import ( func getTestContainer(id, name string) *Container { ctr := &Container{ - config: &containerConfig{ + config: &ContainerConfig{ ID: id, Name: name, RootfsImageID: id, @@ -518,4 +518,15 @@ func TestGetAllContainersTwoContainers(t *testing.T) { ctrs, err := state.AllContainers() assert.NoError(t, err) assert.Equal(t, 2, len(ctrs)) + + // Containers should be ordered by creation time + + // Use assert.EqualValues if the test fails to pretty print diff + // between actual and expected + if !testContainersEqual(testCtr2, ctrs[0]) { + assert.EqualValues(t, testCtr2, ctrs[0]) + } + if !testContainersEqual(testCtr1, ctrs[1]) { + assert.EqualValues(t, testCtr1, ctrs[1]) + } } -- cgit v1.2.3-54-g00ecf