|
| 1 | +package vulnerability |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net" |
| 7 | + "testing" |
| 8 | + |
| 9 | + v1 "github.com/stackrox/rox/generated/api/v1" |
| 10 | + "github.com/stackrox/rox/generated/storage" |
| 11 | + "github.com/stackrox/stackrox-mcp/internal/toolsets/mock" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "google.golang.org/grpc" |
| 15 | + "google.golang.org/grpc/credentials/insecure" |
| 16 | +) |
| 17 | + |
| 18 | +func TestResolveClusterNameToID(t *testing.T) { |
| 19 | + tests := map[string]struct { |
| 20 | + clusterName string |
| 21 | + mockClusters []*storage.Cluster |
| 22 | + mockError error |
| 23 | + expectedID string |
| 24 | + expectError bool |
| 25 | + expectedErrText string |
| 26 | + }{ |
| 27 | + "empty cluster name returns empty ID": { |
| 28 | + clusterName: "", |
| 29 | + mockClusters: []*storage.Cluster{{Id: "cluster-1", Name: "production"}}, |
| 30 | + expectedID: "", |
| 31 | + expectError: false, |
| 32 | + }, |
| 33 | + "cluster name found returns correct ID": { |
| 34 | + clusterName: "production", |
| 35 | + mockClusters: []*storage.Cluster{ |
| 36 | + {Id: "cluster-0", Name: "dev"}, |
| 37 | + {Id: "cluster-1", Name: "production"}, |
| 38 | + {Id: "cluster-2", Name: "staging"}, |
| 39 | + }, |
| 40 | + expectedID: "cluster-1", |
| 41 | + expectError: false, |
| 42 | + }, |
| 43 | + "cluster name not found returns error": { |
| 44 | + clusterName: "nonexistent", |
| 45 | + mockClusters: []*storage.Cluster{ |
| 46 | + {Id: "cluster-1", Name: "production"}, |
| 47 | + }, |
| 48 | + expectedID: "", |
| 49 | + expectError: true, |
| 50 | + expectedErrText: `cluster with name "nonexistent" not found`, |
| 51 | + }, |
| 52 | + "case sensitive matching": { |
| 53 | + clusterName: "Production", |
| 54 | + mockClusters: []*storage.Cluster{ |
| 55 | + {Id: "cluster-1", Name: "production"}, |
| 56 | + }, |
| 57 | + expectedID: "", |
| 58 | + expectError: true, |
| 59 | + expectedErrText: `cluster with name "Production" not found`, |
| 60 | + }, |
| 61 | + "API error propagation": { |
| 62 | + clusterName: "production", |
| 63 | + mockError: errors.New("API connection failed"), |
| 64 | + expectedID: "", |
| 65 | + expectError: true, |
| 66 | + expectedErrText: "failed to fetch clusters:", |
| 67 | + }, |
| 68 | + "exact match required": { |
| 69 | + clusterName: "prod", |
| 70 | + mockClusters: []*storage.Cluster{ |
| 71 | + {Id: "cluster-1", Name: "production"}, |
| 72 | + }, |
| 73 | + expectedID: "", |
| 74 | + expectError: true, |
| 75 | + expectedErrText: `cluster with name "prod" not found`, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for testName, testCase := range tests { |
| 80 | + t.Run(testName, func(t *testing.T) { |
| 81 | + mockService := mock.NewClustersServiceMock(testCase.mockClusters, testCase.mockError) |
| 82 | + grpcServer, listener := mock.SetupClusterServer(mockService) |
| 83 | + defer grpcServer.Stop() |
| 84 | + |
| 85 | + // Create a gRPC client connection to the mock server |
| 86 | + conn, err := grpc.NewClient( |
| 87 | + "passthrough://buffer", |
| 88 | + grpc.WithLocalDNSResolution(), |
| 89 | + grpc.WithContextDialer(func(_ context.Context, _ string) (net.Conn, error) { |
| 90 | + return listener.Dial() |
| 91 | + }), |
| 92 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 93 | + ) |
| 94 | + require.NoError(t, err) |
| 95 | + defer conn.Close() |
| 96 | + |
| 97 | + client := v1.NewClustersServiceClient(conn) |
| 98 | + |
| 99 | + id, err := resolveClusterNameToID(context.Background(), testCase.clusterName, client) |
| 100 | + |
| 101 | + if testCase.expectError { |
| 102 | + require.Error(t, err) |
| 103 | + assert.Contains(t, err.Error(), testCase.expectedErrText) |
| 104 | + } else { |
| 105 | + require.NoError(t, err) |
| 106 | + assert.Equal(t, testCase.expectedID, id) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments