github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/internal/rpc/metadata.go (about)

     1  // Copyright 2019 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package rpc
     6  
     7  import (
     8  	"context"
     9  
    10  	"google.golang.org/grpc/metadata"
    11  
    12  	"go.chromium.org/tast/core/internal/testcontext"
    13  )
    14  
    15  // Keys of metadata.MD. Allowed characters are [a-z0-9._-].
    16  const (
    17  	metadataSoftwareDeps    = "tast-testcontext-softwaredeps"
    18  	metadataHasSoftwareDeps = "tast-testcontext-hassoftwaredeps"
    19  	metadataPrivateAttr     = "tast-testcontext-privateattr"
    20  	metadataTiming          = "tast-timing"
    21  	metadataOutDir          = "tast-outdir"
    22  	metadataLogLastSeq      = "tast-log-last-seq"
    23  )
    24  
    25  // outgoingMetadata extracts CurrentEntity from ctx and converts it to metadata.MD.
    26  // It is called on gRPC clients to forward CurrentEntity over gRPC.
    27  func outgoingMetadata(ctx context.Context) metadata.MD {
    28  	swDeps, hasSwDeps := testcontext.SoftwareDeps(ctx)
    29  	privateAttr, _ := testcontext.PrivateAttr(ctx)
    30  	md := metadata.MD{
    31  		metadataSoftwareDeps: swDeps,
    32  		metadataPrivateAttr:  privateAttr,
    33  	}
    34  	if hasSwDeps {
    35  		md[metadataHasSoftwareDeps] = []string{"1"}
    36  	}
    37  	return md
    38  }
    39  
    40  // incomingCurrentContext creates CurrentEntity from metadata.MD.
    41  // It is called on gRPC servers to forward CurrentEntity over gRPC.
    42  func incomingCurrentContext(md metadata.MD, outDir string) *testcontext.CurrentEntity {
    43  	hasSoftwareDeps := len(md[metadataHasSoftwareDeps]) > 0
    44  	softwareDeps := md[metadataSoftwareDeps]
    45  	privateAttr := md[metadataPrivateAttr]
    46  	return &testcontext.CurrentEntity{
    47  		OutDir:          outDir,
    48  		HasSoftwareDeps: hasSoftwareDeps,
    49  		SoftwareDeps:    softwareDeps,
    50  		// ServiceDeps is not forwarded.
    51  		PrivateAttr: privateAttr,
    52  	}
    53  }