// TestHasObjectMetaSystemFieldValues validates that true is returned if and only if all fields are populated func TestHasObjectMetaSystemFieldValues(t *testing.T) { ctx := api.NewDefaultContext() resource := api.ObjectMeta{} if api.HasObjectMetaSystemFieldValues(&resource) { t.Errorf("the resource does not have all fields yet populated, but incorrectly reports it does") } api.FillObjectMetaSystemFields(ctx, &resource) if !api.HasObjectMetaSystemFieldValues(&resource) { t.Errorf("the resource does have all fields populated, but incorrectly reports it does not") } }
func hasCreated(t *testing.T, pod *api.Pod) func(runtime.Object) bool { return func(obj runtime.Object) bool { actualPod := obj.(*api.Pod) if !api.Semantic.DeepDerivative(pod.Status, actualPod.Status) { t.Errorf("not a deep derivative %#v", actualPod) return false } return api.HasObjectMetaSystemFieldValues(&actualPod.ObjectMeta) } }
func (t *Tester) testCreateHasMetadata(valid runtime.Object) { objectMeta := t.getObjectMetaOrFail(valid) objectMeta.Name = "" objectMeta.GenerateName = "test-" objectMeta.Namespace = t.TestNamespace() obj, err := t.storage.(rest.Creater).Create(t.TestContext(), valid) if err != nil { t.Fatalf("Unexpected error: %v", err) } if obj == nil { t.Fatalf("Unexpected object from result: %#v", obj) } if !api.HasObjectMetaSystemFieldValues(objectMeta) { t.Errorf("storage did not populate object meta field values") } }
func TestRESTCreate(t *testing.T) { table := []struct { ctx api.Context event *api.Event valid bool }{ { ctx: api.NewDefaultContext(), event: testEvent("foo"), valid: true, }, { ctx: api.NewContext(), event: testEvent("bar"), valid: true, }, { ctx: api.WithNamespace(api.NewContext(), "nondefault"), event: testEvent("bazzzz"), valid: false, }, } for _, item := range table { _, storage := NewTestREST() c, err := storage.Create(item.ctx, item.event) if !item.valid { if err == nil { ctxNS := api.NamespaceValue(item.ctx) t.Errorf("unexpected non-error for %v (%v, %v)", item.event.Name, ctxNS, item.event.Namespace) } continue } if err != nil { t.Errorf("%v: Unexpected error %v", item.event.Name, err) continue } if !api.HasObjectMetaSystemFieldValues(&item.event.ObjectMeta) { t.Errorf("storage did not populate object meta field values") } if e, a := item.event, c; !reflect.DeepEqual(e, a) { t.Errorf("diff: %s", util.ObjectDiff(e, a)) } // Ensure we implement the interface _ = rest.Watcher(storage) } }
func TestServiceRegistryCreate(t *testing.T) { storage, registry := NewTestREST(t, nil) svc := &api.Service{ ObjectMeta: api.ObjectMeta{Name: "foo"}, Spec: api.ServiceSpec{ Selector: map[string]string{"bar": "baz"}, SessionAffinity: api.ServiceAffinityNone, Type: api.ServiceTypeClusterIP, Ports: []api.ServicePort{{ Port: 6502, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(6502), }}, }, } ctx := api.NewDefaultContext() created_svc, err := storage.Create(ctx, svc) if err != nil { t.Fatalf("Unexpected error: %v", err) } created_service := created_svc.(*api.Service) if !api.HasObjectMetaSystemFieldValues(&created_service.ObjectMeta) { t.Errorf("storage did not populate object meta field values") } if created_service.Name != "foo" { t.Errorf("Expected foo, but got %v", created_service.Name) } if created_service.CreationTimestamp.IsZero() { t.Errorf("Expected timestamp to be set, got: %v", created_service.CreationTimestamp) } if !makeIPNet(t).Contains(net.ParseIP(created_service.Spec.ClusterIP)) { t.Errorf("Unexpected ClusterIP: %s", created_service.Spec.ClusterIP) } srv, err := registry.GetService(ctx, svc.Name) if err != nil { t.Errorf("unexpected error: %v", err) } if srv == nil { t.Errorf("Failed to find service: %s", svc.Name) } }
// TODO: remove, this is covered by RESTTest.TestCreate func TestCreatePod(t *testing.T) { _, etcdStorage := newEtcdStorage(t) storage := NewStorage(etcdStorage, nil).Pod ctx := api.NewDefaultContext() key, _ := storage.Etcd.KeyFunc(ctx, "foo") pod := validNewPod() obj, err := storage.Create(api.NewDefaultContext(), pod) if err != nil { t.Fatalf("unexpected error: %v", err) } if obj == nil { t.Fatalf("unexpected object: %#v", obj) } actual := &api.Pod{} if err := etcdStorage.Get(key, actual, false); err != nil { t.Fatalf("unexpected extraction error: %v", err) } if !api.HasObjectMetaSystemFieldValues(&actual.ObjectMeta) { t.Errorf("Expected ObjectMeta field values were populated: %#v", actual) } }
func TestServiceRegistryCreateMultiNodePortsService(t *testing.T) { storage, registry := NewTestREST(t, nil) testCases := []struct { svc *api.Service name string expectNodePorts []int }{ { svc: &api.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo1"}, Spec: api.ServiceSpec{ Selector: map[string]string{"bar": "baz"}, SessionAffinity: api.ServiceAffinityNone, Type: api.ServiceTypeNodePort, Ports: []api.ServicePort{ { Name: "port-tcp", Port: 53, NodePort: 30053, TargetPort: intstr.FromInt(6503), Protocol: api.ProtocolTCP, }, { Name: "port-udp", Port: 53, NodePort: 30053, TargetPort: intstr.FromInt(6503), Protocol: api.ProtocolUDP, }, }, }, }, name: "foo1", expectNodePorts: []int{30053, 30053}, }, { svc: &api.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo2"}, Spec: api.ServiceSpec{ Selector: map[string]string{"bar": "baz"}, SessionAffinity: api.ServiceAffinityNone, Type: api.ServiceTypeNodePort, Ports: []api.ServicePort{ { Name: "port-tcp", Port: 54, TargetPort: intstr.FromInt(6504), Protocol: api.ProtocolTCP, }, { Name: "port-udp", Port: 54, NodePort: 30054, TargetPort: intstr.FromInt(6504), Protocol: api.ProtocolUDP, }, }, }, }, name: "foo2", expectNodePorts: []int{30054, 30054}, }, { svc: &api.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo3"}, Spec: api.ServiceSpec{ Selector: map[string]string{"bar": "baz"}, SessionAffinity: api.ServiceAffinityNone, Type: api.ServiceTypeNodePort, Ports: []api.ServicePort{ { Name: "port-tcp", Port: 55, NodePort: 30055, TargetPort: intstr.FromInt(6505), Protocol: api.ProtocolTCP, }, { Name: "port-udp", Port: 55, NodePort: 30056, TargetPort: intstr.FromInt(6506), Protocol: api.ProtocolUDP, }, }, }, }, name: "foo3", expectNodePorts: []int{30055, 30056}, }, } ctx := genericapirequest.NewDefaultContext() for _, test := range testCases { created_svc, err := storage.Create(ctx, test.svc) if err != nil { t.Fatalf("Unexpected error: %v", err) } created_service := created_svc.(*api.Service) if !api.HasObjectMetaSystemFieldValues(&created_service.ObjectMeta) { t.Errorf("storage did not populate object meta field values") } if created_service.Name != test.name { t.Errorf("Expected %s, but got %s", test.name, created_service.Name) } serviceNodePorts := CollectServiceNodePorts(created_service) if !reflect.DeepEqual(serviceNodePorts, test.expectNodePorts) { t.Errorf("Expected %v, but got %v", test.expectNodePorts, serviceNodePorts) } srv, err := registry.GetService(ctx, test.name, &metav1.GetOptions{}) if err != nil { t.Errorf("unexpected error: %v", err) } if srv == nil { t.Errorf("Failed to find service: %s", test.name) } } }