Skip to content

Commit

Permalink
Merge pull request megaport#20 from MegaportPhilipBrowne/fix/integrat…
Browse files Browse the repository at this point in the history
…iontests-auth

fix: auth for integration tests, move to io from ioutil, update BuyMCR calls in tests
  • Loading branch information
mega-nick authored Jan 19, 2024
2 parents f0366b5 + 1b3d476 commit 73fd0e5
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 62 deletions.
4 changes: 2 additions & 2 deletions config/deafult_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (d *DefaultLogger) SetLevel(l Level) {
func (d *DefaultLogger) log(level Level, args ...interface{}) {
if level >= d.level {
msg := fmt.Sprint(args...)
log.Print(fmt.Sprintf("%s: %s", strings.ToUpper(level.String()), msg))
log.Printf("%s: %s", strings.ToUpper(level.String()), msg)
}
}

Expand Down Expand Up @@ -101,7 +101,7 @@ func (d *DefaultLogger) Error(args ...interface{}) {
func (d *DefaultLogger) logf(level Level, format string, args ...interface{}) {
if level >= d.level {
msg := fmt.Sprintf(format, args...)
log.Print(fmt.Sprintf("%s: %s", strings.ToUpper(level.String()), msg))
log.Printf("%s: %s", strings.ToUpper(level.String()), msg)
}
}

Expand Down
1 change: 1 addition & 0 deletions service/authentication/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Authentication struct {
}

func New(cfg *config.Config) *Authentication {
cfg.Client = config.NewHttpClient()
return &Authentication{
Config: cfg,
}
Expand Down
29 changes: 0 additions & 29 deletions service/authentication/authentication_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,3 @@ func TestLoginOauth(t *testing.T) {

logger.Info(token)
}

func TestLoginUsername(t *testing.T) {

if username == "" {
logger.Error("MEGAPORT_USERNAME environment variable not set.")
os.Exit(1)
}

if password == "" {
logger.Error("MEGAPORT_PASSWORD environment variable not set.")
os.Exit(1)
}

auth := New(&cfg)
token, loginErr := auth.LoginUsername(username, password, otp)

assert.NoError(t, loginErr)

if loginErr != nil {
logger.Errorf("LoginError: %s", loginErr.Error())
}

// Session Token is not empty
assert.NotEmpty(t, token)
// SessionToken is a valid guid
assert.NotNil(t, shared.IsGuid(token))

logger.Info(token)
}
3 changes: 2 additions & 1 deletion service/location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package location
import (
"encoding/json"
"errors"
"io"
"io/ioutil"

"github.com/lithammer/fuzzysearch/fuzzy"
Expand Down Expand Up @@ -91,7 +92,7 @@ func (l *Location) GetAllLocations() ([]types.Location, error) {
return nil, compiledResError
}

body, fileErr := ioutil.ReadAll(response.Body)
body, fileErr := io.ReadAll(response.Body)

if fileErr != nil {
return nil, fileErr
Expand Down
4 changes: 2 additions & 2 deletions service/location/location_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func TestMain(m *testing.M) {
Endpoint: MEGAPORTURL,
}

auth := authentication.New(&cfg, clientID, clientSecret)
token, loginErr := auth.Login()
auth := authentication.New(&cfg)
token, loginErr := auth.LoginOauth(clientID, clientSecret)

if loginErr != nil {
logger.Errorf("LoginError: %s", loginErr.Error())
Expand Down
12 changes: 6 additions & 6 deletions service/mcr/mcr_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func TestMain(m *testing.M) {
Endpoint: MEGAPORTURL,
}

auth := authentication.New(&cfg, clientID, clientSecret)
token, loginErr := auth.Login()
auth := authentication.New(&cfg)
token, loginErr := auth.LoginOauth(clientID, clientSecret)

if loginErr != nil {
logger.Errorf("LoginError: %s", loginErr.Error())
Expand All @@ -87,7 +87,7 @@ func TestMCRLifecycle(t *testing.T) {
testLocation := location.GetRandom(TEST_MCR_TEST_LOCATION_MARKET)

logger.Debugf("Test location determined, Location: %s", testLocation.Name)
mcrId, portErr := mcr.BuyMCR(testLocation.ID, "Buy MCR", 1000, 0)
mcrId, portErr := mcr.BuyMCR(testLocation.ID, "Buy MCR", 1, 1000, 0)

if !assert.NoError(portErr) && assert.False(shared.IsGuid(mcrId)) {
mcr.Config.PurchaseError(mcrId, portErr)
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestMCRConnectionAdd(t *testing.T) {
logger.Infof("Test location determined, Location: %s", testLocation.Name)
logger.Debug("Buying MCR")

mcrId, mcrErr := mcr.BuyMCR(testLocation.ID, "MCR and AWS Interconnectivity", 1000, 0)
mcrId, mcrErr := mcr.BuyMCR(testLocation.ID, "MCR and AWS Interconnectivity", 1, 1000, 0)

logger.Infof("MCR Purchased: %s", mcrId)

Expand Down Expand Up @@ -234,7 +234,7 @@ func TestPortSpeedValidation(t *testing.T) {
location := location.New(&cfg)

testLocation, _ := location.GetLocationByName("Global Switch Sydney")
_, buyErr := mcr.BuyMCR(testLocation.ID, "Test MCR", 500, 0)
_, buyErr := mcr.BuyMCR(testLocation.ID, "Test MCR", 1, 500, 0)
assert.EqualError(buyErr, mega_err.ERR_MCR_INVALID_PORT_SPEED)
}

Expand All @@ -247,7 +247,7 @@ func TestCreatePrefixFilterList(t *testing.T) {
testLocation := location.GetRandom(TEST_MCR_TEST_LOCATION_MARKET)

logger.Infof("Test location determined, Location: %s", testLocation.Name)
mcrId, portErr := mcr.BuyMCR(testLocation.ID, "Buy MCR", 1000, 0)
mcrId, portErr := mcr.BuyMCR(testLocation.ID, "Buy MCR", 1, 1000, 0)

if !assert.NoError(portErr) && assert.False(shared.IsGuid(mcrId)) {
mcr.Config.PurchaseError(mcrId, portErr)
Expand Down
4 changes: 2 additions & 2 deletions service/partner/partner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package partner
import (
"encoding/json"
"errors"
"io/ioutil"
"io"

"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/megaport/megaportgo/config"
Expand Down Expand Up @@ -48,7 +48,7 @@ func (p *Partner) GetAllPartnerMegaports() ([]types.PartnerMegaport, error) {
}
defer response.Body.Close()

body, fileErr := ioutil.ReadAll(response.Body)
body, fileErr := io.ReadAll(response.Body)

if fileErr != nil {
return []types.PartnerMegaport{}, fileErr
Expand Down
4 changes: 2 additions & 2 deletions service/partner/partner_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func TestMain(m *testing.M) {
Endpoint: MEGAPORTURL,
}

auth := authentication.New(&cfg, clientID, clientSecret)
token, loginErr := auth.Login()
auth := authentication.New(&cfg)
token, loginErr := auth.LoginOauth(clientID, clientSecret)

if loginErr != nil {
logger.Errorf("LoginError: %s", loginErr.Error())
Expand Down
10 changes: 5 additions & 5 deletions service/port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package port
import (
"encoding/json"
"errors"
"io/ioutil"
"io"
"slices"
"time"

Expand Down Expand Up @@ -56,7 +56,7 @@ func (p *Port) BuyPort(name string, term int, portSpeed int, locationId int, mar

if isLAG {
buyOrder = []types.PortOrder{
types.PortOrder{
{
Name: name,
Term: term,
ProductType: "MEGAPORT",
Expand All @@ -71,7 +71,7 @@ func (p *Port) BuyPort(name string, term int, portSpeed int, locationId int, mar
}
} else {
buyOrder = []types.PortOrder{
types.PortOrder{
{
Name: name,
Term: term,
ProductType: "MEGAPORT",
Expand Down Expand Up @@ -123,7 +123,7 @@ func (p *Port) GetPortDetails(id string) (types.Port, error) {
return types.Port{}, parsedError
}

body, fileErr := ioutil.ReadAll(response.Body)
body, fileErr := io.ReadAll(response.Body)

if fileErr != nil {
return types.Port{}, fileErr
Expand Down Expand Up @@ -156,7 +156,7 @@ func (p *Port) GetPorts() ([]types.Port, error) {
return []types.Port{}, parsedError
}

body, fileErr := ioutil.ReadAll(response.Body)
body, fileErr := io.ReadAll(response.Body)

if fileErr != nil {
return []types.Port{}, fileErr
Expand Down
4 changes: 2 additions & 2 deletions service/port/port_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func TestMain(m *testing.M) {
Endpoint: MEGAPORTURL,
}

auth := authentication.New(&cfg, clientID, clientSecret)
token, loginErr := auth.Login()
auth := authentication.New(&cfg)
token, loginErr := auth.LoginOauth(clientID, clientSecret)

if loginErr != nil {
logger.Errorf("LoginError: %s", loginErr.Error())
Expand Down
4 changes: 2 additions & 2 deletions service/vxc/partner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package vxc
import (
"encoding/json"
"errors"
"io/ioutil"
"io"
"strings"

"github.com/megaport/megaportgo/mega_err"
Expand All @@ -41,7 +41,7 @@ func (v *VXC) LookupPartnerPorts(key string, portSpeed int, partner string, requ
return "", compiledErr
}

body, fileErr := ioutil.ReadAll(response.Body)
body, fileErr := io.ReadAll(response.Body)

if fileErr != nil {
return "", fileErr
Expand Down
19 changes: 12 additions & 7 deletions service/vxc/vxc_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func TestMain(m *testing.M) {
Endpoint: MEGAPORTURL,
}

auth := authentication.New(&cfg, clientID, clientSecret)
token, loginErr := auth.Login()
auth := authentication.New(&cfg)
token, loginErr := auth.LoginOauth(clientID, clientSecret)

if loginErr != nil {
logger.Errorf("LoginError: %s", loginErr.Error())
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestAWSHostedConnectionBuy(t *testing.T) {
testLocation := loc.GetRandom(MCR_LOCATION)

logger.Info("Buying AWS Hosted Connection MCR (A End).")
mcrId, mcrErr := mcr.BuyMCR(testLocation.ID, "AWS Hosted Conection Test MCR", 1000, 0)
mcrId, mcrErr := mcr.BuyMCR(testLocation.ID, "AWS Hosted Conection Test MCR", 1, 1000, 0)
logger.Infof("MCR Purchased: %s", mcrId)

if !assert.NoError(t, mcrErr) && !assert.True(t, shared.IsGuid(mcrId)) {
Expand All @@ -249,7 +249,7 @@ func TestAWSHostedConnectionBuy(t *testing.T) {
VLAN: shared.GenerateRandomVLAN(),
PartnerConfig: types.VXCOrderAEndPartnerConfig{
Interfaces: []types.PartnerConfigInterface{
types.PartnerConfigInterface{
{
IpAddresses: []string{"10.0.0.1/30"},
IpRoutes: []types.IpRoute{
{
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestAWSHostedConnectionBuy(t *testing.T) {
},
},
types.AWSVXCOrderBEndConfiguration{
ProductUID: "b2e0b6b8-2943-4c44-8a07-9ec13060afb2",
ProductUID: "b047870a-adcf-441f-ae34-27a796cdafeb",
PartnerConfig: types.AWSVXCOrderBEndPartnerConfig{
ConnectType: "AWSHC",
Type: "private",
Expand Down Expand Up @@ -407,12 +407,14 @@ func TestBuyAzureExpressRoute(t *testing.T) {
// get partner port
partnerPortId, partnerLookupErr := vxc.LookupPartnerPorts(serviceKey, 1000, PARTNER_AZURE, "")
if partnerLookupErr != nil {
logger.Errorf("Partner lookup error: %s", partnerLookupErr)
t.FailNow()
}

// get partner config
partnerConfig, partnerConfigErr := vxc.MarshallPartnerConfig(serviceKey, PARTNER_AZURE, peerings)
if partnerConfigErr != nil {
logger.Errorf("Partner config error: %s", partnerConfigErr)
t.FailNow()
}

Expand Down Expand Up @@ -448,7 +450,6 @@ func TestBuyAzureExpressRoute(t *testing.T) {
portDeleteStatus, portDeleteErr := port.DeletePort(portId, true)
assert.NoError(t, portDeleteErr)
assert.True(t, portDeleteStatus)

}

func TestBuyGoogleInterconnect(t *testing.T) {
Expand All @@ -466,18 +467,20 @@ func TestBuyGoogleInterconnect(t *testing.T) {
}
port.WaitForPortProvisioning(portId)

pairingKey := "7e51371e-72a3-40b5-b844-2e3efefaee59/us-central1/2"
pairingKey := "27325c3a-b640-4b69-a2d5-cdcca797a151/us-west2/1"
logger.Info("Buying Google Interconnect VXC (B End).")

// get partner port
partnerPortId, partnerLookupErr := vxc.LookupPartnerPorts(pairingKey, 1000, PARTNER_GOOGLE, "")
if partnerLookupErr != nil {
logger.Errorf("Partner lookup error: %s", partnerLookupErr)
t.FailNow()
}

// get partner config
partnerConfig, partnerConfigErr := vxc.MarshallPartnerConfig(pairingKey, PARTNER_GOOGLE, nil)
if partnerConfigErr != nil {
logger.Errorf("Partner config error: %s", partnerConfigErr)
t.FailNow()
}

Expand Down Expand Up @@ -536,12 +539,14 @@ func TestBuyGoogleInterconnectLocation(t *testing.T) {
// get partner port
partnerPortId, partnerLookupErr := vxc.LookupPartnerPorts(pairingKey, 1000, PARTNER_GOOGLE, TEST_LOCATION_C)
if partnerLookupErr != nil {
logger.Errorf("Partner lookup error: %s", partnerLookupErr)
t.FailNow()
}

// get partner config
partnerConfig, partnerConfigErr := vxc.MarshallPartnerConfig(pairingKey, PARTNER_GOOGLE, nil)
if partnerConfigErr != nil {
logger.Errorf("Partner config error: %s", partnerConfigErr)
t.FailNow()
}

Expand Down
7 changes: 5 additions & 2 deletions test/create-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ func main() {
logger := config.NewDefaultLogger()
logger.SetLevel(config.Off)

client := config.NewHttpClient()

cfg := config.Config{
Log: logger,
Endpoint: ENDPOINTURL,
Client: client,
}

auth := authentication.New(&cfg, username, password, "")
auth := authentication.New(&cfg)

fmt.Println("Establishing Session for user")
session, err := auth.Login()
session, err := auth.LoginOauth(username, password)
if err != nil {
fmt.Println("Unable to establish session for user: ", err)
os.Exit(1)
Expand Down

0 comments on commit 73fd0e5

Please sign in to comment.