Skip to content

Commit

Permalink
Move the individual unit test function to suites (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 authored Aug 30, 2022
1 parent 09406ed commit c448f98
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 318 deletions.
14 changes: 11 additions & 3 deletions internal/cloud/aws_dto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ package cloud
import (
"testing"

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

func TestNewAwsMetadataDto(t *testing.T) {
type AwsMetadataDtoTestSuite struct {
suite.Suite
}

func TestAwsMetadataDtoTestSuite(t *testing.T) {
suite.Run(t, new(AwsMetadataDtoTestSuite))
}

func (suite *AwsMetadataDtoTestSuite) TestNewAwsMetadataDto() {

awsMetadata := &AwsMetadata{ //nolint
AmiID: "some-ami",
Expand Down Expand Up @@ -41,5 +49,5 @@ func TestNewAwsMetadataDto(t *testing.T) {
Region: "some-region",
VpcID: "some-vpc-id",
}
assert.Equal(t, expectedDto, awsMetadataDto)
suite.Equal(expectedDto, awsMetadataDto)
}
30 changes: 19 additions & 11 deletions internal/cloud/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cloud/mocks"
)

func TestNewAwsMetadata(t *testing.T) {
type AwsMetadataTestSuite struct {
suite.Suite
}

func TestAwsMetadataTestSuite(t *testing.T) {
suite.Run(t, new(AwsMetadataTestSuite))
}

func (suite *AwsMetadataTestSuite) TestNewAwsMetadata() {
clientMock := new(mocks.HTTPClient)

fixtures := []string{
Expand Down Expand Up @@ -59,18 +67,18 @@ func TestNewAwsMetadata(t *testing.T) {

m, err := NewAwsMetadata()

assert.NoError(t, err)
suite.NoError(err)

assert.Equal(t, "some-ami-id", m.AmiID)
assert.Equal(t, map[string]string{
suite.Equal("some-ami-id", m.AmiID)
suite.Equal(map[string]string{
"root": "/dev/sda",
"ebs1": "/dev/sdb1",
"ebs2": "/dev/sdb2",
}, m.BlockDeviceMapping)
assert.Equal(t, "some-instance", m.InstanceID)
assert.Equal(t, "some-instance-type", m.InstanceType)
assert.Equal(t, "some-account-id", m.IdentityCredentials.EC2.Info.AccountID)
assert.Equal(t, "some-vpc-id", m.Network.Interfaces.Macs["some-mac"].VpcID)
assert.Equal(t, "some-availability-zone", m.Placement.AvailabilityZone)
assert.Equal(t, "some-region", m.Placement.Region)
suite.Equal("some-instance", m.InstanceID)
suite.Equal("some-instance-type", m.InstanceType)
suite.Equal("some-account-id", m.IdentityCredentials.EC2.Info.AccountID)
suite.Equal("some-vpc-id", m.Network.Interfaces.Macs["some-mac"].VpcID)
suite.Equal("some-availability-zone", m.Placement.AvailabilityZone)
suite.Equal("some-region", m.Placement.Region)
}
24 changes: 16 additions & 8 deletions internal/cloud/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cloud/mocks"
)

func TestNewAzureMetadata(t *testing.T) {
type AzureMetadataTestSuite struct {
suite.Suite
}

func TestAzureMetadataTestSuite(t *testing.T) {
suite.Run(t, new(AzureMetadataTestSuite))
}

func (suite *AzureMetadataTestSuite) TestNewAzureMetadata() {
clientMock := new(mocks.HTTPClient)

aFile, _ := os.Open("../../test/fixtures/discovery/azure/azure_metadata.json")
Expand Down Expand Up @@ -187,27 +195,27 @@ func TestNewAzureMetadata(t *testing.T) {
},
}

assert.Equal(t, expectedMeta, m)
assert.NoError(t, err)
suite.Equal(expectedMeta, m)
suite.NoError(err)
}

func TestGetVmUrl(t *testing.T) {
func (suite *AzureMetadataTestSuite) TestGetVmUrl() {
meta := &AzureMetadata{ //nolint
Compute: Compute{ //nolint
ResourceID: "myresourceid",
},
}

assert.Equal(t, "https:/portal.azure.com/#@SUSERDBillingsuse.onmicrosoft.com/resource/myresourceid", meta.GetVMURL())
suite.Equal("https:/portal.azure.com/#@SUSERDBillingsuse.onmicrosoft.com/resource/myresourceid", meta.GetVMURL())
}

func TestGetResourceGroupUrl(t *testing.T) {
func (suite *AzureMetadataTestSuite) TestGetResourceGroupUrl() {
meta := &AzureMetadata{ //nolint
Compute: Compute{ //nolint
SubscriptionID: "xxx",
ResourceGroupName: "myresourcegroupname",
},
}

assert.Equal(t, "https:/portal.azure.com/#@SUSERDBillingsuse.onmicrosoft.com/resource/subscriptions/xxx/resourceGroups/myresourcegroupname/overview", meta.GetResourceGroupURL())
suite.Equal("https:/portal.azure.com/#@SUSERDBillingsuse.onmicrosoft.com/resource/subscriptions/xxx/resourceGroups/myresourcegroupname/overview", meta.GetResourceGroupURL())
}
14 changes: 11 additions & 3 deletions internal/cloud/gcp_dto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ package cloud
import (
"testing"

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

func TestNewGcpMetadataDto(t *testing.T) {
type GcpMetadataDtoTestSuite struct {
suite.Suite
}

func TestGcpMetadataDtoTestSuite(t *testing.T) {
suite.Run(t, new(GcpMetadataDtoTestSuite))
}

func (suite *GcpMetadataDtoTestSuite) TestNewGcpMetadataDto() {

gcpMetadata := &GcpMetadata{
Instance: GcpInstance{
Expand Down Expand Up @@ -54,5 +62,5 @@ func TestNewGcpMetadataDto(t *testing.T) {
ProjectID: "some-project-id",
Zone: "europe-west1-b",
}
assert.Equal(t, expectedDto, gcpMetadataDto)
suite.Equal(expectedDto, gcpMetadataDto)
}
16 changes: 12 additions & 4 deletions internal/cloud/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cloud/mocks"
)

func TestNewGcpMetadata(t *testing.T) {
type GcpMetadataTestSuite struct {
suite.Suite
}

func TestGcpMetadataTestSuite(t *testing.T) {
suite.Run(t, new(GcpMetadataTestSuite))
}

func (suite *GcpMetadataTestSuite) TestNewGcpMetadata() {
clientMock := new(mocks.HTTPClient)

aFile, _ := os.Open("../../test/fixtures/discovery/gcp/gcp_metadata.json")
Expand Down Expand Up @@ -67,6 +75,6 @@ func TestNewGcpMetadata(t *testing.T) {
},
}

assert.Equal(t, expectedMeta, m)
assert.NoError(t, err)
suite.Equal(expectedMeta, m)
suite.NoError(err)
}
74 changes: 41 additions & 33 deletions internal/cloud/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ import (
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cloud/mocks"
)

type CloudMetadataTestSuite struct {
suite.Suite
}

func TestCloudMetadataTestSuite(t *testing.T) {
suite.Run(t, new(CloudMetadataTestSuite))
}

func mockDmidecodeErr() *exec.Cmd {
return exec.Command("error")
}

func TestIdentifyCloudProviderErr(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestIdentifyCloudProviderErr() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand All @@ -27,15 +35,15 @@ func TestIdentifyCloudProviderErr(t *testing.T) {

provider, err := IdentifyCloudProvider()

assert.Equal(t, "", provider)
assert.EqualError(t, err, "exec: \"error\": executable file not found in $PATH")
suite.Equal("", provider)
suite.EqualError(err, "exec: \"error\": executable file not found in $PATH")
}

func mockDmidecodeAzure() *exec.Cmd {
return exec.Command("echo", "7783-7084-3265-9085-8269-3286-77")
}

func TestIdentifyCloudProviderAzure(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestIdentifyCloudProviderAzure() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand All @@ -46,8 +54,8 @@ func TestIdentifyCloudProviderAzure(t *testing.T) {

provider, err := IdentifyCloudProvider()

assert.Equal(t, "azure", provider)
assert.NoError(t, err)
suite.Equal("azure", provider)
suite.NoError(err)
}

func mockDmidecodeAwsSystem() *exec.Cmd {
Expand All @@ -58,7 +66,7 @@ func mockDmidecodeAwsManufacturer() *exec.Cmd {
return exec.Command("echo", "Amazon EC2")
}

func TestIdentifyCloudProviderAwsUsingSystemVersion(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestIdentifyCloudProviderAwsUsingSystemVersion() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand All @@ -73,11 +81,11 @@ func TestIdentifyCloudProviderAwsUsingSystemVersion(t *testing.T) {

provider, err := IdentifyCloudProvider()

assert.Equal(t, "aws", provider)
assert.NoError(t, err)
suite.Equal("aws", provider)
suite.NoError(err)
}

func TestIdentifyCloudProviderAwsUsingManufacturer(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestIdentifyCloudProviderAwsUsingManufacturer() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand All @@ -96,15 +104,15 @@ func TestIdentifyCloudProviderAwsUsingManufacturer(t *testing.T) {

provider, err := IdentifyCloudProvider()

assert.Equal(t, "aws", provider)
assert.NoError(t, err)
suite.Equal("aws", provider)
suite.NoError(err)
}

func mockDmidecodeGcp() *exec.Cmd {
return exec.Command("echo", "Google")
}

func TestIdentifyCloudProviderGcp(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestIdentifyCloudProviderGcp() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand All @@ -127,15 +135,15 @@ func TestIdentifyCloudProviderGcp(t *testing.T) {

provider, err := IdentifyCloudProvider()

assert.Equal(t, "gcp", provider)
assert.NoError(t, err)
suite.Equal("gcp", provider)
suite.NoError(err)
}

func mockDmidecodeNoCloud() *exec.Cmd {
return exec.Command("echo", "")
}

func TestIdentifyCloudProviderNoCloud(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestIdentifyCloudProviderNoCloud() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand All @@ -158,11 +166,11 @@ func TestIdentifyCloudProviderNoCloud(t *testing.T) {

provider, err := IdentifyCloudProvider()

assert.Equal(t, "", provider)
assert.NoError(t, err)
suite.Equal("", provider)
suite.NoError(err)
}

func TestNewCloudInstanceAzure(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestNewCloudInstanceAzure() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand All @@ -188,14 +196,14 @@ func TestNewCloudInstanceAzure(t *testing.T) {

c, err := NewCloudInstance()

assert.NoError(t, err)
assert.Equal(t, "azure", c.Provider)
suite.NoError(err)
suite.Equal("azure", c.Provider)
meta, ok := c.Metadata.(*AzureMetadata)
assert.True(t, ok)
assert.Equal(t, "test", meta.Compute.Name)
suite.True(ok)
suite.Equal("test", meta.Compute.Name)
}

func TestNewCloudInstanceAws(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestNewCloudInstanceAws() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand Down Expand Up @@ -235,14 +243,14 @@ func TestNewCloudInstanceAws(t *testing.T) {

c, err := NewCloudInstance()

assert.NoError(t, err)
assert.Equal(t, "aws", c.Provider)
suite.NoError(err)
suite.Equal("aws", c.Provider)
meta, ok := c.Metadata.(*AwsMetadataDto)
assert.True(t, ok)
assert.Equal(t, "some-id", meta.InstanceID)
suite.True(ok)
suite.Equal("some-id", meta.InstanceID)
}

func TestNewCloudInstanceNoCloud(t *testing.T) {
func (suite *CloudMetadataTestSuite) TestNewCloudInstanceNoCloud() {
mockCommand := new(mocks.CustomCommand)

customExecCommand = mockCommand.Execute
Expand All @@ -265,7 +273,7 @@ func TestNewCloudInstanceNoCloud(t *testing.T) {

c, err := NewCloudInstance()

assert.NoError(t, err)
assert.Equal(t, "", c.Provider)
assert.Equal(t, interface{}(nil), c.Metadata)
suite.NoError(err)
suite.Equal("", c.Provider)
suite.Equal(interface{}(nil), c.Metadata)
}
Loading

0 comments on commit c448f98

Please sign in to comment.