|
| 1 | +package ostest_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/golang/mock/gomock" |
| 10 | + "github.com/rs/zerolog" |
| 11 | + gafclientmocks "github.com/snyk/go-application-framework/pkg/apiclients/mocks" |
| 12 | + "github.com/snyk/go-application-framework/pkg/apiclients/testapi" |
| 13 | + "github.com/snyk/go-application-framework/pkg/configuration" |
| 14 | + gafmocks "github.com/snyk/go-application-framework/pkg/mocks" |
| 15 | + "github.com/snyk/go-application-framework/pkg/runtimeinfo" |
| 16 | + "github.com/snyk/go-application-framework/pkg/workflow" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + |
| 20 | + "github.com/snyk/cli-extension-os-flows/internal/commands/cmdctx" |
| 21 | + "github.com/snyk/cli-extension-os-flows/internal/commands/ostest" |
| 22 | + common "github.com/snyk/cli-extension-os-flows/internal/common" |
| 23 | + "github.com/snyk/cli-extension-os-flows/internal/constants" |
| 24 | + "github.com/snyk/cli-extension-os-flows/internal/errors" |
| 25 | + "github.com/snyk/cli-extension-os-flows/internal/flags" |
| 26 | +) |
| 27 | + |
| 28 | +// TestSBOMResolutionIntegration_DepGraphsPassedToUnifiedTestAPI verifies that when the UV test flow |
| 29 | +// is enabled, dep-graphs from SBOM resolution are properly passed to the Unified Test API. |
| 30 | +func TestSBOMResolutionIntegration_DepGraphsPassedToUnifiedTestAPI(t *testing.T) { |
| 31 | + ctrl := gomock.NewController(t) |
| 32 | + defer ctrl.Finish() |
| 33 | + |
| 34 | + ctx := context.Background() |
| 35 | + mockIctx, mockTestClient, mockDepGraph, orgID, cfg, testLogger := setupSBOMResolutionIntegrationTest(t, ctrl) |
| 36 | + |
| 37 | + // Set up context with dependencies |
| 38 | + testErrFactory := errors.NewErrorFactory(testLogger) |
| 39 | + nopProgressBar := &NopProgressBar{} |
| 40 | + ctx = cmdctx.WithIctx(ctx, mockIctx) |
| 41 | + ctx = cmdctx.WithConfig(ctx, cfg) |
| 42 | + ctx = cmdctx.WithLogger(ctx, testLogger) |
| 43 | + ctx = cmdctx.WithErrorFactory(ctx, testErrFactory) |
| 44 | + ctx = cmdctx.WithProgressBar(ctx, nopProgressBar) |
| 45 | + |
| 46 | + _, _, err := ostest.RunUnifiedTestFlow( |
| 47 | + ctx, |
| 48 | + ".", |
| 49 | + mockTestClient, |
| 50 | + orgID, |
| 51 | + nil, |
| 52 | + nil, |
| 53 | + ) |
| 54 | + |
| 55 | + require.NoError(t, err) |
| 56 | + require.NotNil(t, mockDepGraph) |
| 57 | +} |
| 58 | + |
| 59 | +//nolint:gocritic // Test helper needs to return multiple values for test setup |
| 60 | +func setupSBOMResolutionIntegrationTest( |
| 61 | + t *testing.T, |
| 62 | + ctrl *gomock.Controller, |
| 63 | +) ( |
| 64 | + workflow.InvocationContext, |
| 65 | + testapi.TestClient, |
| 66 | + *testapi.IoSnykApiV1testdepgraphRequestDepGraph, |
| 67 | + string, |
| 68 | + configuration.Configuration, |
| 69 | + *zerolog.Logger, |
| 70 | +) { |
| 71 | + t.Helper() |
| 72 | + |
| 73 | + mockEngine := gafmocks.NewMockEngine(ctrl) |
| 74 | + mockIctx := gafmocks.NewMockInvocationContext(ctrl) |
| 75 | + mockTestClient := gafclientmocks.NewMockTestClient(ctrl) |
| 76 | + |
| 77 | + cfg := configuration.New() |
| 78 | + cfg.Set(constants.EnableExperimentalUvSupportEnvVar, true) |
| 79 | + cfg.Set(configuration.ORGANIZATION, "test-org-id") |
| 80 | + cfg.Set(flags.FlagFile, "uv.lock") |
| 81 | + |
| 82 | + nopLogger := zerolog.Nop() |
| 83 | + |
| 84 | + mockIctx.EXPECT().GetConfiguration().Return(cfg).AnyTimes() |
| 85 | + mockIctx.EXPECT().GetEnhancedLogger().Return(&nopLogger).AnyTimes() |
| 86 | + mockIctx.EXPECT().GetRuntimeInfo().Return(runtimeinfo.New()).AnyTimes() |
| 87 | + mockIctx.EXPECT().GetEngine().Return(mockEngine).AnyTimes() |
| 88 | + mockIctx.EXPECT().GetWorkflowIdentifier().Return(workflow.NewWorkflowIdentifier("test")).AnyTimes() |
| 89 | + |
| 90 | + depGraphBytes, err := os.ReadFile("testdata/uv_depgraph.json") |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + var mockDepGraph testapi.IoSnykApiV1testdepgraphRequestDepGraph |
| 94 | + require.NoError(t, json.Unmarshal(depGraphBytes, &mockDepGraph)) |
| 95 | + |
| 96 | + depGraphData := workflow.NewData( |
| 97 | + workflow.NewTypeIdentifier(common.DepGraphWorkflowID, "depgraph"), |
| 98 | + "application/json", |
| 99 | + depGraphBytes, |
| 100 | + ) |
| 101 | + depGraphData.SetMetaData("Content-Location", "uv.lock") |
| 102 | + |
| 103 | + mockEngine.EXPECT(). |
| 104 | + InvokeWithConfig(common.DepGraphWorkflowID, gomock.Any()). |
| 105 | + DoAndReturn(func(_ workflow.Identifier, cfg configuration.Configuration) ([]workflow.Data, error) { |
| 106 | + // Verify that use-sbom-resolution flag is set |
| 107 | + assert.True(t, cfg.GetBool("use-sbom-resolution"), "use-sbom-resolution flag should be set when UV support is enabled") |
| 108 | + return []workflow.Data{depGraphData}, nil |
| 109 | + }). |
| 110 | + Times(1) |
| 111 | + |
| 112 | + mockTestClient.EXPECT(). |
| 113 | + StartTest(gomock.Any(), gomock.Any()). |
| 114 | + DoAndReturn(func(_ context.Context, params testapi.StartTestParams) (testapi.TestHandle, error) { |
| 115 | + require.NotNil(t, params.Subject) |
| 116 | + |
| 117 | + depGraphSubject, subjectErr := params.Subject.AsDepGraphSubjectCreate() |
| 118 | + require.NoError(t, subjectErr) |
| 119 | + |
| 120 | + assert.Equal(t, mockDepGraph, depGraphSubject.DepGraph) |
| 121 | + |
| 122 | + handle := gafclientmocks.NewMockTestHandle(ctrl) |
| 123 | + handle.EXPECT().Wait(gomock.Any()).Return(nil).Times(1) |
| 124 | + |
| 125 | + result := gafclientmocks.NewMockTestResult(ctrl) |
| 126 | + result.EXPECT().GetExecutionState().Return(testapi.TestExecutionStatesFinished).AnyTimes() |
| 127 | + result.EXPECT().Findings(gomock.Any()).Return([]testapi.FindingData{}, true, nil).AnyTimes() |
| 128 | + handle.EXPECT().Result().Return(result).Times(1) |
| 129 | + |
| 130 | + return handle, nil |
| 131 | + }). |
| 132 | + Times(1) |
| 133 | + |
| 134 | + return mockIctx, mockTestClient, &mockDepGraph, "test-org-id", cfg, &nopLogger |
| 135 | +} |
0 commit comments