blob: cfd75dc779e9b8736dd9233693a89a7fe95f7674 [file] [log] [blame]
// Copyright 2021 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package policy
import (
"context"
"time"
"github.com/golang/protobuf/ptypes/empty"
"go.chromium.org/tast-tests/cros/common/policy"
"go.chromium.org/tast-tests/cros/common/tape"
"go.chromium.org/tast-tests/cros/remote/gaiaenrollment"
"go.chromium.org/tast-tests/cros/remote/policyutil"
"go.chromium.org/tast-tests/cros/services/cros/graphics"
pspb "go.chromium.org/tast-tests/cros/services/cros/policy"
"go.chromium.org/tast/core/ctxutil"
"go.chromium.org/tast/core/rpc"
"go.chromium.org/tast/core/testing"
)
const gaiaEnrollmentTimeout = 7 * time.Minute
func init() {
testing.AddTest(&testing.Test{
Func: GAIAEnrollment,
LacrosStatus: testing.LacrosVariantUnneeded,
Desc: "GAIA Enroll a device without checking policies",
Contacts: []string{
"chromeos-commercial-remote-management@google.com",
"vsavu@google.com",
},
BugComponent: "b:1111632",
Attr: []string{"group:dpanel-end2end", "group:dmserver-enrollment-daily"},
SoftwareDeps: []string{"reboot", "chrome"},
ServiceDeps: []string{
"tast.cros.hwsec.OwnershipService",
"tast.cros.policy.PolicyService",
"tast.cros.tape.Service",
"tast.cros.graphics.ScreenshotService",
},
Timeout: gaiaEnrollmentTimeout,
SearchFlags: []*testing.StringPair{{
Key: "feature_id",
// Enroll devices for production testing (COM_ONB_CUJ6_TASK5_WF1).
Value: "screenplay-89d51ff3-0264-477a-ac03-754fa6a8eb41",
}, {
Key: "feature_id",
// Enroll devices manually (COM_ONB_CUJ7_TASK5_WF1).
Value: "screenplay-2de3fdfc-26eb-4fe5-9959-91cbfed05acb",
}},
Params: []testing.Param{
{
Name: "autopush",
Val: gaiaenrollment.TestParams{
DMServer: policy.DMServerAlphaURL,
PoolID: tape.Enrollment,
},
// TODO b/346725308 Refactor to use utility and known dependency list.
ExtraSearchFlags: []*testing.StringPair{{
Key: "external_dependency", Value: "DMServerAlpha",
}},
},
{
Name: "autopush_new_saml",
Val: gaiaenrollment.TestParams{
DMServer: policy.DMServerAlphaURL,
PoolID: tape.Crosprqa4Com,
},
// TODO b/346725308 Refactor to use utility and known dependency list.
ExtraSearchFlags: []*testing.StringPair{{
Key: "external_dependency", Value: "DMServerAlpha",
}},
},
{
Name: "staging",
Val: gaiaenrollment.TestParams{
DMServer: policy.DMServerStagingURL,
PoolID: tape.Enrollment,
},
// TODO b/346725308 Refactor to use utility and known dependency list.
ExtraSearchFlags: []*testing.StringPair{{
Key: "external_dependency", Value: "DMServerStaging",
}},
},
{
Name: "staging_new_saml",
Val: gaiaenrollment.TestParams{
DMServer: policy.DMServerStagingURL,
PoolID: tape.Crosprqa4Com,
},
// TODO b/346725308 Refactor to use utility and known dependency list.
ExtraSearchFlags: []*testing.StringPair{{
Key: "external_dependency", Value: "DMServerStaging",
}},
},
},
Vars: []string{
tape.ServiceAccountVar,
},
})
}
func GAIAEnrollment(ctx context.Context, s *testing.State) {
param := s.Param().(gaiaenrollment.TestParams)
dmServerURL := param.DMServer
poolID := param.PoolID
defer func(ctx context.Context) {
if err := policyutil.EnsureTPMAndSystemStateAreReset(ctx, s.DUT(), s.RPCHint()); err != nil {
s.Error("Failed to reset TPM after test: ", err)
}
}(ctx)
// Shorten deadline to leave time separately for resetting the TPM and for logging and cleanup.
cleanupCtx, cleanupCancel := ctxutil.Shorten(ctx, 3*time.Minute)
defer cleanupCancel()
ctx, cancel := ctxutil.Shorten(cleanupCtx, 20*time.Second)
defer cancel()
if err := policyutil.EnsureTPMAndSystemStateAreReset(ctx, s.DUT(), s.RPCHint()); err != nil {
s.Fatal("Failed to reset TPM: ", err)
}
cl, err := rpc.Dial(ctx, s.DUT(), s.RPCHint())
if err != nil {
s.Fatal("Failed to connect to the RPC service on the DUT: ", err)
}
defer cl.Close(cleanupCtx)
screenshotService := graphics.NewScreenshotServiceClient(cl.Conn)
captureScreenshotOnError := func(ctx context.Context, hasError func() bool) {
if !hasError() {
return
}
screenshotService.CaptureScreenshot(ctx, &graphics.CaptureScreenshotRequest{FilePrefix: "enrollmentError"})
}
defer captureScreenshotOnError(cleanupCtx, s.HasError)
policyClient := pspb.NewPolicyServiceClient(cl.Conn)
tapeClient, err := tape.NewClient(ctx, []byte(s.RequiredVar(tape.ServiceAccountVar)))
if err != nil {
s.Fatal("Failed to create tape client: ", err)
}
timeout := int32(gaiaEnrollmentTimeout.Seconds())
// Create an account manager and lease a test account for the duration of the test.
accManager, acc, err := tape.NewOwnedTestAccountManagerFromClient(ctx, tapeClient, false /*lock*/, tape.WithTimeout(timeout), tape.WithPoolID(poolID))
if err != nil {
s.Fatal("Failed to create an account manager and lease an account: ", err)
}
defer accManager.CleanUp(cleanupCtx)
// Deprovision the DUT at the end of the test. As devices might get
// provisioned even when the enrollment fails we need to defer the
// deprovisioning before enrolling.
defer func(ctx context.Context) {
if err := tapeClient.DeprovisionHelper(cleanupCtx, cl, acc.OrgUnitPath); err != nil {
s.Error("Failed to deprovision device: ", err)
}
}(cleanupCtx)
if _, err := policyClient.GAIAEnrollUsingChrome(ctx, &pspb.GAIAEnrollUsingChromeRequest{
Username: acc.Username,
Password: acc.Password,
DmserverURL: dmServerURL,
}); err != nil {
s.Fatal("Failed to enroll using chrome: ", err)
}
defer policyClient.StopChrome(cleanupCtx, &empty.Empty{})
}