...
1
16
17 package ipaddress
18
19 import (
20 "testing"
21
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
24 "k8s.io/kubernetes/pkg/apis/networking"
25 )
26
27 func newIPAddress() networking.IPAddress {
28 return networking.IPAddress{
29 ObjectMeta: metav1.ObjectMeta{
30 Name: "192.168.1.1",
31 ResourceVersion: "1",
32 },
33 Spec: networking.IPAddressSpec{
34 ParentRef: &networking.ParentReference{
35 Group: "",
36 Resource: "services",
37 Name: "foo",
38 Namespace: "bar",
39 },
40 },
41 }
42 }
43
44 func TestIPAddressStrategy(t *testing.T) {
45 ctx := genericapirequest.NewDefaultContext()
46 if Strategy.NamespaceScoped() {
47 t.Errorf("ipAddress must not be namespace scoped")
48 }
49 if Strategy.AllowCreateOnUpdate() {
50 t.Errorf("ipAddress should not allow create on update")
51 }
52
53 ipAddress := newIPAddress()
54 Strategy.PrepareForCreate(ctx, &ipAddress)
55
56 errs := Strategy.Validate(ctx, &ipAddress)
57 if len(errs) != 0 {
58 t.Errorf("Unexpected error from validation for ipAddress: %v", errs)
59 }
60
61 newIPAddress := ipAddress.DeepCopy()
62 Strategy.PrepareForUpdate(ctx, newIPAddress, &ipAddress)
63 errs = Strategy.ValidateUpdate(ctx, newIPAddress, &ipAddress)
64 if len(errs) != 0 {
65 t.Errorf("Unexpected error from update validation for ipAddress: %v", errs)
66 }
67
68 invalidIPAddress := newIPAddress.DeepCopy()
69 invalidIPAddress.Name = "invalid/name"
70 invalidIPAddress.ResourceVersion = "4"
71 errs = Strategy.Validate(ctx, invalidIPAddress)
72 if len(errs) == 0 {
73 t.Errorf("Expected error from validation for ipAddress, got none")
74 }
75 errs = Strategy.ValidateUpdate(ctx, invalidIPAddress, &ipAddress)
76 if len(errs) == 0 {
77 t.Errorf("Expected error from update validation for ipAddress, got none")
78 }
79 if invalidIPAddress.ResourceVersion != "4" {
80 t.Errorf("Incoming resource version on update should not be mutated")
81 }
82 }
83
View as plain text