blob: 1dc0529a85651d5bbbd4a29dd115b4833760b5ce [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 tilo_test
import (
"context"
"testing"
"time"
"fuchsia.googlesource.com/infra/infra/fuchsia/testexec"
"fuchsia.googlesource.com/infra/infra/fxtesting"
"fuchsia.googlesource.com/infra/infra/tilo"
"fuchsia.googlesource.com/infra/infra/tilo/resultstore"
mock_resultstore "fuchsia.googlesource.com/infra/infra/tilo/resultstore/mocks"
"github.com/golang/mock/gomock"
)
// TODO(IN-842) Add tests to ensure the Logger behaves as expected when information is
// missing from the given ResulStore Context.
// testBed manages the setup and teardown phases of individual test cases.
type testBed struct {
ctrl *gomock.Controller
tester *loggerTester
}
// Setup creates a mock environment for testing and should be run before every test case.
func (bed *testBed) Setup(t *testing.T) {
bed.ctrl = gomock.NewController(t)
bed.tester = &loggerTester{
MockUploadClient: mock_resultstore.NewMockUploadClient(bed.ctrl),
T: fxtesting.T{t},
Context: resultstore.NewContext(),
}
}
// Teardown is used to clean up after every test-case.
func (bed *testBed) Teardown() {
bed.ctrl.Finish()
}
// loggerTester is a union of all of the objects needed to implement a Logger test case.
// Tests can use the loggerTester as though it were any of the objects embedded in this
// struct.
type loggerTester struct {
fxtesting.T
resultstore.Context
*mock_resultstore.MockUploadClient
}
func test(t *testing.T, clientTest func(*loggerTester, tilo.Logger)) {
testBed := new(testBed)
testBed.Setup(t)
clientTest(testBed.tester, tilo.NewLogger(testBed.tester, testBed.tester))
testBed.Teardown()
}
func TestLogger(t *testing.T) {
t.Run("LogEnvironment", func(t *testing.T) {
t.Run("should create a Configuration for an Environment", func(t *testing.T) {
test(t, func(tester *loggerTester, logger tilo.Logger) {
input := tilo.EnvironmentFoundEvent{
Dimensions: testexec.DimensionSet{DeviceType: "NUC"},
}
expectedConfig := &resultstore.Configuration{
ID: "NUC",
InvocationID: "inv_id",
Properties: map[string]string{
"device_type": "NUC",
},
}
ctx := context.Background()
expectedContext, err := resultstore.SetAuthToken(ctx, "auth_token")
tester.AssertNil(err)
tester.SetAuthToken("auth_token")
tester.SetInvocationName("inv_name")
tester.SetInvocationID("inv_id")
tester.EXPECT().
CreateConfiguration(expectedContext, expectedConfig, "inv_name").
Return(expectedConfig, nil)
tester.AssertNil(logger.LogEnvironment(ctx, input))
})
})
})
t.Run("LogTestFound", func(t *testing.T) {
t.Run("should create a Target for the test", func(t *testing.T) {
test(t, func(tester *loggerTester, logger tilo.Logger) {
input := tilo.TestFoundEvent{
Name: "test_a",
}
expectedTarget := &resultstore.Target{
ID: &resultstore.TargetID{
ID: "test_a",
InvocationID: "inv_id",
},
// dummy start time
StartTime: time.Date(2018, time.December, 30, 0, 0, 0, 0, time.UTC),
}
ctx := context.Background()
expectedContext, err := resultstore.SetAuthToken(ctx, "auth_token")
tester.AssertNil(err)
tester.SetAuthToken("auth_token")
tester.SetInvocationName("inv_name")
tester.SetInvocationID("inv_id")
tester.EXPECT().
CreateTarget(expectedContext, expectedTarget, "inv_name").
Return(expectedTarget, nil)
tester.AssertNil(logger.LogTestFound(ctx, input))
})
})
})
t.Run("LogTestStarted", func(t *testing.T) {
t.Run("should create a ConfiguredTarget and Action, and update the Target for a test", func(t *testing.T) {
test(t, func(tester *loggerTester, logger tilo.Logger) {
input := tilo.TestStartedEvent{
TestName: "test_a",
EnvName: "env_a",
StartTime: time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC),
}
expectedTarget := &resultstore.Target{
Name: "resultstore_target_name",
StartTime: time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC),
}
expectedConfiguredTarget := &resultstore.ConfiguredTarget{
ID: &resultstore.ConfiguredTargetID{
InvocationID: "inv_id",
ConfigID: "env_a",
TargetID: "test_a",
},
StartTime: time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC),
}
outputConfiguredTarget := *expectedConfiguredTarget
outputConfiguredTarget.Name = "cfg_tgt_name"
expectedTestAction := &resultstore.TestAction{
ID: &resultstore.TestActionID{
ID: "test",
InvocationID: "inv_id",
TargetID: "test_a",
ConfigID: "env_a",
},
TestSuite: "test_a",
StartTime: time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC),
}
ctx := context.Background()
expectedContext, err := resultstore.SetAuthToken(ctx, "auth_token")
tester.AssertNil(err)
tester.SetAuthToken("auth_token")
tester.SetInvocationName("inv_name")
tester.SetInvocationID("inv_id")
tester.SetTarget("test_a", "resultstore_target_name")
tester.EXPECT().
CreateConfiguredTarget(expectedContext, expectedConfiguredTarget, "resultstore_target_name").
Return(&outputConfiguredTarget, nil)
tester.EXPECT().
CreateTestAction(expectedContext, expectedTestAction, "cfg_tgt_name").
Return(expectedTestAction, nil)
tester.EXPECT().
UpdateTarget(expectedContext, expectedTarget).
Return(expectedTarget, nil)
tester.AssertNil(logger.LogTestStarted(ctx, input))
})
})
})
t.Run("LogTestFinished", func(t *testing.T) {
t.Run("should udpate and Finish a test's ConfiguredTarget, Action, and Target", func(t *testing.T) {
test(t, func(tester *loggerTester, logger tilo.Logger) {
startTime := time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC)
endTime := startTime.Add(time.Second)
input := tilo.TestFinishedEvent{
TestName: "test_a",
EnvName: "env_a",
StartTime: startTime,
EndTime: endTime,
LogFileURI: "test.log",
TestStatus: tilo.TestPassed,
}
expectedTarget := &resultstore.Target{
Name: "resultstore_target_name",
StartTime: startTime,
Duration: endTime.Sub(startTime),
Status: resultstore.Passed,
}
expectedConfiguredTarget := &resultstore.ConfiguredTarget{
Name: "resultstore_cfg_tgt_name",
StartTime: startTime,
Duration: endTime.Sub(startTime),
Status: resultstore.Passed,
}
expectedTestAction := &resultstore.TestAction{
Name: "resultstore_action_name",
StartTime: startTime,
Duration: endTime.Sub(startTime),
Status: resultstore.Passed,
TestLogURI: "test.log",
}
tester.SetInvocationName("inv_name")
tester.SetInvocationID("inv_id")
ctx := context.Background()
expectedContext, err := resultstore.SetAuthToken(ctx, "auth_token")
tester.AssertNil(err)
tester.SetAuthToken("auth_token")
// Set the names of each entity that would have been created by the
// ResultStore backend because the Logger reads these names when coverting
// test events their ResultStore counterparts.
tester.SetConfiguredTarget("test_a", "env_a", "resultstore_cfg_tgt_name")
tester.SetTarget("test_a", "resultstore_target_name")
tester.SetTestAction("test_a", "env_a", "resultstore_action_name")
tester.EXPECT().
UpdateTestAction(expectedContext, expectedTestAction).
Return(expectedTestAction, nil)
tester.EXPECT().
UpdateConfiguredTarget(expectedContext, expectedConfiguredTarget).
Return(expectedConfiguredTarget, nil)
tester.EXPECT().
FinishConfiguredTarget(expectedContext, "resultstore_cfg_tgt_name").
Return(nil)
tester.EXPECT().
UpdateTarget(expectedContext, expectedTarget).
Return(expectedTarget, nil)
tester.EXPECT().
FinishTarget(expectedContext, "resultstore_target_name").
Return(nil)
tester.AssertNil(logger.LogTestFinished(ctx, input))
})
})
})
t.Run("End", func(t *testing.T) {
t.Run("should finish the Invocation", func(t *testing.T) {
test(t, func(tester *loggerTester, logger tilo.Logger) {
tester.SetInvocationName("inv_name")
ctx := context.Background()
expectedContext, err := resultstore.SetAuthToken(ctx, "auth_token")
tester.AssertNil(err)
tester.SetAuthToken("auth_token")
tester.EXPECT().
FinishInvocation(expectedContext, "inv_name").
Return(nil)
tester.AssertNil(logger.End(ctx))
})
})
})
}