diff --git a/go/tests/validation/common.go b/go/tests/validation/common.go index 98da0bf..7a4c847 100644 --- a/go/tests/validation/common.go +++ b/go/tests/validation/common.go @@ -1,3 +1,6 @@ +// Package validation contains shared helpers for the protovalidate tests. +// The tests for fits.api.v1 live in this package, product-specific tests +// live in subpackages mirroring the proto packages (e.g. mvm, pcvm). package validation import ( @@ -8,31 +11,42 @@ import ( "google.golang.org/protobuf/proto" ) +// TestUUID is a valid UUID to be used in tests. +const TestUUID = "e266fcc6-f6de-4ee1-ba26-baa17bf47b13" + type ( - prototest struct { - name string - msg proto.Message - wantErr bool - wantErrorMessage string + // Prototest is a single validation test case. + Prototest struct { + // Name of the test case. + Name string + // Msg is the message to validate. + Msg proto.Message + // WantErr is true if the validation is expected to fail. + WantErr bool + // WantErrorMessage is the expected error message if WantErr is true. + WantErrorMessage string } - prototests []prototest + // Prototests is a list of validation test cases. + Prototests []Prototest ) -func validateProtos(t *testing.T, tests prototests) { +// ValidateProtos runs protovalidate for every test case and compares the result. +func ValidateProtos(t *testing.T, tests Prototests) { + t.Helper() for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := protovalidate.Validate(tt.msg) - if !tt.wantErr { + t.Run(tt.Name, func(t *testing.T) { + err := protovalidate.Validate(tt.Msg) + if !tt.WantErr { if err != nil { - t.Errorf("validate error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("validate error = %v, wantErr %v", err, tt.WantErr) } return } if err == nil { - t.Fatalf("validate error = nil, wantErr %v", tt.wantErr) + t.Fatalf("validate error = nil, wantErr %v", tt.WantErr) } - if diff := cmp.Diff(err.Error(), tt.wantErrorMessage); diff != "" { + if diff := cmp.Diff(err.Error(), tt.WantErrorMessage); diff != "" { t.Errorf("validate error = %v, diff %v", err, diff) } }) diff --git a/go/tests/validation/common_test.go b/go/tests/validation/common_test.go index de2ff0c..cc8a1fb 100644 --- a/go/tests/validation/common_test.go +++ b/go/tests/validation/common_test.go @@ -8,54 +8,54 @@ import ( ) func TestValidateLabels(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "empty key", - msg: &v1.Labels{Labels: map[string]string{"": "value"}}, - wantErr: true, - wantErrorMessage: `validation error: labels[""]: must be at least 1 characters`, + Name: "empty key", + Msg: &v1.Labels{Labels: map[string]string{"": "value"}}, + WantErr: true, + WantErrorMessage: `validation error: labels[""]: must be at least 1 characters`, }, { - name: "value too long", - msg: &v1.Labels{Labels: map[string]string{"key": strings.Repeat("x", 257)}}, - wantErr: true, - wantErrorMessage: `validation error: labels["key"]: must be at most 256 characters`, + Name: "value too long", + Msg: &v1.Labels{Labels: map[string]string{"key": strings.Repeat("x", 257)}}, + WantErr: true, + WantErrorMessage: `validation error: labels["key"]: must be at most 256 characters`, }, { - name: "untrimmed key and value", - msg: &v1.Labels{Labels: map[string]string{" key ": " value "}}, - wantErr: true, - wantErrorMessage: `validation error: labels: keys and values must not start or end with whitespace`, + Name: "untrimmed key and value", + Msg: &v1.Labels{Labels: map[string]string{" key ": " value "}}, + WantErr: true, + WantErrorMessage: `validation error: labels: keys and values must not start or end with whitespace`, }, { - name: "valid", - msg: &v1.Labels{Labels: map[string]string{"key": "value"}}, - wantErr: false, + Name: "valid", + Msg: &v1.Labels{Labels: map[string]string{"key": "value"}}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateUpdateMeta(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "undefined locking strategy", - msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy(99)}, - wantErr: true, - wantErrorMessage: `validation error: locking_strategy: value must be one of the defined enum values`, + Name: "undefined locking strategy", + Msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy(99)}, + WantErr: true, + WantErrorMessage: `validation error: locking_strategy: value must be one of the defined enum values`, }, { - name: "client locking strategy", - msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_CLIENT}, - wantErr: false, + Name: "client locking strategy", + Msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_CLIENT}, + WantErr: false, }, { - name: "server locking strategy", - msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, - wantErr: false, + Name: "server locking strategy", + Msg: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } diff --git a/go/tests/validation/contact_test.go b/go/tests/validation/contact_test.go deleted file mode 100644 index aac196b..0000000 --- a/go/tests/validation/contact_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package validation - -import ( - "testing" - - mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" - v1 "github.com/fi-ts/api/go/fits/api/v1" -) - -const msgPhone = "must be a valid phone number in E.164 format, e.g. +4915123456789" - -func TestValidateContactServiceCreateRequest(t *testing.T) { - request := func(phone string) *mvmv1.ContactServiceCreateRequest { - return &mvmv1.ContactServiceCreateRequest{ - Tenant: "tenant", - FirstName: "Jane", - LastName: "Doe", - Department: "IT", - Phone: phone, - Email: "jane.doe@example.com", - } - } - - tests := prototests{ - { - name: "valid", - msg: request("+4915123456789"), - wantErr: false, - }, - { - name: "missing phone", - msg: request(""), - wantErr: true, - wantErrorMessage: "validation error: phone: " + msgPhone, - }, - { - name: "phone without country code", - msg: request("015123456789"), - wantErr: true, - wantErrorMessage: "validation error: phone: " + msgPhone, - }, - { - name: "phone with whitespace", - msg: request("+49 151 23456789"), - wantErr: true, - wantErrorMessage: "validation error: phone: " + msgPhone, - }, - } - - validateProtos(t, tests) -} - -func TestValidateContactServiceUpdateRequest(t *testing.T) { - request := func(phone string) *mvmv1.ContactServiceUpdateRequest { - return &mvmv1.ContactServiceUpdateRequest{ - Tenant: "tenant", - Uuid: testUUID, - UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, - FirstName: "Jane", - LastName: "Doe", - Department: "IT", - Phone: phone, - Email: "jane.doe@example.com", - } - } - - tests := prototests{ - { - name: "valid", - msg: request("+4915123456789"), - wantErr: false, - }, - { - name: "invalid phone", - msg: request("0151-23456789"), - wantErr: true, - wantErrorMessage: "validation error: phone: " + msgPhone, - }, - } - - validateProtos(t, tests) -} diff --git a/go/tests/validation/domain_test.go b/go/tests/validation/domain_test.go deleted file mode 100644 index 2315d84..0000000 --- a/go/tests/validation/domain_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package validation - -import ( - "testing" - - mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" -) - -func TestValidateDomain(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.Domain{}, - wantErr: true, - wantErrorMessage: `validation errors: - - uuid: value is empty, which is not a valid UUID - - fqdn: must be within 2 and 128 characters - - tenant: must be within 2 and 128 characters`, - }, - { - name: "valid", - msg: &mvmv1.Domain{ - Uuid: testUUID, - Fqdn: "domain.example.com", - Tenant: "tenant", - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateDomainServiceListRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.DomainServiceListRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: - - tenant: must be within 2 and 128 characters - - location_uuid: value is empty, which is not a valid UUID`, - }, - { - name: "invalid location uuid", - msg: &mvmv1.DomainServiceListRequest{Tenant: "tenant", LocationUuid: "not-a-uuid"}, - wantErr: true, - wantErrorMessage: `validation error: location_uuid: must be a valid UUID`, - }, - { - name: "valid", - msg: &mvmv1.DomainServiceListRequest{Tenant: "tenant", LocationUuid: testUUID}, - wantErr: false, - }, - } - - validateProtos(t, tests) -} diff --git a/go/tests/validation/mvm/contact_test.go b/go/tests/validation/mvm/contact_test.go new file mode 100644 index 0000000..84a5038 --- /dev/null +++ b/go/tests/validation/mvm/contact_test.go @@ -0,0 +1,83 @@ +package mvm + +import ( + "testing" + + mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" + v1 "github.com/fi-ts/api/go/fits/api/v1" + "github.com/fi-ts/api/go/tests/validation" +) + +const msgPhone = "must be a valid phone number in E.164 format, e.g. +4915123456789" + +func TestValidateContactServiceCreateRequest(t *testing.T) { + request := func(phone string) *mvmv1.ContactServiceCreateRequest { + return &mvmv1.ContactServiceCreateRequest{ + Tenant: "tenant", + FirstName: "Jane", + LastName: "Doe", + Department: "IT", + Phone: phone, + Email: "jane.doe@example.com", + } + } + + tests := validation.Prototests{ + { + Name: "valid", + Msg: request("+4915123456789"), + WantErr: false, + }, + { + Name: "missing phone", + Msg: request(""), + WantErr: true, + WantErrorMessage: "validation error: phone: " + msgPhone, + }, + { + Name: "phone without country code", + Msg: request("015123456789"), + WantErr: true, + WantErrorMessage: "validation error: phone: " + msgPhone, + }, + { + Name: "phone with whitespace", + Msg: request("+49 151 23456789"), + WantErr: true, + WantErrorMessage: "validation error: phone: " + msgPhone, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateContactServiceUpdateRequest(t *testing.T) { + request := func(phone string) *mvmv1.ContactServiceUpdateRequest { + return &mvmv1.ContactServiceUpdateRequest{ + Tenant: "tenant", + Uuid: validation.TestUUID, + UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, + FirstName: "Jane", + LastName: "Doe", + Department: "IT", + Phone: phone, + Email: "jane.doe@example.com", + } + } + + tests := validation.Prototests{ + { + Name: "valid", + Msg: request("+4915123456789"), + WantErr: false, + }, + { + Name: "invalid phone", + Msg: request("0151-23456789"), + WantErr: true, + WantErrorMessage: "validation error: phone: " + msgPhone, + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/mvm/domain_test.go b/go/tests/validation/mvm/domain_test.go new file mode 100644 index 0000000..8ad018c --- /dev/null +++ b/go/tests/validation/mvm/domain_test.go @@ -0,0 +1,59 @@ +package mvm + +import ( + "testing" + + mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" + "github.com/fi-ts/api/go/tests/validation" +) + +func TestValidateDomain(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.Domain{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - fqdn: must be within 2 and 128 characters + - tenant: must be within 2 and 128 characters`, + }, + { + Name: "valid", + Msg: &mvmv1.Domain{ + Uuid: validation.TestUUID, + Fqdn: "domain.example.com", + Tenant: "tenant", + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateDomainServiceListRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.DomainServiceListRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: + - tenant: must be within 2 and 128 characters + - location_uuid: value is empty, which is not a valid UUID`, + }, + { + Name: "invalid location uuid", + Msg: &mvmv1.DomainServiceListRequest{Tenant: "tenant", LocationUuid: "not-a-uuid"}, + WantErr: true, + WantErrorMessage: `validation error: location_uuid: must be a valid UUID`, + }, + { + Name: "valid", + Msg: &mvmv1.DomainServiceListRequest{Tenant: "tenant", LocationUuid: validation.TestUUID}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/mvm/ldap_test.go b/go/tests/validation/mvm/ldap_test.go new file mode 100644 index 0000000..db907ca --- /dev/null +++ b/go/tests/validation/mvm/ldap_test.go @@ -0,0 +1,57 @@ +package mvm + +import ( + "testing" + + mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" + "github.com/fi-ts/api/go/tests/validation" +) + +func TestValidateLDAP(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.LDAP{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - fqdn: must be within 2 and 128 characters + - tenant: must be within 2 and 128 characters`, + }, + { + Name: "invalid uuid", + Msg: &mvmv1.LDAP{Uuid: "not-a-uuid", Fqdn: "ldap.example.com", Tenant: "tenant"}, + WantErr: true, + WantErrorMessage: `validation error: uuid: must be a valid UUID`, + }, + { + Name: "valid", + Msg: &mvmv1.LDAP{ + Uuid: validation.TestUUID, + Fqdn: "ldap.example.com", + Tenant: "tenant", + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateLDAPServiceListRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.LDAPServiceListRequest{}, + WantErr: true, + WantErrorMessage: `validation error: tenant: must be within 2 and 128 characters`, + }, + { + Name: "valid", + Msg: &mvmv1.LDAPServiceListRequest{Tenant: "tenant"}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/mvm/location_test.go b/go/tests/validation/mvm/location_test.go new file mode 100644 index 0000000..eb26beb --- /dev/null +++ b/go/tests/validation/mvm/location_test.go @@ -0,0 +1,46 @@ +package mvm + +import ( + "testing" + + mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" + "github.com/fi-ts/api/go/tests/validation" +) + +func TestValidateLocation(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.Location{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - title: must be within 2 and 128 characters`, + }, + { + Name: "invalid uuid", + Msg: &mvmv1.Location{Uuid: "not-a-uuid", Title: "Nürnberg (RZ1)"}, + WantErr: true, + WantErrorMessage: `validation error: uuid: must be a valid UUID`, + }, + { + Name: "valid", + Msg: &mvmv1.Location{Uuid: validation.TestUUID, Title: "Nürnberg (RZ1)"}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateLocationServiceListRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.LocationServiceListRequest{}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/mvm/mvm_test.go b/go/tests/validation/mvm/mvm_test.go new file mode 100644 index 0000000..ad7e5d1 --- /dev/null +++ b/go/tests/validation/mvm/mvm_test.go @@ -0,0 +1,990 @@ +package mvm + +import ( + "strings" + "testing" + + mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" + v1 "github.com/fi-ts/api/go/fits/api/v1" + "github.com/fi-ts/api/go/tests/validation" + "google.golang.org/protobuf/proto" +) + +const ( + msgCpuCount = "must be within 1 and 64 CPU cores" + msgRamSize = "must be within 4 and 512 GiB" + msgDiskSize = "must be within 1 and 8192 GiB" + msgDiskCount = "must contain at most 8 disks" + msgOrderReference = "must be within 2 and 128 characters and not start or end with whitespace" + msgMacAddress = "this string must be a valid mac address" + msgMountPoint = "must be an absolute path without whitespace, e.g. /data/app" + msgDriveLetter = "must be a single uppercase drive letter, e.g. G" + msgAddDriveLetter = "drive letter of a Windows data disk must be within F and Z, except R" + msgFqdn = "must be a valid FQDN, e.g. host.example.com" + msgNameCode = "must be a 4-character code consisting of digits and letters" + msgSnapshotName = "must be within 1 and 128 alphanumeric characters" + msgNotUnspecified = "must not be in list [0]" +) + +func validLinuxDisk() *mvmv1.LinuxDisk { + return &mvmv1.LinuxDisk{ + Name: "appdata", + SizeGb: 50, + MountPoint: proto.String("/appdata"), + DiskType: mvmv1.DiskType_DISK_TYPE_SYSTEM.Enum(), + } +} + +func validWindowsDisk() *mvmv1.WindowsDisk { + return &mvmv1.WindowsDisk{ + Name: "system", + SizeGb: 50, + DriveLetter: proto.String("F"), + DiskType: mvmv1.DiskType_DISK_TYPE_SYSTEM.Enum(), + } +} + +func linuxDisks(n int) []*mvmv1.LinuxDisk { + disks := make([]*mvmv1.LinuxDisk, 0, n) + for range n { + disks = append(disks, validLinuxDisk()) + } + return disks +} + +func validLinuxCreateRequest(modify ...func(*mvmv1.MVMServiceCreateRequest)) *mvmv1.MVMServiceCreateRequest { + req := &mvmv1.MVMServiceCreateRequest{ + ProjectUuid: validation.TestUUID, + NameCode: "ab12", + OsUuid: validation.TestUUID, + LocationUuid: validation.TestUUID, + ContactUuid: validation.TestUUID, + Cpu: 2, + RamGb: 8, + OrderReference: "TICKET-1", + Backup: &mvmv1.Backup{Enabled: true}, + Encryption: &mvmv1.Encryption{Enabled: false}, + Availability: mvmv1.Availability_AVAILABILITY_V1, + ServiceClass: mvmv1.ServiceClass_SERVICE_CLASS_SZ1, + InitialIpAddress: &mvmv1.InitialIPAddress{VlanUuid: validation.TestUUID}, + MvmType: &mvmv1.MVMServiceCreateRequest_Linux{ + Linux: &mvmv1.MVMServiceCreateLinuxRequest{ + LdapUuid: validation.TestUUID, + Disks: []*mvmv1.LinuxDisk{validLinuxDisk()}, + }, + }, + } + for _, m := range modify { + m(req) + } + return req +} + +func validUpdateRequest(modify ...func(*mvmv1.MVMServiceUpdateRequest)) *mvmv1.MVMServiceUpdateRequest { + req := &mvmv1.MVMServiceUpdateRequest{ + Uuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, + OrderReference: "TICKET-1", + } + for _, m := range modify { + m(req) + } + return req +} + +func TestValidateIPAddress(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.IPAddress{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - ip_address: value is empty, which is not a valid IPv4 address`, + }, + { + Name: "hostname instead of ip address", + Msg: &mvmv1.IPAddress{Uuid: validation.TestUUID, IpAddress: "host.example.com"}, + WantErr: true, + WantErrorMessage: "validation error: ip_address: must be a valid IPv4 address", + }, + { + Name: "ipv6 address", + Msg: &mvmv1.IPAddress{Uuid: validation.TestUUID, IpAddress: "2001:db8::1"}, + WantErr: true, + WantErrorMessage: "validation error: ip_address: must be a valid IPv4 address", + }, + { + Name: "valid", + Msg: &mvmv1.IPAddress{ + Uuid: validation.TestUUID, + IpAddress: "10.0.0.1", + }, + WantErr: false, + }, + { + Name: "valid with network interface", + Msg: &mvmv1.IPAddress{ + Uuid: validation.TestUUID, + IpAddress: "10.0.0.1", + NetworkInterface: &mvmv1.NetworkInterface{ + VlanUuid: validation.TestUUID, + MacAddress: "AA:BB:CC:DD:EE:01", + }, + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateNetworkInterface(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.NetworkInterface{}, + WantErr: true, + WantErrorMessage: `validation errors: + - vlan_uuid: value is empty, which is not a valid UUID + - mac_address: ` + msgMacAddress, + }, + { + Name: "invalid mac address", + Msg: &mvmv1.NetworkInterface{ + VlanUuid: validation.TestUUID, + MacAddress: "AA:BB:CC:DD:EE", + }, + WantErr: true, + WantErrorMessage: "validation error: mac_address: " + msgMacAddress, + }, + { + Name: "valid", + Msg: &mvmv1.NetworkInterface{ + VlanUuid: validation.TestUUID, + MacAddress: "AA:BB:CC:DD:EE:01", + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateInitialIPAddress(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.InitialIPAddress{}, + WantErr: true, + WantErrorMessage: `validation error: vlan_uuid: value is empty, which is not a valid UUID`, + }, + { + Name: "valid", + Msg: &mvmv1.InitialIPAddress{VlanUuid: validation.TestUUID}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateManagedVM(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.ManagedVM{}, + WantErr: true, + WantErrorMessage: `validation errors: + - details: exactly one field is required in oneof + - uuid: value is empty, which is not a valid UUID + - fqdn: must be a valid FQDN, e.g. host.example.com + - tenant: must be within 2 and 128 characters + - project_uuid: value is empty, which is not a valid UUID + - os_uuid: value is empty, which is not a valid UUID + - location_uuid: value is empty, which is not a valid UUID + - contact_uuid: value is empty, which is not a valid UUID + - cpu: must be within 1 and 64 CPU cores + - ram_gb: must be within 4 and 512 GiB + - availability: ` + msgNotUnspecified + ` + - service_class: ` + msgNotUnspecified + ` + - order_reference: must be within 2 and 128 characters and not start or end with whitespace`, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateLinuxDetails(t *testing.T) { + // label63 is the longest allowed single label. + label63 := strings.Repeat("a", 63) + // fqdn253 is the longest allowed FQDN: 3*(63+1) + 57 + 4 (".com") = 253 characters. + fqdn253 := strings.Repeat(label63+".", 3) + strings.Repeat("b", 57) + ".com" + // fqdn254 exceeds the total length by one, while every label is still valid. + fqdn254 := strings.Repeat(label63+".", 3) + strings.Repeat("b", 58) + ".com" + if len(fqdn253) != 253 || len(fqdn254) != 254 { + t.Fatalf("got lengths %d and %d, want 253 and 254", len(fqdn253), len(fqdn254)) + } + + tests := validation.Prototests{ + { + Name: "fqdn without domain", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "host"}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "fqdn with numeric tld", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "host.example.123"}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "valid", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "host.example.com"}, + WantErr: false, + }, + { + Name: "valid hyphen inside label", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "my-host.ex-ample.com"}, + WantErr: false, + }, + { + Name: "valid single character label", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "a.example.com"}, + WantErr: false, + }, + { + Name: "valid 63 character label", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: label63 + ".example.com"}, + WantErr: false, + }, + { + Name: "valid 253 characters total", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: fqdn253}, + WantErr: false, + }, + { + Name: "leading hyphen in first label", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "-host.example.com"}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "trailing hyphen in first label", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "host-.example.com"}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "leading hyphen in inner label", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "a.-b.example.com"}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "empty label", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "host..example.com"}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "trailing dot", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "host.example.com."}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "64 character label", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: label63 + "a.example.com"}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "254 characters total", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: fqdn254}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + { + Name: "underscore", + Msg: &mvmv1.LinuxDetails{LdapUuid: validation.TestUUID, LdapFqdn: "my_host.example.com"}, + WantErr: true, + WantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateWindowsDetails(t *testing.T) { + tests := validation.Prototests{ + { + Name: "invalid fqdn", + Msg: &mvmv1.WindowsDetails{DomainUuid: validation.TestUUID, DomainFqdn: "not a fqdn"}, + WantErr: true, + WantErrorMessage: "validation error: domain_fqdn: " + msgFqdn, + }, + { + Name: "valid", + Msg: &mvmv1.WindowsDetails{DomainUuid: validation.TestUUID, DomainFqdn: "host.domain.example.com"}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateLinuxDisk(t *testing.T) { + withDisk := func(modify func(*mvmv1.LinuxDisk)) *mvmv1.LinuxDisk { + d := validLinuxDisk() + modify(d) + return d + } + + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.LinuxDisk{}, + WantErr: true, + WantErrorMessage: `validation errors: + - name: must be within 2 and 128 characters + - size_gb: ` + msgDiskSize, + }, + { + Name: "valid system disk", + Msg: validLinuxDisk(), + WantErr: false, + }, + { + Name: "valid data disk without mount point", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { + d.MountPoint = nil + d.DiskType = mvmv1.DiskType_DISK_TYPE_DATA.Enum() + d.AutoExtend = proto.Bool(true) + }), + WantErr: false, + }, + { + Name: "valid root mount point", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("/") }), + WantErr: false, + }, + { + Name: "valid mount point with trailing slash and special characters", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("/data/my+dir/") }), + WantErr: false, + }, + { + Name: "relative mount point", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("data/app") }), + WantErr: true, + WantErrorMessage: "validation error: mount_point: " + msgMountPoint, + }, + { + Name: "empty mount point", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("") }), + WantErr: true, + WantErrorMessage: "validation error: mount_point: " + msgMountPoint, + }, + { + Name: "mount point with whitespace", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("/data/my dir") }), + WantErr: true, + WantErrorMessage: "validation error: mount_point: " + msgMountPoint, + }, + { + Name: "min disk size", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.SizeGb = 1 }), + WantErr: false, + }, + { + Name: "max disk size", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.SizeGb = 8192 }), + WantErr: false, + }, + { + Name: "disk too large", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.SizeGb = 8193 }), + WantErr: true, + WantErrorMessage: "validation error: size_gb: " + msgDiskSize, + }, + { + Name: "invalid uuid", + Msg: withDisk(func(d *mvmv1.LinuxDisk) { d.Uuid = proto.String("not-a-uuid") }), + WantErr: true, + WantErrorMessage: "validation error: uuid: must be a valid UUID", + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateWindowsDisk(t *testing.T) { + withDisk := func(modify func(*mvmv1.WindowsDisk)) *mvmv1.WindowsDisk { + d := validWindowsDisk() + modify(d) + return d + } + + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.WindowsDisk{}, + WantErr: true, + WantErrorMessage: `validation errors: + - name: must be within 2 and 128 characters + - size_gb: ` + msgDiskSize, + }, + { + Name: "valid system disk", + Msg: validWindowsDisk(), + WantErr: false, + }, + { + Name: "lowercase drive letter", + Msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("g") }), + WantErr: true, + WantErrorMessage: "validation error: drive_letter: " + msgDriveLetter, + }, + { + Name: "reserved drive letter is valid in responses", + Msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("R") }), + WantErr: false, + }, + { + Name: "valid os disk drive letter", + Msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("C") }), + WantErr: false, + }, + { + Name: "two drive letters", + Msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("FG") }), + WantErr: true, + WantErrorMessage: "validation error: drive_letter: " + msgDriveLetter, + }, + { + Name: "drive letter with colon", + Msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("F:") }), + WantErr: true, + WantErrorMessage: "validation error: drive_letter: " + msgDriveLetter, + }, + { + Name: "digit as drive letter", + Msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("1") }), + WantErr: true, + WantErrorMessage: "validation error: drive_letter: " + msgDriveLetter, + }, + { + Name: "disk too large", + Msg: withDisk(func(d *mvmv1.WindowsDisk) { d.SizeGb = 8193 }), + WantErr: true, + WantErrorMessage: "validation error: size_gb: " + msgDiskSize, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceGetRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.MVMServiceGetRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - project_uuid: value is empty, which is not a valid UUID`, + }, + { + Name: "valid", + Msg: &mvmv1.MVMServiceGetRequest{Uuid: validation.TestUUID, ProjectUuid: validation.TestUUID}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceCreateWindowsRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.MVMServiceCreateWindowsRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: + - domain_uuid: value is empty, which is not a valid UUID + - disks: must contain at least 1 item(s)`, + }, + { + Name: "invalid disk", + Msg: &mvmv1.MVMServiceCreateWindowsRequest{ + DomainUuid: validation.TestUUID, + Disks: []*mvmv1.WindowsDisk{{ + Name: "system", + SizeGb: 50, + DriveLetter: proto.String("FG"), + }}, + }, + WantErr: true, + WantErrorMessage: "validation error: disks[0].drive_letter: " + msgDriveLetter, + }, + { + Name: "valid", + Msg: &mvmv1.MVMServiceCreateWindowsRequest{ + DomainUuid: validation.TestUUID, + Disks: []*mvmv1.WindowsDisk{validWindowsDisk()}, + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceCreateRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "valid linux", + Msg: validLinuxCreateRequest(), + WantErr: false, + }, + { + Name: "valid windows", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.MvmType = &mvmv1.MVMServiceCreateRequest_Windows{ + Windows: &mvmv1.MVMServiceCreateWindowsRequest{ + DomainUuid: validation.TestUUID, + Disks: []*mvmv1.WindowsDisk{validWindowsDisk()}, + }, + } + }), + WantErr: false, + }, + { + Name: "valid bounds", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.Cpu = 64 + r.RamGb = 512 + }), + WantErr: false, + }, + { + Name: "no cpu", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.Cpu = 0 }), + WantErr: true, + WantErrorMessage: "validation error: cpu: " + msgCpuCount, + }, + { + Name: "too many cpus", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.Cpu = 65 }), + WantErr: true, + WantErrorMessage: "validation error: cpu: " + msgCpuCount, + }, + { + Name: "too little ram", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.RamGb = 3 }), + WantErr: true, + WantErrorMessage: "validation error: ram_gb: " + msgRamSize, + }, + { + Name: "too much ram", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.RamGb = 513 }), + WantErr: true, + WantErrorMessage: "validation error: ram_gb: " + msgRamSize, + }, + { + Name: "invalid name code", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.NameCode = "ab-1" }), + WantErr: true, + WantErrorMessage: "validation error: name_code: " + msgNameCode, + }, + { + Name: "missing order reference", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.OrderReference = "" }), + WantErr: true, + WantErrorMessage: "validation error: order_reference: " + msgOrderReference, + }, + { + Name: "untrimmed order reference", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.OrderReference = " TICKET-1 " }), + WantErr: true, + WantErrorMessage: "validation error: order_reference: " + msgOrderReference, + }, + { + Name: "no disks", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.GetLinux().Disks = nil + }), + WantErr: true, + WantErrorMessage: "validation error: linux.disks: must contain at least 1 item(s)", + }, + { + Name: "max disks", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.GetLinux().Disks = linuxDisks(8) + }), + WantErr: false, + }, + { + Name: "too many disks", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.GetLinux().Disks = linuxDisks(9) + }), + WantErr: true, + WantErrorMessage: "validation error: linux.disks: " + msgDiskCount, + }, + { + Name: "invalid disk", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.GetLinux().Disks[0].SizeGb = 0 + }), + WantErr: true, + WantErrorMessage: "validation error: linux.disks[0].size_gb: " + msgDiskSize, + }, + { + Name: "missing initial ip address", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.InitialIpAddress = nil + }), + WantErr: true, + WantErrorMessage: "validation error: initial_ip_address: value is required", + }, + { + Name: "missing backup configuration", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.Backup = nil + }), + WantErr: true, + WantErrorMessage: "validation error: backup: value is required", + }, + { + Name: "missing encryption configuration", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.Encryption = nil + }), + WantErr: true, + WantErrorMessage: "validation error: encryption: value is required", + }, + { + Name: "unspecified availability", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.Availability = mvmv1.Availability_AVAILABILITY_UNSPECIFIED + }), + WantErr: true, + WantErrorMessage: "validation error: availability: " + msgNotUnspecified, + }, + { + Name: "unspecified service class", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.ServiceClass = mvmv1.ServiceClass_SERVICE_CLASS_UNSPECIFIED + }), + WantErr: true, + WantErrorMessage: "validation error: service_class: " + msgNotUnspecified, + }, + { + Name: "missing os type", + Msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { + r.MvmType = nil + }), + WantErr: true, + WantErrorMessage: "validation error: mvm_type: exactly one field is required in oneof", + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceUpdateRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "valid without changes", + Msg: validUpdateRequest(), + WantErr: false, + }, + { + Name: "valid cpu and ram", + Msg: validUpdateRequest(func(r *mvmv1.MVMServiceUpdateRequest) { + r.Cpu = proto.Uint32(4) + r.RamGb = proto.Uint32(16) + }), + WantErr: false, + }, + { + Name: "too many cpus", + Msg: validUpdateRequest(func(r *mvmv1.MVMServiceUpdateRequest) { r.Cpu = proto.Uint32(65) }), + WantErr: true, + WantErrorMessage: "validation error: cpu: " + msgCpuCount, + }, + { + Name: "zero ram", + Msg: validUpdateRequest(func(r *mvmv1.MVMServiceUpdateRequest) { r.RamGb = proto.Uint32(0) }), + WantErr: true, + WantErrorMessage: "validation error: ram_gb: " + msgRamSize, + }, + { + Name: "missing order reference", + Msg: validUpdateRequest(func(r *mvmv1.MVMServiceUpdateRequest) { r.OrderReference = "" }), + WantErr: true, + WantErrorMessage: "validation error: order_reference: " + msgOrderReference, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceAddDisksRequest(t *testing.T) { + disks := func(n int) []*mvmv1.Disk { + d := make([]*mvmv1.Disk, 0, n) + for range n { + d = append(d, &mvmv1.Disk{Disk: &mvmv1.Disk_Linux{Linux: validLinuxDisk()}}) + } + return d + } + windowsDisk := func(letter string) []*mvmv1.Disk { + d := validWindowsDisk() + d.DiskType = mvmv1.DiskType_DISK_TYPE_DATA.Enum() + d.DriveLetter = nil + if letter != "" { + d.DriveLetter = proto.String(letter) + } + return []*mvmv1.Disk{{Disk: &mvmv1.Disk_Windows{Windows: d}}} + } + request := func(d []*mvmv1.Disk) *mvmv1.MVMServiceAddDisksRequest { + return &mvmv1.MVMServiceAddDisksRequest{ + Uuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + OrderReference: "TICKET-1", + Disks: d, + } + } + + tests := validation.Prototests{ + { + Name: "valid", + Msg: request(disks(1)), + WantErr: false, + }, + { + Name: "no disks", + Msg: request(nil), + WantErr: true, + WantErrorMessage: "validation error: disks: must contain at least 1 item(s)", + }, + { + Name: "too many disks", + Msg: request(disks(9)), + WantErr: true, + WantErrorMessage: "validation error: disks: " + msgDiskCount, + }, + { + Name: "disk without os type", + Msg: request([]*mvmv1.Disk{{}}), + WantErr: true, + WantErrorMessage: "validation error: disks[0].disk: exactly one field is required in oneof", + }, + { + Name: "invalid disk", + Msg: request([]*mvmv1.Disk{{Disk: &mvmv1.Disk_Linux{Linux: &mvmv1.LinuxDisk{ + Name: "data", + SizeGb: 10, + MountPoint: proto.String("data"), + }}}}), + WantErr: true, + WantErrorMessage: "validation error: disks[0].linux.mount_point: " + msgMountPoint, + }, + { + Name: "valid windows drive letter", + Msg: request(windowsDisk("G")), + WantErr: false, + }, + { + Name: "windows disk without drive letter", + Msg: request(windowsDisk("")), + WantErr: false, + }, + { + Name: "windows drive letter before F", + Msg: request(windowsDisk("E")), + WantErr: true, + WantErrorMessage: "validation error: disks: " + msgAddDriveLetter, + }, + { + Name: "windows drive letter R", + Msg: request(windowsDisk("R")), + WantErr: true, + WantErrorMessage: "validation error: disks: " + msgAddDriveLetter, + }, + { + Name: "windows lowercase drive letter", + Msg: request(windowsDisk("g")), + WantErr: true, + WantErrorMessage: `validation errors: + - disks: ` + msgAddDriveLetter + ` + - disks[0].windows.drive_letter: ` + msgDriveLetter, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceUpdateDiskRequest(t *testing.T) { + request := func(size *uint64) *mvmv1.MVMServiceUpdateDiskRequest { + return &mvmv1.MVMServiceUpdateDiskRequest{ + Uuid: validation.TestUUID, + DiskUuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + OrderReference: "TICKET-1", + SizeGb: size, + } + } + + tests := validation.Prototests{ + { + Name: "valid without size", + Msg: request(nil), + WantErr: false, + }, + { + Name: "valid size", + Msg: request(proto.Uint64(100)), + WantErr: false, + }, + { + Name: "zero size", + Msg: request(proto.Uint64(0)), + WantErr: true, + WantErrorMessage: "validation error: size_gb: " + msgDiskSize, + }, + { + Name: "too large", + Msg: request(proto.Uint64(8193)), + WantErr: true, + WantErrorMessage: "validation error: size_gb: " + msgDiskSize, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceDeleteRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.MVMServiceDeleteRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - project_uuid: value is empty, which is not a valid UUID + - order_reference: ` + msgOrderReference, + }, + { + Name: "order reference too short", + Msg: &mvmv1.MVMServiceDeleteRequest{ + Uuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + OrderReference: "x", + }, + WantErr: true, + WantErrorMessage: "validation error: order_reference: " + msgOrderReference, + }, + { + Name: "valid", + Msg: &mvmv1.MVMServiceDeleteRequest{ + Uuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + OrderReference: "TICKET-1", + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceAddSnapshotRequest(t *testing.T) { + request := func(name string) *mvmv1.MVMServiceAddSnapshotRequest { + return &mvmv1.MVMServiceAddSnapshotRequest{ + Uuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + OrderReference: "TICKET-1", + Name: name, + } + } + + tests := validation.Prototests{ + { + Name: "valid", + Msg: request("beforeUpgrade1"), + WantErr: false, + }, + { + Name: "name with special characters", + Msg: request("before-upgrade"), + WantErr: true, + WantErrorMessage: "validation error: name: " + msgSnapshotName, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateSnapshot(t *testing.T) { + tests := validation.Prototests{ + { + Name: "valid", + Msg: &mvmv1.Snapshot{Uuid: validation.TestUUID, Name: "snapshot1"}, + WantErr: false, + }, + { + Name: "name with whitespace", + Msg: &mvmv1.Snapshot{Uuid: validation.TestUUID, Name: "my snapshot"}, + WantErr: true, + WantErrorMessage: "validation error: name: " + msgSnapshotName, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateMVMServiceReinstallRequest(t *testing.T) { + request := func(modify ...func(*mvmv1.MVMServiceReinstallRequest)) *mvmv1.MVMServiceReinstallRequest { + req := &mvmv1.MVMServiceReinstallRequest{ + Uuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + OrderReference: "TICKET-1", + } + for _, m := range modify { + m(req) + } + return req + } + + tests := validation.Prototests{ + { + Name: "valid without changes keeps current configuration", + Msg: request(), + WantErr: false, + }, + { + Name: "valid with disabled backup and enabled encryption", + Msg: request(func(r *mvmv1.MVMServiceReinstallRequest) { + r.OsUuid = proto.String(validation.TestUUID) + r.Backup = &mvmv1.Backup{Enabled: false} + r.Encryption = &mvmv1.Encryption{Enabled: true} + }), + WantErr: false, + }, + { + Name: "invalid os uuid", + Msg: request(func(r *mvmv1.MVMServiceReinstallRequest) { + r.OsUuid = proto.String("not-a-uuid") + }), + WantErr: true, + WantErrorMessage: "validation error: os_uuid: must be a valid UUID", + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/mvm/os_test.go b/go/tests/validation/mvm/os_test.go new file mode 100644 index 0000000..316ba26 --- /dev/null +++ b/go/tests/validation/mvm/os_test.go @@ -0,0 +1,57 @@ +package mvm + +import ( + "testing" + + mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" + "github.com/fi-ts/api/go/tests/validation" +) + +func TestValidateOS(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.OS{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - title: must be within 2 and 128 characters`, + }, + { + Name: "invalid uuid", + Msg: &mvmv1.OS{Uuid: "not-a-uuid", Title: "Ubuntu 24.04", Type: mvmv1.OSType_OS_TYPE_LINUX}, + WantErr: true, + WantErrorMessage: `validation error: uuid: must be a valid UUID`, + }, + { + Name: "valid linux", + Msg: &mvmv1.OS{Uuid: validation.TestUUID, Title: "Ubuntu 24.04", Type: mvmv1.OSType_OS_TYPE_LINUX}, + WantErr: false, + }, + { + Name: "valid windows", + Msg: &mvmv1.OS{Uuid: validation.TestUUID, Title: "Windows Server 2022", Type: mvmv1.OSType_OS_TYPE_WINDOWS}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateOSServiceListRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.OSServiceListRequest{}, + WantErr: true, + WantErrorMessage: `validation error: tenant: must be within 2 and 128 characters`, + }, + { + Name: "valid", + Msg: &mvmv1.OSServiceListRequest{Tenant: "tenant"}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/mvm/stage_test.go b/go/tests/validation/mvm/stage_test.go new file mode 100644 index 0000000..9234006 --- /dev/null +++ b/go/tests/validation/mvm/stage_test.go @@ -0,0 +1,46 @@ +package mvm + +import ( + "testing" + + mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" + "github.com/fi-ts/api/go/tests/validation" +) + +func TestValidateStage(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.Stage{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - title: must be within 2 and 128 characters`, + }, + { + Name: "invalid uuid", + Msg: &mvmv1.Stage{Uuid: "not-a-uuid", Title: "production"}, + WantErr: true, + WantErrorMessage: `validation error: uuid: must be a valid UUID`, + }, + { + Name: "valid", + Msg: &mvmv1.Stage{Uuid: validation.TestUUID, Title: "production"}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidateStageServiceListRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &mvmv1.StageServiceListRequest{}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/mvm/vlan_test.go b/go/tests/validation/mvm/vlan_test.go new file mode 100644 index 0000000..3e4575a --- /dev/null +++ b/go/tests/validation/mvm/vlan_test.go @@ -0,0 +1,50 @@ +package mvm + +import ( + "testing" + + mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" + "github.com/fi-ts/api/go/tests/validation" +) + +func TestValidateVLAN(t *testing.T) { + vlan := func(id int32) *mvmv1.VLAN { + return &mvmv1.VLAN{ + Uuid: validation.TestUUID, + VlanId: id, + SubnetTitle: "pod01-vlan2001 (Production)", + SubnetCidr: "10.0.0.0/24", + LocationUuid: validation.TestUUID, + Tenant: "tenant", + } + } + + const msgVlanID = "validation error: vlan_id: must be greater than or equal to 1 and less than or equal to 4094" + + tests := validation.Prototests{ + { + Name: "min vlan id", + Msg: vlan(1), + WantErr: false, + }, + { + Name: "max vlan id", + Msg: vlan(4094), + WantErr: false, + }, + { + Name: "vlan id zero", + Msg: vlan(0), + WantErr: true, + WantErrorMessage: msgVlanID, + }, + { + Name: "vlan id too large", + Msg: vlan(4095), + WantErr: true, + WantErrorMessage: msgVlanID, + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/mvm_test.go b/go/tests/validation/mvm_test.go deleted file mode 100644 index 481c3ca..0000000 --- a/go/tests/validation/mvm_test.go +++ /dev/null @@ -1,910 +0,0 @@ -package validation - -import ( - "testing" - - mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" - v1 "github.com/fi-ts/api/go/fits/api/v1" - "google.golang.org/protobuf/proto" -) - -const ( - msgCpuCount = "must be within 1 and 64 CPU cores" - msgRamSize = "must be within 4 and 512 GiB" - msgDiskSize = "must be within 1 and 8192 GiB" - msgDiskCount = "must contain at most 8 disks" - msgOrderReference = "must be within 2 and 128 characters and not start or end with whitespace" - msgMacAddress = "this string must be a valid mac address" - msgMountPoint = "must be an absolute path without whitespace, e.g. /data/app" - msgDriveLetter = "must be a single uppercase drive letter, e.g. G" - msgAddDriveLetter = "drive letter of a Windows data disk must be within F and Z, except R" - msgFqdn = "must be a valid FQDN, e.g. host.example.com" - msgNameCode = "must be a 4-character code consisting of digits and letters" - msgSnapshotName = "must be within 1 and 128 alphanumeric characters" - msgNotUnspecified = "must not be in list [0]" -) - -func validLinuxDisk() *mvmv1.LinuxDisk { - return &mvmv1.LinuxDisk{ - Name: "appdata", - SizeGb: 50, - MountPoint: proto.String("/appdata"), - DiskType: mvmv1.DiskType_DISK_TYPE_SYSTEM.Enum(), - } -} - -func validWindowsDisk() *mvmv1.WindowsDisk { - return &mvmv1.WindowsDisk{ - Name: "system", - SizeGb: 50, - DriveLetter: proto.String("F"), - DiskType: mvmv1.DiskType_DISK_TYPE_SYSTEM.Enum(), - } -} - -func linuxDisks(n int) []*mvmv1.LinuxDisk { - disks := make([]*mvmv1.LinuxDisk, 0, n) - for range n { - disks = append(disks, validLinuxDisk()) - } - return disks -} - -func validLinuxCreateRequest(modify ...func(*mvmv1.MVMServiceCreateRequest)) *mvmv1.MVMServiceCreateRequest { - req := &mvmv1.MVMServiceCreateRequest{ - ProjectUuid: testUUID, - NameCode: "ab12", - OsUuid: testUUID, - LocationUuid: testUUID, - ContactUuid: testUUID, - Cpu: 2, - RamGb: 8, - OrderReference: "TICKET-1", - Backup: &mvmv1.Backup{Enabled: true}, - Encryption: &mvmv1.Encryption{Enabled: false}, - Availability: mvmv1.Availability_AVAILABILITY_V1, - ServiceClass: mvmv1.ServiceClass_SERVICE_CLASS_SZ1, - InitialIpAddress: &mvmv1.InitialIPAddress{VlanUuid: testUUID}, - MvmType: &mvmv1.MVMServiceCreateRequest_Linux{ - Linux: &mvmv1.MVMServiceCreateLinuxRequest{ - LdapUuid: testUUID, - Disks: []*mvmv1.LinuxDisk{validLinuxDisk()}, - }, - }, - } - for _, m := range modify { - m(req) - } - return req -} - -func validUpdateRequest(modify ...func(*mvmv1.MVMServiceUpdateRequest)) *mvmv1.MVMServiceUpdateRequest { - req := &mvmv1.MVMServiceUpdateRequest{ - Uuid: testUUID, - ProjectUuid: testUUID, - UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, - OrderReference: "TICKET-1", - } - for _, m := range modify { - m(req) - } - return req -} - -func TestValidateIPAddress(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.IPAddress{}, - wantErr: true, - wantErrorMessage: `validation errors: - - uuid: value is empty, which is not a valid UUID - - ip_address: value is empty, which is not a valid IPv4 address`, - }, - { - name: "hostname instead of ip address", - msg: &mvmv1.IPAddress{Uuid: testUUID, IpAddress: "host.example.com"}, - wantErr: true, - wantErrorMessage: "validation error: ip_address: must be a valid IPv4 address", - }, - { - name: "ipv6 address", - msg: &mvmv1.IPAddress{Uuid: testUUID, IpAddress: "2001:db8::1"}, - wantErr: true, - wantErrorMessage: "validation error: ip_address: must be a valid IPv4 address", - }, - { - name: "valid", - msg: &mvmv1.IPAddress{ - Uuid: testUUID, - IpAddress: "10.0.0.1", - }, - wantErr: false, - }, - { - name: "valid with network interface", - msg: &mvmv1.IPAddress{ - Uuid: testUUID, - IpAddress: "10.0.0.1", - NetworkInterface: &mvmv1.NetworkInterface{ - VlanUuid: testUUID, - MacAddress: "AA:BB:CC:DD:EE:01", - }, - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateNetworkInterface(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.NetworkInterface{}, - wantErr: true, - wantErrorMessage: `validation errors: - - vlan_uuid: value is empty, which is not a valid UUID - - mac_address: ` + msgMacAddress, - }, - { - name: "invalid mac address", - msg: &mvmv1.NetworkInterface{ - VlanUuid: testUUID, - MacAddress: "AA:BB:CC:DD:EE", - }, - wantErr: true, - wantErrorMessage: "validation error: mac_address: " + msgMacAddress, - }, - { - name: "valid", - msg: &mvmv1.NetworkInterface{ - VlanUuid: testUUID, - MacAddress: "AA:BB:CC:DD:EE:01", - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateInitialIPAddress(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.InitialIPAddress{}, - wantErr: true, - wantErrorMessage: `validation error: vlan_uuid: value is empty, which is not a valid UUID`, - }, - { - name: "valid", - msg: &mvmv1.InitialIPAddress{VlanUuid: testUUID}, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateManagedVM(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.ManagedVM{}, - wantErr: true, - wantErrorMessage: `validation errors: - - details: exactly one field is required in oneof - - uuid: value is empty, which is not a valid UUID - - fqdn: must be a valid FQDN, e.g. host.example.com - - tenant: must be within 2 and 128 characters - - project_uuid: value is empty, which is not a valid UUID - - os_uuid: value is empty, which is not a valid UUID - - location_uuid: value is empty, which is not a valid UUID - - contact_uuid: value is empty, which is not a valid UUID - - cpu: must be within 1 and 64 CPU cores - - ram_gb: must be within 4 and 512 GiB - - availability: ` + msgNotUnspecified + ` - - service_class: ` + msgNotUnspecified + ` - - order_reference: must be within 2 and 128 characters and not start or end with whitespace`, - }, - } - - validateProtos(t, tests) -} - -func TestValidateLinuxDetails(t *testing.T) { - tests := prototests{ - { - name: "fqdn without domain", - msg: &mvmv1.LinuxDetails{LdapUuid: testUUID, LdapFqdn: "host"}, - wantErr: true, - wantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, - }, - { - name: "fqdn with numeric tld", - msg: &mvmv1.LinuxDetails{LdapUuid: testUUID, LdapFqdn: "host.example.123"}, - wantErr: true, - wantErrorMessage: "validation error: ldap_fqdn: " + msgFqdn, - }, - { - name: "valid", - msg: &mvmv1.LinuxDetails{LdapUuid: testUUID, LdapFqdn: "host.example.com"}, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateWindowsDetails(t *testing.T) { - tests := prototests{ - { - name: "invalid fqdn", - msg: &mvmv1.WindowsDetails{DomainUuid: testUUID, DomainFqdn: "not a fqdn"}, - wantErr: true, - wantErrorMessage: "validation error: domain_fqdn: " + msgFqdn, - }, - { - name: "valid", - msg: &mvmv1.WindowsDetails{DomainUuid: testUUID, DomainFqdn: "host.domain.example.com"}, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateLinuxDisk(t *testing.T) { - withDisk := func(modify func(*mvmv1.LinuxDisk)) *mvmv1.LinuxDisk { - d := validLinuxDisk() - modify(d) - return d - } - - tests := prototests{ - { - name: "empty", - msg: &mvmv1.LinuxDisk{}, - wantErr: true, - wantErrorMessage: `validation errors: - - name: must be within 2 and 128 characters - - size_gb: ` + msgDiskSize, - }, - { - name: "valid system disk", - msg: validLinuxDisk(), - wantErr: false, - }, - { - name: "valid data disk without mount point", - msg: withDisk(func(d *mvmv1.LinuxDisk) { - d.MountPoint = nil - d.DiskType = mvmv1.DiskType_DISK_TYPE_DATA.Enum() - d.AutoExtend = proto.Bool(true) - }), - wantErr: false, - }, - { - name: "valid root mount point", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("/") }), - wantErr: false, - }, - { - name: "valid mount point with trailing slash and special characters", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("/data/my+dir/") }), - wantErr: false, - }, - { - name: "relative mount point", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("data/app") }), - wantErr: true, - wantErrorMessage: "validation error: mount_point: " + msgMountPoint, - }, - { - name: "empty mount point", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("") }), - wantErr: true, - wantErrorMessage: "validation error: mount_point: " + msgMountPoint, - }, - { - name: "mount point with whitespace", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.MountPoint = proto.String("/data/my dir") }), - wantErr: true, - wantErrorMessage: "validation error: mount_point: " + msgMountPoint, - }, - { - name: "min disk size", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.SizeGb = 1 }), - wantErr: false, - }, - { - name: "max disk size", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.SizeGb = 8192 }), - wantErr: false, - }, - { - name: "disk too large", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.SizeGb = 8193 }), - wantErr: true, - wantErrorMessage: "validation error: size_gb: " + msgDiskSize, - }, - { - name: "invalid uuid", - msg: withDisk(func(d *mvmv1.LinuxDisk) { d.Uuid = proto.String("not-a-uuid") }), - wantErr: true, - wantErrorMessage: "validation error: uuid: must be a valid UUID", - }, - } - - validateProtos(t, tests) -} - -func TestValidateWindowsDisk(t *testing.T) { - withDisk := func(modify func(*mvmv1.WindowsDisk)) *mvmv1.WindowsDisk { - d := validWindowsDisk() - modify(d) - return d - } - - tests := prototests{ - { - name: "empty", - msg: &mvmv1.WindowsDisk{}, - wantErr: true, - wantErrorMessage: `validation errors: - - name: must be within 2 and 128 characters - - size_gb: ` + msgDiskSize, - }, - { - name: "valid system disk", - msg: validWindowsDisk(), - wantErr: false, - }, - { - name: "lowercase drive letter", - msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("g") }), - wantErr: true, - wantErrorMessage: "validation error: drive_letter: " + msgDriveLetter, - }, - { - name: "reserved drive letter is valid in responses", - msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("R") }), - wantErr: false, - }, - { - name: "valid os disk drive letter", - msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("C") }), - wantErr: false, - }, - { - name: "two drive letters", - msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("FG") }), - wantErr: true, - wantErrorMessage: "validation error: drive_letter: " + msgDriveLetter, - }, - { - name: "drive letter with colon", - msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("F:") }), - wantErr: true, - wantErrorMessage: "validation error: drive_letter: " + msgDriveLetter, - }, - { - name: "digit as drive letter", - msg: withDisk(func(d *mvmv1.WindowsDisk) { d.DriveLetter = proto.String("1") }), - wantErr: true, - wantErrorMessage: "validation error: drive_letter: " + msgDriveLetter, - }, - { - name: "disk too large", - msg: withDisk(func(d *mvmv1.WindowsDisk) { d.SizeGb = 8193 }), - wantErr: true, - wantErrorMessage: "validation error: size_gb: " + msgDiskSize, - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceGetRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.MVMServiceGetRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: - - uuid: value is empty, which is not a valid UUID - - project_uuid: value is empty, which is not a valid UUID`, - }, - { - name: "valid", - msg: &mvmv1.MVMServiceGetRequest{Uuid: testUUID, ProjectUuid: testUUID}, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceCreateWindowsRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.MVMServiceCreateWindowsRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: - - domain_uuid: value is empty, which is not a valid UUID - - disks: must contain at least 1 item(s)`, - }, - { - name: "invalid disk", - msg: &mvmv1.MVMServiceCreateWindowsRequest{ - DomainUuid: testUUID, - Disks: []*mvmv1.WindowsDisk{{ - Name: "system", - SizeGb: 50, - DriveLetter: proto.String("FG"), - }}, - }, - wantErr: true, - wantErrorMessage: "validation error: disks[0].drive_letter: " + msgDriveLetter, - }, - { - name: "valid", - msg: &mvmv1.MVMServiceCreateWindowsRequest{ - DomainUuid: testUUID, - Disks: []*mvmv1.WindowsDisk{validWindowsDisk()}, - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceCreateRequest(t *testing.T) { - tests := prototests{ - { - name: "valid linux", - msg: validLinuxCreateRequest(), - wantErr: false, - }, - { - name: "valid windows", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.MvmType = &mvmv1.MVMServiceCreateRequest_Windows{ - Windows: &mvmv1.MVMServiceCreateWindowsRequest{ - DomainUuid: testUUID, - Disks: []*mvmv1.WindowsDisk{validWindowsDisk()}, - }, - } - }), - wantErr: false, - }, - { - name: "valid bounds", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.Cpu = 64 - r.RamGb = 512 - }), - wantErr: false, - }, - { - name: "no cpu", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.Cpu = 0 }), - wantErr: true, - wantErrorMessage: "validation error: cpu: " + msgCpuCount, - }, - { - name: "too many cpus", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.Cpu = 65 }), - wantErr: true, - wantErrorMessage: "validation error: cpu: " + msgCpuCount, - }, - { - name: "too little ram", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.RamGb = 3 }), - wantErr: true, - wantErrorMessage: "validation error: ram_gb: " + msgRamSize, - }, - { - name: "too much ram", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.RamGb = 513 }), - wantErr: true, - wantErrorMessage: "validation error: ram_gb: " + msgRamSize, - }, - { - name: "invalid name code", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.NameCode = "ab-1" }), - wantErr: true, - wantErrorMessage: "validation error: name_code: " + msgNameCode, - }, - { - name: "missing order reference", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.OrderReference = "" }), - wantErr: true, - wantErrorMessage: "validation error: order_reference: " + msgOrderReference, - }, - { - name: "untrimmed order reference", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { r.OrderReference = " TICKET-1 " }), - wantErr: true, - wantErrorMessage: "validation error: order_reference: " + msgOrderReference, - }, - { - name: "no disks", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.GetLinux().Disks = nil - }), - wantErr: true, - wantErrorMessage: "validation error: linux.disks: must contain at least 1 item(s)", - }, - { - name: "max disks", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.GetLinux().Disks = linuxDisks(8) - }), - wantErr: false, - }, - { - name: "too many disks", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.GetLinux().Disks = linuxDisks(9) - }), - wantErr: true, - wantErrorMessage: "validation error: linux.disks: " + msgDiskCount, - }, - { - name: "invalid disk", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.GetLinux().Disks[0].SizeGb = 0 - }), - wantErr: true, - wantErrorMessage: "validation error: linux.disks[0].size_gb: " + msgDiskSize, - }, - { - name: "missing initial ip address", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.InitialIpAddress = nil - }), - wantErr: true, - wantErrorMessage: "validation error: initial_ip_address: value is required", - }, - { - name: "missing backup configuration", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.Backup = nil - }), - wantErr: true, - wantErrorMessage: "validation error: backup: value is required", - }, - { - name: "missing encryption configuration", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.Encryption = nil - }), - wantErr: true, - wantErrorMessage: "validation error: encryption: value is required", - }, - { - name: "unspecified availability", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.Availability = mvmv1.Availability_AVAILABILITY_UNSPECIFIED - }), - wantErr: true, - wantErrorMessage: "validation error: availability: " + msgNotUnspecified, - }, - { - name: "unspecified service class", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.ServiceClass = mvmv1.ServiceClass_SERVICE_CLASS_UNSPECIFIED - }), - wantErr: true, - wantErrorMessage: "validation error: service_class: " + msgNotUnspecified, - }, - { - name: "missing os type", - msg: validLinuxCreateRequest(func(r *mvmv1.MVMServiceCreateRequest) { - r.MvmType = nil - }), - wantErr: true, - wantErrorMessage: "validation error: mvm_type: exactly one field is required in oneof", - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceUpdateRequest(t *testing.T) { - tests := prototests{ - { - name: "valid without changes", - msg: validUpdateRequest(), - wantErr: false, - }, - { - name: "valid cpu and ram", - msg: validUpdateRequest(func(r *mvmv1.MVMServiceUpdateRequest) { - r.Cpu = proto.Uint32(4) - r.RamGb = proto.Uint32(16) - }), - wantErr: false, - }, - { - name: "too many cpus", - msg: validUpdateRequest(func(r *mvmv1.MVMServiceUpdateRequest) { r.Cpu = proto.Uint32(65) }), - wantErr: true, - wantErrorMessage: "validation error: cpu: " + msgCpuCount, - }, - { - name: "zero ram", - msg: validUpdateRequest(func(r *mvmv1.MVMServiceUpdateRequest) { r.RamGb = proto.Uint32(0) }), - wantErr: true, - wantErrorMessage: "validation error: ram_gb: " + msgRamSize, - }, - { - name: "missing order reference", - msg: validUpdateRequest(func(r *mvmv1.MVMServiceUpdateRequest) { r.OrderReference = "" }), - wantErr: true, - wantErrorMessage: "validation error: order_reference: " + msgOrderReference, - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceAddDisksRequest(t *testing.T) { - disks := func(n int) []*mvmv1.Disk { - d := make([]*mvmv1.Disk, 0, n) - for range n { - d = append(d, &mvmv1.Disk{Disk: &mvmv1.Disk_Linux{Linux: validLinuxDisk()}}) - } - return d - } - windowsDisk := func(letter string) []*mvmv1.Disk { - d := validWindowsDisk() - d.DiskType = mvmv1.DiskType_DISK_TYPE_DATA.Enum() - d.DriveLetter = nil - if letter != "" { - d.DriveLetter = proto.String(letter) - } - return []*mvmv1.Disk{{Disk: &mvmv1.Disk_Windows{Windows: d}}} - } - request := func(d []*mvmv1.Disk) *mvmv1.MVMServiceAddDisksRequest { - return &mvmv1.MVMServiceAddDisksRequest{ - Uuid: testUUID, - ProjectUuid: testUUID, - OrderReference: "TICKET-1", - Disks: d, - } - } - - tests := prototests{ - { - name: "valid", - msg: request(disks(1)), - wantErr: false, - }, - { - name: "no disks", - msg: request(nil), - wantErr: true, - wantErrorMessage: "validation error: disks: must contain at least 1 item(s)", - }, - { - name: "too many disks", - msg: request(disks(9)), - wantErr: true, - wantErrorMessage: "validation error: disks: " + msgDiskCount, - }, - { - name: "disk without os type", - msg: request([]*mvmv1.Disk{{}}), - wantErr: true, - wantErrorMessage: "validation error: disks[0].disk: exactly one field is required in oneof", - }, - { - name: "invalid disk", - msg: request([]*mvmv1.Disk{{Disk: &mvmv1.Disk_Linux{Linux: &mvmv1.LinuxDisk{ - Name: "data", - SizeGb: 10, - MountPoint: proto.String("data"), - }}}}), - wantErr: true, - wantErrorMessage: "validation error: disks[0].linux.mount_point: " + msgMountPoint, - }, - { - name: "valid windows drive letter", - msg: request(windowsDisk("G")), - wantErr: false, - }, - { - name: "windows disk without drive letter", - msg: request(windowsDisk("")), - wantErr: false, - }, - { - name: "windows drive letter before F", - msg: request(windowsDisk("E")), - wantErr: true, - wantErrorMessage: "validation error: disks: " + msgAddDriveLetter, - }, - { - name: "windows drive letter R", - msg: request(windowsDisk("R")), - wantErr: true, - wantErrorMessage: "validation error: disks: " + msgAddDriveLetter, - }, - { - name: "windows lowercase drive letter", - msg: request(windowsDisk("g")), - wantErr: true, - wantErrorMessage: `validation errors: - - disks: ` + msgAddDriveLetter + ` - - disks[0].windows.drive_letter: ` + msgDriveLetter, - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceUpdateDiskRequest(t *testing.T) { - request := func(size *uint64) *mvmv1.MVMServiceUpdateDiskRequest { - return &mvmv1.MVMServiceUpdateDiskRequest{ - Uuid: testUUID, - DiskUuid: testUUID, - ProjectUuid: testUUID, - OrderReference: "TICKET-1", - SizeGb: size, - } - } - - tests := prototests{ - { - name: "valid without size", - msg: request(nil), - wantErr: false, - }, - { - name: "valid size", - msg: request(proto.Uint64(100)), - wantErr: false, - }, - { - name: "zero size", - msg: request(proto.Uint64(0)), - wantErr: true, - wantErrorMessage: "validation error: size_gb: " + msgDiskSize, - }, - { - name: "too large", - msg: request(proto.Uint64(8193)), - wantErr: true, - wantErrorMessage: "validation error: size_gb: " + msgDiskSize, - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceDeleteRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &mvmv1.MVMServiceDeleteRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: - - uuid: value is empty, which is not a valid UUID - - project_uuid: value is empty, which is not a valid UUID - - order_reference: ` + msgOrderReference, - }, - { - name: "order reference too short", - msg: &mvmv1.MVMServiceDeleteRequest{ - Uuid: testUUID, - ProjectUuid: testUUID, - OrderReference: "x", - }, - wantErr: true, - wantErrorMessage: "validation error: order_reference: " + msgOrderReference, - }, - { - name: "valid", - msg: &mvmv1.MVMServiceDeleteRequest{ - Uuid: testUUID, - ProjectUuid: testUUID, - OrderReference: "TICKET-1", - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceAddSnapshotRequest(t *testing.T) { - request := func(name string) *mvmv1.MVMServiceAddSnapshotRequest { - return &mvmv1.MVMServiceAddSnapshotRequest{ - Uuid: testUUID, - ProjectUuid: testUUID, - OrderReference: "TICKET-1", - Name: name, - } - } - - tests := prototests{ - { - name: "valid", - msg: request("beforeUpgrade1"), - wantErr: false, - }, - { - name: "name with special characters", - msg: request("before-upgrade"), - wantErr: true, - wantErrorMessage: "validation error: name: " + msgSnapshotName, - }, - } - - validateProtos(t, tests) -} - -func TestValidateSnapshot(t *testing.T) { - tests := prototests{ - { - name: "valid", - msg: &mvmv1.Snapshot{Uuid: testUUID, Name: "snapshot1"}, - wantErr: false, - }, - { - name: "name with whitespace", - msg: &mvmv1.Snapshot{Uuid: testUUID, Name: "my snapshot"}, - wantErr: true, - wantErrorMessage: "validation error: name: " + msgSnapshotName, - }, - } - - validateProtos(t, tests) -} - -func TestValidateMVMServiceReinstallRequest(t *testing.T) { - request := func(modify ...func(*mvmv1.MVMServiceReinstallRequest)) *mvmv1.MVMServiceReinstallRequest { - req := &mvmv1.MVMServiceReinstallRequest{ - Uuid: testUUID, - ProjectUuid: testUUID, - OrderReference: "TICKET-1", - } - for _, m := range modify { - m(req) - } - return req - } - - tests := prototests{ - { - name: "valid without changes keeps current configuration", - msg: request(), - wantErr: false, - }, - { - name: "valid with disabled backup and enabled encryption", - msg: request(func(r *mvmv1.MVMServiceReinstallRequest) { - r.OsUuid = proto.String(testUUID) - r.Backup = &mvmv1.Backup{Enabled: false} - r.Encryption = &mvmv1.Encryption{Enabled: true} - }), - wantErr: false, - }, - { - name: "invalid os uuid", - msg: request(func(r *mvmv1.MVMServiceReinstallRequest) { - r.OsUuid = proto.String("not-a-uuid") - }), - wantErr: true, - wantErrorMessage: "validation error: os_uuid: must be a valid UUID", - }, - } - - validateProtos(t, tests) -} diff --git a/go/tests/validation/pcvm/pcvm_test.go b/go/tests/validation/pcvm/pcvm_test.go new file mode 100644 index 0000000..a5f1874 --- /dev/null +++ b/go/tests/validation/pcvm/pcvm_test.go @@ -0,0 +1,124 @@ +package pcvm + +import ( + "testing" + + pcvmv1 "github.com/fi-ts/api/go/fits/api/pcvm/v1" + v1 "github.com/fi-ts/api/go/fits/api/v1" + "github.com/fi-ts/api/go/tests/validation" +) + +func TestValidatePublicCloudVMServiceGetRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &pcvmv1.PublicCloudVMServiceGetRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - project_uuid: value is empty, which is not a valid UUID`, + }, + { + Name: "valid", + Msg: &pcvmv1.PublicCloudVMServiceGetRequest{Uuid: validation.TestUUID, ProjectUuid: validation.TestUUID}, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidatePublicCloudVMServiceCreateRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &pcvmv1.PublicCloudVMServiceCreateRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: + - project_uuid: value is empty, which is not a valid UUID + - name: must be within 2 and 128 characters`, + }, + { + Name: "valid", + Msg: &pcvmv1.PublicCloudVMServiceCreateRequest{ + ProjectUuid: validation.TestUUID, + Name: "my-vm", + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidatePublicCloudVMServiceUpdateRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &pcvmv1.PublicCloudVMServiceUpdateRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: + - uuid: value is empty, which is not a valid UUID + - project_uuid: value is empty, which is not a valid UUID + - update_meta: value is required`, + }, + { + Name: "valid", + Msg: &pcvmv1.PublicCloudVMServiceUpdateRequest{ + Uuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidatePublicCloudVMServiceValidateCreateRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &pcvmv1.PublicCloudVMServiceValidateCreateRequest{}, + WantErr: true, + WantErrorMessage: `validation error: create: value is required`, + }, + { + Name: "valid", + Msg: &pcvmv1.PublicCloudVMServiceValidateCreateRequest{ + Create: &pcvmv1.PublicCloudVMServiceCreateRequest{ + ProjectUuid: validation.TestUUID, + Name: "my-vm", + }, + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} + +func TestValidatePublicCloudVMServiceValidateUpdateRequest(t *testing.T) { + tests := validation.Prototests{ + { + Name: "empty", + Msg: &pcvmv1.PublicCloudVMServiceValidateUpdateRequest{}, + WantErr: true, + WantErrorMessage: `validation error: update: value is required`, + }, + { + Name: "valid", + Msg: &pcvmv1.PublicCloudVMServiceValidateUpdateRequest{ + Update: &pcvmv1.PublicCloudVMServiceUpdateRequest{ + Uuid: validation.TestUUID, + ProjectUuid: validation.TestUUID, + UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, + }, + }, + WantErr: false, + }, + } + + validation.ValidateProtos(t, tests) +} diff --git a/go/tests/validation/pcvm_test.go b/go/tests/validation/pcvm_test.go deleted file mode 100644 index 4557e38..0000000 --- a/go/tests/validation/pcvm_test.go +++ /dev/null @@ -1,123 +0,0 @@ -package validation - -import ( - "testing" - - pcvmv1 "github.com/fi-ts/api/go/fits/api/pcvm/v1" - v1 "github.com/fi-ts/api/go/fits/api/v1" -) - -func TestValidatePublicCloudVMServiceGetRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &pcvmv1.PublicCloudVMServiceGetRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: - - uuid: value is empty, which is not a valid UUID - - project_uuid: value is empty, which is not a valid UUID`, - }, - { - name: "valid", - msg: &pcvmv1.PublicCloudVMServiceGetRequest{Uuid: testUUID, ProjectUuid: testUUID}, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidatePublicCloudVMServiceCreateRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &pcvmv1.PublicCloudVMServiceCreateRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: - - project_uuid: value is empty, which is not a valid UUID - - name: must be within 2 and 128 characters`, - }, - { - name: "valid", - msg: &pcvmv1.PublicCloudVMServiceCreateRequest{ - ProjectUuid: testUUID, - Name: "my-vm", - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidatePublicCloudVMServiceUpdateRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &pcvmv1.PublicCloudVMServiceUpdateRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: - - uuid: value is empty, which is not a valid UUID - - project_uuid: value is empty, which is not a valid UUID - - update_meta: value is required`, - }, - { - name: "valid", - msg: &pcvmv1.PublicCloudVMServiceUpdateRequest{ - Uuid: testUUID, - ProjectUuid: testUUID, - UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidatePublicCloudVMServiceValidateCreateRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &pcvmv1.PublicCloudVMServiceValidateCreateRequest{}, - wantErr: true, - wantErrorMessage: `validation error: create: value is required`, - }, - { - name: "valid", - msg: &pcvmv1.PublicCloudVMServiceValidateCreateRequest{ - Create: &pcvmv1.PublicCloudVMServiceCreateRequest{ - ProjectUuid: testUUID, - Name: "my-vm", - }, - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} - -func TestValidatePublicCloudVMServiceValidateUpdateRequest(t *testing.T) { - tests := prototests{ - { - name: "empty", - msg: &pcvmv1.PublicCloudVMServiceValidateUpdateRequest{}, - wantErr: true, - wantErrorMessage: `validation error: update: value is required`, - }, - { - name: "valid", - msg: &pcvmv1.PublicCloudVMServiceValidateUpdateRequest{ - Update: &pcvmv1.PublicCloudVMServiceUpdateRequest{ - Uuid: testUUID, - ProjectUuid: testUUID, - UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, - }, - }, - wantErr: false, - }, - } - - validateProtos(t, tests) -} diff --git a/go/tests/validation/project_test.go b/go/tests/validation/project_test.go index 8420987..544d553 100644 --- a/go/tests/validation/project_test.go +++ b/go/tests/validation/project_test.go @@ -7,117 +7,115 @@ import ( "google.golang.org/protobuf/proto" ) -const testUUID = "e266fcc6-f6de-4ee1-ba26-baa17bf47b13" - func TestValidateProject(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "empty", - msg: &v1.Project{}, - wantErr: true, - wantErrorMessage: `validation error: name: must be within 2 and 128 characters`, + Name: "empty", + Msg: &v1.Project{}, + WantErr: true, + WantErrorMessage: `validation error: name: must be within 2 and 128 characters`, }, { - name: "invalid name", - msg: &v1.Project{ + Name: "invalid name", + Msg: &v1.Project{ Name: "a", Description: "a project", }, - wantErr: true, - wantErrorMessage: `validation error: name: must be within 2 and 128 characters`, + WantErr: true, + WantErrorMessage: `validation error: name: must be within 2 and 128 characters`, }, { - name: "valid", - msg: &v1.Project{ + Name: "valid", + Msg: &v1.Project{ Name: "project", Description: "a project", }, - wantErr: false, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateProjectServiceGetRequest(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "empty", - msg: &v1.ProjectServiceGetRequest{}, - wantErr: true, - wantErrorMessage: `validation error: project: value is empty, which is not a valid UUID`, + Name: "empty", + Msg: &v1.ProjectServiceGetRequest{}, + WantErr: true, + WantErrorMessage: `validation error: project: value is empty, which is not a valid UUID`, }, { - name: "valid", - msg: &v1.ProjectServiceGetRequest{Project: testUUID}, - wantErr: false, + Name: "valid", + Msg: &v1.ProjectServiceGetRequest{Project: TestUUID}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateProjectServiceListRequest(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "invalid name", - msg: &v1.ProjectServiceListRequest{Name: proto.String("a")}, - wantErr: true, - wantErrorMessage: `validation error: name: must be within 2 and 128 characters`, + Name: "invalid name", + Msg: &v1.ProjectServiceListRequest{Name: proto.String("a")}, + WantErr: true, + WantErrorMessage: `validation error: name: must be within 2 and 128 characters`, }, { - name: "valid", - msg: &v1.ProjectServiceListRequest{Name: proto.String("project")}, - wantErr: false, + Name: "valid", + Msg: &v1.ProjectServiceListRequest{Name: proto.String("project")}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateProjectServiceCreateRequest(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "empty", - msg: &v1.ProjectServiceCreateRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: + Name: "empty", + Msg: &v1.ProjectServiceCreateRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: - tenant: must be within 2 and 128 characters - name: must be within 2 and 128 characters`, }, { - name: "valid", - msg: &v1.ProjectServiceCreateRequest{ + Name: "valid", + Msg: &v1.ProjectServiceCreateRequest{ Tenant: "tenant", Name: "project", Description: "a project", }, - wantErr: false, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateProjectServiceUpdateRequest(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "empty", - msg: &v1.ProjectServiceUpdateRequest{}, - wantErr: true, - wantErrorMessage: `validation errors: + Name: "empty", + Msg: &v1.ProjectServiceUpdateRequest{}, + WantErr: true, + WantErrorMessage: `validation errors: - project: value is empty, which is not a valid UUID - update_meta: value is required`, }, { - name: "valid", - msg: &v1.ProjectServiceUpdateRequest{ - Project: testUUID, + Name: "valid", + Msg: &v1.ProjectServiceUpdateRequest{ + Project: TestUUID, UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, }, - wantErr: false, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } diff --git a/go/tests/validation/tenant_test.go b/go/tests/validation/tenant_test.go index 32654ad..7a86f08 100644 --- a/go/tests/validation/tenant_test.go +++ b/go/tests/validation/tenant_test.go @@ -8,68 +8,68 @@ import ( ) func TestValidateTenant(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "empty", - msg: &v1.Tenant{}, - wantErr: true, - wantErrorMessage: `validation error: name: must be within 2 and 128 characters`, + Name: "empty", + Msg: &v1.Tenant{}, + WantErr: true, + WantErrorMessage: `validation error: name: must be within 2 and 128 characters`, }, { - name: "valid", - msg: &v1.Tenant{Name: "tenant", Description: "a tenant"}, - wantErr: false, + Name: "valid", + Msg: &v1.Tenant{Name: "tenant", Description: "a tenant"}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateTenantServiceListRequest(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "invalid name", - msg: &v1.TenantServiceListRequest{Name: proto.String("a")}, - wantErr: true, - wantErrorMessage: `validation error: name: must be within 2 and 128 characters`, + Name: "invalid name", + Msg: &v1.TenantServiceListRequest{Name: proto.String("a")}, + WantErr: true, + WantErrorMessage: `validation error: name: must be within 2 and 128 characters`, }, { - name: "valid", - msg: &v1.TenantServiceListRequest{Name: proto.String("tenant")}, - wantErr: false, + Name: "valid", + Msg: &v1.TenantServiceListRequest{Name: proto.String("tenant")}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateTenantServiceUpdateRequest(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "missing update_meta", - msg: &v1.TenantServiceUpdateRequest{}, - wantErr: true, - wantErrorMessage: `validation error: update_meta: value is required`, + Name: "missing update_meta", + Msg: &v1.TenantServiceUpdateRequest{}, + WantErr: true, + WantErrorMessage: `validation error: update_meta: value is required`, }, { - name: "invalid email", - msg: &v1.TenantServiceUpdateRequest{ + Name: "invalid email", + Msg: &v1.TenantServiceUpdateRequest{ Email: proto.String("not-an-email"), }, - wantErr: true, - wantErrorMessage: `validation errors: + WantErr: true, + WantErrorMessage: `validation errors: - update_meta: value is required - email: must be a valid email address`, }, { - name: "valid", - msg: &v1.TenantServiceUpdateRequest{ + Name: "valid", + Msg: &v1.TenantServiceUpdateRequest{ UpdateMeta: &v1.UpdateMeta{LockingStrategy: v1.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER}, Email: proto.String("user@example.com"), }, - wantErr: false, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } diff --git a/go/tests/validation/token_test.go b/go/tests/validation/token_test.go index bbf83a9..b35553a 100644 --- a/go/tests/validation/token_test.go +++ b/go/tests/validation/token_test.go @@ -8,139 +8,139 @@ import ( ) func TestValidateTokenServiceGetRequest(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "empty", - msg: &v1.TokenServiceGetRequest{}, - wantErr: true, - wantErrorMessage: `validation error: uuid: value is empty, which is not a valid UUID`, + Name: "empty", + Msg: &v1.TokenServiceGetRequest{}, + WantErr: true, + WantErrorMessage: `validation error: uuid: value is empty, which is not a valid UUID`, }, { - name: "valid", - msg: &v1.TokenServiceGetRequest{Uuid: testUUID}, - wantErr: false, + Name: "valid", + Msg: &v1.TokenServiceGetRequest{Uuid: TestUUID}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateTokenServiceRevokeRequest(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "empty", - msg: &v1.TokenServiceRevokeRequest{}, - wantErr: true, - wantErrorMessage: `validation error: uuid: value is empty, which is not a valid UUID`, + Name: "empty", + Msg: &v1.TokenServiceRevokeRequest{}, + WantErr: true, + WantErrorMessage: `validation error: uuid: value is empty, which is not a valid UUID`, }, { - name: "valid", - msg: &v1.TokenServiceRevokeRequest{Uuid: testUUID}, - wantErr: false, + Name: "valid", + Msg: &v1.TokenServiceRevokeRequest{Uuid: TestUUID}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateToken(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "user token with permissions", - msg: &v1.Token{ + Name: "user token with permissions", + Msg: &v1.Token{ TokenType: v1.TokenType_TOKEN_TYPE_USER, Permissions: []*v1.PermissionsByVisibility{{}}, }, - wantErr: true, - wantErrorMessage: `validation errors: + WantErr: true, + WantErrorMessage: `validation errors: - token type user must not have permissions - uuid: value is empty, which is not a valid UUID - user: must be at least 2 characters`, }, { - name: "valid user token", - msg: &v1.Token{ - Uuid: testUUID, + Name: "valid user token", + Msg: &v1.Token{ + Uuid: TestUUID, User: "user", TokenType: v1.TokenType_TOKEN_TYPE_USER, }, - wantErr: false, + WantErr: false, }, { - name: "valid api token with permissions", - msg: &v1.Token{ - Uuid: testUUID, + Name: "valid api token with permissions", + Msg: &v1.Token{ + Uuid: TestUUID, User: "user", TokenType: v1.TokenType_TOKEN_TYPE_API, Permissions: []*v1.PermissionsByVisibility{{}}, }, - wantErr: false, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateProjectPermissions(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "invalid project", - msg: &v1.ProjectPermissions{Project: "not-uuid"}, - wantErr: true, - wantErrorMessage: `validation error: project: must be '*' or a uuid`, + Name: "invalid project", + Msg: &v1.ProjectPermissions{Project: "not-uuid"}, + WantErr: true, + WantErrorMessage: `validation error: project: must be '*' or a uuid`, }, { - name: "wildcard", - msg: &v1.ProjectPermissions{Project: "*"}, - wantErr: false, + Name: "wildcard", + Msg: &v1.ProjectPermissions{Project: "*"}, + WantErr: false, }, { - name: "uuid", - msg: &v1.ProjectPermissions{Project: testUUID}, - wantErr: false, + Name: "uuid", + Msg: &v1.ProjectPermissions{Project: TestUUID}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateTenantPermissions(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "invalid login", - msg: &v1.TenantPermissions{Login: "a"}, - wantErr: true, - wantErrorMessage: `validation error: login: must be '*' or within 2 and 128 characters`, + Name: "invalid login", + Msg: &v1.TenantPermissions{Login: "a"}, + WantErr: true, + WantErrorMessage: `validation error: login: must be '*' or within 2 and 128 characters`, }, { - name: "wildcard", - msg: &v1.TenantPermissions{Login: "*"}, - wantErr: false, + Name: "wildcard", + Msg: &v1.TenantPermissions{Login: "*"}, + WantErr: false, }, { - name: "login", - msg: &v1.TenantPermissions{Login: "tenant"}, - wantErr: false, + Name: "login", + Msg: &v1.TenantPermissions{Login: "tenant"}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } func TestValidateMethodPermission(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "subject too long", - msg: &v1.MethodPermission{Subject: strings.Repeat("x", 257)}, - wantErr: true, - wantErrorMessage: `validation error: subject: must be at most 256 characters`, + Name: "subject too long", + Msg: &v1.MethodPermission{Subject: strings.Repeat("x", 257)}, + WantErr: true, + WantErrorMessage: `validation error: subject: must be at most 256 characters`, }, { - name: "valid", - msg: &v1.MethodPermission{Subject: "project", Methods: []string{"List"}}, - wantErr: false, + Name: "valid", + Msg: &v1.MethodPermission{Subject: "project", Methods: []string{"List"}}, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } diff --git a/go/tests/validation/version_test.go b/go/tests/validation/version_test.go index 9cecde0..2c39f7f 100644 --- a/go/tests/validation/version_test.go +++ b/go/tests/validation/version_test.go @@ -8,24 +8,24 @@ import ( ) func TestValidateVersion(t *testing.T) { - tests := prototests{ + tests := Prototests{ { - name: "version too long", - msg: &v1.Version{Version: strings.Repeat("x", 257)}, - wantErr: true, - wantErrorMessage: `validation error: version: must be shorter than 256 characters`, + Name: "version too long", + Msg: &v1.Version{Version: strings.Repeat("x", 257)}, + WantErr: true, + WantErrorMessage: `validation error: version: must be shorter than 256 characters`, }, { - name: "valid", - msg: &v1.Version{ + Name: "valid", + Msg: &v1.Version{ Version: "1.0.0", Revision: "abc123", GitSha1: "deadbeef", BuildDate: "2026-01-01T00:00:00Z", }, - wantErr: false, + WantErr: false, }, } - validateProtos(t, tests) + ValidateProtos(t, tests) } diff --git a/go/tests/validation/vlan_test.go b/go/tests/validation/vlan_test.go deleted file mode 100644 index 10301c1..0000000 --- a/go/tests/validation/vlan_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package validation - -import ( - "testing" - - mvmv1 "github.com/fi-ts/api/go/fits/api/mvm/v1" -) - -func TestValidateVLAN(t *testing.T) { - vlan := func(id int32) *mvmv1.VLAN { - return &mvmv1.VLAN{ - Uuid: testUUID, - VlanId: id, - SubnetTitle: "pod01-vlan2001 (Production)", - SubnetCidr: "10.0.0.0/24", - LocationUuid: testUUID, - Tenant: "tenant", - } - } - - const msgVlanID = "validation error: vlan_id: must be greater than or equal to 1 and less than or equal to 4094" - - tests := prototests{ - { - name: "min vlan id", - msg: vlan(1), - wantErr: false, - }, - { - name: "max vlan id", - msg: vlan(4094), - wantErr: false, - }, - { - name: "vlan id zero", - msg: vlan(0), - wantErr: true, - wantErrorMessage: msgVlanID, - }, - { - name: "vlan id too large", - msg: vlan(4095), - wantErr: true, - wantErrorMessage: msgVlanID, - }, - } - - validateProtos(t, tests) -}