|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/go-jose/go-jose/v4/jwt" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/mock" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + authorizationv1 "k8s.io/api/authorization/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + "k8s.io/client-go/kubernetes/fake" |
| 16 | + ktesting "k8s.io/client-go/testing" |
| 17 | + |
| 18 | + "github.com/argoproj/argo-workflows/v4/persist/sqldb" |
| 19 | + "github.com/argoproj/argo-workflows/v4/persist/sqldb/mocks" |
| 20 | + workflowpkg "github.com/argoproj/argo-workflows/v4/pkg/apiclient/workflow" |
| 21 | + "github.com/argoproj/argo-workflows/v4/pkg/apis/workflow/v1alpha1" |
| 22 | + v1alpha "github.com/argoproj/argo-workflows/v4/pkg/client/clientset/versioned/fake" |
| 23 | + "github.com/argoproj/argo-workflows/v4/server/auth" |
| 24 | + authtypes "github.com/argoproj/argo-workflows/v4/server/auth/types" |
| 25 | + "github.com/argoproj/argo-workflows/v4/server/clusterworkflowtemplate" |
| 26 | + "github.com/argoproj/argo-workflows/v4/server/workflow/store" |
| 27 | + "github.com/argoproj/argo-workflows/v4/server/workflowtemplate" |
| 28 | + "github.com/argoproj/argo-workflows/v4/util/instanceid" |
| 29 | + "github.com/argoproj/argo-workflows/v4/util/logging" |
| 30 | +) |
| 31 | + |
| 32 | +// offloadedWorkflow builds a workflow whose node status lives in the offload table. |
| 33 | +func offloadedWorkflow(name, uid, version string) v1alpha1.Workflow { |
| 34 | + return v1alpha1.Workflow{ |
| 35 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "argo", UID: types.UID(uid)}, |
| 36 | + Status: v1alpha1.WorkflowStatus{OffloadNodeStatusVersion: version}, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// offloadTestServer builds the smallest server that can serve ListWorkflows, and hands back |
| 41 | +// the offload mock so tests can assert on how it was called. It deliberately does not reuse |
| 42 | +// getWorkflowServer, which is shared by many other tests and does not expose the mock. |
| 43 | +func offloadTestServer(t *testing.T, wfs ...v1alpha1.Workflow) (Server, context.Context, *mocks.OffloadNodeStatusRepo) { |
| 44 | + t.Helper() |
| 45 | + |
| 46 | + offloadNodeStatusRepo := &mocks.OffloadNodeStatusRepo{} |
| 47 | + offloadNodeStatusRepo.On("IsEnabled", mock.Anything).Return(true) |
| 48 | + |
| 49 | + archivedRepo := &mocks.WorkflowArchive{} |
| 50 | + archivedRepo.On("CountWorkflows", mock.Anything, mock.Anything).Return(int64(0), nil) |
| 51 | + archivedRepo.On("ListWorkflows", mock.Anything, mock.Anything).Return(v1alpha1.Workflows{}, nil) |
| 52 | + archivedRepo.On("HasMoreWorkflows", mock.Anything, mock.Anything).Return(false, nil) |
| 53 | + |
| 54 | + kubeClientSet := fake.NewClientset() |
| 55 | + kubeClientSet.PrependReactor("create", "selfsubjectaccessreviews", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) { |
| 56 | + return true, &authorizationv1.SelfSubjectAccessReview{ |
| 57 | + Status: authorizationv1.SubjectAccessReviewStatus{Allowed: true}, |
| 58 | + }, nil |
| 59 | + }) |
| 60 | + |
| 61 | + wfClientset := v1alpha.NewClientset() |
| 62 | + ctx := logging.TestContext(t.Context()) |
| 63 | + ctx = context.WithValue(context.WithValue(context.WithValue(ctx, auth.WfKey, wfClientset), auth.KubeKey, kubeClientSet), auth.ClaimsKey, &authtypes.Claims{Claims: jwt.Claims{Subject: "my-sub"}}) |
| 64 | + |
| 65 | + // An empty instance ID keeps the store from requiring an instance-id label on the fixtures. |
| 66 | + instanceIDSvc := instanceid.NewService("") |
| 67 | + wfStore, err := store.NewSQLiteStore(instanceIDSvc) |
| 68 | + require.NoError(t, err) |
| 69 | + for i := range wfs { |
| 70 | + require.NoError(t, wfStore.Add(&wfs[i])) |
| 71 | + } |
| 72 | + |
| 73 | + namespace := "argo" |
| 74 | + server := NewServer(ctx, instanceIDSvc, offloadNodeStatusRepo, archivedRepo, wfClientset, wfStore, nil, workflowtemplate.NewClientStore(), clusterworkflowtemplate.NewClientStore(), nil, &namespace, nil) |
| 75 | + return server, ctx, offloadNodeStatusRepo |
| 76 | +} |
| 77 | + |
| 78 | +// TestListWorkflows_PassesOnlyPageKeys asserts that the offload query is scoped to the |
| 79 | +// workflows on this page, rather than to the whole namespace. |
| 80 | +func TestListWorkflows_PassesOnlyPageKeys(t *testing.T) { |
| 81 | + wantedA := offloadedWorkflow("offloaded-a", "uid-a", "v1") |
| 82 | + wantedB := offloadedWorkflow("offloaded-b", "uid-b", "v2") |
| 83 | + server, ctx, offloadNodeStatusRepo := offloadTestServer(t, wantedA, wantedB, offloadedWorkflow("inline", "uid-c", "")) |
| 84 | + |
| 85 | + offloadNodeStatusRepo.On("List", mock.Anything, mock.Anything).Return(map[sqldb.UUIDVersion]v1alpha1.Nodes{}, nil) |
| 86 | + |
| 87 | + _, err := server.ListWorkflows(ctx, &workflowpkg.WorkflowListRequest{Namespace: "argo"}) |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + offloadNodeStatusRepo.AssertNumberOfCalls(t, "List", 1) |
| 91 | + call := offloadNodeStatusRepo.Calls[len(offloadNodeStatusRepo.Calls)-1] |
| 92 | + require.Equal(t, "List", call.Method) |
| 93 | + assert.Equal(t, "argo", call.Arguments[0]) |
| 94 | + assert.ElementsMatch(t, []sqldb.UUIDVersion{ |
| 95 | + {UID: "uid-a", Version: "v1"}, |
| 96 | + {UID: "uid-b", Version: "v2"}, |
| 97 | + }, call.Arguments[1], "List must be given exactly the offloaded keys on this page") |
| 98 | +} |
| 99 | + |
| 100 | +// TestListWorkflows_SkipsQueryWhenPageHasNoOffload asserts that a page with nothing offloaded |
| 101 | +// issues no offload query at all. This is the common case for most users. |
| 102 | +func TestListWorkflows_SkipsQueryWhenPageHasNoOffload(t *testing.T) { |
| 103 | + server, ctx, offloadNodeStatusRepo := offloadTestServer(t, offloadedWorkflow("inline-a", "uid-a", ""), offloadedWorkflow("inline-b", "uid-b", "")) |
| 104 | + |
| 105 | + _, err := server.ListWorkflows(ctx, &workflowpkg.WorkflowListRequest{Namespace: "argo"}) |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + offloadNodeStatusRepo.AssertNotCalled(t, "List") |
| 109 | +} |
0 commit comments