Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
ps: Avoid error when the storage is uninitialized
Browse files Browse the repository at this point in the history
Running ignite ps for the first time on a new machine fails with:
```
FATA[0000] open /var/lib/firecracker/vm: no such file or directory
```

This change handles the error returned when the backend storage is
uninitialized. NewPsOptions() returns an empty list of VMs and allows
the ps execution to continue without any error.
  • Loading branch information
darkowlzz committed Feb 6, 2021
1 parent fcf1faf commit 6f16174
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/ignite/run/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package run
import (
"bytes"
"fmt"
"strings"
"text/template"

api "github.com/weaveworks/ignite/pkg/apis/ignite"
Expand All @@ -27,6 +28,11 @@ type PsOptions struct {
func (pf *PsFlags) NewPsOptions() (po *PsOptions, err error) {
po = &PsOptions{PsFlags: pf}
po.allVMs, err = providers.Client.VMs().FindAll(filter.NewVMFilterAll("", po.All))
// If the storage is uninitialized, avoid failure and continue with empty
// VM list.
if err != nil && strings.Contains(err.Error(), "no such file or directory") {
err = nil
}
return
}

Expand Down
26 changes: 26 additions & 0 deletions cmd/ignite/run/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import (
"time"

api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/apis/ignite/scheme"
meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/libgitops/pkg/runtime"
"github.com/weaveworks/libgitops/pkg/storage"
"github.com/weaveworks/libgitops/pkg/storage/cache"
"gotest.tools/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/weaveworks/ignite/pkg/util"
Expand Down Expand Up @@ -167,3 +173,23 @@ func TestPs(t *testing.T) {
}

}

// TestNewPsOptionsStorageNotExists tests that no error is returned if the
// storage directory doesn't exist and NewPsOptions() succeeds with empty VM
// list.
func TestNewPsOptionsStorageNotExists(t *testing.T) {
// Path to a directory that doesn't exist.
dir := "/tmp/ignite-fake-storage-path"
storage := cache.NewCache(
storage.NewGenericStorage(
storage.NewGenericRawStorage(dir), scheme.Serializer))

// Create ignite client with the storage.
providers.Client = client.NewClient(storage)

// Create a new PsOptions and check result.
pf := &PsFlags{}
po, err := pf.NewPsOptions()
assert.NilError(t, err)
assert.Equal(t, 0, len(po.allVMs))
}

0 comments on commit 6f16174

Please # to comment.