From 04970bca5cd65e98dcbf086bfac6dda9fd8389c4 Mon Sep 17 00:00:00 2001 From: Anish Ramasekar Date: Thu, 27 Jan 2022 19:38:46 +0000 Subject: [PATCH] test: use multiple test functions Signed-off-by: Anish Ramasekar --- pkg/k8s/token.go | 2 +- test/e2eprovider/server/server_test.go | 36 +++++++++----------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/pkg/k8s/token.go b/pkg/k8s/token.go index 22cd93f9f..15decf8fe 100644 --- a/pkg/k8s/token.go +++ b/pkg/k8s/token.go @@ -127,7 +127,7 @@ func (c *TokenClient) PodServiceAccountTokenAttrs(namespace, podName, serviceAcc outputs[audience] = tr.Status } - klog.V(4).InfoS("Fetched service account token attrs for CSIDriver", "driver", c.driverName) + klog.V(4).InfoS("Fetched service account token attrs for CSIDriver", "driver", c.driverName, "podUID", podUID) tokens, err := json.Marshal(outputs) if err != nil { return nil, err diff --git a/test/e2eprovider/server/server_test.go b/test/e2eprovider/server/server_test.go index 7cfd136cb..5022674fc 100644 --- a/test/e2eprovider/server/server_test.go +++ b/test/e2eprovider/server/server_test.go @@ -215,31 +215,19 @@ func TestRotation(t *testing.T) { } func TestValidateTokens(t *testing.T) { - tests := []struct { - name string - tokens string - audiences string - wantErr bool - }{ - { - name: "no tokens", - tokens: "", - audiences: "aud1,aud2", - wantErr: true, - }, - { - name: "matching tokens for audience", - tokens: `{"aud1":{"token":"eyJhbGciOiJSUzI1NiIsImtpZCI6InRhVDBxbzhQVEZ1ajB1S3BYUUxIclRsR01XakxjemJNOTlzWVMxSlNwbWcifQ.eyJhdWQiOlsiYXBpOi8vQXp1cmVBRGlUb2tlbkV4Y2hhbmdlIl0sImV4cCI6MTY0MzIzNDY0NywiaWF0IjoxNjQzMjMxMDQ3LCJpc3MiOiJodHRwczovL2t1YmVybmV0ZXMuZGVmYXVsdC5zdmMuY2x1c3Rlci5sb2NhbCIsImt1YmVybmV0ZXMuaW8iOnsibmFtZXNwYWNlIjoidGVzdC12MWFscGhhM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kIjoiMjViNGY1NzgtM2U4MC00NTczLWJlOGQtZTdmNDA5ZDI0MmI2In19LCJuYmYiOjE2NDMyMzEwNDcsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDp0ZXN0LXYxYWxwaGExOmRlZmF1bHQifQ.BT0YGI7bGdSNaIBqIEnVL0Ky5t-fynaemSGxjGdKOPl0E22UIVGDpAMUhaS19i20c-Dqs-Kn0N-R5QyDNpZg8vOL5KIFqu2kSYNbKxtQW7TPYIsV0d9wUZjLSr54DKrmyXNMGRoT2bwcF4yyfmO46eMmZSaXN8Y4lgapeabg6CBVVQYHD-GrgXf9jVLeJfCQkTuojK1iXOphyD6NqlGtVCaY1jWxbBMibN0q214vKvQboub8YMuvclGdzn_l_ZQSTjvhBj9I-W1t-JArVjqHoIb8_FlR9BSgzgL7V3Jki55vmiOdEYqMErJWrIZPP3s8qkU5hhO9rSVEd3LJHponvQ","expirationTimestamp":"2022-01-26T22:04:07Z"}}` //nolint + audiences := "aud1" + + if err := validateTokens(audiences, tokens); err != nil { + t.Errorf("validateTokens() error = %v, wantErr nil", err) } +} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := validateTokens(tt.audiences, tt.tokens); (err != nil) != tt.wantErr { - t.Errorf("validateTokens() error = %v, wantErr %v", err, tt.wantErr) - } - }) +func TestValidateTokensError(t *testing.T) { + tokens := "" + audiences := "aud1,aud2" + + if err := validateTokens(audiences, tokens); err == nil { + t.Errorf("validateTokens() error is nil, want error") } }