go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/quota/internal/quotakeys/policy_config_test.go (about)

     1  // Copyright 2022 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 quotakeys
    16  
    17  import (
    18  	"testing"
    19  
    20  	"go.chromium.org/luci/server/quota/quotapb"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  	. "go.chromium.org/luci/common/testing/assertions"
    24  )
    25  
    26  func TestPolicyConfigID(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	Convey(`PolicyConfigID`, t, func() {
    30  		Convey(`content addressed`, func() {
    31  			id := &quotapb.PolicyConfigID{
    32  				AppId:         "app",
    33  				Realm:         "proj:@project",
    34  				VersionScheme: 1,
    35  				Version:       "deadbeef",
    36  			}
    37  			key := PolicyConfigID(id)
    38  			So(key, ShouldResemble, `"a~p~app~proj:@project~1~deadbeef`)
    39  
    40  			newID, err := ParsePolicyConfigID(key)
    41  			So(err, ShouldBeNil)
    42  			So(newID, ShouldResembleProto, id)
    43  		})
    44  
    45  		Convey(`manually versioned`, func() {
    46  			id := &quotapb.PolicyConfigID{
    47  				AppId:   "app",
    48  				Realm:   "proj:@project",
    49  				Version: "deadbeef",
    50  			}
    51  			key := PolicyConfigID(id)
    52  			So(key, ShouldResemble, `"a~p~app~proj:@project~0~deadbeef`)
    53  
    54  			newID, err := ParsePolicyConfigID(key)
    55  			So(err, ShouldBeNil)
    56  			So(newID, ShouldResembleProto, id)
    57  		})
    58  	})
    59  }
    60  
    61  func TestPolicyKey(t *testing.T) {
    62  	t.Parallel()
    63  
    64  	Convey(`PolicyKey`, t, func() {
    65  		key := &quotapb.PolicyKey{
    66  			Namespace:    "ns",
    67  			Name:         "name",
    68  			ResourceType: "resource",
    69  		}
    70  		keyStr := PolicyKey(key)
    71  		So(keyStr, ShouldResemble, `ns~name~resource`)
    72  
    73  		newKey, err := ParsePolicyKey(keyStr)
    74  		So(err, ShouldBeNil)
    75  		So(newKey, ShouldResembleProto, key)
    76  	})
    77  }
    78  
    79  func TestPolicyID(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	Convey(`PolicyID`, t, func() {
    83  		id := &quotapb.PolicyID{
    84  			Config: &quotapb.PolicyConfigID{
    85  				AppId:         "app",
    86  				Realm:         "proj:@project",
    87  				VersionScheme: 1,
    88  				Version:       "deadbeef",
    89  			},
    90  			Key: &quotapb.PolicyKey{
    91  				Namespace:    "ns",
    92  				Name:         "name",
    93  				ResourceType: "resource",
    94  			},
    95  		}
    96  		ref := PolicyRef(id)
    97  		So(ref, ShouldResembleProto, &quotapb.PolicyRef{
    98  			Config: `"a~p~app~proj:@project~1~deadbeef`,
    99  			Key:    `ns~name~resource`,
   100  		})
   101  
   102  		newID, err := ParsePolicyRef(ref)
   103  		So(err, ShouldBeNil)
   104  		So(newID, ShouldResembleProto, id)
   105  	})
   106  }