Skip to content

Commit

Permalink
refactor: replace client and profile instantiation helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrombley committed Jul 2, 2021
1 parent 966c897 commit 6a8916b
Show file tree
Hide file tree
Showing 45 changed files with 1,435 additions and 2,030 deletions.
391 changes: 192 additions & 199 deletions cmd/newrelic/command.go

Large diffs are not rendered by default.

318 changes: 156 additions & 162 deletions cmd/newrelic/command_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,165 +2,159 @@

package main

import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

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

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

"github.com/newrelic/newrelic-client-go/newrelic"
"github.com/newrelic/newrelic-client-go/pkg/nerdgraph"
mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers"
)

func TestInitializeProfile(t *testing.T) {

apiKey := os.Getenv("NEW_RELIC_API_KEY")
envAccountID := os.Getenv("NEW_RELIC_ACCOUNT_ID")
if apiKey == "" || envAccountID == "" {
t.Skipf("NEW_RELIC_API_KEY and NEW_RELIC_ACCOUNT_ID are required to run this test")
}

f, err := ioutil.TempDir("/tmp", "newrelic")
defer os.RemoveAll(f)
assert.NoError(t, err)
config.DefaultConfigDirectory = f

// Init without the necessary environment variables
os.Setenv("NEW_RELIC_API_KEY", "")
os.Setenv("NEW_RELIC_ACCOUNT_ID", "")
initializeProfile(context.Background())

// Load credentials from disk
c, err := credentials.LoadCredentials(f)
assert.NoError(t, err)
assert.Equal(t, 0, len(c.Profiles))
assert.Equal(t, f, c.ConfigDirectory)
assert.Equal(t, "", c.DefaultProfile)

// Init with environment
os.Setenv("NEW_RELIC_API_KEY", apiKey)
os.Setenv("NEW_RELIC_ACCOUNT_ID", envAccountID)
initializeProfile(context.Background())

// Initialize the new configuration directory
c, err = credentials.LoadCredentials(f)
assert.NoError(t, err)
assert.Equal(t, 1, len(c.Profiles))
assert.Equal(t, f, c.ConfigDirectory)
assert.Equal(t, defaultProfileName, c.DefaultProfile)
assert.Equal(t, apiKey, c.Profiles[defaultProfileName].APIKey)
assert.NotEmpty(t, c.Profiles[defaultProfileName].Region)
assert.NotEmpty(t, c.Profiles[defaultProfileName].AccountID)
assert.NotEmpty(t, c.Profiles[defaultProfileName].LicenseKey)
assert.NotEmpty(t, c.Profiles[defaultProfileName].InsightsInsertKey)

// Ensure that we don't Fatal out if the default profile already exists, but
// was not specified in the default-profile.json.
if err = os.Remove(fmt.Sprintf("%s/%s.json", f, credentials.DefaultProfileFile)); err != nil {
t.Fatal(err)
}

initializeProfile(context.Background())
}

func TestFetchLicenseKey_missingKey(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(missingLicenseKey))
})

ts := httptest.NewServer(handler)
tc := mock.NewTestConfig(t, ts)

nr := &newrelic.NewRelic{
NerdGraph: nerdgraph.New(tc),
}

response, err := fetchLicenseKey(context.Background(), nr, 0)
require.Error(t, err, types.ErrorFetchingLicenseKey)
require.Empty(t, response)
}

func TestFetchLicenseKey_nilKey(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(withNilLicenseKey))
})

ts := httptest.NewServer(handler)
tc := mock.NewTestConfig(t, ts)

nr := &newrelic.NewRelic{
NerdGraph: nerdgraph.New(tc),
}

response, err := fetchLicenseKey(context.Background(), nr, 0)
require.Error(t, err, types.ErrorFetchingLicenseKey)
require.Empty(t, response)
}
func TestFetchLicenseKey_withKey(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(withLicenseKey))
})

ts := httptest.NewServer(handler)
tc := mock.NewTestConfig(t, ts)

nr := &newrelic.NewRelic{
NerdGraph: nerdgraph.New(tc),
}

response, err := fetchLicenseKey(context.Background(), nr, 0)
require.NoError(t, err)
require.Equal(t, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", response)
}

var missingLicenseKey string = `
{
"data": {
"actor": {
"account": {
}
}
}
}
`

var withLicenseKey string = `
{
"data": {
"actor": {
"account": {
"licenseKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
}
`

var withNilLicenseKey string = `
{
"data": {
"actor": {
"account": {
"licenseKey": null
}
}
}
}
`
// import (
// "context"
// "net/http"
// "net/http/httptest"
// "testing"

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

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

// "github.com/newrelic/newrelic-client-go/newrelic"
// "github.com/newrelic/newrelic-client-go/pkg/nerdgraph"
// mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers"
// )

// func TestInitializeProfile(t *testing.T) {

// apiKey := os.Getenv("NEW_RELIC_API_KEY")
// envAccountID := os.Getenv("NEW_RELIC_ACCOUNT_ID")
// if apiKey == "" || envAccountID == "" {
// t.Skipf("NEW_RELIC_API_KEY and NEW_RELIC_ACCOUNT_ID are required to run this test")
// }

// f, err := ioutil.TempDir("/tmp", "newrelic")
// defer os.RemoveAll(f)
// assert.NoError(t, err)
// config.DefaultConfigDirectory = f

// // Init without the necessary environment variables
// os.Setenv("NEW_RELIC_API_KEY", "")
// os.Setenv("NEW_RELIC_ACCOUNT_ID", "")
// initializeProfile(context.Background())

// // Load credentials from disk
// c, err := credentials.LoadCredentials(f)
// assert.NoError(t, err)
// assert.Equal(t, 0, len(c.Profiles))
// assert.Equal(t, f, c.ConfigDirectory)
// assert.Equal(t, "", c.DefaultProfile)

// // Init with environment
// os.Setenv("NEW_RELIC_API_KEY", apiKey)
// os.Setenv("NEW_RELIC_ACCOUNT_ID", envAccountID)
// initializeProfile(context.Background())

// // Initialize the new configuration directory
// c, err = credentials.LoadCredentials(f)
// assert.NoError(t, err)
// assert.Equal(t, 1, len(c.Profiles))
// assert.Equal(t, f, c.ConfigDirectory)
// assert.Equal(t, defaultProfileName, c.DefaultProfile)
// assert.Equal(t, apiKey, c.Profiles[defaultProfileName].APIKey)
// assert.NotEmpty(t, c.Profiles[defaultProfileName].Region)
// assert.NotEmpty(t, c.Profiles[defaultProfileName].AccountID)
// assert.NotEmpty(t, c.Profiles[defaultProfileName].LicenseKey)
// assert.NotEmpty(t, c.Profiles[defaultProfileName].InsightsInsertKey)

// // Ensure that we don't Fatal out if the default profile already exists, but
// // was not specified in the default-profile.json.
// if err = os.Remove(fmt.Sprintf("%s/%s.json", f, credentials.DefaultProfileFile)); err != nil {
// t.Fatal(err)
// }

// initializeProfile(context.Background())
// }

// func TestFetchLicenseKey_missingKey(t *testing.T) {
// handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// _, _ = w.Write([]byte(missingLicenseKey))
// })

// ts := httptest.NewServer(handler)
// tc := mock.NewTestConfig(t, ts)

// nr := &newrelic.NewRelic{
// NerdGraph: nerdgraph.New(tc),
// }

// response, err := fetchLicenseKey(context.Background(), nr, 0)
// require.Error(t, err, types.ErrorFetchingLicenseKey)
// require.Empty(t, response)
// }

// func TestFetchLicenseKey_nilKey(t *testing.T) {
// handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// _, _ = w.Write([]byte(withNilLicenseKey))
// })

// ts := httptest.NewServer(handler)
// tc := mock.NewTestConfig(t, ts)

// nr := &newrelic.NewRelic{
// NerdGraph: nerdgraph.New(tc),
// }

// response, err := fetchLicenseKey(context.Background(), nr, 0)
// require.Error(t, err, types.ErrorFetchingLicenseKey)
// require.Empty(t, response)
// }
// func TestFetchLicenseKey_withKey(t *testing.T) {
// handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// _, _ = w.Write([]byte(withLicenseKey))
// })

// ts := httptest.NewServer(handler)
// tc := mock.NewTestConfig(t, ts)

// nr := &newrelic.NewRelic{
// NerdGraph: nerdgraph.New(tc),
// }

// response, err := fetchLicenseKey(context.Background(), nr, 0)
// require.NoError(t, err)
// require.Equal(t, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", response)
// }

// var missingLicenseKey string = `
// {
// "data": {
// "actor": {
// "account": {
// }
// }
// }
// }
// `

// var withLicenseKey string = `
// {
// "data": {
// "actor": {
// "account": {
// "licenseKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
// }
// }
// }
// }
// `

// var withNilLicenseKey string = `
// {
// "data": {
// "actor": {
// "account": {
// "licenseKey": null
// }
// }
// }
// }
// `
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ require (
github.com/client9/misspell v0.3.4
github.com/fatih/color v1.12.0
github.com/git-chglog/git-chglog v0.14.2
github.com/go-openapi/strfmt v0.20.1 // indirect
github.com/go-task/task/v3 v3.4.3
github.com/golangci/golangci-lint v1.39.0
github.com/google/uuid v1.2.0
github.com/goreleaser/goreleaser v0.157.0
github.com/hokaccha/go-prettyjson v0.0.0-20210113012101-fb4e108d2519
github.com/imdario/mergo v0.3.12
github.com/itchyny/gojq v0.12.4
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/jedib0t/go-pretty/v6 v6.2.2
github.com/joshdk/go-junit v0.0.0-20210226021600-6145f504ca0d
github.com/llorllale/go-gitlint v0.0.0-20210608233938-d6303cc52cc5
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.4.1
github.com/newrelic/newrelic-client-go v0.60.2
github.com/newrelic/tutone v0.6.1
github.com/pkg/errors v0.9.1
Expand Down
Loading

0 comments on commit 6a8916b

Please sign in to comment.