Skip to content

Commit

Permalink
Refactor saptune and subscriptions tests (#293)
Browse files Browse the repository at this point in the history
* Refactor saptune package tests

* Refactor subscription code tests
  • Loading branch information
arbulu89 authored Nov 16, 2023
1 parent 16efc17 commit 374f542
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 55 deletions.
80 changes: 32 additions & 48 deletions internal/core/saptune/saptune_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package saptune
package saptune_test

import (
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/core/saptune"
"github.com/trento-project/agent/pkg/utils/mocks"
)

Expand All @@ -23,16 +24,11 @@ func (suite *SaptuneTestSuite) TestNewSaptune() {
[]byte("3.1.0"), nil,
)

saptuneRetriever, err := NewSaptune(mockCommand)

expectedSaptune := Saptune{
Version: "3.1.0",
IsJSONSupported: true,
executor: mockCommand,
}
saptuneRetriever, err := saptune.NewSaptune(mockCommand)

suite.NoError(err)
suite.Equal(expectedSaptune, saptuneRetriever)
suite.Equal("3.1.0", saptuneRetriever.Version)
suite.Equal(true, saptuneRetriever.IsJSONSupported)
}

func (suite *SaptuneTestSuite) TestNewSaptuneUnsupportedSaptuneVer() {
Expand All @@ -42,16 +38,11 @@ func (suite *SaptuneTestSuite) TestNewSaptuneUnsupportedSaptuneVer() {
[]byte("3.0.0"), nil,
)

saptuneRetriever, err := NewSaptune(mockCommand)

expectedSaptune := Saptune{
Version: "3.0.0",
IsJSONSupported: false,
executor: mockCommand,
}
saptuneRetriever, err := saptune.NewSaptune(mockCommand)

suite.NoError(err)
suite.Equal(expectedSaptune, saptuneRetriever)
suite.Equal("3.0.0", saptuneRetriever.Version)
suite.Equal(false, saptuneRetriever.IsJSONSupported)
}

func (suite *SaptuneTestSuite) TestNewSaptuneSaptuneVersionUnknownErr() {
Expand All @@ -61,31 +52,25 @@ func (suite *SaptuneTestSuite) TestNewSaptuneSaptuneVersionUnknownErr() {
nil, errors.New("Error: exec: \"rpm\": executable file not found in $PATH"),
)

saptuneRetriever, err := NewSaptune(mockCommand)
saptuneRetriever, err := saptune.NewSaptune(mockCommand)

expectedSaptune := Saptune{
Version: "",
IsJSONSupported: false,
}

suite.EqualError(err, ErrSaptuneVersionUnknown.Error()+": Error: exec: \"rpm\": executable file not found in $PATH")
suite.Equal(expectedSaptune, saptuneRetriever)
suite.EqualError(err, saptune.ErrSaptuneVersionUnknown.Error()+": Error: exec: \"rpm\": executable file not found in $PATH")
suite.Equal("", saptuneRetriever.Version)
suite.Equal(false, saptuneRetriever.IsJSONSupported)
}

func (suite *SaptuneTestSuite) TestRunCommand() {
mockCommand := new(mocks.CommandExecutor)

saptuneRetriever := Saptune{
Version: "3.0.0",
IsJSONSupported: false,
executor: mockCommand,
}

saptuneOutput := []byte("some_output")

mockCommand.On("Exec", "saptune", "some_command").Return(
saptuneOutput, nil,
)
mockCommand.
On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "saptune").
Return([]byte("3.0.0"), nil).
On("Exec", "saptune", "some_command").
Return(saptuneOutput, nil)

saptuneRetriever, _ := saptune.NewSaptune(mockCommand)

statusOutput, err := saptuneRetriever.RunCommand("some_command")

Expand All @@ -98,17 +83,15 @@ func (suite *SaptuneTestSuite) TestRunCommand() {
func (suite *SaptuneTestSuite) TestRunCommandJSON() {
mockCommand := new(mocks.CommandExecutor)

saptuneRetriever := Saptune{
Version: "3.1.0",
IsJSONSupported: true,
executor: mockCommand,
}

saptuneOutput := []byte("{\"some_json_key\": \"some_value\"}")

mockCommand.On("Exec", "saptune", "--format", "json", "status").Return(
saptuneOutput, nil,
)
mockCommand.
On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "saptune").
Return([]byte("3.1.0"), nil).
On("Exec", "saptune", "--format", "json", "status").
Return(saptuneOutput, nil)

saptuneRetriever, _ := saptune.NewSaptune(mockCommand)

statusOutput, err := saptuneRetriever.RunCommandJSON("status")

Expand All @@ -121,15 +104,16 @@ func (suite *SaptuneTestSuite) TestRunCommandJSON() {
func (suite *SaptuneTestSuite) TestRunCommandJSONNoJSONSupported() {
mockCommand := new(mocks.CommandExecutor)

saptuneRetriever := Saptune{
IsJSONSupported: false,
executor: mockCommand,
}
mockCommand.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "saptune").Return(
[]byte("3.0.0"), nil,
)

saptuneRetriever, _ := saptune.NewSaptune(mockCommand)

statusOutput, err := saptuneRetriever.RunCommandJSON("status")

expectedOutput := []byte(nil)

suite.EqualError(err, ErrUnsupportedSaptuneVer.Error())
suite.EqualError(err, saptune.ErrUnsupportedSaptuneVer.Error())
suite.Equal(expectedOutput, statusOutput)
}
15 changes: 8 additions & 7 deletions internal/core/subscription/subscription_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package subscription
package subscription_test

import (
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/suite"

"github.com/trento-project/agent/internal/core/subscription"
"github.com/trento-project/agent/pkg/utils/mocks"
)

Expand All @@ -31,10 +32,10 @@ func (suite *SubscriptionTestSuite) TestNewSubscriptions() {
subsOutput, nil,
)

subs, err := NewSubscriptions(mockCommand)
subs, err := subscription.NewSubscriptions(mockCommand)

expectedSubs := Subscriptions{
&Subscription{
expectedSubs := subscription.Subscriptions{
&subscription.Subscription{
Identifier: "SLES_SAP",
Version: "15.2",
Arch: "x86_64",
Expand All @@ -44,7 +45,7 @@ func (suite *SubscriptionTestSuite) TestNewSubscriptions() {
SubscriptionStatus: "ACTIVE",
Type: "internal",
},
&Subscription{ //nolint
&subscription.Subscription{ //nolint
Identifier: "sle-module-public-cloud",
Version: "15.2",
Arch: "x86_64",
Expand All @@ -63,8 +64,8 @@ func (suite *SubscriptionTestSuite) TestNewSubscriptionsErr() {
nil, errors.New("some error"),
)

subs, err := NewSubscriptions(mockCommand)
subs, err := subscription.NewSubscriptions(mockCommand)

suite.Equal(Subscriptions(nil), subs)
suite.Equal(subscription.Subscriptions(nil), subs)
suite.EqualError(err, "some error")
}

0 comments on commit 374f542

Please sign in to comment.