Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): don't overwrite stack name/desc on stack update #20362

Merged
merged 4 commits into from
Dec 17, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
1. [20348](https://github.com/influxdata/influxdb/pull/20348): Ensure `config.toml` is initialized on fresh `influxdb2` installs.
1. [20349](https://github.com/influxdata/influxdb/pull/20349): Ensure `influxdb` service sees default env variables when running under `init.d`.
1. [20317](https://github.com/influxdata/influxdb/pull/20317): Don't ignore failures to set password during initial user onboarding.
1. [20362](https://github.com/influxdata/influxdb/pull/20362): Don't overwrite stack name/description on `influx stack update`.

## v2.0.3 [2020-12-14]

Expand Down
13 changes: 11 additions & 2 deletions cmd/influx/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,10 +888,19 @@ func (b *cmdTemplateBuilder) stackUpdateRunEFn(cmd *cobra.Command, args []string
return ierror.Wrap(err, "required stack id is invalid")
}

var name, description *string

if cmd.Flags().Lookup("stack-name").Changed {
name = &b.name
}
if cmd.Flags().Lookup("stack-description").Changed {
description = &b.description
}

update := pkger.StackUpdate{
ID: *stackID,
Name: &b.name,
Description: &b.description,
Name: name,
Description: description,
TemplateURLs: b.urls,
}

Expand Down
124 changes: 120 additions & 4 deletions cmd/influx/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,118 @@ func Test_Template_Commands(t *testing.T) {
t.Run(tt.name, fn)
}
})

t.Run("update", func(t *testing.T) {
tests := []struct {
name string
args []string
expectedStack pkger.Stack
}{
{
name: "when stack name and decription are set",
args: []string{"--stack-name=updated_name", "--stack-description=updated_description"},
expectedStack: pkger.Stack{
OrgID: 1,
Events: []pkger.StackEvent{{
Name: "updated_name",
Description: "updated_description",
}},
},
},
{
name: "when only stack name is set",
args: []string{"--stack-name=updated_name"},
expectedStack: pkger.Stack{
OrgID: 1,
Events: []pkger.StackEvent{{
Name: "updated_name",
Description: "original_description",
}},
},
},
{
name: "when only stack description is set",
args: []string{"--stack-description=updated_description"},
expectedStack: pkger.Stack{
OrgID: 1,
Events: []pkger.StackEvent{{
Name: "original_name",
Description: "updated_description",
}},
},
},
{
name: "when neither stack name/description is set",
args: []string{},
expectedStack: pkger.Stack{
OrgID: 1,
Events: []pkger.StackEvent{{
Name: "original_name",
Description: "original_description",
}},
},
},
}

for _, tt := range tests {
fn := func(t *testing.T) {
defer addEnvVars(t, envVarsZeroMap)()

outBuf := new(bytes.Buffer)
defer func() {
if t.Failed() && outBuf.Len() > 0 {
t.Log(outBuf.String())
}
}()

builder := newInfluxCmdBuilder(
in(new(bytes.Buffer)),
out(outBuf),
)

initialStack := pkger.Stack{
ID: 9000,
OrgID: 1,
Events: []pkger.StackEvent{
{
Name: "original_name",
Description: "original_description",
},
},
}
rootCmd := builder.cmd(func(f *globalFlags, opt genericCLIOpts) *cobra.Command {
echoSVC := &fakePkgSVC{
updateStackFn: func(ctx context.Context, stUpdate pkger.StackUpdate) (pkger.Stack, error) {
returnStack := initialStack
if stUpdate.Name != nil {
returnStack.Events[0].Name = *stUpdate.Name
}
if stUpdate.Description != nil {
returnStack.Events[0].Description = *stUpdate.Description
}
return returnStack, nil
},
}
return newCmdPkgerBuilder(fakeSVCFn(echoSVC), f, opt).cmdStacks()
})

baseArgs := []string{"stacks", "update", "--stack-id=" + influxdb.ID(1).String(), "--json"}

rootCmd.SetArgs(append(baseArgs, tt.args...))

err := rootCmd.Execute()
require.NoError(t, err)
var stack pkger.Stack
testDecodeJSONBody(t, outBuf, &stack)
if tt.expectedStack.ID == 0 {
tt.expectedStack.ID = 9000
}
assert.Equal(t, tt.expectedStack, stack)
}

t.Run(tt.name, fn)
}
})
})
}

Expand Down Expand Up @@ -776,10 +888,11 @@ func testPkgWritesToBuffer(newCmdFn func(w io.Writer) *cobra.Command, args templ
}

type fakePkgSVC struct {
initStackFn func(ctx context.Context, userID influxdb.ID, stack pkger.StackCreate) (pkger.Stack, error)
exportFn func(ctx context.Context, setters ...pkger.ExportOptFn) (*pkger.Template, error)
dryRunFn func(ctx context.Context, orgID, userID influxdb.ID, opts ...pkger.ApplyOptFn) (pkger.ImpactSummary, error)
applyFn func(ctx context.Context, orgID, userID influxdb.ID, opts ...pkger.ApplyOptFn) (pkger.ImpactSummary, error)
initStackFn func(ctx context.Context, userID influxdb.ID, stack pkger.StackCreate) (pkger.Stack, error)
updateStackFn func(ctx context.Context, upd pkger.StackUpdate) (pkger.Stack, error)
exportFn func(ctx context.Context, setters ...pkger.ExportOptFn) (*pkger.Template, error)
dryRunFn func(ctx context.Context, orgID, userID influxdb.ID, opts ...pkger.ApplyOptFn) (pkger.ImpactSummary, error)
applyFn func(ctx context.Context, orgID, userID influxdb.ID, opts ...pkger.ApplyOptFn) (pkger.ImpactSummary, error)
}

var _ pkger.SVC = (*fakePkgSVC)(nil)
Expand Down Expand Up @@ -812,6 +925,9 @@ func (f *fakePkgSVC) ReadStack(ctx context.Context, id influxdb.ID) (pkger.Stack
}

func (f *fakePkgSVC) UpdateStack(ctx context.Context, upd pkger.StackUpdate) (pkger.Stack, error) {
if f.updateStackFn != nil {
return f.updateStackFn(ctx, upd)
}
panic("not implemented")
}

Expand Down