Skip to content

Commit

Permalink
PMM-12702 Fix SSLSni checker. (#2625)
Browse files Browse the repository at this point in the history
* PMM-12702 Fix SSLSni checker.

* revert agentversion.go

* PMM-12702 Fix linter.

* PMM-12702 Fix linter and usage of As.
  • Loading branch information
BupycHuk authored Nov 22, 2023
1 parent 1b1661c commit 182ea1e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
12 changes: 6 additions & 6 deletions managed/models/agentversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type AgentNotSupportedError struct {
MinAgentVersion string
}

func (e *AgentNotSupportedError) Error() string {
func (e AgentNotSupportedError) Error() string {
return fmt.Sprintf("'%s' functionality is not supported by pmm-agent %q version %q. Required minimum version is %q", e.Functionality,
e.AgentID, e.AgentVersion, e.MinAgentVersion)
}
Expand All @@ -44,11 +44,11 @@ func PMMAgentSupported(q *reform.Querier, pmmAgentID, functionalityPrefix string
if err != nil {
return errors.Errorf("failed to get PMM Agent: %s", err)
}
return isAgentSupported(pmmAgent, functionalityPrefix, pmmMinVersion)
return IsAgentSupported(pmmAgent, functionalityPrefix, pmmMinVersion)
}

// isAgentSupported contains logic for PMMAgentSupported.
func isAgentSupported(agentModel *Agent, functionalityPrefix string, pmmMinVersion *version.Version) error {
// IsAgentSupported contains logic for PMMAgentSupported.
func IsAgentSupported(agentModel *Agent, functionalityPrefix string, pmmMinVersion *version.Version) error {
if agentModel == nil {
return errors.New("nil agent")
}
Expand All @@ -61,7 +61,7 @@ func isAgentSupported(agentModel *Agent, functionalityPrefix string, pmmMinVersi
}

if pmmAgentVersion.LessThan(pmmMinVersion) {
return errors.WithStack(&AgentNotSupportedError{
return errors.WithStack(AgentNotSupportedError{
AgentID: agentModel.AgentID,
Functionality: functionalityPrefix,
AgentVersion: *agentModel.Version,
Expand All @@ -74,7 +74,7 @@ func isAgentSupported(agentModel *Agent, functionalityPrefix string, pmmMinVersi
func IsPostgreSQLSSLSniSupported(q *reform.Querier, pmmAgentID string) (bool, error) {
err := PMMAgentSupported(q, pmmAgentID, "postgresql SSL sni check", PMMAgentMinVersionForPostgreSQLSSLSni)
switch {
case errors.Is(err, &AgentNotSupportedError{}):
case errors.As(err, &AgentNotSupportedError{}):
return false, nil
case err == nil:
return true, nil
Expand Down
96 changes: 91 additions & 5 deletions managed/models/agentversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package models
package models_test

import (
"testing"
"time"

"github.com/AlekSi/pointer"
"github.com/hashicorp/go-version"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/reform.v1"
"gopkg.in/reform.v1/dialects/postgresql"

"github.com/percona/pmm/managed/models"
"github.com/percona/pmm/managed/utils/testdb"
)

func TestPMMAgentSupported(t *testing.T) {
Expand Down Expand Up @@ -64,11 +71,11 @@ func TestPMMAgentSupported(t *testing.T) {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
agentModel := Agent{
agentModel := models.Agent{
AgentID: "Test agent ID",
Version: pointer.ToString(test.agentVersion),
}
err := isAgentSupported(&agentModel, prefix, minVersion)
err := models.IsAgentSupported(&agentModel, prefix, minVersion)
if test.errString == "" {
assert.NoError(t, err)
} else {
Expand All @@ -78,12 +85,91 @@ func TestPMMAgentSupported(t *testing.T) {
}

t.Run("No version info", func(t *testing.T) {
err := isAgentSupported(&Agent{AgentID: "Test agent ID"}, prefix, version.Must(version.NewVersion("2.30.0")))
err := models.IsAgentSupported(&models.Agent{AgentID: "Test agent ID"}, prefix, version.Must(version.NewVersion("2.30.0")))
assert.Contains(t, err.Error(), "has no version info")
})

t.Run("Nil agent", func(t *testing.T) {
err := isAgentSupported(nil, prefix, version.Must(version.NewVersion("2.30.0")))
err := models.IsAgentSupported(nil, prefix, version.Must(version.NewVersion("2.30.0")))
assert.Contains(t, err.Error(), "nil agent")
})
}

func TestIsPostgreSQLSSLSniSupported(t *testing.T) {
now, origNowF := models.Now(), models.Now
models.Now = func() time.Time {
return now
}
sqlDB := testdb.Open(t, models.SetupFixtures, nil)
defer func() {
models.Now = origNowF
require.NoError(t, sqlDB.Close())
}()

setup := func(t *testing.T) (q *reform.Querier, teardown func(t *testing.T)) {
t.Helper()
db := reform.NewDB(sqlDB, postgresql.Dialect, reform.NewPrintfLogger(t.Logf))
tx, err := db.Begin()
require.NoError(t, err)
q = tx.Querier

for _, str := range []reform.Struct{
&models.Node{
NodeID: "N1",
NodeType: models.GenericNodeType,
NodeName: "Generic Node",
},

&models.Agent{
AgentID: "New",
AgentType: models.PMMAgentType,
RunsOnNodeID: pointer.ToString("N1"),
Version: pointer.ToString("2.41.0"),
},

&models.Agent{
AgentID: "Old",
AgentType: models.PMMAgentType,
RunsOnNodeID: pointer.ToString("N1"),
Version: pointer.ToString("2.40.1"),
},
} {
require.NoError(t, q.Insert(str), "failed to INSERT %+v", str)
}

teardown = func(t *testing.T) {
t.Helper()
require.NoError(t, tx.Rollback())
}
return
}
q, teardown := setup(t)
defer teardown(t)

tests := []struct {
pmmAgentID string
expected bool
}{
{
"New",
true,
},
{
"Old",
false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.pmmAgentID, func(t *testing.T) {
actual, err := models.IsPostgreSQLSSLSniSupported(q, tt.pmmAgentID)
assert.Equal(t, tt.expected, actual)
assert.NoError(t, err)
})
}

t.Run("Non-existing ID", func(t *testing.T) {
_, err := models.IsPostgreSQLSSLSniSupported(q, "Not exist")
assert.Error(t, err)
})
}

0 comments on commit 182ea1e

Please sign in to comment.