go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucictx/types_test.go (about)

     1  // Copyright 2016 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 lucictx
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"testing"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  	. "go.chromium.org/luci/common/testing/assertions"
    24  )
    25  
    26  func TestPredefinedTypes(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	Convey("Test predefined types", t, func() {
    30  		c := context.Background()
    31  		Convey("local_auth", func() {
    32  			So(GetLocalAuth(c), ShouldBeNil)
    33  
    34  			localAuth := &LocalAuth{
    35  				RpcPort: 100,
    36  				Secret:  []byte("foo"),
    37  				Accounts: []*LocalAuthAccount{
    38  					{Id: "test", Email: "some@example.com"},
    39  				},
    40  				DefaultAccountId: "test",
    41  			}
    42  
    43  			c = SetLocalAuth(c, localAuth)
    44  			data := getCurrent(c).sections["local_auth"]
    45  			var v any
    46  			So(json.Unmarshal(*data, &v), ShouldBeNil)
    47  			So(v, ShouldResemble, map[string]any{
    48  				"accounts": []any{map[string]any{
    49  					"email": "some@example.com",
    50  					"id":    "test",
    51  				}},
    52  				"default_account_id": "test",
    53  				"secret":             "Zm9v",
    54  				"rpc_port":           100.0,
    55  			})
    56  
    57  			So(GetLocalAuth(c), ShouldResembleProto, localAuth)
    58  		})
    59  
    60  		Convey("swarming", func() {
    61  			So(GetSwarming(c), ShouldBeNil)
    62  
    63  			s := &Swarming{
    64  				SecretBytes: []byte("foo"),
    65  				Task: &Task{
    66  					Server: "https://backend.appspot.com",
    67  					TaskId: "task_id",
    68  					BotDimensions: []string{
    69  						"k1:v1",
    70  					},
    71  				},
    72  			}
    73  			c = SetSwarming(c, s)
    74  			data := getCurrent(c).sections["swarming"]
    75  			var v any
    76  			So(json.Unmarshal(*data, &v), ShouldBeNil)
    77  			So(v, ShouldResemble, map[string]any{
    78  				"secret_bytes": "Zm9v",
    79  				"task": map[string]any{
    80  					"server":         "https://backend.appspot.com",
    81  					"task_id":        "task_id",
    82  					"bot_dimensions": []any{"k1:v1"},
    83  				},
    84  			})
    85  			So(GetSwarming(c), ShouldResembleProto, s)
    86  		})
    87  
    88  		Convey("resultdb", func() {
    89  			So(GetResultDB(c), ShouldBeNil)
    90  
    91  			resultdb := &ResultDB{
    92  				Hostname: "test.results.cr.dev",
    93  				CurrentInvocation: &ResultDBInvocation{
    94  					Name:        "invocations/build:1",
    95  					UpdateToken: "foobarbazsecretoken",
    96  				}}
    97  			c = SetResultDB(c, resultdb)
    98  			data := getCurrent(c).sections["resultdb"]
    99  			var v any
   100  			So(json.Unmarshal(*data, &v), ShouldBeNil)
   101  			So(v, ShouldResemble, map[string]any{
   102  				"current_invocation": map[string]any{
   103  					"name":         "invocations/build:1",
   104  					"update_token": "foobarbazsecretoken",
   105  				},
   106  				"hostname": "test.results.cr.dev",
   107  			})
   108  
   109  			So(GetResultDB(c), ShouldResembleProto, resultdb)
   110  		})
   111  
   112  		Convey("realm", func() {
   113  			So(GetRealm(c), ShouldBeNil)
   114  
   115  			r := &Realm{
   116  				Name: "test:realm",
   117  			}
   118  			c = SetRealm(c, r)
   119  			data := getCurrent(c).sections["realm"]
   120  			So(string(*data), ShouldEqual, `{"name":"test:realm"}`)
   121  			So(GetRealm(c), ShouldResembleProto, r)
   122  			proj, realm := CurrentRealm(c)
   123  			So(proj, ShouldEqual, "test")
   124  			So(realm, ShouldEqual, "realm")
   125  		})
   126  
   127  		Convey("buildbucket", func() {
   128  			So(GetBuildbucket(c), ShouldBeNil)
   129  
   130  			b := &Buildbucket{
   131  				Hostname:           "hostname",
   132  				ScheduleBuildToken: "a token",
   133  			}
   134  			c = SetBuildbucket(c, b)
   135  			data := getCurrent(c).sections["buildbucket"]
   136  			var v any
   137  			So(json.Unmarshal(*data, &v), ShouldBeNil)
   138  			So(v, ShouldResemble, map[string]any{
   139  				"hostname":             "hostname",
   140  				"schedule_build_token": "a token",
   141  			})
   142  			So(GetBuildbucket(c), ShouldResembleProto, b)
   143  		})
   144  	})
   145  }