Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions client/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ func (c *Channel) runReceiver() {
ID: msg.Id,
Payload: p.PbmSwitchPitr,
}
case *agentpb.ServerMessage_ParseDefaultsFile:
c.requests <- &ServerRequest{
ID: msg.Id,
Payload: p.ParseDefaultsFile,
}

// responses
case *agentpb.ServerMessage_Pong:
Expand Down
30 changes: 17 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ const (

// Client represents pmm-agent's connection to nginx/pmm-managed.
type Client struct {
cfg *config.Config
supervisor supervisor
connectionChecker connectionChecker
softwareVersioner softwareVersioner
cfg *config.Config
supervisor supervisor
connectionChecker connectionChecker
softwareVersioner softwareVersioner
defaultsFileParser defaultsFileParser

l *logrus.Entry
backoff *backoff.Backoff
Expand All @@ -78,16 +79,17 @@ type Client struct {
// New creates new client.
//
// Caller should call Run.
func New(cfg *config.Config, supervisor supervisor, connectionChecker connectionChecker, sv softwareVersioner) *Client {
func New(cfg *config.Config, supervisor supervisor, connectionChecker connectionChecker, sv softwareVersioner, dfp defaultsFileParser) *Client {
return &Client{
cfg: cfg,
supervisor: supervisor,
connectionChecker: connectionChecker,
softwareVersioner: sv,
l: logrus.WithField("component", "client"),
backoff: backoff.New(backoffMinDelay, backoffMaxDelay),
done: make(chan struct{}),
dialTimeout: dialTimeout,
cfg: cfg,
supervisor: supervisor,
connectionChecker: connectionChecker,
softwareVersioner: sv,
l: logrus.WithField("component", "client"),
backoff: backoff.New(backoffMinDelay, backoffMaxDelay),
done: make(chan struct{}),
dialTimeout: dialTimeout,
defaultsFileParser: dfp,
}
}

Expand Down Expand Up @@ -436,6 +438,8 @@ func (c *Client) processChannelRequests(ctx context.Context) {
resp.Error = err.Error()
}
responsePayload = &resp
case *agentpb.ParseDefaultsFileRequest:
responsePayload = c.defaultsFileParser.ParseDefaultsFile(p)
default:
c.l.Errorf("Unhandled server request: %v.", req)
}
Expand Down
14 changes: 7 additions & 7 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestClient(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

cfg := &config.Config{}
client := New(cfg, nil, nil, nil)
client := New(cfg, nil, nil, nil, nil)
cancel()
err := client.Run(ctx)
assert.EqualError(t, err, "missing PMM Server address: context canceled")
Expand All @@ -93,7 +93,7 @@ func TestClient(t *testing.T) {
Address: "127.0.0.1:1",
},
}
client := New(cfg, nil, nil, nil)
client := New(cfg, nil, nil, nil, nil)
cancel()
err := client.Run(ctx)
assert.EqualError(t, err, "missing Agent ID: context canceled")
Expand All @@ -110,7 +110,7 @@ func TestClient(t *testing.T) {
Address: "127.0.0.1:1",
},
}
client := New(cfg, nil, nil, nil)
client := New(cfg, nil, nil, nil, nil)
err := client.Run(ctx)
assert.EqualError(t, err, "failed to dial: context deadline exceeded")
})
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestClient(t *testing.T) {
s.On("Changes").Return(make(<-chan *agentpb.StateChangedRequest))
s.On("QANRequests").Return(make(<-chan *agentpb.QANCollectRequest))

client := New(cfg, &s, nil, nil)
client := New(cfg, &s, nil, nil, nil)
err := client.Run(context.Background())
assert.NoError(t, err)
assert.Equal(t, serverMD, client.GetServerConnectMetadata())
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestClient(t *testing.T) {
},
}

client := New(cfg, nil, nil, nil)
client := New(cfg, nil, nil, nil, nil)
client.dialTimeout = 100 * time.Millisecond
err := client.Run(ctx)
assert.EqualError(t, err, "failed to get server metadata: rpc error: code = Canceled desc = context canceled", "%+v", err)
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestGetActionTimeout(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(prototext.Format(tc.req), func(t *testing.T) {
client := New(nil, nil, nil, nil)
client := New(nil, nil, nil, nil, nil)
actual := client.getActionTimeout(tc.req)
assert.Equal(t, tc.expected, actual)
})
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestUnexpectedActionType(t *testing.T) {
s.On("Changes").Return(make(<-chan *agentpb.StateChangedRequest))
s.On("QANRequests").Return(make(<-chan *agentpb.QANCollectRequest))

client := New(cfg, s, nil, nil)
client := New(cfg, s, nil, nil, nil)
err := client.Run(context.Background())
assert.NoError(t, err)
assert.Equal(t, serverMD, client.GetServerConnectMetadata())
Expand Down
5 changes: 5 additions & 0 deletions client/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

//go:generate mockery -name=connectionChecker -case=snake -inpkg -testonly
//go:generate mockery -name=supervisor -case=snake -inpkg -testonly
//go:generate mockery -name=defaultsFileParser -case=snake -inpkg -testonly

// connectionChecker is a subset of methods of connectionchecker.ConnectionChecker used by this package.
// We use it instead of real type for testing and to avoid dependency cycle.
Expand All @@ -48,3 +49,7 @@ type supervisor interface {
// Collector added to use client as Prometheus collector
prometheus.Collector
}

type defaultsFileParser interface {
ParseDefaultsFile(req *agentpb.ParseDefaultsFileRequest) *agentpb.ParseDefaultsFileResponse
}
4 changes: 3 additions & 1 deletion commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/percona/pmm-agent/client"
"github.com/percona/pmm-agent/config"
"github.com/percona/pmm-agent/connectionchecker"
"github.com/percona/pmm-agent/defaultsfile"
"github.com/percona/pmm-agent/versioner"
)

Expand Down Expand Up @@ -75,8 +76,9 @@ func run(ctx context.Context, cfg *config.Config, configFilepath string) {

supervisor := supervisor.NewSupervisor(ctx, &cfg.Paths, &cfg.Ports, &cfg.Server)
connectionChecker := connectionchecker.New(&cfg.Paths)
defaultsFileParser := defaultsfile.New()
v := versioner.New(&versioner.RealExecFunctions{})
client := client.New(cfg, supervisor, connectionChecker, v)
client := client.New(cfg, supervisor, connectionChecker, v, defaultsFileParser)
localServer := agentlocal.NewServer(cfg, supervisor, client, configFilepath)

go func() {
Expand Down
110 changes: 110 additions & 0 deletions defaultsfile/defaults_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// pmm-agent
// 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 defaultsfile provides managing of defaults file.
package defaultsfile

import (
"fmt"
"os/user"
"path/filepath"
"strings"

"github.com/percona/pmm/api/agentpb"
"github.com/percona/pmm/api/inventorypb"
"github.com/pkg/errors"
"gopkg.in/ini.v1"
)

// DefaultsFile is a struct with database specs fetched from file.
type DefaultsFile struct {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case of new struct DefaultFileParser, DefaultsFile can be private.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great remark. Thank you, changed.

username string
password string
host string
port uint32
}

// New creates new DefaultsFile.
func New() *DefaultsFile {
return &DefaultsFile{}
}

// ParseDefaultsFile parses given defaultsFile. It returns the database specs.
func (d *DefaultsFile) ParseDefaultsFile(req *agentpb.ParseDefaultsFileRequest) *agentpb.ParseDefaultsFileResponse {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's create one more struct like DefaultsFileParser with no fields and make this method part of it.
because TBH it looks weird that DefaultsFile has method which calls another function which returns DefaultsFile and fields of it used. So I think having separate struct will make code cleaner.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
Just created "Parser" struct because package is "defaultsfile" and with DefaultsFileParser linter raises error about stuttering. type name will be used as defaultsfile.DefaultsFileParser by other packages, and that stutters

var res agentpb.ParseDefaultsFileResponse
defaultsFile, err := parseDefaultsFile(req.ConfigPath, req.ServiceType)
if err != nil {
res.Error = err.Error()
return &res
}

res.Username = defaultsFile.username
res.Password = defaultsFile.password
res.Host = defaultsFile.host
res.Port = defaultsFile.port

return &res
}

func parseDefaultsFile(configPath string, serviceType inventorypb.ServiceType) (*DefaultsFile, error) {
if len(configPath) == 0 {
return nil, errors.New("configPath for DefaultsFile is empty")
}

switch serviceType {
case inventorypb.ServiceType_MYSQL_SERVICE:
return parseMySQLDefaultsFile(configPath)
case inventorypb.ServiceType_EXTERNAL_SERVICE:
case inventorypb.ServiceType_HAPROXY_SERVICE:
case inventorypb.ServiceType_MONGODB_SERVICE:
case inventorypb.ServiceType_POSTGRESQL_SERVICE:
case inventorypb.ServiceType_PROXYSQL_SERVICE:
case inventorypb.ServiceType_SERVICE_TYPE_INVALID:
return nil, errors.Errorf("unimplemented service type %s", serviceType)
}

return nil, errors.Errorf("unimplemented service type %s", serviceType)
}

func parseMySQLDefaultsFile(configPath string) (*DefaultsFile, error) {
configPath, err := expandPath(configPath)
if err != nil {
return nil, fmt.Errorf("fail to normalize path: %w", err)
}

cfg, err := ini.Load(configPath)
if err != nil {
return nil, fmt.Errorf("fail to read config file: %w", err)
}

port, _ := cfg.Section("client").Key("port").Uint()
return &DefaultsFile{
username: cfg.Section("client").Key("user").String(),
password: cfg.Section("client").Key("password").String(),
host: cfg.Section("client").Key("host").String(),
port: uint32(port),
Comment thread
BupycHuk marked this conversation as resolved.
}, nil
}

func expandPath(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("failed to expand path: %w", err)
}
return filepath.Join(usr.HomeDir, path[2:]), nil
}
return path, nil
}
74 changes: 74 additions & 0 deletions defaultsfile/defaults_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// pmm-agent
// 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 defaultsfile

import (
"testing"

"github.com/percona/pmm/api/agentpb"
"github.com/percona/pmm/api/inventorypb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDefaultsFileParser(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
req *agentpb.ParseDefaultsFileRequest
expectedErr string
}{
{
name: "Valid MySQL file",
req: &agentpb.ParseDefaultsFileRequest{
ServiceType: inventorypb.ServiceType_MYSQL_SERVICE,
ConfigPath: "../testdata/defaultsfile/.my.cnf",
},
},
{
name: "File not found",
req: &agentpb.ParseDefaultsFileRequest{
ServiceType: inventorypb.ServiceType_MYSQL_SERVICE,
ConfigPath: "path/to/invalid/file.cnf",
},
expectedErr: `no such file or directory`,
},
{
name: "Service type not supported",
req: &agentpb.ParseDefaultsFileRequest{
ServiceType: inventorypb.ServiceType_HAPROXY_SERVICE,
ConfigPath: "../testdata/defaultsfile/.my.cnf",
},
expectedErr: `unimplemented service type HAPROXY_SERVICE`,
},
}

for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
c := New()
resp := c.ParseDefaultsFile(testCase.req)
require.NotNil(t, resp)
if testCase.expectedErr == "" {
assert.Empty(t, resp.Error)
} else {
require.NotEmpty(t, resp.Error)
assert.Regexp(t, `.*`+testCase.expectedErr+`.*`, resp.Error)
}
})
}
}