Skip to content

Commit

Permalink
Merge pull request #793 from newrelic/chore/assert-platform-supported
Browse files Browse the repository at this point in the history
Chore/assert platform supported
  • Loading branch information
Julien4218 authored Apr 8, 2021
2 parents b80fd87 + aa76b9d commit 19a119a
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 8 deletions.
8 changes: 7 additions & 1 deletion internal/install/discovery/mock_discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ type MockDiscoverer struct {
}

func NewMockDiscoverer() *MockDiscoverer {
m := &types.DiscoveryManifest{}
m := &types.DiscoveryManifest{
OS: "linux",
}

return &MockDiscoverer{
DiscoveryManifest: m,
}
}

func (d *MockDiscoverer) Os(os string) {
d.DiscoveryManifest.OS = os
}

func (d *MockDiscoverer) Discover(context.Context) (*types.DiscoveryManifest, error) {
return d.DiscoveryManifest, nil
}
13 changes: 10 additions & 3 deletions internal/install/execution/install_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func (s *InstallStatus) RecipeSkipped(event RecipeStatusEvent) {
}
}

func (s *InstallStatus) InstallComplete() {
s.completed()
func (s *InstallStatus) InstallComplete(err error) {
s.completed(err)

for _, r := range s.statusSubscriber {
if err := r.InstallComplete(s); err != nil {
Expand Down Expand Up @@ -344,10 +344,17 @@ func (s *InstallStatus) withRecipeEvent(e RecipeStatusEvent, rs RecipeStatusType
s.Timestamp = utils.GetTimestamp()
}

func (s *InstallStatus) completed() {
func (s *InstallStatus) completed(err error) {
s.Complete = true
s.Timestamp = utils.GetTimestamp()

if err != nil {
statusError := StatusError{
Message: err.Error(),
}
s.Error = statusError
}

log.WithFields(log.Fields{
"timestamp": s.Timestamp,
}).Debug("completed")
Expand Down
4 changes: 2 additions & 2 deletions internal/install/execution/install_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestInstallStatus_statusUpdateMethods(t *testing.T) {
require.Equal(t, result.Status, RecipeStatusTypes.SKIPPED)
require.False(t, s.hasFailed())

s.InstallComplete()
s.InstallComplete(nil)
require.True(t, s.Complete)
require.NotNil(t, s.Timestamp)
}
Expand All @@ -159,7 +159,7 @@ func TestInstallStatus_failAvailableOnComplete(t *testing.T) {

s.RecipesAvailable([]types.Recipe{r})

s.InstallComplete()
s.InstallComplete(nil)
require.Equal(t, RecipeStatusTypes.FAILED, s.Statuses[0].Status)
}

Expand Down
19 changes: 18 additions & 1 deletion internal/install/recipe_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -114,7 +115,7 @@ func (i *RecipeInstaller) Install() error {
return err
}

i.status.InstallComplete()
i.status.InstallComplete(err)

return err
}
Expand All @@ -129,6 +130,11 @@ func (i *RecipeInstaller) discoverAndRun(ctx context.Context) error {

i.status.DiscoveryComplete(*m)

err = i.assertDiscoveryValid(ctx, m)
if err != nil {
return err
}

if i.RecipesProvided() {
// Run the targeted (AKA stitched path) installer.
return i.targetedInstall(ctx, m)
Expand All @@ -138,6 +144,17 @@ func (i *RecipeInstaller) discoverAndRun(ctx context.Context) error {
return i.guidedInstall(ctx, m)
}

func (i *RecipeInstaller) assertDiscoveryValid(ctx context.Context, m *types.DiscoveryManifest) error {
if m.OS == "" {
return fmt.Errorf("failed to identify a valid operating system")
}
if !(strings.ToLower(m.OS) == "linux" || strings.ToLower(m.OS) == "windows") {
return fmt.Errorf("operating system %s is not supported", m.OS)
}
log.Debugf("Done asserting valid operating system for %s", m.OS)
return nil
}

func (i *RecipeInstaller) installRecipes(ctx context.Context, m *types.DiscoveryManifest, recipes []types.Recipe) error {
log.WithFields(log.Fields{
"recipe_count": len(recipes),
Expand Down
84 changes: 84 additions & 0 deletions internal/install/recipe_installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,90 @@ func TestInstall_DiscoveryComplete(t *testing.T) {
require.Equal(t, 1, statusReporter.DiscoveryCompleteCallCount)
}

func TestInstall_FailsOnInvalidOs(t *testing.T) {
ic := InstallerContext{}
discover := discovery.NewMockDiscoverer()
discover.Os("darwin")
statusReporter := execution.NewMockStatusReporter()
statusReporters = []execution.StatusSubscriber{statusReporter}
status = execution.NewInstallStatus(statusReporters)
f.FetchRecipeVals = []types.Recipe{
{
Name: infraAgentRecipeName,
DisplayName: infraAgentRecipeName,
ValidationNRQL: "testNrql",
},
}

i := RecipeInstaller{ic, discover, l, f, e, v, ff, status, p, pi, lkf}

err := i.Install()
require.Error(t, err)
}

func TestInstall_FailsOnNoOs(t *testing.T) {
ic := InstallerContext{}
discover := discovery.NewMockDiscoverer()
discover.Os("")
statusReporter := execution.NewMockStatusReporter()
statusReporters = []execution.StatusSubscriber{statusReporter}
status = execution.NewInstallStatus(statusReporters)
f.FetchRecipeVals = []types.Recipe{
{
Name: infraAgentRecipeName,
DisplayName: infraAgentRecipeName,
ValidationNRQL: "testNrql",
},
}

i := RecipeInstaller{ic, discover, l, f, e, v, ff, status, p, pi, lkf}

err := i.Install()
require.Error(t, err)
}

func TestInstall_DoesntFailForWindowsOs(t *testing.T) {
ic := InstallerContext{}
discover := discovery.NewMockDiscoverer()
discover.Os("windows")
statusReporter := execution.NewMockStatusReporter()
statusReporters = []execution.StatusSubscriber{statusReporter}
status = execution.NewInstallStatus(statusReporters)
f.FetchRecipeVals = []types.Recipe{
{
Name: infraAgentRecipeName,
DisplayName: infraAgentRecipeName,
ValidationNRQL: "testNrql",
},
}

i := RecipeInstaller{ic, discover, l, f, e, v, ff, status, p, pi, lkf}

err := i.Install()
require.NoError(t, err)
}

func TestInstall_DoesntFailForLinuxOs(t *testing.T) {
ic := InstallerContext{}
discover := discovery.NewMockDiscoverer()
discover.Os("linux")
statusReporter := execution.NewMockStatusReporter()
statusReporters = []execution.StatusSubscriber{statusReporter}
status = execution.NewInstallStatus(statusReporters)
f.FetchRecipeVals = []types.Recipe{
{
Name: infraAgentRecipeName,
DisplayName: infraAgentRecipeName,
ValidationNRQL: "testNrql",
},
}

i := RecipeInstaller{ic, discover, l, f, e, v, ff, status, p, pi, lkf}

err := i.Install()
require.NoError(t, err)
}

func TestInstall_RecipesAvailable(t *testing.T) {
ic := InstallerContext{}
statusReporters = []execution.StatusSubscriber{execution.NewMockStatusReporter()}
Expand Down
2 changes: 1 addition & 1 deletion internal/install/validation/polling_recipe_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (m *PollingRecipeValidator) waitForData(ctx context.Context, dm types.Disco
ticker := time.NewTicker(m.interval)
defer ticker.Stop()

progressMsg := "Checking for data in New Relic..."
progressMsg := "Checking for data in New Relic (this may take a few minutes)..."
m.progressIndicator.Start(progressMsg)
defer m.progressIndicator.Stop()

Expand Down

0 comments on commit 19a119a

Please sign in to comment.