From 3ac1c4366f9198b76ff29ebe8d96cdb15dddded4 Mon Sep 17 00:00:00 2001 From: kerthcet Date: Sat, 8 Oct 2022 13:03:21 +0800 Subject: [PATCH] Optimize testcases arrangement Signed-off-by: kerthcet --- pkg/registry/core/pod/strategy_test.go | 37 +++++++++++--------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/pkg/registry/core/pod/strategy_test.go b/pkg/registry/core/pod/strategy_test.go index a6e4a530eb7..5cf45abbd86 100644 --- a/pkg/registry/core/pod/strategy_test.go +++ b/pkg/registry/core/pod/strategy_test.go @@ -1066,13 +1066,16 @@ func newPodWithHugePageValue(resourceName api.ResourceName, value resource.Quant func TestPodStrategyValidate(t *testing.T) { const containerName = "container" - errTest := []struct { - name string - pod *api.Pod + + tests := []struct { + name string + pod *api.Pod + wantErr bool }{ { - name: "a new pod setting container with indivisible hugepages values", - pod: newPodWithHugePageValue(api.ResourceHugePagesPrefix+"1Mi", resource.MustParse("1.1Mi")), + name: "a new pod setting container with indivisible hugepages values", + pod: newPodWithHugePageValue(api.ResourceHugePagesPrefix+"1Mi", resource.MustParse("1.1Mi")), + wantErr: true, }, { name: "a new pod setting init-container with indivisible hugepages values", @@ -1100,6 +1103,7 @@ func TestPodStrategyValidate(t *testing.T) { }, }, }, + wantErr: true, }, { name: "a new pod setting init-container with indivisible hugepages values while container with divisible hugepages values", @@ -1141,21 +1145,8 @@ func TestPodStrategyValidate(t *testing.T) { }, }, }, + wantErr: true, }, - } - - for _, tc := range errTest { - t.Run(tc.name, func(t *testing.T) { - if errs := Strategy.Validate(genericapirequest.NewContext(), tc.pod); len(errs) == 0 { - t.Error("expected failure") - } - }) - } - - tests := []struct { - name string - pod *api.Pod - }{ { name: "a new pod setting container with divisible hugepages values", pod: &api.Pod{ @@ -1189,8 +1180,12 @@ func TestPodStrategyValidate(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - if errs := Strategy.Validate(genericapirequest.NewContext(), tc.pod); len(errs) != 0 { - t.Errorf("unexpected error:%v", errs) + errs := Strategy.Validate(genericapirequest.NewContext(), tc.pod) + if tc.wantErr && len(errs) == 0 { + t.Errorf("expected errors but got none") + } + if !tc.wantErr && len(errs) != 0 { + t.Errorf("unexpected errors: %v", errs.ToAggregate()) } }) }