test: Exit early during resource helper test

The test will panic if it fails, and should instead check and exit
when invalid conditions are hit.
This commit is contained in:
Clayton Coleman 2019-11-03 15:20:53 -05:00
parent 8a9b8c87c4
commit b453106777
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3

View File

@ -592,7 +592,7 @@ func TestHelperReplace(t *testing.T) {
Req: expectPut, Req: expectPut,
}, },
} }
for i, tt := range tests { for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) { t.Run(tt.Name, func(t *testing.T) {
client := &fake.RESTClient{ client := &fake.RESTClient{
GroupVersion: corev1GV, GroupVersion: corev1GV,
@ -607,24 +607,24 @@ func TestHelperReplace(t *testing.T) {
} }
_, err := modifier.Replace(tt.Namespace, "foo", tt.Overwrite, tt.Object) _, err := modifier.Replace(tt.Namespace, "foo", tt.Overwrite, tt.Object)
if (err != nil) != tt.Err { if (err != nil) != tt.Err {
t.Errorf("%d: unexpected error: %t %v", i, tt.Err, err) t.Fatalf("unexpected error: %t %v", tt.Err, err)
} }
if err != nil { if err != nil {
return return
} }
if tt.Req != nil && !tt.Req(tt.ExpectPath, client.Req) { if tt.Req != nil && (client.Req == nil || !tt.Req(tt.ExpectPath, client.Req)) {
t.Errorf("%d: unexpected request: %#v", i, client.Req) t.Fatalf("unexpected request: %#v", client.Req)
} }
body, err := ioutil.ReadAll(client.Req.Body) body, err := ioutil.ReadAll(client.Req.Body)
if err != nil { if err != nil {
t.Fatalf("%d: unexpected error: %#v", i, err) t.Fatalf("unexpected error: %#v", err)
} }
expect := []byte{} expect := []byte{}
if tt.ExpectObject != nil { if tt.ExpectObject != nil {
expect = []byte(runtime.EncodeOrDie(corev1Codec, tt.ExpectObject)) expect = []byte(runtime.EncodeOrDie(corev1Codec, tt.ExpectObject))
} }
if !reflect.DeepEqual(expect, body) { if !reflect.DeepEqual(expect, body) {
t.Errorf("%d: unexpected body: %s", i, string(body)) t.Fatalf("unexpected body: %s", string(body))
} }
}) })
} }