Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jarcoal/httpmock v1.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jarcoal/httpmock v1.4.0 h1:BvhqnH0JAYbNudL2GMJKgOHe2CtKlzJ/5rWKyp+hc2k=
github.com/jarcoal/httpmock v1.4.0/go.mod h1:ftW1xULwo+j0R0JJkJIIi7UKigZUXCLLanykgjwBXL0=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
33 changes: 33 additions & 0 deletions internal/lambdalabs/v1/capabilities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package v1

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

v1 "github.com/brevdev/compute/pkg/v1"
)

func TestLambdaLabsClient_GetCapabilities(t *testing.T) {
client := &LambdaLabsClient{}
capabilities, err := client.GetCapabilities(context.Background())
require.NoError(t, err)

assert.Contains(t, capabilities, v1.CapabilityCreateInstance)
assert.Contains(t, capabilities, v1.CapabilityTerminateInstance)
assert.Contains(t, capabilities, v1.CapabilityRebootInstance)
assert.NotContains(t, capabilities, v1.CapabilityStopStartInstance)
}

func TestLambdaLabsCredential_GetCapabilities(t *testing.T) {
cred := &LambdaLabsCredential{}
capabilities, err := cred.GetCapabilities(context.Background())
require.NoError(t, err)

assert.Contains(t, capabilities, v1.CapabilityCreateInstance)
assert.Contains(t, capabilities, v1.CapabilityTerminateInstance)
assert.Contains(t, capabilities, v1.CapabilityRebootInstance)
assert.NotContains(t, capabilities, v1.CapabilityStopStartInstance)
}
53 changes: 53 additions & 0 deletions internal/lambdalabs/v1/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package v1

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

openapi "github.com/brevdev/cloud/internal/lambdalabs/gen/lambdalabs"
v1 "github.com/brevdev/compute/pkg/v1"
)

func TestLambdaLabsClient_GetAPIType(t *testing.T) {
client := &LambdaLabsClient{}
assert.Equal(t, v1.APITypeGlobal, client.GetAPIType())
}

func TestLambdaLabsClient_GetCloudProviderID(t *testing.T) {
client := &LambdaLabsClient{}
assert.Equal(t, v1.CloudProviderID("lambdalabs"), client.GetCloudProviderID())
}

func TestLambdaLabsClient_MakeClient(t *testing.T) {
client := &LambdaLabsClient{
refID: "test-ref-id",
apiKey: "test-api-key",
}

newClient, err := client.MakeClient(context.Background(), "test-tenant")
require.NoError(t, err)
lambdaClient, ok := newClient.(*LambdaLabsClient)
require.True(t, ok)
assert.Equal(t, client, lambdaClient)
}

func TestLambdaLabsClient_GetReferenceID(t *testing.T) {
client := &LambdaLabsClient{refID: "test-ref-id"}
assert.Equal(t, "test-ref-id", client.GetReferenceID())
}

func TestLambdaLabsClient_makeAuthContext(t *testing.T) {
client := &LambdaLabsClient{apiKey: "test-api-key"}
ctx := client.makeAuthContext(context.Background())

auth := ctx.Value(openapi.ContextBasicAuth)
require.NotNil(t, auth)

basicAuth, ok := auth.(openapi.BasicAuth)
require.True(t, ok)
assert.Equal(t, "test-api-key", basicAuth.UserName)
assert.Equal(t, "", basicAuth.Password)
}
49 changes: 49 additions & 0 deletions internal/lambdalabs/v1/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package v1

import (
openapi "github.com/brevdev/cloud/internal/lambdalabs/gen/lambdalabs"
"github.com/jarcoal/httpmock"
)

const (
testInstanceID = "test-instance-id"
nonexistentInstance = "nonexistent-instance"
)

func setupMockClient() (*LambdaLabsClient, func()) {
httpmock.Activate()
client := NewLambdaLabsClient("test-ref-id", "test-api-key")
return client, httpmock.DeactivateAndReset
}

func createMockInstance(instanceID string) openapi.Instance {
name := "test-instance"
ip := "192.168.1.100"
privateIP := "10.0.1.100"
hostname := "test-instance.lambda.ai"

return openapi.Instance{
Id: instanceID,
Name: *openapi.NewNullableString(&name),
Ip: *openapi.NewNullableString(&ip),
PrivateIp: *openapi.NewNullableString(&privateIP),
Status: "active",
SshKeyNames: []string{"test-key"},
FileSystemNames: []string{},
Region: &openapi.Region{
Name: "us-west-1",
Description: "US West 1",
},
InstanceType: &openapi.InstanceType{
Name: "gpu_1x_a10",
Description: "1x NVIDIA A10 GPU",
GpuDescription: "NVIDIA A10",
PriceCentsPerHour: 100,
Specs: openapi.InstanceTypeSpecs{
MemoryGib: 32,
StorageGib: 512,
},
},
Hostname: *openapi.NewNullableString(&hostname),
}
}
52 changes: 52 additions & 0 deletions internal/lambdalabs/v1/credential_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package v1

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

v1 "github.com/brevdev/compute/pkg/v1"
)

func TestLambdaLabsCredential_GetReferenceID(t *testing.T) {
cred := &LambdaLabsCredential{
RefID: "test-ref-id",
APIKey: "test-api-key",
}

assert.Equal(t, "test-ref-id", cred.GetReferenceID())
}

func TestLambdaLabsCredential_GetAPIType(t *testing.T) {
cred := &LambdaLabsCredential{}
assert.Equal(t, v1.APITypeGlobal, cred.GetAPIType())
}

func TestLambdaLabsCredential_GetCloudProviderID(t *testing.T) {
cred := &LambdaLabsCredential{}
assert.Equal(t, v1.CloudProviderID("lambdalabs"), cred.GetCloudProviderID())
}

func TestLambdaLabsCredential_GetTenantID(t *testing.T) {
cred := &LambdaLabsCredential{APIKey: "test-key"}
tenantID, err := cred.GetTenantID()
assert.NoError(t, err)
assert.Contains(t, tenantID, "lambdalabs-")
}

func TestLambdaLabsCredential_MakeClient(t *testing.T) {
cred := &LambdaLabsCredential{
RefID: "test-ref-id",
APIKey: "test-api-key",
}

client, err := cred.MakeClient(context.Background(), "test-tenant")
require.NoError(t, err)

lambdaClient, ok := client.(*LambdaLabsClient)
require.True(t, ok)
assert.Equal(t, "test-ref-id", lambdaClient.refID)
assert.Equal(t, "test-api-key", lambdaClient.apiKey)
}
79 changes: 79 additions & 0 deletions internal/lambdalabs/v1/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package v1

import (
"testing"

"github.com/stretchr/testify/assert"

v1 "github.com/brevdev/compute/pkg/v1"
)

func TestConvertLambdaLabsInstanceToV1Instance(t *testing.T) {
lambdaInstance := createMockInstance("test-instance-id")

v1Instance := convertLambdaLabsInstanceToV1Instance(lambdaInstance)

assert.Equal(t, "test-instance-id", string(v1Instance.CloudID))
assert.Equal(t, "test-instance", v1Instance.Name)
assert.Equal(t, v1.LifecycleStatusRunning, v1Instance.Status.LifecycleStatus)
assert.Equal(t, "192.168.1.100", v1Instance.PublicIP)
assert.Equal(t, "10.0.1.100", v1Instance.PrivateIP)
assert.Equal(t, "us-west-1", v1Instance.Location)
assert.Equal(t, "gpu_1x_a10", v1Instance.InstanceType)
}

func TestConvertLambdaLabsStatusToV1Status(t *testing.T) {
tests := []struct {
lambdaStatus string
expected v1.LifecycleStatus
}{
{"active", v1.LifecycleStatusRunning},
{"booting", v1.LifecycleStatusPending},
{"unhealthy", v1.LifecycleStatusRunning},
{"terminating", v1.LifecycleStatusTerminating},
{"terminated", v1.LifecycleStatusTerminated},
{"error", v1.LifecycleStatusFailed},
}

for _, test := range tests {
t.Run(test.lambdaStatus, func(t *testing.T) {
result := convertLambdaLabsStatusToV1Status(test.lambdaStatus)
assert.Equal(t, test.expected, result)
})
}
}

func TestMergeInstanceForUpdate(t *testing.T) {
client := &LambdaLabsClient{}
original := v1.Instance{
CloudID: "test-id",
Name: "original-name",
Status: v1.Status{LifecycleStatus: v1.LifecycleStatusRunning},
}

update := v1.Instance{
Name: "updated-name",
Status: v1.Status{LifecycleStatus: v1.LifecycleStatusTerminated},
}

merged := client.MergeInstanceForUpdate(original, update)

assert.Equal(t, "updated-name", merged.Name)
assert.Equal(t, v1.LifecycleStatusTerminated, merged.Status.LifecycleStatus)
}

func TestMergeInstanceTypeForUpdate(t *testing.T) {
client := &LambdaLabsClient{}
original := v1.InstanceType{
ID: "test-id",
Type: "original-type",
}

update := v1.InstanceType{
Type: "updated-type",
}

merged := client.MergeInstanceTypeForUpdate(original, update)

assert.Equal(t, "updated-type", merged.Type)
}
Loading
Loading