Skip to content

Commit

Permalink
Update in-memory namespace on use
Browse files Browse the repository at this point in the history
Ensure the in-memory state of a must-gather/inspect's path and
project are consistent with the context being used, regardless of
wether the context is new.

Fixes #171
  • Loading branch information
gvnnn authored and Gianluca Stella committed Jun 20, 2024
1 parent 1f7cc34 commit 1f10fbe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func useContext(path string, omcConfigFile string, idFlag string) error {
NewContexts = append(NewContexts, types.Context{Id: c.Id, Path: c.Path, Current: "*", Project: c.Project})
configId = c.Id
found = true
vars.Namespace = c.Project
} else {
NewContexts = append(NewContexts, types.Context{Id: c.Id, Path: c.Path, Current: "", Project: c.Project})
}
Expand All @@ -80,8 +81,10 @@ func useContext(path string, omcConfigFile string, idFlag string) error {
}
if len(namespaces) == 1 {
NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: namespaces[0]})
vars.Namespace = namespaces[0]
} else {
NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: defaultProject})
vars.Namespace = defaultProject
}
}

Expand Down
38 changes: 38 additions & 0 deletions cmd/use/use_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package use

import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/gmeghnag/omc/types"
"github.com/gmeghnag/omc/vars"
)

func TestUseContext(t *testing.T) {
Expand Down Expand Up @@ -152,6 +154,9 @@ func TestUseContext(t *testing.T) {
if !compareConfig(actualConfig, expectedConfig, tc.ignoreConfigID) {
t.Errorf("Expected file content %+v, got %+v", expectedConfig, actualConfig)
}

// Ensure state matches the current path and project
checkContextConsistency(t, actualConfig)
})
}
}
Expand Down Expand Up @@ -195,3 +200,36 @@ func compareConfig(actualConfig types.Config, expectedConfig types.Config, ignor
// This line executes after all the condition checks.
return compareContexts(actualConfig.Contexts, expectedConfig.Contexts, ignoreConfigID)
}

// check that what's stored in memory matches the currently selected context
func checkContextConsistency(t *testing.T, config types.Config) {
t.Helper()
c, err := currentContext(config.Contexts)
if err != nil {
t.Error(err)
}

if c.Path != vars.MustGatherRootPath {
t.Errorf("in-memory path does not match current context. Want %q, got %q",
c.Path,
vars.MustGatherRootPath,
)
}

if c.Project != vars.Namespace {
t.Errorf("in-memory project does not match current context. Want %q, got %q",
c.Project,
vars.Namespace,
)
}
}

func currentContext(contexts []types.Context) (types.Context, error) {
for _, c := range contexts {
if c.Current == "*" {
return c, nil
}
}

return types.Context{}, errors.New("could not find current context")
}

0 comments on commit 1f10fbe

Please sign in to comment.