|
| 1 | +package adapters_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "google.golang.org/api/aiplatform/v1" |
| 10 | + |
| 11 | + "github.com/overmindtech/cli/discovery" |
| 12 | + "github.com/overmindtech/cli/sdp-go" |
| 13 | + "github.com/overmindtech/cli/sources/gcp/dynamic" |
| 14 | + gcpshared "github.com/overmindtech/cli/sources/gcp/shared" |
| 15 | + "github.com/overmindtech/cli/sources/shared" |
| 16 | +) |
| 17 | + |
| 18 | +func TestAIPlatformCustomJob(t *testing.T) { |
| 19 | + ctx := context.Background() |
| 20 | + projectID := "test-project" |
| 21 | + linker := gcpshared.NewLinker() |
| 22 | + jobID := "test-job" |
| 23 | + |
| 24 | + customJob := &aiplatform.GoogleCloudAiplatformV1CustomJob{ |
| 25 | + Name: fmt.Sprintf("projects/%s/locations/global/customJobs/%s", projectID, jobID), |
| 26 | + JobSpec: &aiplatform.GoogleCloudAiplatformV1CustomJobSpec{ |
| 27 | + ServiceAccount: "[email protected]", |
| 28 | + Network: fmt.Sprintf("projects/%s/global/networks/default", projectID), |
| 29 | + }, |
| 30 | + EncryptionSpec: &aiplatform.GoogleCloudAiplatformV1EncryptionSpec{ |
| 31 | + KmsKeyName: "projects/test-project/locations/global/keyRings/my-keyring/cryptoKeys/my-key", |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + jobList := &aiplatform.GoogleCloudAiplatformV1ListCustomJobsResponse{ |
| 36 | + CustomJobs: []*aiplatform.GoogleCloudAiplatformV1CustomJob{customJob}, |
| 37 | + } |
| 38 | + |
| 39 | + sdpItemType := gcpshared.AIPlatformCustomJob |
| 40 | + |
| 41 | + expectedCallAndResponses := map[string]shared.MockResponse{ |
| 42 | + fmt.Sprintf("https://aiplatform.googleapis.com/v1/projects/%s/locations/global/customJobs/%s", projectID, jobID): { |
| 43 | + StatusCode: http.StatusOK, |
| 44 | + Body: customJob, |
| 45 | + }, |
| 46 | + fmt.Sprintf("https://aiplatform.googleapis.com/v1/projects/%s/locations/global/customJobs", projectID): { |
| 47 | + StatusCode: http.StatusOK, |
| 48 | + Body: jobList, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + t.Run("Get", func(t *testing.T) { |
| 53 | + httpCli := shared.NewMockHTTPClientProvider(expectedCallAndResponses) |
| 54 | + adapter, err := dynamic.MakeAdapter(sdpItemType, linker, httpCli, projectID) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("Failed to create adapter for %s: %v", sdpItemType, err) |
| 57 | + } |
| 58 | + |
| 59 | + sdpItem, err := adapter.Get(ctx, projectID, jobID, true) |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("Failed to get custom job: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + if sdpItem.GetType() != sdpItemType.String() { |
| 65 | + t.Errorf("Expected type %s, got %s", sdpItemType.String(), sdpItem.GetType()) |
| 66 | + } |
| 67 | + |
| 68 | + t.Run("StaticTests", func(t *testing.T) { |
| 69 | + queryTests := shared.QueryTests{ |
| 70 | + { |
| 71 | + // encryptionSpec.kmsKeyName |
| 72 | + ExpectedType: gcpshared.CloudKMSCryptoKey.String(), |
| 73 | + ExpectedMethod: sdp.QueryMethod_GET, |
| 74 | + ExpectedQuery: shared.CompositeLookupKey("global", "my-keyring", "my-key"), |
| 75 | + ExpectedScope: projectID, |
| 76 | + ExpectedBlastPropagation: &sdp.BlastPropagation{ |
| 77 | + In: true, |
| 78 | + Out: false, |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + // jobSpec.network |
| 83 | + ExpectedType: gcpshared.ComputeNetwork.String(), |
| 84 | + ExpectedMethod: sdp.QueryMethod_GET, |
| 85 | + ExpectedQuery: "default", |
| 86 | + ExpectedScope: projectID, |
| 87 | + ExpectedBlastPropagation: &sdp.BlastPropagation{ |
| 88 | + In: true, |
| 89 | + Out: false, |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + // jobSpec.serviceAccount |
| 94 | + ExpectedType: gcpshared.IAMServiceAccount.String(), |
| 95 | + ExpectedMethod: sdp.QueryMethod_GET, |
| 96 | + ExpectedQuery: "[email protected]", |
| 97 | + ExpectedScope: projectID, |
| 98 | + ExpectedBlastPropagation: &sdp.BlastPropagation{ |
| 99 | + In: true, |
| 100 | + Out: false, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + shared.RunStaticTests(t, adapter, sdpItem, queryTests) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + t.Run("List", func(t *testing.T) { |
| 110 | + httpCli := shared.NewMockHTTPClientProvider(expectedCallAndResponses) |
| 111 | + adapter, err := dynamic.MakeAdapter(sdpItemType, linker, httpCli, projectID) |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("Failed to create adapter for %s: %v", sdpItemType, err) |
| 114 | + } |
| 115 | + |
| 116 | + listable, ok := adapter.(discovery.ListableAdapter) |
| 117 | + if !ok { |
| 118 | + t.Fatalf("Adapter for %s does not implement ListableAdapter", sdpItemType) |
| 119 | + } |
| 120 | + |
| 121 | + sdpItems, err := listable.List(ctx, projectID, true) |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("Failed to list custom jobs: %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + if len(sdpItems) != 1 { |
| 127 | + t.Errorf("Expected 1 custom job, got %d", len(sdpItems)) |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("ErrorHandling", func(t *testing.T) { |
| 132 | + errorResponses := map[string]shared.MockResponse{ |
| 133 | + fmt.Sprintf("https://aiplatform.googleapis.com/v1/projects/%s/locations/global/customJobs/%s", projectID, jobID): { |
| 134 | + StatusCode: http.StatusNotFound, |
| 135 | + Body: map[string]interface{}{"error": "Custom job not found"}, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + httpCli := shared.NewMockHTTPClientProvider(errorResponses) |
| 140 | + adapter, err := dynamic.MakeAdapter(sdpItemType, linker, httpCli, projectID) |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("Failed to create adapter for %s: %v", sdpItemType, err) |
| 143 | + } |
| 144 | + |
| 145 | + _, err = adapter.Get(ctx, projectID, jobID, true) |
| 146 | + if err == nil { |
| 147 | + t.Error("Expected error when getting non-existent custom job, but got nil") |
| 148 | + } |
| 149 | + }) |
| 150 | +} |
0 commit comments