Skip to content

Commit

Permalink
Refactor tests to split ObjectMeta from TypeMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Oct 24, 2014
1 parent 7550c14 commit 644eb70
Show file tree
Hide file tree
Showing 55 changed files with 733 additions and 627 deletions.
27 changes: 17 additions & 10 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,16 @@ func runAtomicPutTest(c *client.Client) {
var svc api.Service
err := c.Post().Path("services").Body(
&api.Service{
TypeMeta: api.TypeMeta{Name: "atomicservice", APIVersion: latest.Version},
Port: 12345,
Labels: map[string]string{
"name": "atomicService",
TypeMeta: api.TypeMeta{
APIVersion: latest.Version,
},
ObjectMeta: api.ObjectMeta{
Name: "atomicservice",
Labels: map[string]string{
"name": "atomicService",
},
},
Port: 12345,
// This is here because validation requires it.
Selector: map[string]string{
"foo": "bar",
Expand Down Expand Up @@ -330,7 +335,12 @@ func runAtomicPutTest(c *client.Client) {
func runServiceTest(client *client.Client) {
ctx := api.NewDefaultContext()
pod := api.Pod{
TypeMeta: api.TypeMeta{Name: "foo"},
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"name": "thisisalonglabel",
},
},
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta1",
Expand All @@ -348,9 +358,6 @@ func runServiceTest(client *client.Client) {
CurrentState: api.PodState{
PodIP: "1.2.3.4",
},
Labels: map[string]string{
"name": "thisisalonglabel",
},
}
_, err := client.CreatePod(ctx, &pod)
if err != nil {
Expand All @@ -360,7 +367,7 @@ func runServiceTest(client *client.Client) {
glog.Fatalf("FAILED: pod never started running %v", err)
}
svc1 := api.Service{
TypeMeta: api.TypeMeta{Name: "service1"},
ObjectMeta: api.ObjectMeta{Name: "service1"},
Selector: map[string]string{
"name": "thisisalonglabel",
},
Expand All @@ -375,7 +382,7 @@ func runServiceTest(client *client.Client) {
}
// A second service with the same port.
svc2 := api.Service{
TypeMeta: api.TypeMeta{Name: "service2"},
ObjectMeta: api.ObjectMeta{Name: "service2"},
Selector: map[string]string{
"name": "thisisalonglabel",
},
Expand Down
4 changes: 2 additions & 2 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func validateObject(obj runtime.Object) (errors []error) {
errors = append(errors, validateObject(&t.Items[i])...)
}
case *api.Service:
api.ValidNamespace(ctx, &t.TypeMeta)
api.ValidNamespace(ctx, &t.ObjectMeta)
errors = validation.ValidateService(t)
case *api.ServiceList:
for i := range t.Items {
errors = append(errors, validateObject(&t.Items[i])...)
}
case *api.Pod:
api.ValidNamespace(ctx, &t.TypeMeta)
api.ValidNamespace(ctx, &t.ObjectMeta)
errors = validation.ValidateManifest(&t.DesiredState.Manifest)
case *api.PodList:
for i := range t.Items {
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ func TestValidNamespace(t *testing.T) {
ctx := api.NewDefaultContext()
namespace, _ := api.NamespaceFrom(ctx)
resource := api.ReplicationController{}
if !api.ValidNamespace(ctx, &resource.TypeMeta) {
if !api.ValidNamespace(ctx, &resource.ObjectMeta) {
t.Errorf("expected success")
}
if namespace != resource.Namespace {
t.Errorf("expected resource to have the default namespace assigned during validation")
}
resource = api.ReplicationController{TypeMeta: api.TypeMeta{Namespace: "other"}}
if api.ValidNamespace(ctx, &resource.TypeMeta) {
resource = api.ReplicationController{ObjectMeta: api.ObjectMeta{Namespace: "other"}}
if api.ValidNamespace(ctx, &resource.ObjectMeta) {
t.Errorf("Expected error that resource and context errors do not match because resource has different namespace")
}
ctx = api.NewContext()
if api.ValidNamespace(ctx, &resource.TypeMeta) {
if api.ValidNamespace(ctx, &resource.ObjectMeta) {
t.Errorf("Expected error that resource and context errors do not match since context has no namespace")
}

Expand Down
20 changes: 19 additions & 1 deletion pkg/api/latest/latest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
// APIVersion and Kind must remain blank in memory.
j.APIVersion = ""
j.Kind = ""
},
func(j *internal.ObjectMeta, c fuzz.Continue) {
j.Name = c.RandString()
// TODO: Fix JSON/YAML packages and/or write custom encoding
// for uint64's. Somehow the LS *byte* of this is lost, but
Expand All @@ -49,6 +51,13 @@ var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
c.Fuzz(&nsec)
j.CreationTimestamp = util.Unix(sec, nsec).Rfc3339Copy()
},
func(j *internal.ListMeta, c fuzz.Continue) {
// TODO: Fix JSON/YAML packages and/or write custom encoding
// for uint64's. Somehow the LS *byte* of this is lost, but
// only when all 8 bytes are set.
j.ResourceVersion = strconv.FormatUint(c.RandUint64()>>8, 10)
j.SelfLink = c.RandString()
},
func(j *internal.ObjectReference, c fuzz.Continue) {
// We have to customize the randomization of TypeMetas because their
// APIVersion and Kind must remain blank in memory.
Expand Down Expand Up @@ -133,14 +142,23 @@ func TestInternalRoundTrip(t *testing.T) {
}

func TestResourceVersioner(t *testing.T) {
pod := internal.Pod{TypeMeta: internal.TypeMeta{ResourceVersion: "10"}}
pod := internal.Pod{ObjectMeta: internal.ObjectMeta{ResourceVersion: "10"}}
version, err := ResourceVersioner.ResourceVersion(&pod)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if version != "10" {
t.Errorf("unexpected version %v", version)
}

podList := internal.PodList{ListMeta: internal.ListMeta{ResourceVersion: "10"}}
version, err = ResourceVersioner.ResourceVersion(&podList)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if version != "10" {
t.Errorf("unexpected version %v", version)
}
}

func TestCodec(t *testing.T) {
Expand Down
11 changes: 3 additions & 8 deletions pkg/api/ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestGetReference(t *testing.T) {
}{
"pod": {
obj: &Pod{
TypeMeta: TypeMeta{
ObjectMeta: ObjectMeta{
Name: "foo",
UID: "bar",
ResourceVersion: "42",
Expand All @@ -52,25 +52,20 @@ func TestGetReference(t *testing.T) {
},
"serviceList": {
obj: &ServiceList{
TypeMeta: TypeMeta{
Name: "foo",
UID: "bar",
ListMeta: ListMeta{
ResourceVersion: "42",
SelfLink: "/api/v1beta2/services",
},
},
ref: &ObjectReference{
Kind: "ServiceList",
APIVersion: "v1beta2",
Name: "foo",
UID: "bar",
ResourceVersion: "42",
},
},
"badSelfLink": {
obj: &ServiceList{
TypeMeta: TypeMeta{
Name: "foo",
ListMeta: ListMeta{
ResourceVersion: "42",
SelfLink: "v1beta2/services",
},
Expand Down
25 changes: 24 additions & 1 deletion pkg/api/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,26 @@ var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
// APIVersion and Kind must remain blank in memory.
j.APIVersion = ""
j.Kind = ""

j.Name = c.RandString()
// TODO: Fix JSON/YAML packages and/or write custom encoding
// for uint64's. Somehow the LS *byte* of this is lost, but
// only when all 8 bytes are set.
j.ResourceVersion = strconv.FormatUint(c.RandUint64()>>8, 10)
j.SelfLink = c.RandString()

var sec, nsec int64
c.Fuzz(&sec)
c.Fuzz(&nsec)
j.CreationTimestamp = util.Unix(sec, nsec).Rfc3339Copy()
},
func(j *api.TypeMeta, c fuzz.Continue) {
// We have to customize the randomization of TypeMetas because their
// APIVersion and Kind must remain blank in memory.
j.APIVersion = ""
j.Kind = ""
},
func(j *api.ObjectMeta, c fuzz.Continue) {
j.Name = c.RandString()
// TODO: Fix JSON/YAML packages and/or write custom encoding
// for uint64's. Somehow the LS *byte* of this is lost, but
Expand All @@ -65,6 +79,13 @@ var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
c.Fuzz(&nsec)
j.CreationTimestamp = util.Unix(sec, nsec).Rfc3339Copy()
},
func(j *api.ListMeta, c fuzz.Continue) {
// TODO: Fix JSON/YAML packages and/or write custom encoding
// for uint64's. Somehow the LS *byte* of this is lost, but
// only when all 8 bytes are set.
j.ResourceVersion = strconv.FormatUint(c.RandUint64()>>8, 10)
j.SelfLink = c.RandString()
},
func(intstr *util.IntOrString, c fuzz.Continue) {
// util.IntOrString will panic if its kind is set wrong.
if c.RandBool() {
Expand Down Expand Up @@ -173,7 +194,9 @@ func TestTypes(t *testing.T) {

func TestEncode_Ptr(t *testing.T) {
pod := &api.Pod{
Labels: map[string]string{"name": "foo"},
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"name": "foo"},
},
}
obj := runtime.Object(pod)
data, err := latest.Codec.Encode(obj)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1beta1/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestMinionListConversionToNew(t *testing.T) {
return v1beta1.Minion{TypeMeta: v1beta1.TypeMeta{ID: id}}
}
newMinion := func(id string) newer.Minion {
return newer.Minion{TypeMeta: newer.TypeMeta{Name: id}}
return newer.Minion{ObjectMeta: newer.ObjectMeta{Name: id}}
}
oldMinions := []v1beta1.Minion{
oldMinion("foo"),
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestMinionListConversionToOld(t *testing.T) {
return v1beta1.Minion{TypeMeta: v1beta1.TypeMeta{ID: id}}
}
newMinion := func(id string) newer.Minion {
return newer.Minion{TypeMeta: newer.TypeMeta{Name: id}}
return newer.Minion{ObjectMeta: newer.ObjectMeta{Name: id}}
}
oldMinions := []v1beta1.Minion{
oldMinion("foo"),
Expand Down
Loading

0 comments on commit 644eb70

Please sign in to comment.