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

ps: Handle error when the backend storage is uninitialized #778

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}