Skip to content

Commit

Permalink
chore(refactor): testing around license key fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruzina committed Nov 15, 2022
1 parent 5014f42 commit 80f5a6b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 72 deletions.
32 changes: 16 additions & 16 deletions internal/install/license_key_fetcher.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package install

import (
"context"

log "github.com/sirupsen/logrus"

"github.com/newrelic/newrelic-cli/internal/client"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/install/recipes"
"github.com/newrelic/newrelic-cli/internal/utils"
)

// relies on the Nerdgraph service
type ServiceLicenseKeyFetcher struct {
client recipes.NerdGraphClient
LicenseKey string
}
// FIXME not sure if this really relies on NerdGraph service

var (
getActiveProfileAccountID = configAPI.GetActiveProfileAccountID
getActiveProfileName = configAPI.GetActiveProfileName
fetchLicenseKey = client.FetchLicenseKey
)

type LicenseKeyFetcher interface {
FetchLicenseKey(context.Context) (string, error)
FetchLicenseKey() (string, error)
}

func NewServiceLicenseKeyFetcher(client recipes.NerdGraphClient) LicenseKeyFetcher {
f := ServiceLicenseKeyFetcher{
client: client,
}
type ServiceLicenseKeyFetcher struct {
LicenseKey string
}

return &f
func NewServiceLicenseKeyFetcher() *ServiceLicenseKeyFetcher {
return &ServiceLicenseKeyFetcher{}
}

func (f *ServiceLicenseKeyFetcher) FetchLicenseKey(ctx context.Context) (string, error) {
func (f *ServiceLicenseKeyFetcher) FetchLicenseKey() (string, error) {

if f.LicenseKey != "" {
return f.LicenseKey, nil
}

accountID := configAPI.GetActiveProfileAccountID()
licenseKey, err := client.FetchLicenseKey(accountID, configAPI.GetActiveProfileName())
accountID := getActiveProfileAccountID()
licenseKey, err := fetchLicenseKey(accountID, getActiveProfileName())
if err != nil {
return "", err
}
Expand Down
53 changes: 53 additions & 0 deletions internal/install/license_key_fetcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package install

import (
"errors"
"strings"
"testing"

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

func TestFetchLicenseKeyReturnsSavedKey(t *testing.T) {
expectedLicenseKey := "12345somelicensekey"
licenseKeyFetcher := NewServiceLicenseKeyFetcher()
licenseKeyFetcher.LicenseKey = expectedLicenseKey

actualLicenseKey, err := licenseKeyFetcher.FetchLicenseKey()

assert.NoError(t, err)
assert.Equal(t, expectedLicenseKey, actualLicenseKey)
}

func TestFetchLicenseKeyReturnsErrorIfFetchErrors(t *testing.T) {
realFetchLicenseKey := fetchLicenseKey
defer func() {
fetchLicenseKey = realFetchLicenseKey
}()
fetchLicenseKey = func(int, string) (string, error) {
return "", errors.New("couldn't fetch key")
}

licenseKey, err := NewServiceLicenseKeyFetcher().FetchLicenseKey()

assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "couldn't fetch key"))
assert.True(t, 0 == len(licenseKey))
}

func TestFetchLicenseKeyFetchesAndStoresKey(t *testing.T) {
realFetchLicenseKey := fetchLicenseKey
defer func() {
fetchLicenseKey = realFetchLicenseKey
}()
fetchLicenseKey = func(int, string) (string, error) {
return "hey-ima-key", nil
}

licenseKeyFetcher := NewServiceLicenseKeyFetcher()
actualLicenseKey, err := licenseKeyFetcher.FetchLicenseKey()

assert.NoError(t, err)
assert.Equal(t, "hey-ima-key", actualLicenseKey)
assert.Equal(t, "hey-ima-key", licenseKeyFetcher.LicenseKey)
}
23 changes: 0 additions & 23 deletions internal/install/mock_license_key_fetcher.go

This file was deleted.

19 changes: 19 additions & 0 deletions internal/install/mocks/mock_license_key_fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mocks

type MockLicenseKeyFetcher struct {
FetchLicenseKeyFunc func() (string, error)
}

func NewMockLicenseKeyFetcher() *MockLicenseKeyFetcher {
f := MockLicenseKeyFetcher{}
f.FetchLicenseKeyFunc = defaultFetchLicenseKeyFunc
return &f
}

func (f *MockLicenseKeyFetcher) FetchLicenseKey() (string, error) {
return f.FetchLicenseKeyFunc()
}

func defaultFetchLicenseKeyFunc() (string, error) {
return "mockLicenseKey", nil
}
24 changes: 12 additions & 12 deletions internal/install/recipe_install_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
)

type RecipeInstallBuilder struct {
configValidator *diagnose.MockConfigValidator
recipeFetcher *recipes.MockRecipeFetcher
status *execution.InstallStatus
licenseKeyFetcher *MockLicenseKeyFetcher
configValidator *diagnose.MockConfigValidator
recipeFetcher *recipes.MockRecipeFetcher
status *execution.InstallStatus
//licenseKeyFetcher *install_mocks.MockLicenseKeyFetcher
shouldInstallCore func() bool
installerContext types.InstallerContext
recipeLogForwarder *execution.MockRecipeLogForwarder
Expand Down Expand Up @@ -44,7 +44,7 @@ func NewRecipeInstallBuilder() *RecipeInstallBuilder {
// Default to not skip core
rib.shouldInstallCore = func() bool { return true }
rib.installerContext = types.InstallerContext{}
rib.licenseKeyFetcher = NewMockLicenseKeyFetcher()
//rib.licenseKeyFetcher = install_mocks.NewMockLicenseKeyFetcher()
rib.recipeLogForwarder = execution.NewMockRecipeLogForwarder()
rib.recipeVarProvider = execution.NewMockRecipeVarProvider()
rib.recipeVarProvider.Vars = map[string]string{}
Expand Down Expand Up @@ -72,12 +72,12 @@ func (rib *RecipeInstallBuilder) WithRecipeDetectionResult(detectionResult *reci
return rib
}

func (rib *RecipeInstallBuilder) WithLicenseKeyFetchResult(result error) *RecipeInstallBuilder {
rib.licenseKeyFetcher.FetchLicenseKeyFunc = func(ctx context.Context) (string, error) {
return "", result
}
return rib
}
//func (rib *RecipeInstallBuilder) WithLicenseKeyFetchResult(result error) *RecipeInstallBuilder {
// rib.licenseKeyFetcher.FetchLicenseKeyFunc = func() (string, error) {
// return "", result
// }
// return rib
//}

func (rib *RecipeInstallBuilder) WithConfigValidatorError(err error) *RecipeInstallBuilder {
rib.configValidator.Error = err
Expand Down Expand Up @@ -160,7 +160,7 @@ func (rib *RecipeInstallBuilder) Build() *RecipeInstall {
}
recipeInstall.shouldInstallCore = rib.shouldInstallCore
recipeInstall.InstallerContext = rib.installerContext
recipeInstall.licenseKeyFetcher = rib.licenseKeyFetcher
//recipeInstall.licenseKeyFetcher = rib.licenseKeyFetcher
recipeInstall.recipeLogForwarder = rib.recipeLogForwarder
recipeInstall.recipeVarPreparer = rib.recipeVarProvider
recipeInstall.recipeExecutor = rib.recipeExecutor
Expand Down
37 changes: 17 additions & 20 deletions internal/install/recipe_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ var (
validateManifest = discovery.ValidateManifest
getLatestCliVersionReleased = cli.GetLatestReleaseVersion
isLatestCliVersionInstalled = cli.IsLatestVersion
licenseKeyFetcher = NewServiceLicenseKeyFetcher()
)

type RecipeInstall struct {
types.InstallerContext
recipeFetcher recipes.RecipeFetcher
recipeExecutor execution.RecipeExecutor
recipeValidator validation.RecipeValidator
recipeFileFetcher RecipeFileFetcher
recipeLogForwarder execution.LogForwarder
status *execution.InstallStatus
prompter Prompter
licenseKeyFetcher LicenseKeyFetcher
recipeFetcher recipes.RecipeFetcher
recipeExecutor execution.RecipeExecutor
recipeValidator validation.RecipeValidator
recipeFileFetcher RecipeFileFetcher
recipeLogForwarder execution.LogForwarder
status *execution.InstallStatus
prompter Prompter
//licenseKeyFetcher LicenseKeyFetcher
configValidator ConfigValidator
recipeVarPreparer RecipeVarPreparer
agentValidator validation.AgentValidator
Expand Down Expand Up @@ -74,19 +75,17 @@ func NewRecipeInstaller(ic types.InstallerContext, nrClient *newrelic.NewRelic)
recipeFetcher = recipes.NewEmbeddedRecipeFetcher()
}

//mv := discovery.NewManifestValidator()
ff := recipes.NewRecipeFileFetcher([]string{})
lf := execution.NewRecipeLogForwarder()
ers := []execution.StatusSubscriber{
execution.NewNerdStorageStatusReporter(&nrClient.NerdStorage),
execution.NewTerminalStatusReporter(),
execution.NewInstallEventsReporter(&nrClient.InstallEvents),
}
lkf := NewServiceLicenseKeyFetcher(&nrClient.NerdGraph)
//lkf := NewServiceLicenseKeyFetcher()
slg := execution.NewPlatformLinkGenerator()
statusRollup := execution.NewInstallStatus(ers, slg)

//d := discovery.NewPSUtilDiscoverer()
re := execution.NewGoTaskRecipeExecutor()
v := validation.NewPollingRecipeValidator(&nrClient.Nrdb)
cv := diagnose.NewConfigValidator(nrClient)
Expand All @@ -95,21 +94,19 @@ func NewRecipeInstaller(ic types.InstallerContext, nrClient *newrelic.NewRelic)
av := validation.NewAgentValidator()

i := RecipeInstall{
//discoverer: d,
//manifestValidator: mv,
recipeFetcher: recipeFetcher,
recipeExecutor: re,
recipeValidator: v,
recipeFileFetcher: ff,
recipeLogForwarder: lf,
status: statusRollup,
prompter: p,
licenseKeyFetcher: lkf,
configValidator: cv,
recipeVarPreparer: rvp,
agentValidator: av,
progressIndicator: ux.NewSpinnerProgressIndicator(),
processEvaluator: recipes.NewProcessEvaluator(),
//licenseKeyFetcher: lkf,
configValidator: cv,
recipeVarPreparer: rvp,
agentValidator: av,
progressIndicator: ux.NewSpinnerProgressIndicator(),
processEvaluator: recipes.NewProcessEvaluator(),
}

i.InstallerContext = ic
Expand Down Expand Up @@ -639,7 +636,7 @@ func (i *RecipeInstall) executeAndValidateWithProgress(ctx context.Context, m *t
successChan := make(chan string)

go func() {
licenseKey, err := i.licenseKeyFetcher.FetchLicenseKey(ctx)
licenseKey, err := licenseKeyFetcher.FetchLicenseKey()
if err != nil {
errorChan <- err
return
Expand Down
3 changes: 2 additions & 1 deletion internal/install/recipe_installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ func TestInstallWhenKeyFetchError(t *testing.T) {
}
statusReporter := execution.NewMockStatusReporter()
expected := errors.New("Some error")
recipeInstall := NewRecipeInstallBuilder().WithStatusReporter(statusReporter).WithLicenseKeyFetchResult(expected).WithRecipeDetectionResult(r).Build()
recipeInstall := NewRecipeInstallBuilder().WithStatusReporter(statusReporter).WithRecipeDetectionResult(r).Build()

err := recipeInstall.Install()

assert.Error(t, err)
Expand Down

0 comments on commit 80f5a6b

Please sign in to comment.