Skip to content

Commit

Permalink
Refactor cloud package tests (#291)
Browse files Browse the repository at this point in the history
* Refactor cloud package tests

* Remove not needed nolint marks

* Pipe mock usage calls and move mocks to test setup

* Add brackets to case to improve readability
  • Loading branch information
arbulu89 authored Nov 20, 2023
1 parent 51a2b68 commit 56711ae
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 360 deletions.
18 changes: 9 additions & 9 deletions internal/core/cloud/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const (
)

type AWSMetadata struct {
AmiID string `json:"ami-id"` // nolint
AmiID string `json:"ami-id"`
BlockDeviceMapping map[string]string `json:"block-device-mapping"`
IdentityCredentials IdentityCredentials `json:"identity-credentials"`
InstanceID string `json:"instance-id"` //nolint
InstanceType string `json:"instance-type,omitempty"` //nolint
InstanceID string `json:"instance-id"`
InstanceType string `json:"instance-type,omitempty"`
Network AWSNetwork `json:"network"`
Placement Placement `json:"placement"`
}
Expand Down Expand Up @@ -52,7 +52,7 @@ type Placement struct {
Region string `json:"region"`
}

func NewAWSMetadata() (*AWSMetadata, error) {
func NewAWSMetadata(client HTTPClient) (*AWSMetadata, error) {
var err error
awsMetadata := &AWSMetadata{
AmiID: "",
Expand Down Expand Up @@ -86,7 +86,7 @@ func NewAWSMetadata() (*AWSMetadata, error) {
}

firstElementsList := []string{fmt.Sprintf("%s/", awsMetadataResource)}
metadata, err := buildAWSMetadata(awsMetadataURL, firstElementsList)
metadata, err := buildAWSMetadata(client, awsMetadataURL, firstElementsList)
if err != nil {
return nil, err
}
Expand All @@ -104,13 +104,13 @@ func NewAWSMetadata() (*AWSMetadata, error) {
return awsMetadata, err
}

func buildAWSMetadata(url string, elements []string) (map[string]interface{}, error) {
func buildAWSMetadata(client HTTPClient, url string, elements []string) (map[string]interface{}, error) {
metadata := make(map[string]interface{})

for _, element := range elements {
newURL := url + element

response, err := requestMetadata(newURL)
response, err := requestMetadata(client, newURL)
if err != nil {
return metadata, err
}
Expand All @@ -119,7 +119,7 @@ func buildAWSMetadata(url string, elements []string) (map[string]interface{}, er
currentElement := strings.Trim(element, "/")
newElements := strings.Split(fmt.Sprintf("%v", response), "\n")

metadata[currentElement], err = buildAWSMetadata(newURL, newElements)
metadata[currentElement], err = buildAWSMetadata(client, newURL, newElements)
if err != nil {
return nil, err
}
Expand All @@ -131,7 +131,7 @@ func buildAWSMetadata(url string, elements []string) (map[string]interface{}, er
return metadata, nil
}

func requestMetadata(url string) (interface{}, error) {
func requestMetadata(client HTTPClient, url string) (interface{}, error) {
req, _ := http.NewRequest(http.MethodGet, url, nil)

resp, err := client.Do(req)
Expand Down
15 changes: 8 additions & 7 deletions internal/core/cloud/aws_dto_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cloud
package cloud_test

import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/core/cloud"
)

type AWSMetadataDtoTestSuite struct {
Expand All @@ -16,7 +17,7 @@ func TestAWSMetadataDtoTestSuite(t *testing.T) {

func (suite *AWSMetadataDtoTestSuite) TestNewAWSMetadataDto() {

awsMetadata := &AWSMetadata{ //nolint
awsMetadata := &cloud.AWSMetadata{
AmiID: "some-ami",
BlockDeviceMapping: map[string]string{
"ami": "sda",
Expand All @@ -26,20 +27,20 @@ func (suite *AWSMetadataDtoTestSuite) TestNewAWSMetadataDto() {
},
InstanceID: "some-instance",
InstanceType: "some-instance-type",
Placement: Placement{
Placement: cloud.Placement{
AvailabilityZone: "some-availability-zone",
Region: "some-region",
},
}

awsMetadata.IdentityCredentials.EC2.Info.AccountID = "some-account"
awsMetadata.Network.Interfaces.Macs = make(map[string]MacEntry)
macEntry := MacEntry{VpcID: "some-vpc-id"}
awsMetadata.Network.Interfaces.Macs = make(map[string]cloud.MacEntry)
macEntry := cloud.MacEntry{VpcID: "some-vpc-id"}
awsMetadata.Network.Interfaces.Macs["eth1"] = macEntry

awsMetadataDto := NewAWSMetadataDto(awsMetadata)
awsMetadataDto := cloud.NewAWSMetadataDto(awsMetadata)

expectedDto := &AWSMetadataDto{
expectedDto := &cloud.AWSMetadataDto{
AccountID: "some-account",
AmiID: "some-ami",
AvailabilityZone: "some-availability-zone",
Expand Down
9 changes: 4 additions & 5 deletions internal/core/cloud/aws_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cloud
package cloud_test

import (
"bytes"
Expand All @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/core/cloud"
"github.com/trento-project/agent/internal/core/cloud/mocks"
"github.com/trento-project/agent/test/helpers"
)
Expand Down Expand Up @@ -54,7 +55,7 @@ func (suite *AWSMetadataTestSuite) TestNewAWSMetadata() {
bodyText, _ := io.ReadAll(aFile)
body := io.NopCloser(bytes.NewReader(bodyText))

response := &http.Response{ //nolint
response := &http.Response{
StatusCode: 200,
Body: body,
}
Expand All @@ -64,9 +65,7 @@ func (suite *AWSMetadataTestSuite) TestNewAWSMetadata() {
).Once()
}

client = clientMock

m, err := NewAWSMetadata()
m, err := cloud.NewAWSMetadata(clientMock)

suite.NoError(err)

Expand Down
2 changes: 1 addition & 1 deletion internal/core/cloud/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ type Subnet struct {
Prefix string `json:"prefix,omitempty"`
}

func NewAzureMetadata() (*AzureMetadata, error) {
func NewAzureMetadata(client HTTPClient) (*AzureMetadata, error) {
var err error
m := &AzureMetadata{
Compute: Compute{
Expand Down
64 changes: 31 additions & 33 deletions internal/core/cloud/azure_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//nolint:lll
package cloud
package cloud_test

import (
"bytes"
Expand All @@ -10,6 +9,7 @@ import (

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/core/cloud"
"github.com/trento-project/agent/internal/core/cloud/mocks"
"github.com/trento-project/agent/test/helpers"
)
Expand All @@ -29,7 +29,7 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
bodyText, _ := io.ReadAll(aFile)
body := io.NopCloser(bytes.NewReader(bodyText))

response := &http.Response{ //nolint
response := &http.Response{
StatusCode: 200,
Body: body,
}
Expand All @@ -38,27 +38,25 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
response, nil,
)

client = clientMock
m, err := cloud.NewAzureMetadata(clientMock)

m, err := NewAzureMetadata()

expectedMeta := &AzureMetadata{
Compute: Compute{
expectedMeta := &cloud.AzureMetadata{
Compute: cloud.Compute{
AzEnvironment: "AzurePublicCloud",
EvictionPolicy: "",
IsHostCompatibilityLayerVM: "false",
LicenseType: "",
Location: "westeurope",
Name: "vmhana01",
Offer: "sles-sap-15-sp2-byos",
OsProfile: OsProfile{
OsProfile: cloud.OsProfile{
AdminUserName: "cloudadmin",
ComputerName: "vmhana01",
DisablePasswordAuthentication: "true",
},
OsType: "Linux",
PlacementGroupID: "",
Plan: Plan{
Plan: cloud.Plan{
Name: "",
Product: "",
Publisher: "",
Expand All @@ -68,7 +66,7 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
Priority: "",
Provider: "Microsoft.Compute",

PublicKeys: []*PublicKey{
PublicKeys: []*cloud.PublicKey{
{
KeyData: "ssh-rsa content\n",
Path: "/home/cloudadmin/.ssh/authorized_keys",
Expand All @@ -77,13 +75,13 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
Publisher: "SUSE",
ResourceGroupName: "test",
ResourceID: "/subscriptions/xxxxx/resourceGroups/test/providers/Microsoft.Compute/virtualMachines/vmhana01",
SecurityProfile: SecurityProfile{
SecurityProfile: cloud.SecurityProfile{
SecureBootEnabled: "false",
VirtualTpmEnabled: "false",
},
Sku: "gen2",
StorageProfile: StorageProfile{
DataDisks: []*Disk{
StorageProfile: cloud.StorageProfile{
DataDisks: []*cloud.Disk{
{
Caching: "None",
CreateOption: "Empty",
Expand All @@ -92,8 +90,8 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
"uri": "",
},
Lun: "0",
ManagedDisk: ManagedDisk{
ID: "/subscriptions/xxxxx/resourceGroups/test/providers/Microsoft.Compute/disks/disk-hana01-Data01", //nolint:lll
ManagedDisk: cloud.ManagedDisk{
ID: "/subscriptions/xxxxx/resourceGroups/test/providers/Microsoft.Compute/disks/disk-hana01-Data01",
StorageAccountType: "Premium_LRS",
},
Name: "disk-hana01-Data01",
Expand All @@ -110,8 +108,8 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
"uri": "",
},
Lun: "1",
ManagedDisk: ManagedDisk{
ID: "/subscriptions/xxxxx/resourceGroups/test/providers/Microsoft.Compute/disks/disk-hana01-Data02", //nolint:lll
ManagedDisk: cloud.ManagedDisk{
ID: "/subscriptions/xxxxx/resourceGroups/test/providers/Microsoft.Compute/disks/disk-hana01-Data02",
StorageAccountType: "Premium_LRS",
},
Name: "disk-hana01-Data02",
Expand All @@ -121,14 +119,14 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
WriteAcceleratorEnabled: "false",
},
},
ImageReference: ImageReference{
ImageReference: cloud.ImageReference{
ID: "",
Offer: "sles-sap-15-sp2-byos",
Publisher: "SUSE",
Sku: "gen2",
Version: "latest",
},
OsDisk: Disk{
OsDisk: cloud.Disk{
Caching: "ReadWrite",
CreateOption: "FromImage",
DiffDiskSettings: map[string]string{
Expand All @@ -142,7 +140,7 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
"uri": "",
},
Lun: "",
ManagedDisk: ManagedDisk{
ManagedDisk: cloud.ManagedDisk{
ID: "/subscriptions/xxxxx/resourceGroups/test/providers/Microsoft.Compute/disks/disk-hana01-Os",
StorageAccountType: "Premium_LRS",
},
Expand All @@ -169,26 +167,26 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
VMSize: "Standard_E4s_v3",
Zone: "",
},
Network: Network{
Interfaces: []*Interface{
Network: cloud.Network{
Interfaces: []*cloud.Interface{
{
Ipv4: IP{
Addresses: []*Address{
Ipv4: cloud.IP{
Addresses: []*cloud.Address{
{
PrivateIP: "10.74.1.10",
PublicIP: "1.2.3.4",
},
},
Subnets: []*Subnet{
Subnets: []*cloud.Subnet{
{
Address: "10.74.1.0",
Prefix: "24",
},
},
},
Ipv6: IP{
Addresses: []*Address{},
Subnets: []*Subnet(nil),
Ipv6: cloud.IP{
Addresses: []*cloud.Address{},
Subnets: []*cloud.Subnet(nil),
},
MacAddress: "000D3A2267C3",
},
Expand All @@ -201,8 +199,8 @@ func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
}

func (suite *AzureMetadataTestSuite) TestGetVmUrl() {
meta := &AzureMetadata{ //nolint
Compute: Compute{ //nolint
meta := &cloud.AzureMetadata{
Compute: cloud.Compute{
ResourceID: "myresourceid",
},
}
Expand All @@ -211,8 +209,8 @@ func (suite *AzureMetadataTestSuite) TestGetVmUrl() {
}

func (suite *AzureMetadataTestSuite) TestGetResourceGroupUrl() {
meta := &AzureMetadata{ //nolint
Compute: Compute{ //nolint
meta := &cloud.AzureMetadata{
Compute: cloud.Compute{
SubscriptionID: "xxx",
ResourceGroupName: "myresourcegroupname",
},
Expand Down
2 changes: 0 additions & 2 deletions internal/core/cloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ import "net/http"
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

var client HTTPClient = &http.Client{Transport: &http.Transport{Proxy: nil}} //nolint
2 changes: 1 addition & 1 deletion internal/core/cloud/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type GCPProject struct {
ProjectID string `json:"projectId,omitempty"`
}

func NewGCPMetadata() (*GCPMetadata, error) {
func NewGCPMetadata(client HTTPClient) (*GCPMetadata, error) {
var err error
m := &GCPMetadata{
Instance: GCPInstance{
Expand Down
Loading

0 comments on commit 56711ae

Please sign in to comment.