go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/settings/external_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 settings
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/smarty/assertions"
    24  
    25  	"go.chromium.org/luci/common/logging"
    26  	"go.chromium.org/luci/common/logging/memlogger"
    27  
    28  	. "github.com/smartystreets/goconvey/convey"
    29  )
    30  
    31  func TestExternalStorage(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	Convey("Works", t, func() {
    35  		ctx := memlogger.Use(context.Background())
    36  		log := logging.Get(ctx).(*memlogger.MemLogger)
    37  
    38  		s := ExternalStorage{}
    39  
    40  		// Initially empty.
    41  		b, _, err := s.FetchAllSettings(ctx)
    42  		So(err, ShouldBeNil)
    43  		So(b.Values, ShouldBeNil)
    44  
    45  		// Added some.
    46  		So(s.Load(ctx, strings.NewReader(`{"k1":"a", "k2": "b", "k3": "c"}`)), ShouldBeNil)
    47  		So(lastLogJSON(log), assertions.ShouldEqualJSON, `{
    48  			"added": {"k1":"a", "k2": "b", "k3": "c"},
    49  			"changed": {},
    50  			"removed": {}
    51  		}`)
    52  		log.Reset()
    53  
    54  		b, _, err = s.FetchAllSettings(ctx)
    55  		So(err, ShouldBeNil)
    56  		So(b.Values, ShouldResemble, map[string]*json.RawMessage{
    57  			"k1": {34, 97, 34}, // "a"
    58  			"k2": {34, 98, 34}, // "b"
    59  			"k3": {34, 99, 34}, // "c"
    60  		})
    61  
    62  		// Removed some and mutated some.
    63  		So(s.Load(ctx, strings.NewReader(`{"k1":"d", "k3": "c"}`)), ShouldBeNil)
    64  		So(lastLogJSON(log), assertions.ShouldEqualJSON, `{
    65  			"added": {},
    66  			"changed": {"k1": {"old": "a", "new": "d"}},
    67  			"removed": {"k2": "b"}
    68  		}`)
    69  		log.Reset()
    70  
    71  		b, _, err = s.FetchAllSettings(ctx)
    72  		So(err, ShouldBeNil)
    73  		So(b.Values, ShouldResemble, map[string]*json.RawMessage{
    74  			"k1": {34, 100, 34}, // "d"
    75  			"k3": {34, 99, 34},  // "c"
    76  		})
    77  
    78  		// Errors do not change settings.
    79  		So(s.Load(ctx, strings.NewReader("???")), ShouldNotBeNil)
    80  		b, _, err = s.FetchAllSettings(ctx)
    81  		So(err, ShouldBeNil)
    82  		So(b.Values, ShouldResemble, map[string]*json.RawMessage{
    83  			"k1": {34, 100, 34}, // "d"
    84  			"k3": {34, 99, 34},  // "c"
    85  		})
    86  	})
    87  }
    88  
    89  // lastLogJSON returns JSON-serialized 'data' portion of the last log entry.
    90  func lastLogJSON(log *memlogger.MemLogger) string {
    91  	m := log.Messages()
    92  	if len(m) == 0 {
    93  		return ""
    94  	}
    95  	blob, err := json.MarshalIndent(m[len(m)-1].Data, "", "  ")
    96  	if err != nil {
    97  		panic(err)
    98  	}
    99  	return string(blob)
   100  }