Skip to content

Commit

Permalink
instance command tests to use helper
Browse files Browse the repository at this point in the history
  • Loading branch information
darrellwarde committed Aug 14, 2024
1 parent e560daa commit de63811
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 627 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
164 changes: 32 additions & 132 deletions neo4j/aura/internal/subcommands/instance/create_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
package instance_test

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/neo4j/cli/common/clicfg"
"github.com/neo4j/cli/common/clictx"
"github.com/neo4j/cli/neo4j/aura"
"github.com/neo4j/cli/neo4j/aura/internal/test/testutils"
"github.com/neo4j/cli/test/utils/testfs"
"github.com/stretchr/testify/assert"
)

func TestCreateFreeInstance(t *testing.T) {
Expand Down Expand Up @@ -57,29 +47,10 @@ func TestCreateFreeInstance(t *testing.T) {
}

func TestCreateProfessionalInstance(t *testing.T) {
assert := assert.New(t)

mux := http.NewServeMux()

var authCounter = 0
mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) {
authCounter++
res.WriteHeader(200)
res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`))
})

var postCounter = 0
mux.HandleFunc("/v1/instances", func(res http.ResponseWriter, req *http.Request) {
postCounter++

assert.Equal(http.MethodPost, req.Method)
assert.Equal("/v1/instances", req.URL.Path)
body, err := io.ReadAll(req.Body)
assert.Nil(err)
assert.Equal(`{"cloud_provider":"gcp","memory":"4GB","name":"Instance01","region":"europe-west1","tenant_id":"YOUR_TENANT_ID","type":"professional-db","version":"5"}`, string(body))
helper := testutils.NewAuraTestHelper(t)
defer helper.Close()

res.WriteHeader(200)
res.Write([]byte(`{
mockHandler := helper.NewRequestHandlerMock("/v1/instances", http.StatusOK, `{
"data": {
"id": "db1d1234",
"connection_url": "YOUR_CONNECTION_URL",
Expand All @@ -91,114 +62,43 @@ func TestCreateProfessionalInstance(t *testing.T) {
"type": "professional-db",
"name": "Instance01"
}
}`))

})

server := httptest.NewServer(mux)
defer server.Close()

cmd := aura.NewCmd()
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"instance", "create", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), "--region", "europe-west1", "--name", "Instance01", "--type", "professional-db", "--tenant-id", "YOUR_TENANT_ID", "--cloud-provider", "gcp", "--memory", "4GB"})

fs, err := testfs.GetTestFs(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}`)

helper.ExecuteCommand("instance create --region europe-west1 --name Instance01 --type professional-db --tenant-id YOUR_TENANT_ID --cloud-provider gcp --memory 4GB")

mockHandler.AssertCalledTimes(1)
mockHandler.AssertCalledWithMethod(http.MethodPost)
mockHandler.AssertCalledWithBody(`{"cloud_provider":"gcp","memory":"4GB","name":"Instance01","region":"europe-west1","tenant_id":"YOUR_TENANT_ID","type":"professional-db","version":"5"}`)

helper.AssertOutJson(`{
"data": {
"id": "db1d1234",
"connection_url": "YOUR_CONNECTION_URL",
"username": "neo4j",
"password": "letMeIn123!",
"tenant_id": "YOUR_TENANT_ID",
"cloud_provider": "gcp",
"region": "europe-west1",
"type": "professional-db",
"name": "Instance01"
}
}`)
assert.Nil(err)

cfg, err := clicfg.NewConfig(fs)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
assert.Nil(err)

err = cmd.ExecuteContext(ctx)
assert.Nil(err)

out, err := io.ReadAll(b)
assert.Nil(err)

assert.Equal(1, authCounter)
assert.Equal(1, postCounter)

assert.Equal(`{
"data": {
"id": "db1d1234",
"connection_url": "YOUR_CONNECTION_URL",
"username": "neo4j",
"password": "letMeIn123!",
"tenant_id": "YOUR_TENANT_ID",
"cloud_provider": "gcp",
"region": "europe-west1",
"type": "professional-db",
"name": "Instance01"
}
}
`, string(out))
`)
}

func TestCreateProfessionalInstanceNoMemory(t *testing.T) {
assert := assert.New(t)

mux := http.NewServeMux()

var authCounter = 0
mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) {
authCounter++
})

var postCounter = 0
mux.HandleFunc("/v1/instances", func(res http.ResponseWriter, req *http.Request) {
postCounter++
})

server := httptest.NewServer(mux)
defer server.Close()

cmd := aura.NewCmd()
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"instance", "create", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), "--region", "europe-west1", "--name", "Instance01", "--type", "professional-db", "--tenant-id", "YOUR_TENANT_ID", "--cloud-provider", "gcp"})

fs, err := testfs.GetTestFs(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`)
assert.Nil(err)

cfg, err := clicfg.NewConfig(fs)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
assert.Nil(err)
helper := testutils.NewAuraTestHelper(t)
defer helper.Close()

err = cmd.ExecuteContext(ctx)
assert.ErrorContains(err, `required flag(s) "memory" not set`)
mockHandler := helper.NewRequestHandlerMock("/v1/instances", http.StatusOK, "")

assert.Equal(0, authCounter)
assert.Equal(0, postCounter)
helper.ExecuteCommand("instance create --region europe-west1 --name Instance01 --type professional-db --tenant-id YOUR_TENANT_ID --cloud-provider gcp")

out, err := io.ReadAll(b)
assert.Nil(err)
mockHandler.AssertCalledTimes(0)

assert.Equal(`Error: required flag(s) "memory" not set
Usage:
helper.AssertErr(`Error: required flag(s) "memory" not set
`)
helper.AssertOut(`Usage:
aura instance create [flags]
Flags:
Expand All @@ -217,5 +117,5 @@ Global Flags:
--base-url string
--output string
`, string(out))
`)
}
107 changes: 26 additions & 81 deletions neo4j/aura/internal/subcommands/instance/get_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
package instance_test

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/neo4j/cli/common/clicfg"
"github.com/neo4j/cli/common/clictx"
"github.com/neo4j/cli/neo4j/aura"
"github.com/neo4j/cli/test/utils/testfs"
"github.com/stretchr/testify/assert"
"github.com/neo4j/cli/neo4j/aura/internal/test/testutils"
)

func TestGetInstance(t *testing.T) {
assert := assert.New(t)
helper := testutils.NewAuraTestHelper(t)
defer helper.Close()

var instanceId = "2f49c2b3"
instanceId := "2f49c2b3"

mux := http.NewServeMux()

var authCounter = 0
mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) {
authCounter++
res.WriteHeader(200)
res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`))
})

var getCounter = 0
mux.HandleFunc(fmt.Sprintf("/v1/instances/%s", instanceId), func(res http.ResponseWriter, req *http.Request) {
getCounter++

assert.Equal(http.MethodGet, req.Method)
assert.Equal(fmt.Sprintf("/v1/instances/%s", instanceId), req.URL.Path)

res.WriteHeader(200)
res.Write([]byte(`{
mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/instances/%s", instanceId), http.StatusOK, `{
"data": {
"id": "2f49c2b3",
"name": "Production",
Expand All @@ -52,58 +28,27 @@ func TestGetInstance(t *testing.T) {
"memory": "8GB",
"storage": "16GB"
}
}`))
})

server := httptest.NewServer(mux)
defer server.Close()

cmd := aura.NewCmd()
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"instance", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), instanceId})

fs, err := testfs.GetTestFs(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}`)

helper.ExecuteCommand(fmt.Sprintf("instance get %s", instanceId))

mockHandler.AssertCalledTimes(1)
mockHandler.AssertCalledWithMethod(http.MethodGet)

helper.AssertOutJson(`{
"data": {
"id": "2f49c2b3",
"name": "Production",
"status": "running",
"tenant_id": "YOUR_TENANT_ID",
"cloud_provider": "gcp",
"connection_url": "YOUR_CONNECTION_URL",
"metrics_integration_url": "YOUR_METRICS_INTEGRATION_ENDPOINT",
"region": "europe-west1",
"type": "enterprise-db",
"memory": "8GB",
"storage": "16GB"
}
}`)
assert.Nil(err)

cfg, err := clicfg.NewConfig(fs)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
assert.Nil(err)

err = cmd.ExecuteContext(ctx)
assert.Nil(err)

out, err := io.ReadAll(b)
assert.Nil(err)

assert.Equal(1, authCounter)
assert.Equal(1, getCounter)

assert.Equal(`{
"data": {
"id": "2f49c2b3",
"name": "Production",
"status": "running",
"tenant_id": "YOUR_TENANT_ID",
"cloud_provider": "gcp",
"connection_url": "YOUR_CONNECTION_URL",
"metrics_integration_url": "YOUR_METRICS_INTEGRATION_ENDPOINT",
"region": "europe-west1",
"type": "enterprise-db",
"memory": "8GB",
"storage": "16GB"
}
}
`, string(out))
`)
}
Loading

0 comments on commit de63811

Please sign in to comment.