go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/api/config/v1/disk_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 config
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/config/validation"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  	. "go.chromium.org/luci/common/testing/assertions"
    25  )
    26  
    27  func TestDisk(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	Convey("Disk", t, func() {
    31  		Convey("isValidImage", func() {
    32  			Convey("invalid", func() {
    33  				So(isValidImage("image"), ShouldBeFalse)
    34  				So(isValidImage("global/image"), ShouldBeFalse)
    35  				So(isValidImage("projects/image"), ShouldBeFalse)
    36  				So(isValidImage("projects/global/image"), ShouldBeFalse)
    37  				So(isValidImage("projects/project/region/image"), ShouldBeFalse)
    38  				So(isValidImage("projects/project/region/images/image"), ShouldBeFalse)
    39  			})
    40  
    41  			Convey("valid", func() {
    42  				So(isValidImage("global/images/image"), ShouldBeTrue)
    43  				So(isValidImage("projects/project/global/images/image"), ShouldBeTrue)
    44  			})
    45  		})
    46  
    47  		Convey("GetImageBase", func() {
    48  			Convey("short", func() {
    49  				d := &Disk{
    50  					Image: "global/images/image",
    51  				}
    52  				So(d.GetImageBase(), ShouldEqual, "image")
    53  			})
    54  
    55  			Convey("long", func() {
    56  				d := &Disk{
    57  					Image: "projects/project/global/images/image",
    58  				}
    59  				So(d.GetImageBase(), ShouldEqual, "image")
    60  			})
    61  		})
    62  
    63  		Convey("Validate", func() {
    64  			c := &validation.Context{Context: context.Background()}
    65  			Convey("invalid", func() {
    66  				Convey("Persistent + NVMe", func() {
    67  					d := &Disk{
    68  						Type:      "zones/zone/diskTypes/pd-standard",
    69  						Interface: DiskInterface_NVME,
    70  						Image:     "",
    71  					}
    72  					d.Validate(c)
    73  					err := c.Finalize().(*validation.Error).Errors
    74  					So(err, ShouldContainErr, "persistent disk must use SCSI")
    75  				})
    76  				Convey("Persistent + No Image", func() {
    77  					d := &Disk{
    78  						Type:      "zones/zone/diskTypes/pd-ssd",
    79  						Interface: DiskInterface_SCSI,
    80  						Image:     "",
    81  					}
    82  					d.Validate(c)
    83  					err := c.Finalize().(*validation.Error).Errors
    84  					So(err, ShouldContainErr, "image must match")
    85  				})
    86  				Convey("Scratch + Image", func() {
    87  					d := &Disk{
    88  						Type:      "zones/zone/diskTypes/local-ssd",
    89  						Interface: DiskInterface_NVME,
    90  						Image:     "global/images/image",
    91  					}
    92  					d.Validate(c)
    93  					err := c.Finalize().(*validation.Error).Errors
    94  					So(err, ShouldContainErr, "local ssd cannot use an image")
    95  				})
    96  			})
    97  
    98  			Convey("valid", func() {
    99  				Convey("Persistent + SCSI + Image", func() {
   100  					d := &Disk{
   101  						Type:      "zones/zone/diskTypes/pd-standard",
   102  						Interface: DiskInterface_SCSI,
   103  						Image:     "global/images/image",
   104  					}
   105  					d.Validate(c)
   106  					So(c.Finalize(), ShouldBeNil)
   107  				})
   108  				Convey("Scratch + No Image", func() {
   109  					d := &Disk{
   110  						Type:      "zones/zone/diskTypes/local-ssd",
   111  						Interface: DiskInterface_NVME,
   112  						Image:     "",
   113  					}
   114  					d.Validate(c)
   115  					So(c.Finalize(), ShouldBeNil)
   116  				})
   117  			})
   118  		})
   119  	})
   120  }