-
Notifications
You must be signed in to change notification settings - Fork 12
PMM-4879: defaults-file param #171
base: main
Are you sure you want to change the base?
Changes from 13 commits
b85b140
16b5d40
c97ebdd
fa676c4
019efcf
c872a6e
49cfcf3
95f50de
c261e2f
fca7544
5e0abde
46da034
e4188c2
55a1a43
14a8539
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,11 +25,15 @@ import ( | |
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os/user" | ||
"path/filepath" | ||
"reflect" | ||
"regexp" | ||
"strings" | ||
"text/template" | ||
|
||
"gopkg.in/ini.v1" | ||
|
||
"github.com/go-openapi/runtime" | ||
httptransport "github.com/go-openapi/runtime/client" | ||
inventorypb "github.com/percona/pmm/api/inventorypb/json/client" | ||
|
@@ -69,6 +73,10 @@ type Command interface { | |
Run() (Result, error) | ||
} | ||
|
||
type ApplyDefaults interface { | ||
ApplyDefaults(cfg *ini.File) | ||
} | ||
|
||
// TODO remove Command above, rename CommandWithContext to Command | ||
type CommandWithContext interface { | ||
// TODO rename to Run | ||
|
@@ -115,6 +123,7 @@ type globalFlagsValues struct { | |
ServerInsecureTLS bool | ||
Debug bool | ||
Trace bool | ||
DefaultConfig string | ||
} | ||
|
||
// GlobalFlags contains pmm-admin core flags values. | ||
|
@@ -184,6 +193,37 @@ func (e errFromNginx) GoString() string { | |
return fmt.Sprintf("errFromNginx(%q)", string(e)) | ||
} | ||
|
||
func ConfigureDefaults(config string, cmd ApplyDefaults) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think its better to rename it to |
||
if config != "" { | ||
var err error | ||
config, err = expandPath(config) | ||
if err != nil { | ||
return fmt.Errorf("fail to normalize path: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
} | ||
cfg, err := ini.Load(config) | ||
if err != nil { | ||
return fmt.Errorf("fail to read config file: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
} | ||
|
||
cmd.ApplyDefaults(cfg) | ||
} else { | ||
logrus.Debug("default config not provided") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func expandPath(path string) (string, error) { | ||
if strings.HasPrefix(path, "~/") { | ||
usr, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
} | ||
return filepath.Join(usr.HomeDir, path[2:]), nil | ||
} | ||
return path, nil | ||
} | ||
|
||
// SetupClients configures local and PMM Server API clients. | ||
func SetupClients(ctx context.Context, serverURL string) { | ||
agentlocal.SetTransport(ctx, GlobalFlags.Debug || GlobalFlags.Trace) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,12 @@ import ( | |
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/user" | ||
"strings" | ||
"testing" | ||
|
||
"gopkg.in/ini.v1" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's remove this blank line |
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
@@ -110,3 +113,63 @@ func TestReadFile(t *testing.T) { | |
require.Empty(t, certificate) | ||
}) | ||
} | ||
|
||
type cmdWithDefaultsApply struct { | ||
applyDefaultCalled bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. used for tests only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why don't use mock libraries? |
||
user string | ||
password string | ||
} | ||
|
||
func (c *cmdWithDefaultsApply) ApplyDefaults(cfg *ini.File) { | ||
c.user = cfg.Section("client").Key("user").String() | ||
c.password = cfg.Section("client").Key("password").String() | ||
c.applyDefaultCalled = true | ||
} | ||
|
||
func TestConfigureDefaults(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
t.Run("ApplyDefaults is called if command supports it", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
file, e := os.Open("../testdata/.my.cnf") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BupycHuk you were asking for it. I use real file in cmd test, but tmp files for |
||
if e != nil { | ||
t.Fatal(e) | ||
} | ||
|
||
cmd := &cmdWithDefaultsApply{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
|
||
if err := ConfigureDefaults(file.Name(), cmd); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, "root", cmd.user) | ||
assert.Equal(t, "toor", cmd.password) | ||
}) | ||
|
||
t.Run("ApplyDefaults is not called if pass is not setup", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
cmd := &cmdWithDefaultsApply{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
|
||
if err := ConfigureDefaults("", cmd); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, "", cmd.user) | ||
assert.Equal(t, "", cmd.password) | ||
assert.False(t, cmd.applyDefaultCalled) | ||
}) | ||
} | ||
|
||
func TestExpandPath(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
t.Run("relative to userhome", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
actual, err := expandPath("~/") | ||
assert.NoError(t, err) | ||
usr, err := user.Current() | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, usr.HomeDir, actual) | ||
}) | ||
t.Run("relative to userhome", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it doesn't look relative to userhome |
||
originalPath := "./test" | ||
actual, err := expandPath(originalPath) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, originalPath, actual) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// pmm-admin | ||
// Copyright 2019 Percona LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package commands | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
func DefaultConfig(val string) (f *os.File, cleanup func(), e error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
file, err := ioutil.TempFile("", "test-pmm-admin-defaults-*.cnf") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
f = file | ||
cleanup = func() { | ||
_ = os.Remove(file.Name()) | ||
} | ||
|
||
if _, err := file.WriteString(val); err != nil { | ||
return nil, nil, err | ||
} | ||
if err := file.Sync(); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,17 @@ import ( | |
"strconv" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"gopkg.in/ini.v1" | ||
|
||
"github.com/percona/pmm-admin/agentlocal" | ||
|
||
Comment on lines
+24
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please fix imports to split them in 3 blocks
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here and in other files |
||
"github.com/AlekSi/pointer" | ||
"github.com/alecthomas/units" | ||
"github.com/percona/pmm/api/managementpb/json/client" | ||
mysql "github.com/percona/pmm/api/managementpb/json/client/my_sql" | ||
|
||
"github.com/percona/pmm-admin/agentlocal" | ||
"github.com/percona/pmm-admin/commands" | ||
) | ||
|
||
|
@@ -116,6 +121,25 @@ type addMySQLCommand struct { | |
CreateUser bool | ||
} | ||
|
||
func (cmd *addMySQLCommand) ApplyDefaults(cfg *ini.File) { | ||
defaultUsername := cfg.Section("client").Key("user").String() | ||
if defaultUsername != "" { | ||
if cmd.Username == "" { | ||
cmd.Username = defaultUsername | ||
} else { | ||
logrus.Debug("default username is not used, it is already set") | ||
} | ||
} | ||
defaultPassword := cfg.Section("client").Key("password").String() | ||
if defaultPassword != "" { | ||
if cmd.Password == "" { | ||
cmd.Password = defaultPassword | ||
} else { | ||
logrus.Debug("default password is not used, it is already set") | ||
} | ||
} | ||
} | ||
|
||
func (cmd *addMySQLCommand) GetServiceName() string { | ||
return cmd.ServiceName | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,10 @@ import ( | |
"strings" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/percona/pmm-admin/commands" | ||
|
||
mysql "github.com/percona/pmm/api/managementpb/json/client/my_sql" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
@@ -160,3 +164,87 @@ func TestRun(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestApplyDefaults(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
t.Run("password and username is set", func(t *testing.T) { | ||
file, cleanup, e := commands.DefaultConfig("[client]\nuser=root\npassword=toor\n") | ||
if e != nil { | ||
t.Fatal(e) | ||
} | ||
defer cleanup() | ||
|
||
cmd := &addMySQLCommand{} | ||
|
||
commands.ConfigureDefaults(file.Name(), cmd) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
|
||
assert.Equal(t, "root", cmd.Username) | ||
assert.Equal(t, "toor", cmd.Password) | ||
}) | ||
|
||
t.Run("password and username from config have lower priority", func(t *testing.T) { | ||
logrus.SetLevel(logrus.TraceLevel) | ||
file, cleanup, e := commands.DefaultConfig("[client]\nuser=root\npassword=toor\n") | ||
if e != nil { | ||
t.Fatal(e) | ||
} | ||
defer cleanup() | ||
|
||
cmd := &addMySQLCommand{ | ||
Username: "default-username", | ||
Password: "default-password", | ||
} | ||
|
||
commands.ConfigureDefaults(file.Name(), cmd) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
|
||
assert.Equal(t, "default-username", cmd.Username) | ||
assert.Equal(t, "default-password", cmd.Password) | ||
}) | ||
|
||
t.Run("not updated if not set", func(t *testing.T) { | ||
file, cleanup, e := commands.DefaultConfig("") | ||
if e != nil { | ||
t.Fatal(e) | ||
} | ||
defer cleanup() | ||
|
||
cmd := &addMySQLCommand{ | ||
Username: "default-username", | ||
Password: "default-password", | ||
} | ||
|
||
commands.ConfigureDefaults(file.Name(), cmd) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
|
||
assert.Equal(t, "default-username", cmd.Username) | ||
assert.Equal(t, "default-password", cmd.Password) | ||
}) | ||
|
||
t.Run("only username is set", func(t *testing.T) { | ||
file, cleanup, e := commands.DefaultConfig("[client]\nuser=root\n") | ||
if e != nil { | ||
t.Fatal(e) | ||
} | ||
defer cleanup() | ||
|
||
cmd := &addMySQLCommand{} | ||
|
||
commands.ConfigureDefaults(file.Name(), cmd) | ||
|
||
assert.Equal(t, "root", cmd.Username) | ||
assert.Equal(t, "", cmd.Password) | ||
}) | ||
|
||
t.Run("only password is set", func(t *testing.T) { | ||
file, cleanup, e := commands.DefaultConfig("[client]\npassword=toor\n") | ||
if e != nil { | ||
t.Fatal(e) | ||
} | ||
defer cleanup() | ||
|
||
cmd := &addMySQLCommand{} | ||
|
||
commands.ConfigureDefaults(file.Name(), cmd) | ||
|
||
assert.Equal(t, "", cmd.Username) | ||
assert.Equal(t, "toor", cmd.Password) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[client] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
host=127.0.0.1 | ||
port=12345 | ||
user=root | ||
password=toor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫 [golangci-lint] reported by reviewdog 🐶
exported type
ApplyDefaults
should have comment or be unexported (golint)