blob: 9f8a9c8efd7706a7b5cb2008d5eb5e0e37bb3a7a [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package resultstore_test
import (
"context"
"reflect"
"testing"
"time"
"fuchsia.googlesource.com/infra/infra/tilo/resultstore"
mocks "fuchsia.googlesource.com/infra/infra/tilo/resultstore/mocks"
"github.com/golang/mock/gomock"
api "google.golang.org/genproto/googleapis/devtools/resultstore/v2"
"google.golang.org/genproto/protobuf/field_mask"
)
var (
// The value of entity start times and durations is not important for these tests,
// so we use the same value everywhere.
may18_1993 = time.Date(1993, time.May, 18, 0, 0, 0, 0, time.UTC)
// The specific values of the UUIDs are not important for these tests. We just want to
// verify that the client sets some value other than the empty string, so we just use
// the same UUID everywhere.
testUUID = "uuid"
)
// uploadClientTestBed manages the setup and teardown phases of individual test cases.
type uploadClientTestBed struct {
MockRPCClient *mocks.MockResultStoreUploadClient
ctrl *gomock.Controller
}
// Setup creates the client-under-test and should be run before every test case.
func (bed *uploadClientTestBed) Setup(t *testing.T, authToken string) (resultstore.UploadClient, context.Context) {
// Cache the mock controller so we can Finish() it during test teardown.
bed.ctrl = gomock.NewController(t)
bed.MockRPCClient = mocks.NewMockResultStoreUploadClient(bed.ctrl)
// Force UploadClient to re-use a fake UUID for testing by embedding the UUID in the
// context object.
ctx, err := resultstore.SetTestUUID(context.Background(), testUUID)
if err != nil {
t.Fatal(err)
}
ctx, err = resultstore.SetAuthToken(ctx, authToken)
if err != nil {
t.Fatal(err)
}
return resultstore.NewUploadClient(bed.MockRPCClient), ctx
}
// Teardown is used to clean up after every test-case.
func (bed *uploadClientTestBed) Teardown() {
bed.ctrl.Finish()
}
// ExpectRPC is used to expect that a specific RPC message is recieved by the test bed.
// If an invalid message or no message is received, the test will fail.
func (bed *uploadClientTestBed) ExpectRPC() *mocks.MockResultStoreUploadClientMockRecorder {
return bed.MockRPCClient.EXPECT()
}
func expectEqual(t *testing.T, expected interface{}, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Objects do not match:\n")
t.Errorf("Expected: %+v\n", expected)
t.Errorf("Actual: %+v\n", actual)
}
}
func TestUploadClient_CreateInvocation(t *testing.T) {
t.Run("should return an Invocation from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.Invocation{
ProjectID: "123456789",
ID: "invocation_id",
Users: []string{"user"},
Labels: []string{"label"},
Properties: map[string]string{"key": "value"},
LogURL: "http://test.log",
StartTime: may18_1993,
Status: resultstore.Passed,
}
apiResponse := &api.Invocation{
Name: "resultstore_invocation_name",
Id: &api.Invocation_Id{
InvocationId: "invocation_id",
},
}
expectedOutput := &resultstore.Invocation{
Name: "resultstore_invocation_name",
ID: "invocation_id",
}
testBed.ExpectRPC().
CreateInvocation(ctx, &api.CreateInvocationRequest{
RequestId: testUUID,
AuthorizationToken: "auth_token",
InvocationId: "invocation_id",
Invocation: input.ToResultStoreInvocation(),
}).
Return(apiResponse, nil)
output, err := client.CreateInvocation(ctx, input)
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}
func TestUploadClient_UpdateInvocation(t *testing.T) {
t.Run("should return an Invocation from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.Invocation{
ID: "invocation_id",
Properties: map[string]string{"key": "value"},
ProjectID: "project_id",
StartTime: may18_1993,
Duration: time.Hour,
Users: []string{"users"},
Labels: []string{"label"},
LogURL: "url",
Status: resultstore.Passed,
}
apiResponse := &api.Invocation{
Name: "resultstore_invocation_name",
Id: &api.Invocation_Id{
InvocationId: "invocation_id",
},
}
expectedOutput := &resultstore.Invocation{
Name: "resultstore_invocation_name",
ID: "invocation_id",
}
testBed.ExpectRPC().
UpdateInvocation(ctx, &api.UpdateInvocationRequest{
AuthorizationToken: "auth_token",
Invocation: input.ToResultStoreInvocation(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"timing.duration", "status_attributes"},
},
}).
Return(apiResponse, nil)
output, err := client.UpdateInvocation(ctx, input)
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}
func TestUploadClient_FinishInvocation(t *testing.T) {
t.Run("should return an Invocation from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
testBed.ExpectRPC().
FinishInvocation(ctx, &api.FinishInvocationRequest{
AuthorizationToken: "auth_token",
Name: "invocation_name",
}).
Return(&api.FinishInvocationResponse{}, nil)
if err := client.FinishInvocation(ctx, "invocation_name"); err != nil {
t.Fatal(err)
}
})
}
func TestUploadClient_CreateConfiguration(t *testing.T) {
t.Run("should return a Configuration from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.Configuration{
ID: "configuration_id",
InvocationID: "invocation_id",
Properties: map[string]string{"key": "value"},
}
apiResponse := &api.Configuration{
Name: "resultstore_configuration_name",
Id: &api.Configuration_Id{
ConfigurationId: "configuration_id",
InvocationId: "invocation_id",
},
}
expectedOutput := &resultstore.Configuration{
Name: "resultstore_configuration_name",
ID: "configuration_id",
InvocationID: "invocation_id",
}
testBed.ExpectRPC().
CreateConfiguration(ctx, &api.CreateConfigurationRequest{
RequestId: testUUID,
AuthorizationToken: "auth_token",
Parent: "invocation_name",
ConfigId: "configuration_id",
Configuration: input.ToResultStoreConfiguration(),
}).
Return(apiResponse, nil)
output, err := client.CreateConfiguration(ctx, input, "invocation_name")
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}
func TestUploadClient_CreateTarget(t *testing.T) {
t.Run("should return a Target from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.Target{
ID: &resultstore.TargetID{
ID: "target_id",
},
Properties: map[string]string{"key": "value"},
StartTime: may18_1993,
Status: resultstore.Passed,
}
apiResponse := &api.Target{
Name: "resultstore_target_name",
Id: &api.Target_Id{
TargetId: "target_id",
InvocationId: "invocation_id",
},
}
expectedOutput := &resultstore.Target{
Name: "resultstore_target_name",
ID: &resultstore.TargetID{
ID: "target_id",
InvocationID: "invocation_id",
},
}
testBed.ExpectRPC().
CreateTarget(ctx, &api.CreateTargetRequest{
RequestId: testUUID,
AuthorizationToken: "auth_token",
Parent: "invocation_name",
TargetId: input.ID.ID,
Target: input.ToResultStoreTarget(),
}).
Return(apiResponse, nil)
output, err := client.CreateTarget(ctx, input, "invocation_name")
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}
func TestUploadClient_UpdateTarget(t *testing.T) {
t.Run("should return a Target from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.Target{
ID: &resultstore.TargetID{
ID: "target_id",
InvocationID: "invocation_id",
},
Properties: map[string]string{"key": "value"},
StartTime: may18_1993,
Duration: time.Hour,
Status: resultstore.Passed,
}
apiResponse := &api.Target{
Name: "resultstore_target_name",
Id: &api.Target_Id{
TargetId: "target_id",
InvocationId: "invocation_id",
},
}
expectedOutput := &resultstore.Target{
Name: "resultstore_target_name",
ID: &resultstore.TargetID{
ID: "target_id",
InvocationID: "invocation_id",
},
}
testBed.ExpectRPC().
UpdateTarget(ctx, &api.UpdateTargetRequest{
AuthorizationToken: "auth_token",
Target: input.ToResultStoreTarget(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"timing.duration", "status_attributes"},
},
}).
Return(apiResponse, nil)
output, err := client.UpdateTarget(ctx, input)
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}
func TestUploadClient_FinishTarget(t *testing.T) {
t.Run("should return a Target from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
testBed.ExpectRPC().
FinishTarget(ctx, &api.FinishTargetRequest{
AuthorizationToken: "auth_token",
Name: "target_name",
}).
Return(&api.FinishTargetResponse{}, nil)
if err := client.FinishTarget(ctx, "target_name"); err != nil {
t.Fatal(err)
}
})
}
func TestUploadClient_CreateConfiguredTarget(t *testing.T) {
t.Run("should return a ConfiguredTarget from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.ConfiguredTarget{
ID: &resultstore.ConfiguredTargetID{
InvocationID: "invocation_id",
TargetID: "target_id",
ConfigID: "configuration_id",
},
Properties: map[string]string{"key": "value"},
StartTime: may18_1993,
Status: resultstore.Passed,
}
apiResponse := &api.ConfiguredTarget{
Name: "resultstore_configured_target_name",
Id: &api.ConfiguredTarget_Id{
InvocationId: "invocation_id",
ConfigurationId: "configuration_id",
TargetId: "target_id",
},
}
expectedOutput := &resultstore.ConfiguredTarget{
Name: "resultstore_configured_target_name",
ID: &resultstore.ConfiguredTargetID{
InvocationID: "invocation_id",
ConfigID: "configuration_id",
TargetID: "target_id",
},
}
testBed.ExpectRPC().
CreateConfiguredTarget(ctx, &api.CreateConfiguredTargetRequest{
RequestId: testUUID,
AuthorizationToken: "auth_token",
Parent: "target_name",
ConfigId: input.ID.ConfigID,
ConfiguredTarget: input.ToResultStoreConfiguredTarget(),
}).
Return(apiResponse, nil)
output, err := client.CreateConfiguredTarget(ctx, input, "target_name")
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}
func TestUploadClient_UpdateConfiguredTarget(t *testing.T) {
t.Run("should return a ConfiguredTarget from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.ConfiguredTarget{
ID: &resultstore.ConfiguredTargetID{
InvocationID: "invocation_id",
TargetID: "target_id",
ConfigID: "configuration_id",
},
Properties: map[string]string{"key": "value"},
Status: resultstore.Passed,
StartTime: may18_1993,
Duration: time.Hour,
}
apiResponse := &api.ConfiguredTarget{
Name: "resultstore_configured_target_name",
Id: &api.ConfiguredTarget_Id{
InvocationId: "invocation_id",
ConfigurationId: "configuration_id",
TargetId: "target_id",
},
}
expectedOutput := &resultstore.ConfiguredTarget{
Name: "resultstore_configured_target_name",
ID: &resultstore.ConfiguredTargetID{
InvocationID: "invocation_id",
ConfigID: "configuration_id",
TargetID: "target_id",
},
}
testBed.ExpectRPC().
UpdateConfiguredTarget(ctx, &api.UpdateConfiguredTargetRequest{
AuthorizationToken: "auth_token",
ConfiguredTarget: input.ToResultStoreConfiguredTarget(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"timing.duration", "status_attributes"},
},
}).
Return(apiResponse, nil)
output, err := client.UpdateConfiguredTarget(ctx, input)
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}
func TestUploadClient_FinishConfiguredTarget(t *testing.T) {
t.Run("should return a ConfiguredTarget from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
testBed.ExpectRPC().
FinishConfiguredTarget(ctx, &api.FinishConfiguredTargetRequest{
AuthorizationToken: "auth_token",
Name: "configured_target_name",
}).
Return(&api.FinishConfiguredTargetResponse{}, nil)
if err := client.FinishConfiguredTarget(ctx, "configured_target_name"); err != nil {
t.Fatal(err)
}
})
}
func TestUploadClient_CreateTestAction(t *testing.T) {
t.Run("should return an Action from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.TestAction{
ID: &resultstore.TestActionID{
ID: "test",
InvocationID: "invocation_id",
TargetID: "target_id",
ConfigID: "configuration_id",
},
TestSuite: "test_suite",
TestLogURI: "http://test.log",
StartTime: may18_1993,
Status: resultstore.Passed,
}
apiResponse := &api.Action{
Name: "resultstore_action_name",
Id: &api.Action_Id{
InvocationId: "invocation_id",
ConfigurationId: "configuration_id",
TargetId: "target_id",
},
}
expectedOutput := &resultstore.TestAction{
Name: "resultstore_action_name",
ID: &resultstore.TestActionID{
InvocationID: "invocation_id",
ConfigID: "configuration_id",
TargetID: "target_id",
},
}
testBed.ExpectRPC().
CreateAction(ctx, &api.CreateActionRequest{
RequestId: testUUID,
AuthorizationToken: "auth_token",
Parent: "configured_target_name",
ActionId: "test",
Action: input.ToResultStoreAction(),
}).
Return(apiResponse, nil)
output, err := client.CreateTestAction(ctx, input, "configured_target_name")
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}
func TestUploadClient_UpdateTestAction(t *testing.T) {
t.Run("should return an Action from an RPC response", func(t *testing.T) {
testBed := new(uploadClientTestBed)
client, ctx := testBed.Setup(t, "auth_token")
defer testBed.Teardown()
input := &resultstore.TestAction{
ID: &resultstore.TestActionID{
ID: "test",
InvocationID: "invocation_id",
TargetID: "target_id",
ConfigID: "configuration_id",
},
TestSuite: "test_suite",
TestLogURI: "http://test.log",
StartTime: may18_1993,
Duration: time.Hour,
Status: resultstore.Passed,
}
apiResponse := &api.Action{
Name: "resultstore_action_name",
Id: &api.Action_Id{
ActionId: "action_id",
InvocationId: "invocation_id",
ConfigurationId: "configuration_id",
TargetId: "target_id",
},
}
expectedOutput := &resultstore.TestAction{
Name: "resultstore_action_name",
ID: &resultstore.TestActionID{
ID: "action_id",
InvocationID: "invocation_id",
ConfigID: "configuration_id",
TargetID: "target_id",
},
}
testBed.ExpectRPC().
UpdateAction(ctx, &api.UpdateActionRequest{
AuthorizationToken: "auth_token",
Action: input.ToResultStoreAction(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"timing.duration", "status_attributes"},
},
}).
Return(apiResponse, nil)
output, err := client.UpdateTestAction(ctx, input)
if err != nil {
t.Fatal(err)
}
expectEqual(t, expectedOutput, output)
})
}