go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/quota/quotatestmonkeypatch/doc.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 quotatestmonkeypatch should be imported for its side-effects in
    16  // tests.
    17  //
    18  // This will monkey-patch the production lua script used by the
    19  // go.chromium.org/luci/server/quota library to include a compatible (but slow)
    20  // version of msgpack, which is missing from the current implementation of
    21  // miniredis.
    22  //
    23  // Do not use this in your production binaries.
    24  //
    25  // Usage
    26  //
    27  //	import _ "go.chromium.org/luci/server/quota/quotatestmonkeypatch"
    28  package quotatestmonkeypatch
    29  
    30  import (
    31  	"strings"
    32  	"text/template"
    33  
    34  	"github.com/gomodule/redigo/redis"
    35  
    36  	"go.chromium.org/luci/server/quota"
    37  	"go.chromium.org/luci/server/quota/internal/lua"
    38  	"go.chromium.org/luci/server/quota/internal/luatest/testdata/luamsgpack"
    39  )
    40  
    41  func init() {
    42  	ctx := struct {
    43  		LuaMsgpack   string
    44  		UpdateScript string
    45  	}{
    46  		luamsgpack.GetAssetString("MessagePack.lua"),
    47  		lua.GetAssetString("update-accounts.gen.lua"),
    48  	}
    49  	buf := &strings.Builder{}
    50  	// we put UpdateScript lexically earlier in the file to make stack traces
    51  	// easier to figure out.
    52  	err := template.Must(template.New("test-script.lua").Parse(`
    53  	return (function(cmsgpack, DUMP); {{.UpdateScript}}
    54  	end)((function()
    55  	{{.LuaMsgpack}}
    56  	end)(), function(...)
    57  		local toprint = {}
    58  	  for i, v in next, arg do
    59  			if type(v) == "table" then
    60  			  v = cjson.encode(v)
    61  			end
    62  			toprint[i] = v
    63  		end
    64  		print(unpack(toprint))
    65    end)
    66  	`)).Execute(buf, ctx)
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  	quota.UpdateAccountsScript = redis.NewScript(-1, buf.String())
    71  }