Merge pull request #19527 from influxdata/fix-put-variable

fix(variables): use Update function for Put Variable
pull/19467/head
Alirie Gray 2020-09-11 12:53:11 -07:00 committed by GitHub
commit 127d894960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 1 deletions

View File

@ -215,9 +215,12 @@ func (s *Service) CreateVariable(ctx context.Context, v *influxdb.Variable) erro
})
}
// ReplaceVariable puts a variable in the store
// ReplaceVariable replaces a variable that exists in the store or creates it if it does not
func (s *Service) ReplaceVariable(ctx context.Context, v *influxdb.Variable) error {
return s.kv.Update(ctx, func(tx Tx) error {
if found, _ := s.findVariableByID(ctx, tx, v.ID); found != nil {
return s.putVariable(ctx, tx, v, PutUpdate())
}
return s.putVariable(ctx, tx, v, PutNew())
})
}

View File

@ -85,6 +85,10 @@ func VariableService(
name: "UpdateVariable",
fn: UpdateVariable,
},
{
name: "ReplaceVariable",
fn: ReplaceVariable,
},
{
name: "DeleteVariable",
fn: DeleteVariable,
@ -1132,6 +1136,114 @@ func UpdateVariable(init func(VariableFields, *testing.T) (influxdb.VariableServ
}
}
// ReplaceVariable tests influxdb.VariableService ReplaceVariable interface method
func ReplaceVariable(init func(VariableFields, *testing.T) (influxdb.VariableService, string, func()), t *testing.T) {
type args struct {
id influxdb.ID
newVariable *influxdb.Variable
}
type wants struct {
err *influxdb.Error
variables []*influxdb.Variable
}
tests := []struct {
name string
fields VariableFields
args args
wants wants
}{
{
name: "updating a variable's name",
fields: VariableFields{
TimeGenerator: fakeGenerator,
Variables: []*influxdb.Variable{
{
ID: MustIDBase16(idA),
OrganizationID: influxdb.ID(7),
Name: "existing-variable",
Arguments: &influxdb.VariableArguments{
Type: "constant",
Values: influxdb.VariableConstantValues{},
},
CRUDLog: influxdb.CRUDLog{
CreatedAt: oldFakeDate,
UpdatedAt: fakeDate,
},
},
},
},
args: args{
id: MustIDBase16(idB),
newVariable: &influxdb.Variable{
ID: MustIDBase16(idA),
OrganizationID: influxdb.ID(7),
Name: "renamed-variable",
Arguments: &influxdb.VariableArguments{
Type: "constant",
Values: influxdb.VariableConstantValues{},
},
CRUDLog: influxdb.CRUDLog{
CreatedAt: oldFakeDate,
UpdatedAt: fakeDate,
},
},
},
wants: wants{
err: nil,
variables: []*influxdb.Variable{
{
ID: MustIDBase16(idA),
OrganizationID: influxdb.ID(7),
Name: "renamed-variable",
Arguments: &influxdb.VariableArguments{
Type: "constant",
Values: influxdb.VariableConstantValues{},
},
CRUDLog: influxdb.CRUDLog{
CreatedAt: oldFakeDate,
UpdatedAt: fakeDate,
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, _, done := init(tt.fields, t)
defer done()
ctx := context.Background()
err := s.ReplaceVariable(ctx, tt.args.newVariable)
influxErrsEqual(t, tt.wants.err, err)
if err != nil {
return
}
variable, err := s.FindVariableByID(ctx, tt.args.id)
if err != nil {
return
}
if variable != nil {
if tt.args.newVariable.Name != "" && variable.Name != tt.args.newVariable.Name {
t.Fatalf("variable name not updated")
}
}
variables, err := s.FindVariables(ctx, influxdb.VariableFilter{})
if err != nil {
t.Fatalf("failed to retrieve variables: %v", err)
}
if diff := cmp.Diff(variables, tt.wants.variables, variableCmpOptions...); diff != "" {
t.Fatalf("found unexpected variables -got/+want\ndiff %s", diff)
}
})
}
}
// DeleteVariable tests influxdb.VariableService DeleteVariable interface method
func DeleteVariable(init func(VariableFields, *testing.T) (influxdb.VariableService, string, func()), t *testing.T) {
type args struct {