Skip to content

Commit

Permalink
Merge pull request #23 from fabi200123/unit-tests
Browse files Browse the repository at this point in the history
Adding more unit-tests
  • Loading branch information
gabriel-samfira authored Jun 17, 2024
2 parents 494d2ef + 7f69768 commit a48fa7c
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 8 deletions.
72 changes: 72 additions & 0 deletions internal/client/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
"github.com/cloudbase/garm-provider-gcp/internal/spec"
"github.com/cloudbase/garm-provider-gcp/internal/util"
"github.com/googleapis/gax-go/v2"
"github.com/googleapis/gax-go/v2/apierror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"google.golang.org/api/googleapi"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -215,6 +217,76 @@ func TestDeleteInstance(t *testing.T) {
mockClient.AssertExpectations(t)
}

func TestDeleteInstanceNotFound(t *testing.T) {
ctx := context.Background()
mockClient := new(MockGcpClient)
WaitOp = func(op *compute.Operation, ctx context.Context, opts ...gax.CallOption) error {
return nil
}
gcpCli := &GcpCli{
cfg: &config.Config{
Zone: "europe-west1-d",
ProjectId: "my-project",
NetworkID: "my-network",
SubnetworkID: "my-subnetwork",
CredentialsFile: "path/to/credentials.json",
ExternalIPAccess: true,
},
client: mockClient,
}

instanceName := "garm-instance"
mockOperation := &compute.Operation{}
mockErr, _ := apierror.FromError(&googleapi.Error{
Code: 404,
})
mockClient.On("Delete", ctx, &computepb.DeleteInstanceRequest{
Project: gcpCli.cfg.ProjectId,
Zone: gcpCli.cfg.Zone,
Instance: util.GetInstanceName(instanceName),
}, mock.Anything).Return(mockOperation, mockErr)

err := gcpCli.DeleteInstance(ctx, instanceName)
assert.NoError(t, err)

mockClient.AssertExpectations(t)
}

func TestDeleteInstanceError(t *testing.T) {
ctx := context.Background()
mockClient := new(MockGcpClient)
WaitOp = func(op *compute.Operation, ctx context.Context, opts ...gax.CallOption) error {
return nil
}
gcpCli := &GcpCli{
cfg: &config.Config{
Zone: "europe-west1-d",
ProjectId: "my-project",
NetworkID: "my-network",
SubnetworkID: "my-subnetwork",
CredentialsFile: "path/to/credentials.json",
ExternalIPAccess: true,
},
client: mockClient,
}

instanceName := "garm-instance"
mockOperation := &compute.Operation{}
mockErr, _ := apierror.FromError(&googleapi.Error{
Code: 403,
})
mockClient.On("Delete", ctx, &computepb.DeleteInstanceRequest{
Project: gcpCli.cfg.ProjectId,
Zone: gcpCli.cfg.Zone,
Instance: util.GetInstanceName(instanceName),
}, mock.Anything).Return(mockOperation, mockErr)

err := gcpCli.DeleteInstance(ctx, instanceName)
assert.Error(t, err)

mockClient.AssertExpectations(t)
}

func TestStopInstance(t *testing.T) {
ctx := context.Background()
mockClient := new(MockGcpClient)
Expand Down
29 changes: 21 additions & 8 deletions internal/spec/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func TestJsonSchemaValidation(t *testing.T) {
"example_label": "example_value"
},
"network_tags": ["example_tag"],
"source_snapshot": "snapshot-id"
"source_snapshot": "snapshot-id",
"enable_boot_debug": true,
"runner_install_template": "install-template",
"extra_context": {
"key": "value"
}
}`),
errString: "",
},
Expand Down Expand Up @@ -79,20 +84,22 @@ func TestJsonSchemaValidation(t *testing.T) {
}

func TestMergeExtraSpecs(t *testing.T) {
enable_boot_debug := true
tests := []struct {
name string
extraSpecs *extraSpecs
}{
{
name: "ValidExtraSpecs",
extraSpecs: &extraSpecs{
NetworkID: "projects/garm-testing/global/networks/garm-2",
SubnetworkID: "projects/garm-testing/regions/europe-west1/subnetworks/garm",
DiskSize: 100,
NicType: "VIRTIO_NET",
CustomLabels: map[string]string{"key1": "value1"},
NetworkTags: []string{"tag1", "tag2"},
SourceSnapshot: "projects/garm-testing/global/snapshots/garm-snapshot",
NetworkID: "projects/garm-testing/global/networks/garm-2",
SubnetworkID: "projects/garm-testing/regions/europe-west1/subnetworks/garm",
DiskSize: 100,
NicType: "VIRTIO_NET",
CustomLabels: map[string]string{"key1": "value1"},
NetworkTags: []string{"tag1", "tag2"},
SourceSnapshot: "projects/garm-testing/global/snapshots/garm-snapshot",
EnableBootDebug: &enable_boot_debug,
},
},
{
Expand Down Expand Up @@ -147,6 +154,12 @@ func TestMergeExtraSpecs(t *testing.T) {
}
}
}
if tt.extraSpecs.EnableBootDebug != nil {
if *tt.extraSpecs.EnableBootDebug != spec.EnableBootDebug {
assert.Equal(t, *tt.extraSpecs.EnableBootDebug, spec.EnableBootDebug, "expected EnableBootDebug to be %t, got %t", *tt.extraSpecs.EnableBootDebug, spec.EnableBootDebug)
}
}

})
}
}
Expand Down
37 changes: 37 additions & 0 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,49 @@ func TestGcpInstanceToParamsInstance(t *testing.T) {
},
errString: "",
},
{
name: "Name set in metadata",
gcpInstance: &computepb.Instance{
Name: proto.String("garm-instance"),
Labels: map[string]string{"ostype": "linux"},
Disks: []*computepb.AttachedDisk{{Architecture: proto.String("x86_64")}},
Metadata: &computepb.Metadata{
Items: []*computepb.Items{
{Key: proto.String("runner_name"), Value: proto.String("Garm-Instance")},
},
},
Status: proto.String("RUNNING"),
},
expected: params.ProviderInstance{
ProviderID: "garm-instance",
Name: "Garm-Instance",
OSType: "linux",
OSArch: "x86_64",
Status: "running",
},
errString: "",
},
{
name: "NilGcpInstance",
gcpInstance: nil,
expected: params.ProviderInstance{},
errString: "instance ID is nil",
},
{
name: "NilName",
gcpInstance: &computepb.Instance{
Labels: map[string]string{"ostype": "linux"},
Disks: []*computepb.AttachedDisk{{Architecture: proto.String("x86_64")}},
Metadata: &computepb.Metadata{
Items: []*computepb.Items{
{Key: proto.String("runner_name"), Value: proto.String("Garm-Instance")},
},
},
Status: proto.String("RUNNING"),
},
expected: params.ProviderInstance{},
errString: "instance name is nil",
},
}

for _, tt := range tests {
Expand Down
59 changes: 59 additions & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import (
"github.com/cloudbase/garm-provider-gcp/internal/spec"
"github.com/cloudbase/garm-provider-gcp/internal/util"
"github.com/googleapis/gax-go/v2"
"github.com/googleapis/gax-go/v2/apierror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"google.golang.org/api/googleapi"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -92,7 +94,64 @@ func TestCreateInstance(t *testing.T) {
result, err := gcpProvider.CreateInstance(ctx, bootstrapParams)
assert.NoError(t, err)
assert.Equal(t, expectedInstance, result)
}

func TestCreateInstanceError(t *testing.T) {
ctx := context.Background()
mockClient := new(client.MockGcpClient)
spec.DefaultToolFetch = func(osType params.OSType, osArch params.OSArch, tools []params.RunnerApplicationDownload) (params.RunnerApplicationDownload, error) {
return params.RunnerApplicationDownload{
OS: proto.String("linux"),
Architecture: proto.String("amd64"),
DownloadURL: proto.String("MockURL"),
Filename: proto.String("garm-runner"),
}, nil
}
client.WaitOp = func(op *compute.Operation, ctx context.Context, opts ...gax.CallOption) error {
return nil
}
gcpProvider := &GcpProvider{
gcpCli: &client.GcpCli{},
controllerID: "my-controller",
}
config := config.Config{
Zone: "europe-west1-d",
ProjectId: "my-project",
NetworkID: "my-network",
SubnetworkID: "my-subnetwork",
CredentialsFile: "path/to/credentials.json",
ExternalIPAccess: true,
}
gcpProvider.gcpCli.SetClient(mockClient)
gcpProvider.gcpCli.SetConfig(&config)

mockOperation := &compute.Operation{}
mockErr, _ := apierror.FromError(&googleapi.Error{
Code: 404,
})
mockClient.On("Insert", mock.Anything, mock.Anything, mock.Anything).Return(mockOperation, mockErr)
bootstrapParams := params.BootstrapInstance{
Name: "garm-instance",
Flavor: "n1-standard-1",
Image: "projects/garm-testing/global/images/garm-image",
Tools: []params.RunnerApplicationDownload{
{
OS: proto.String("linux"),
Architecture: proto.String("amd64"),
DownloadURL: proto.String("MockURL"),
Filename: proto.String("garm-runner"),
},
},
OSType: params.Linux,
OSArch: params.Amd64,
PoolID: "my-pool",
ExtraSpecs: json.RawMessage(`{}`),
}
expectedInstance := params.ProviderInstance{}

result, err := gcpProvider.CreateInstance(ctx, bootstrapParams)
assert.Error(t, err)
assert.Equal(t, expectedInstance, result)
}

func TestGetInstance(t *testing.T) {
Expand Down

0 comments on commit a48fa7c

Please sign in to comment.