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

Refactor/mocks sub package #1393

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions internal/install/discovery/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package discovery

import (
"context"

"github.com/newrelic/newrelic-cli/internal/install/types"
)

// ManifestDiscoverer is responsible for discovering information about the host system.
type ManifestDiscoverer interface {
DiscoverManifest(context.Context, Discoverer) (*types.DiscoveryManifest, error)
ValidateManifest(*types.DiscoveryManifest, Validator) error
}

type Discoverer interface {
Discover(context.Context) (*types.DiscoveryManifest, error)
}

type Validator interface {
Validate(m *types.DiscoveryManifest) error
}
29 changes: 29 additions & 0 deletions internal/install/discovery/manifest_discovery_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package discovery

import (
"context"
"fmt"

log "github.com/sirupsen/logrus"

"github.com/newrelic/newrelic-cli/internal/install/types"
)

func DiscoverManifest(ctx context.Context, discoverer Discoverer) (*types.DiscoveryManifest, error) {
log.Debug("discovering system information")
m, err := discoverer.Discover(ctx)
if err != nil {
return nil, fmt.Errorf("there was an error discovering system info: %s", err)
}

return m, nil
}

func ValidateManifest(manifest *types.DiscoveryManifest, validator Validator) error {
err := validator.Validate(manifest)
if err != nil {
return err
}
log.Debugf("Done asserting valid operating system for OS:%s and PlatformVersion:%s", manifest.OS, manifest.PlatformVersion)
return nil
}
61 changes: 61 additions & 0 deletions internal/install/discovery/manifest_discovery_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package discovery

import (
"context"
"errors"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/newrelic/newrelic-cli/internal/install/discovery/mocks"
"github.com/newrelic/newrelic-cli/internal/install/types"
)

func TestDiscoverManifestReturnsValidManifest(t *testing.T) {
mockDiscoverer := mocks.NewDiscoverer(t)
mockDiscoverer.On("Discover", mock.Anything).Return(&types.DiscoveryManifest{
OS: "darwin",
PlatformVersion: "10.13",
}, nil)

manifest, err := DiscoverManifest(context.Background(), mockDiscoverer)

assert.NoError(t, err)
assert.NotNil(t, manifest)
assert.Equal(t, "darwin", manifest.OS)
assert.Equal(t, "10.13", manifest.PlatformVersion)
}

func TestDiscoverManifestErrorsOnInvalidManifest(t *testing.T) {
expectedError := errors.New("discovery errored")
mockDiscoverer := mocks.NewDiscoverer(t)
mockDiscoverer.On("Discover", mock.Anything).Return(nil, expectedError)

manifest, err := DiscoverManifest(context.Background(), mockDiscoverer)

assert.Nil(t, manifest)
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "discovery errored"))
}

func TestValidateManifestReturnsNoErrorOnValidManifest(t *testing.T) {
mockValidator := mocks.NewValidator(t)
mockValidator.On("Validate", mock.Anything).Return(nil)

err := ValidateManifest(&types.DiscoveryManifest{OS: "valid"}, mockValidator)

assert.NoError(t, err)
}

func TestValidateManifestReturnsErrorOnInvalidManifest(t *testing.T) {
expectedError := errors.New("validation error")
mockValidator := mocks.NewValidator(t)
mockValidator.On("Validate", mock.Anything).Return(expectedError)

err := ValidateManifest(&types.DiscoveryManifest{OS: "invalid"}, mockValidator)

assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "validation error"))
}
8 changes: 2 additions & 6 deletions internal/install/discovery/manifest_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import (
)

type ManifestValidator struct {
validators []validator
}

type validator interface {
Validate(m *types.DiscoveryManifest) error
validators []Validator
}

var (
Expand All @@ -22,7 +18,7 @@ var (
// NewManifestValidator returns a new instance of ManifestValidator.
func NewManifestValidator() *ManifestValidator {
mv := ManifestValidator{
validators: []validator{},
validators: []Validator{},
}

mv.validators = append(mv.validators, NewOsValidator())
Expand Down
44 changes: 27 additions & 17 deletions internal/install/discovery/manifest_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,56 @@ import (
"fmt"
"testing"

"github.com/newrelic/newrelic-cli/internal/install/types"

"github.com/stretchr/testify/require"
)

func Test_ShouldSucceedManifestOsDarwin(t *testing.T) {
discover := NewMockDiscoverer()
discover.SetOs("darwin")
discover.SetPlatformVersion("10.14")
manifest := &types.DiscoveryManifest{
OS: "darwin",
PlatformVersion: "10.14",
}

err := NewManifestValidator().Validate(manifest)

err := NewManifestValidator().Validate(discover.GetManifest())
require.NoError(t, err)
}

func Test_ShouldFailManifestOsDarwinOldVersion(t *testing.T) {
discover := NewMockDiscoverer()
discover.SetOs("darwin")
discover.SetPlatformVersion("10.13")
manifest := &types.DiscoveryManifest{
OS: "darwin",
PlatformVersion: "10.13",
}

err := NewManifestValidator().Validate(manifest)

err := NewManifestValidator().Validate(discover.GetManifest())
require.Error(t, err)
require.Contains(t, err.Error(), errorPrefix)
require.Contains(t, err.Error(), "darwin")
}

func Test_ShouldFailManifestWindowsVersion(t *testing.T) {
discover := NewMockDiscoverer()
discover.SetOs("windows")
discover.SetPlatformVersion("1")
manifest := &types.DiscoveryManifest{
OS: "windows",
PlatformVersion: "1",
}

result := NewManifestValidator().Validate(manifest)

result := NewManifestValidator().Validate(discover.GetManifest())
require.Contains(t, result.Error(), errorPrefix)
require.Contains(t, result.Error(), fmt.Sprintf(versionNoLongerSupported, "windows"))
}

func Test_ShouldFailManifestUbuntuVersion(t *testing.T) {
discover := NewMockDiscoverer()
discover.SetOs("linux")
discover.SetPlatform("ubuntu")
discover.SetPlatformVersion("12.04")
manifest := &types.DiscoveryManifest{
OS: "linux",
Platform: "ubuntu",
PlatformVersion: "10.14",
}

result := NewManifestValidator().Validate(manifest)

result := NewManifestValidator().Validate(discover.GetManifest())
require.Contains(t, result.Error(), errorPrefix)
require.Contains(t, result.Error(), fmt.Sprintf(versionNoLongerSupported, "linux/ubuntu"))
}
Binary file not shown.
Binary file not shown.
22 changes: 0 additions & 22 deletions internal/install/discovery/mockProcesses/JavaDaemonTest.java

This file was deleted.

49 changes: 0 additions & 49 deletions internal/install/discovery/mock_discoverer.go

This file was deleted.

57 changes: 0 additions & 57 deletions internal/install/discovery/mock_empty_discoverer.go

This file was deleted.

16 changes: 0 additions & 16 deletions internal/install/discovery/mock_os_validator.go

This file was deleted.

Loading