Skip to content

Commit

Permalink
fix: idempotent component install update (#1599)
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-stewart authored Mar 22, 2024
1 parent 96d673a commit 8fe68d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
24 changes: 12 additions & 12 deletions cli/cmd/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ var (

// componentsUpdateCmd represents the update sub-command inside the components command
componentsUpdateCmd = &cobra.Command{
Use: "update <component>",
Short: "Update an existing component",
Long: `Update an existing component`,
Args: cobra.ExactArgs(1),
RunE: runComponentsUpdate,
Use: "update <component>",
Aliases: []string{"upgrade"},
Short: "Update an existing component",
Long: `Update an existing component`,
Args: cobra.ExactArgs(1),
RunE: runComponentsUpdate,
}

// componentsUninstallCmd represents the uninstall sub-command inside the components command
Expand Down Expand Up @@ -434,11 +435,10 @@ func installComponent(args []string) (err error) {

installedVersion := component.InstalledVersion()
if installedVersion != nil {
return errors.Errorf(
"component %s is already installed. To upgrade run '%s'",
color.HiYellowString(componentName),
color.HiGreenString("lacework component upgrade %s", componentName),
)
cli.OutputHuman("Component %s is already installed. To upgrade run '%s'\n",
component.Name,
color.HiGreenString("lacework component update %s", component.Name))
return nil
}

cli.OutputChecklist(successIcon, fmt.Sprintf("Component %s found\n", component.Name))
Expand Down Expand Up @@ -628,8 +628,8 @@ func updateComponent(args []string) (err error) {
}

if installedVersion.Equal(targetVersion) {
return errors.Errorf("You are already running version %s of this component",
color.HiYellowString(installedVersion.String()))
cli.OutputHuman("Component %s is version %s.\n", component.Name, color.HiYellowString(installedVersion.String()))
return nil
}

cli.StartProgress(fmt.Sprintf("Staging component %s...", color.HiYellowString(componentName)))
Expand Down
30 changes: 17 additions & 13 deletions integration/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCDKComponentList(t *testing.T) {
func TestComponentList(t *testing.T) {
dir := setup()

out := run(t, dir, "component", "list")
Expand All @@ -38,7 +38,7 @@ func TestCDKComponentList(t *testing.T) {
cleanup(dir)
}

func TestCDKComponentListJSON(t *testing.T) {
func TestComponentListJSON(t *testing.T) {
dir := setup()

out := run(t, dir, "component", "list", "--json")
Expand All @@ -60,7 +60,7 @@ func TestCDKComponentListJSON(t *testing.T) {
cleanup(dir)
}

func TestCDKComponentShow(t *testing.T) {
func TestComponentShow(t *testing.T) {
dir := setup()

out := run(t, dir, "component", "show", "component-example")
Expand All @@ -74,7 +74,7 @@ func TestCDKComponentShow(t *testing.T) {
cleanup(dir)
}

func TestCDKComponentShowJSON(t *testing.T) {
func TestComponentShowJSON(t *testing.T) {
dir := setup()

out := run(t, dir, "component", "show", "component-example", "--json")
Expand All @@ -95,7 +95,7 @@ func TestCDKComponentShowJSON(t *testing.T) {
cleanup(dir)
}

func TestCDKComponentInstall(t *testing.T) {
func TestComponentInstall(t *testing.T) {
dir := setup()

run(t, dir, "component", "install", "component-example")
Expand All @@ -109,15 +109,12 @@ func TestCDKComponentInstall(t *testing.T) {
out = run(t, dir, "component-example")
assert.Contains(t, out, "component")

outBytes, errBytes, exitcode := LaceworkCLIWithHome(dir, "component", "install", "component-example")
assert.NotContains(t, outBytes.String(), "Installation completed.", "STDOUT should be empty")
assert.Contains(t, errBytes.String(), "already installed")
assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one")
run(t, dir, "component", "install", "component-example")

cleanup(dir)
}

func TestCDKComponentUpdate(t *testing.T) {
func TestComponentUpdate(t *testing.T) {
dir := setup()

run(t, dir, "component", "install", "component-example", "--version", "0.9.0")
Expand All @@ -132,6 +129,13 @@ func TestCDKComponentUpdate(t *testing.T) {
assert.Contains(t, out, "component")
})

t.Run("upgrade", func(t *testing.T) {
run(t, dir, "component", "update", "component-example", "--version", "0.9.1")

run(t, dir, "component-example")
assert.Contains(t, out, "component")
})

t.Run("downgrade", func(t *testing.T) {
run(t, dir, "component", "update", "component-example", "--version", "0.8.0")

Expand All @@ -142,7 +146,7 @@ func TestCDKComponentUpdate(t *testing.T) {
cleanup(dir)
}

func TestCDKComponentUninstall(t *testing.T) {
func TestComponentUninstall(t *testing.T) {
dir := setup()

run(t, dir, "component", "install", "component-example")
Expand All @@ -164,7 +168,7 @@ func TestCDKComponentUninstall(t *testing.T) {
cleanup(dir)
}

func TestCDKComponentDev(t *testing.T) {
func TestComponentDev(t *testing.T) {
dir := setup()

// GoLang scaffolding is too slow for the test
Expand All @@ -184,7 +188,7 @@ func TestCDKComponentDev(t *testing.T) {
cleanup(dir)
}

func TestCDKComponentDevEnter(t *testing.T) {
func TestComponentDevEnter(t *testing.T) {
dir := setup()

run(t, dir, "component", "install", "component-example")
Expand Down

0 comments on commit 8fe68d3

Please sign in to comment.