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

PMM-12702 Fix SSLSni checker. #2625

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 7 additions & 4 deletions managed/models/agentversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package models

import (
"fmt"

ademidoff marked this conversation as resolved.
Show resolved Hide resolved
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
"gopkg.in/reform.v1"
Expand All @@ -37,18 +36,22 @@ 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)
}
func (e *AgentNotSupportedError) Is(err error) bool {
_, ok := err.(*AgentNotSupportedError)

Choose a reason for hiding this comment

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

🚫 [golangci] reported by reviewdog 🐶
type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)

return ok
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why it's needed? It's analog of errors.Is(), no?

Copy link
Member Author

Choose a reason for hiding this comment

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

errors.Is didn't work correctly and actually calls this method to compare, so I had to implement it this way.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that we need As there instead of Is. We need to check error type, but not exact copy.

Copy link
Member Author

Choose a reason for hiding this comment

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

As didn't work as well

Copy link
Contributor

Choose a reason for hiding this comment

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

Why? What's so special about this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have no idea

Copy link
Member Author

Choose a reason for hiding this comment

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

the reason was because IsAgentSupported was returning a pointer and that's why it didn't work.


// PMMAgentSupported checks if pmm agent version satisfies required min version.
func PMMAgentSupported(q *reform.Querier, pmmAgentID, functionalityPrefix string, pmmMinVersion *version.Version) error {
pmmAgent, err := FindAgentByID(q, pmmAgentID)
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 Down
97 changes: 92 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,92 @@ 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)
})
}