github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/clienttestutils/clienttestutils.go (about)

     1  // Copyright 2018 Google Inc.
     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  //     https://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 clienttestutils contains utility functions for the client test, in part platform-specific.
    16  package clienttestutils
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"google.golang.org/protobuf/encoding/prototext"
    24  	"google.golang.org/protobuf/proto"
    25  
    26  	fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak"
    27  )
    28  
    29  // WriteSignedServiceConfig saves the given signed config to a test location.
    30  func WriteSignedServiceConfig(dirpath, filename string, cfg *fspb.SignedClientServiceConfig) error {
    31  	b, err := proto.Marshal(cfg)
    32  	if err != nil {
    33  		return fmt.Errorf("unable to serialize signed service config: %v", err)
    34  	}
    35  
    36  	configPath := filepath.Join(dirpath, filename)
    37  	if err := os.WriteFile(configPath, b, 0644); err != nil {
    38  		return fmt.Errorf("unable to write signed service config[%s]: %v", configPath, err)
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  // WriteServiceConfig saves the given config to a test location.
    45  func WriteServiceConfig(dirpath, filename string, cfg *fspb.ClientServiceConfig) error {
    46  	b, err := prototext.Marshal(cfg)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	configPath := filepath.Join(dirpath, filename)
    52  	if err := os.WriteFile(configPath, b, 0644); err != nil {
    53  		return fmt.Errorf("unable to write service config[%s]: %v", configPath, err)
    54  	}
    55  
    56  	return nil
    57  }