go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/appengine/rpc/projects_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 rpc
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/gae/impl/memory"
    22  	"go.chromium.org/luci/gae/service/datastore"
    23  
    24  	"go.chromium.org/luci/gce/api/projects/v1"
    25  	"go.chromium.org/luci/gce/appengine/model"
    26  
    27  	. "github.com/smartystreets/goconvey/convey"
    28  	. "go.chromium.org/luci/common/testing/assertions"
    29  )
    30  
    31  func TestProjects(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	Convey("Projects", t, func() {
    35  		srv := &Projects{}
    36  		c := memory.Use(context.Background())
    37  		datastore.GetTestable(c).AutoIndex(true)
    38  		datastore.GetTestable(c).Consistent(true)
    39  
    40  		Convey("List", func() {
    41  			Convey("invalid", func() {
    42  				Convey("page token", func() {
    43  					req := &projects.ListRequest{
    44  						PageToken: "token",
    45  					}
    46  					_, err := srv.List(c, req)
    47  					So(err, ShouldErrLike, "invalid page token")
    48  				})
    49  			})
    50  
    51  			Convey("valid", func() {
    52  				Convey("nil", func() {
    53  					Convey("none", func() {
    54  						rsp, err := srv.List(c, nil)
    55  						So(err, ShouldBeNil)
    56  						So(rsp.Projects, ShouldBeEmpty)
    57  					})
    58  
    59  					Convey("one", func() {
    60  						p := &model.Project{
    61  							ID: "id",
    62  						}
    63  						So(datastore.Put(c, p), ShouldBeNil)
    64  
    65  						rsp, err := srv.List(c, nil)
    66  						So(err, ShouldBeNil)
    67  						So(rsp.Projects, ShouldHaveLength, 1)
    68  					})
    69  				})
    70  
    71  				Convey("empty", func() {
    72  					Convey("none", func() {
    73  						req := &projects.ListRequest{}
    74  						rsp, err := srv.List(c, req)
    75  						So(err, ShouldBeNil)
    76  						So(rsp.Projects, ShouldBeEmpty)
    77  					})
    78  
    79  					Convey("one", func() {
    80  						p := &model.Project{
    81  							ID: "id",
    82  						}
    83  						So(datastore.Put(c, p), ShouldBeNil)
    84  
    85  						req := &projects.ListRequest{}
    86  						rsp, err := srv.List(c, req)
    87  						So(err, ShouldBeNil)
    88  						So(rsp.Projects, ShouldHaveLength, 1)
    89  					})
    90  				})
    91  
    92  				Convey("pages", func() {
    93  					So(datastore.Put(c, &model.Project{ID: "id1"}), ShouldBeNil)
    94  					So(datastore.Put(c, &model.Project{ID: "id2"}), ShouldBeNil)
    95  					So(datastore.Put(c, &model.Project{ID: "id3"}), ShouldBeNil)
    96  
    97  					Convey("default", func() {
    98  						req := &projects.ListRequest{}
    99  						rsp, err := srv.List(c, req)
   100  						So(err, ShouldBeNil)
   101  						So(rsp.Projects, ShouldNotBeEmpty)
   102  					})
   103  
   104  					Convey("one", func() {
   105  						req := &projects.ListRequest{
   106  							PageSize: 1,
   107  						}
   108  						rsp, err := srv.List(c, req)
   109  						So(err, ShouldBeNil)
   110  						So(rsp.NextPageToken, ShouldNotBeEmpty)
   111  						So(rsp.Projects, ShouldHaveLength, 1)
   112  
   113  						req.PageToken = rsp.NextPageToken
   114  						rsp, err = srv.List(c, req)
   115  						So(err, ShouldBeNil)
   116  						So(rsp.NextPageToken, ShouldNotBeEmpty)
   117  						So(rsp.Projects, ShouldHaveLength, 1)
   118  
   119  						req.PageToken = rsp.NextPageToken
   120  						rsp, err = srv.List(c, req)
   121  						So(err, ShouldBeNil)
   122  						So(rsp.NextPageToken, ShouldBeEmpty)
   123  						So(rsp.Projects, ShouldHaveLength, 1)
   124  					})
   125  
   126  					Convey("two", func() {
   127  						req := &projects.ListRequest{
   128  							PageSize: 2,
   129  						}
   130  						rsp, err := srv.List(c, req)
   131  						So(err, ShouldBeNil)
   132  						So(rsp.NextPageToken, ShouldNotBeEmpty)
   133  						So(rsp.Projects, ShouldHaveLength, 2)
   134  
   135  						req.PageToken = rsp.NextPageToken
   136  						rsp, err = srv.List(c, req)
   137  						So(err, ShouldBeNil)
   138  						So(rsp.NextPageToken, ShouldBeEmpty)
   139  						So(rsp.Projects, ShouldHaveLength, 1)
   140  					})
   141  
   142  					Convey("many", func() {
   143  						req := &projects.ListRequest{
   144  							PageSize: 200,
   145  						}
   146  						rsp, err := srv.List(c, req)
   147  						So(err, ShouldBeNil)
   148  						So(rsp.NextPageToken, ShouldBeEmpty)
   149  						So(rsp.Projects, ShouldHaveLength, 3)
   150  					})
   151  				})
   152  			})
   153  		})
   154  
   155  		Convey("Ensure", func() {
   156  			Convey("Binary", func() {
   157  				req := &projects.EnsureRequest{
   158  					Id: "id",
   159  					Project: &projects.Config{
   160  						Project: "project",
   161  						Region: []string{
   162  							"region1",
   163  							"region2",
   164  						},
   165  						Revision: "revision-1",
   166  					},
   167  				}
   168  				cfg, err := srv.Ensure(c, req)
   169  				So(err, ShouldBeNil)
   170  				So(cfg, ShouldNotBeEmpty)
   171  				So(cfg, ShouldResembleProto, &projects.Config{
   172  					Project: "project",
   173  					Region: []string{
   174  						"region1",
   175  						"region2",
   176  					},
   177  					Revision: "revision-1",
   178  				})
   179  			})
   180  		})
   181  	})
   182  }