// 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 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, }}, }, } 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) } }
func (t *Tester) TestCreateHasMetadata(valid runtime.Object) { objectMeta, err := api.ObjectMetaFor(valid) if err != nil { t.Fatalf("object does not have ObjectMeta: %v\n%#v", err, 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") } }
// TODO: remove, this is covered by RESTTest.TestCreate func TestCreatePod(t *testing.T) { _, helper := newHelper(t) storage := NewStorage(helper, 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 := helper.ExtractObj(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) } }