go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luciexe/exe/props_test.go (about)

     1  // Copyright 2019 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 exe
    16  
    17  import (
    18  	"testing"
    19  
    20  	"google.golang.org/protobuf/types/known/structpb"
    21  
    22  	bbpb "go.chromium.org/luci/buildbucket/proto"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  
    26  	. "go.chromium.org/luci/common/testing/assertions"
    27  )
    28  
    29  type testStruct struct {
    30  	Field string `json:"field"`
    31  }
    32  
    33  func TestProperties(t *testing.T) {
    34  	Convey(`test property helpers`, t, func() {
    35  		props := &structpb.Struct{}
    36  
    37  		expectedStruct := &testStruct{Field: "hi"}
    38  		expectedProto := &bbpb.Build{SummaryMarkdown: "there"}
    39  		expectedStrings := []string{"not", "a", "struct"}
    40  		So(WriteProperties(props, map[string]any{
    41  			"struct":  expectedStruct,
    42  			"proto":   expectedProto,
    43  			"strings": expectedStrings,
    44  			"null":    Null,
    45  		}), ShouldBeNil)
    46  		So(props, ShouldResembleProto, &structpb.Struct{
    47  			Fields: map[string]*structpb.Value{
    48  				"struct": {Kind: &structpb.Value_StructValue{StructValue: &structpb.Struct{
    49  					Fields: map[string]*structpb.Value{
    50  						"field": {Kind: &structpb.Value_StringValue{
    51  							StringValue: "hi",
    52  						}},
    53  					}},
    54  				}},
    55  				"proto": {Kind: &structpb.Value_StructValue{StructValue: &structpb.Struct{
    56  					Fields: map[string]*structpb.Value{
    57  						"summary_markdown": {Kind: &structpb.Value_StringValue{
    58  							StringValue: "there",
    59  						}},
    60  					}},
    61  				}},
    62  				"strings": {Kind: &structpb.Value_ListValue{ListValue: &structpb.ListValue{
    63  					Values: []*structpb.Value{
    64  						{Kind: &structpb.Value_StringValue{StringValue: "not"}},
    65  						{Kind: &structpb.Value_StringValue{StringValue: "a"}},
    66  						{Kind: &structpb.Value_StringValue{StringValue: "struct"}},
    67  					},
    68  				}}},
    69  				"null": {Kind: &structpb.Value_NullValue{NullValue: 0}},
    70  			},
    71  		})
    72  
    73  		readStruct := &testStruct{}
    74  		extraStruct := &testStruct{}
    75  		readProto := &bbpb.Build{}
    76  		var readStrings []string
    77  		readNil := any(100) // not currently nil
    78  		So(ParseProperties(props, map[string]any{
    79  			"struct":       readStruct,
    80  			"extra_struct": extraStruct,
    81  			"proto":        readProto,
    82  			"strings":      &readStrings,
    83  			"null":         &readNil,
    84  		}), ShouldBeNil)
    85  		So(readStruct, ShouldResemble, expectedStruct)
    86  		So(extraStruct, ShouldResemble, &testStruct{})
    87  		So(readStrings, ShouldResemble, expectedStrings)
    88  		So(readNil, ShouldResemble, nil)
    89  		So(readProto, ShouldResembleProto, expectedProto)
    90  
    91  		// now, delete some keys
    92  		So(WriteProperties(props, map[string]any{
    93  			"struct":         nil,
    94  			"proto":          nil,
    95  			"does_not_exist": nil,
    96  		}), ShouldBeNil)
    97  		So(props, ShouldResembleProto, &structpb.Struct{
    98  			Fields: map[string]*structpb.Value{
    99  				"strings": {Kind: &structpb.Value_ListValue{ListValue: &structpb.ListValue{
   100  					Values: []*structpb.Value{
   101  						{Kind: &structpb.Value_StringValue{StringValue: "not"}},
   102  						{Kind: &structpb.Value_StringValue{StringValue: "a"}},
   103  						{Kind: &structpb.Value_StringValue{StringValue: "struct"}},
   104  					},
   105  				}}},
   106  				"null": {Kind: &structpb.Value_NullValue{NullValue: 0}},
   107  			},
   108  		})
   109  	})
   110  }