| // Copyright 2025 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package rsmsg |
| |
| import ( |
| "testing" |
| |
| "github.com/google/go-cmp/cmp" |
| "google.golang.org/protobuf/testing/protocmp" |
| |
| rspb "google.golang.org/genproto/googleapis/devtools/resultstore/v2" |
| |
| _ "github.com/bazelbuild/rsclient/internal/pkg/testsetup" |
| ) |
| |
| // TestNewInvocation verifies that NewInvocation correctly initializes an Invocation |
| // with the provided ID, status, and labels. |
| func TestNewInvocation(t *testing.T) { |
| tests := []struct { |
| name string |
| id string |
| status rspb.Status |
| labels []string |
| want *rspb.Invocation |
| }{ |
| { |
| name: "id only", |
| id: "i1", |
| status: StatusUnspecified, |
| want: &rspb.Invocation{ |
| Id: &rspb.Invocation_Id{InvocationId: "i1"}, |
| }, |
| }, |
| { |
| name: "with status", |
| id: "i1", |
| status: Building, |
| want: &rspb.Invocation{ |
| Id: &rspb.Invocation_Id{InvocationId: "i1"}, |
| StatusAttributes: &rspb.StatusAttributes{Status: Building}, |
| }, |
| }, |
| { |
| name: "with labels", |
| id: "i1", |
| status: StatusUnspecified, |
| labels: []string{"a", "b"}, |
| want: &rspb.Invocation{ |
| Id: &rspb.Invocation_Id{InvocationId: "i1"}, |
| InvocationAttributes: &rspb.InvocationAttributes{ |
| Labels: []string{"a", "b"}, |
| }, |
| }, |
| }, |
| } |
| |
| for _, tc := range tests { |
| t.Run(tc.name, func(t *testing.T) { |
| got := NewInvocation(tc.id, tc.status, tc.labels...) |
| if diff := cmp.Diff(tc.want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewInvocation() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| // TestNewStatusAttributes verifies that NewStatusAttributes correctly initializes |
| // StatusAttributes with the provided status. |
| func TestNewStatusAttributes(t *testing.T) { |
| got := NewStatusAttributes(Building) |
| want := &rspb.StatusAttributes{Status: Building} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewStatusAttributes() mismatch (-want +got):\n%s", diff) |
| } |
| } |
| |
| // TestStatusConvenienceHelpers verifies that the common status convenience helpers |
| // (e.g., StatusBuilding, StatusBuilt) return the correct StatusAttributes. |
| func TestStatusConvenienceHelpers(t *testing.T) { |
| t.Run("StatusUnspecifiedAttr", func(t *testing.T) { |
| got := StatusUnspecifiedAttr() |
| want := &rspb.StatusAttributes{Status: StatusUnspecified} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("StatusUnspecifiedAttr() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("StatusBuilding", func(t *testing.T) { |
| got := StatusBuilding() |
| want := &rspb.StatusAttributes{Status: Building} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("StatusBuilding() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("StatusBuilt", func(t *testing.T) { |
| got := StatusBuilt() |
| want := &rspb.StatusAttributes{Status: Built} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("StatusBuilt() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("StatusFailed", func(t *testing.T) { |
| got := StatusFailed() |
| want := &rspb.StatusAttributes{Status: Failed} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("StatusFailed() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("StatusCancelled", func(t *testing.T) { |
| got := StatusCancelled() |
| want := &rspb.StatusAttributes{Status: Cancelled} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("StatusCancelled() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| |
| // TestWithStatus verifies that WithStatus correctly sets the status of an existing Invocation. |
| func TestWithStatus(t *testing.T) { |
| inv := &rspb.Invocation{Id: &rspb.Invocation_Id{InvocationId: "i1"}} |
| got := WithStatus(inv, Built) |
| want := &rspb.Invocation{ |
| Id: &rspb.Invocation_Id{InvocationId: "i1"}, |
| StatusAttributes: &rspb.StatusAttributes{Status: Built}, |
| } |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("WithStatus() mismatch (-want +got):\n%s", diff) |
| } |
| } |
| |
| // TestNewResourceHelpers verifies that the resource-specific helpers (NewTarget, |
| // NewConfiguredTarget, NewAction) correctly initialize resources with the provided names. |
| func TestNewResourceHelpers(t *testing.T) { |
| t.Run("NewTarget", func(t *testing.T) { |
| name := "invocations/i1/targets/t1" |
| got := NewTarget(name) |
| want := &rspb.Target{Name: name} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewTarget() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("NewConfiguredTarget", func(t *testing.T) { |
| name := "invocations/i1/targets/t1/configuredTargets/c1" |
| got := NewConfiguredTarget(name) |
| want := &rspb.ConfiguredTarget{Name: name} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewConfiguredTarget() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("NewAction", func(t *testing.T) { |
| name := "invocations/i1/targets/t1/configuredTargets/c1/actions/a1" |
| got := NewAction(name) |
| want := &rspb.Action{Name: name} |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewAction() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| |
| // TestNewUploadRequests verifies that the resource-specific UploadRequest helpers |
| // correctly initialize an UploadRequest with the provided operation and resource. |
| func TestNewUploadRequests(t *testing.T) { |
| inv := &rspb.Invocation{Name: "invocations/i1"} |
| id := &rspb.UploadRequest_Id{TargetId: "t1"} |
| target := &rspb.Target{Name: "invocations/i1/targets/t1"} |
| ct := &rspb.ConfiguredTarget{Name: "invocations/i1/targets/t1/configuredTargets/c1"} |
| action := &rspb.Action{Name: "invocations/i1/targets/t1/configuredTargets/c1/actions/a1"} |
| |
| t.Run("Invocation", func(t *testing.T) { |
| got := NewInvocationUploadRequest(rspb.UploadRequest_CREATE, inv) |
| want := &rspb.UploadRequest{ |
| UploadOperation: rspb.UploadRequest_CREATE, |
| Resource: &rspb.UploadRequest_Invocation{Invocation: inv}, |
| } |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewInvocationUploadRequest() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("Target", func(t *testing.T) { |
| got := NewTargetUploadRequest(rspb.UploadRequest_CREATE, id, target) |
| want := &rspb.UploadRequest{ |
| Id: id, |
| UploadOperation: rspb.UploadRequest_CREATE, |
| Resource: &rspb.UploadRequest_Target{Target: target}, |
| } |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewTargetUploadRequest() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("ConfiguredTarget", func(t *testing.T) { |
| got := NewConfiguredTargetUploadRequest(rspb.UploadRequest_CREATE, id, ct) |
| want := &rspb.UploadRequest{ |
| Id: id, |
| UploadOperation: rspb.UploadRequest_CREATE, |
| Resource: &rspb.UploadRequest_ConfiguredTarget{ConfiguredTarget: ct}, |
| } |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewConfiguredTargetUploadRequest() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| |
| t.Run("Action", func(t *testing.T) { |
| got := NewActionUploadRequest(rspb.UploadRequest_CREATE, id, action) |
| want := &rspb.UploadRequest{ |
| Id: id, |
| UploadOperation: rspb.UploadRequest_CREATE, |
| Resource: &rspb.UploadRequest_Action{Action: action}, |
| } |
| if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" { |
| t.Errorf("NewActionUploadRequest() mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |