go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tools/cmd/fakecas/main.go (about)

     1  // Copyright 2021 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"log"
    21  	"net"
    22  	"os"
    23  
    24  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/fakes"
    25  	regrpc "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
    26  	bsgrpc "google.golang.org/genproto/googleapis/bytestream"
    27  	"google.golang.org/grpc"
    28  
    29  	"go.chromium.org/luci/common/system/signals"
    30  )
    31  
    32  func main() {
    33  	port := flag.Int("port", 0, "local port number used by fake server")
    34  	addrFile := flag.String("addr-file", "", "dump listening address in this file")
    35  	flag.Parse()
    36  
    37  	s := grpc.NewServer()
    38  	cas := fakes.NewCAS()
    39  	ex := &fakes.Exec{}
    40  	bsgrpc.RegisterByteStreamServer(s, cas)
    41  	regrpc.RegisterContentAddressableStorageServer(s, cas)
    42  	regrpc.RegisterCapabilitiesServer(s, ex)
    43  
    44  	lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
    45  	if err != nil {
    46  		log.Fatalf("failed to listen: %v\n", err)
    47  	}
    48  	log.Printf("listening address: %s\n", lis.Addr())
    49  
    50  	if *addrFile != "" {
    51  		if err := os.WriteFile(*addrFile, []byte(lis.Addr().String()), 0600); err != nil {
    52  			log.Fatalf("failed to write addrFile: %v", err)
    53  		}
    54  	}
    55  
    56  	defer signals.HandleInterrupt(func() {
    57  		log.Println("shutting down fake CAS gRPC server...")
    58  		s.GracefulStop()
    59  	})()
    60  
    61  	log.Println("starting CAS fake server...")
    62  	if err := s.Serve(lis); err != nil {
    63  		log.Fatalf("failed to serve fake CAS gRPC server: %v\n", err)
    64  	}
    65  }