Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions central/alert/datastore/datastore_sac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/env"
"github.com/stackrox/rox/pkg/fixtures"
"github.com/stackrox/rox/pkg/fixtures/fixtureconsts"
"github.com/stackrox/rox/pkg/postgres/pgtest"
"github.com/stackrox/rox/pkg/postgres/schema"
"github.com/stackrox/rox/pkg/rocksdb"
Expand Down Expand Up @@ -441,7 +442,7 @@ var alertUnrestrictedSACSearchTestCases = map[string]alertSACSearchResult{
testconsts.NamespaceB: 3,
testconsts.NamespaceC: 2,
},
"prod cluster": {"stackrox": 1},
fixtureconsts.Cluster1: {"stackrox": 1},
},
},
"full read-write access should see all alerts": {
Expand All @@ -455,7 +456,7 @@ var alertUnrestrictedSACSearchTestCases = map[string]alertSACSearchResult{
testconsts.NamespaceB: 3,
testconsts.NamespaceC: 2,
},
"prod cluster": {"stackrox": 1},
fixtureconsts.Cluster1: {"stackrox": 1},
},
},
}
Expand Down
5 changes: 3 additions & 2 deletions central/alerttest/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package alerttest

import (
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/fixtures/fixtureconsts"
)

// Constants for unit tests that need fake Alerts.
const (
FakeAlertID = "fake-alert-id"
FakeAlertID = fixtureconsts.AlertFake
FakeClusterName = "fakeCluster"
FakePolicyID = "fake-policy-id"
FakePolicyID = fixtureconsts.PolicyFake
FakeTag1 = "FakeTag1"
FakeTag2 = "FakeTag2"
FakeTag3 = "FakeTag3"
Expand Down
16 changes: 8 additions & 8 deletions central/cluster/datastore/datastore_sac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (s *clusterDatastoreSACSuite) TestGetCluster() {
cluster.Id = clusterID
cluster.Priority = 1

cases := testutils.GenericClusterSACGetTestCases(context.Background(), s.T(), clusterID, "not-"+clusterID, resources.Cluster)
cases := testutils.GenericClusterSACGetTestCases(context.Background(), s.T(), clusterID, testconsts.Cluster3, resources.Cluster)

for name, c := range cases {
s.Run(name, func() {
Expand Down Expand Up @@ -352,7 +352,7 @@ func (s *clusterDatastoreSACSuite) TestGetClusters() {
defer s.deleteCluster(clusterID2)
s.Require().NoError(err)
cluster2.Id = clusterID2
otherClusterID := "someOtherClusterID"
otherClusterID := testconsts.Cluster3

cases := getMultiClusterTestCases(context.Background(), clusterID1, clusterID2, otherClusterID)

Expand Down Expand Up @@ -525,7 +525,7 @@ func (s *clusterDatastoreSACSuite) TestRemoveCluster() {
sac.AllowFixedScopes(
sac.AccessModeScopeKeys(storage.Access_READ_ACCESS, storage.Access_READ_WRITE_ACCESS),
sac.ResourceScopeKeys(resources.Cluster),
sac.ClusterScopeKeys(clusterID, "otherthan"+clusterID)))
sac.ClusterScopeKeys(clusterID, testconsts.Cluster3)))
removeErr := s.datastore.RemoveCluster(ctx, clusterID, &doneSignal)
s.NoError(removeErr)
if removeErr == nil {
Expand Down Expand Up @@ -1016,7 +1016,7 @@ func (s *clusterDatastoreSACSuite) TestCount() {
defer s.deleteCluster(clusterID2)
s.Require().NoError(err)
cluster2.Id = clusterID2
otherClusterID := "someOtherClusterID"
otherClusterID := testconsts.Cluster3

cases := getMultiClusterTestCases(context.Background(), clusterID1, clusterID2, otherClusterID)

Expand All @@ -1041,7 +1041,7 @@ func (s *clusterDatastoreSACSuite) TestCountClusters() {
defer s.deleteCluster(clusterID2)
s.Require().NoError(err)
cluster2.Id = clusterID2
otherClusterID := "someOtherClusterID"
otherClusterID := testconsts.Cluster3

cases := getMultiClusterTestCases(context.Background(), clusterID1, clusterID2, otherClusterID)

Expand All @@ -1066,7 +1066,7 @@ func (s *clusterDatastoreSACSuite) TestSearch() {
defer s.deleteCluster(clusterID2)
s.Require().NoError(err)
cluster2.Id = clusterID2
otherClusterID := "someOtherClusterID"
otherClusterID := testconsts.Cluster3

cases := getMultiClusterTestCases(context.Background(), clusterID1, clusterID2, otherClusterID)

Expand Down Expand Up @@ -1095,7 +1095,7 @@ func (s *clusterDatastoreSACSuite) TestSearchRawClusters() {
defer s.deleteCluster(clusterID2)
s.Require().NoError(err)
cluster2.Id = clusterID2
otherClusterID := "someOtherClusterID"
otherClusterID := testconsts.Cluster3

cases := getMultiClusterTestCases(context.Background(), clusterID1, clusterID2, otherClusterID)

Expand Down Expand Up @@ -1129,7 +1129,7 @@ func (s *clusterDatastoreSACSuite) TestSearchResults() {
defer s.deleteCluster(clusterID2)
s.Require().NoError(err)
cluster2.Id = clusterID2
otherClusterID := "someOtherClusterID"
otherClusterID := testconsts.Cluster3

cases := getMultiClusterTestCases(context.Background(), clusterID1, clusterID2, otherClusterID)

Expand Down
7 changes: 4 additions & 3 deletions central/deployment/datastore/datastore_impl_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stackrox/rox/pkg/postgres/pgtest"
"github.com/stackrox/rox/pkg/postgres/schema"
"github.com/stackrox/rox/pkg/sac"
"github.com/stackrox/rox/pkg/sac/testconsts"
"github.com/stackrox/rox/pkg/scancomponent"
pkgSearch "github.com/stackrox/rox/pkg/search"
"github.com/stackrox/rox/pkg/search/postgres/mapping"
Expand Down Expand Up @@ -116,9 +117,9 @@ func (s *DeploymentPostgresDataStoreTestSuite) TestSearchWithPostgres() {
img3 := fixtures.GetImageWithUniqueComponents(5)
img3.Id = uuid.NewV4().String()
img3.Scan.OperatingSystem = "saturn"
dep1 := fixtures.GetDeploymentWithImage("c1", "n1", img1)
dep2 := fixtures.GetDeploymentWithImage("c1", "n2", img2)
dep3 := fixtures.GetDeploymentWithImage("c2", "n1", img3)
dep1 := fixtures.GetDeploymentWithImage(testconsts.Cluster1, "n1", img1)
dep2 := fixtures.GetDeploymentWithImage(testconsts.Cluster1, "n2", img2)
dep3 := fixtures.GetDeploymentWithImage(testconsts.Cluster2, "n1", img3)

// Upsert images.
s.NoError(s.imageDatastore.UpsertImage(ctx, img1))
Expand Down
3 changes: 1 addition & 2 deletions central/deployment/datastoretest/datastore_sac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import (
)

const (
otherClusterID = "OtherClusterID"
otherNamespace = "OtherNamespace"
)

Expand Down Expand Up @@ -220,7 +219,7 @@ func (s *deploymentDatastoreSACSuite) setupMultipleDeploymentReadTest() ([]strin
deploymentID2 := deployment2.GetId()
deployment3 := s.pushDeploymentToStore(testconsts.Cluster2, testconsts.NamespaceB)
deploymentID3 := deployment3.GetId()
deployment4 := s.pushDeploymentToStore(otherClusterID, otherNamespace)
deployment4 := s.pushDeploymentToStore(testconsts.Cluster3, otherNamespace)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the unused otherClusterID constant from the file as well.

deploymentID4 := deployment4.GetId()
pushedIDs := []string{deploymentID1, deploymentID2, deploymentID3, deploymentID4}
IDtoDeployment := map[string]*storage.Deployment{
Expand Down
8 changes: 5 additions & 3 deletions central/networkbaseline/datastore/datastore_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/stackrox/rox/pkg/postgres/pgtest"
"github.com/stackrox/rox/pkg/rocksdb"
"github.com/stackrox/rox/pkg/sac"
"github.com/stackrox/rox/pkg/sac/testconsts"
"github.com/stackrox/rox/pkg/uuid"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -91,7 +93,7 @@ func (suite *NetworkBaselineDataStoreTestSuite) TestNoAccessAllowed() {

suite.Error(suite.datastore.DeleteNetworkBaseline(ctx, expectedBaseline.GetDeploymentId()), "permission denied")
// BTW if we try to delete non-existent/already deleted baseline, it should just return nil
suite.Nil(suite.datastore.DeleteNetworkBaseline(ctx, "non-existent deployment ID"))
suite.Nil(suite.datastore.DeleteNetworkBaseline(ctx, uuid.Nil.String()))
}

func (suite *NetworkBaselineDataStoreTestSuite) TestNetworkBaselines() {
Expand Down Expand Up @@ -130,7 +132,7 @@ func (suite *NetworkBaselineDataStoreTestSuite) TestSAC() {
sac.AllowFixedScopes(
sac.AccessModeScopeKeys(storage.Access_READ_ACCESS),
sac.ResourceScopeKeys(resources.DeploymentExtension),
sac.ClusterScopeKeys("a-wrong-cluster")))
sac.ClusterScopeKeys(testconsts.Cluster3)))

ctxWithReadAccess :=
sac.WithGlobalAccessScopeChecker(
Expand All @@ -147,7 +149,7 @@ func (suite *NetworkBaselineDataStoreTestSuite) TestSAC() {
sac.AllowFixedScopes(
sac.AccessModeScopeKeys(storage.Access_READ_WRITE_ACCESS),
sac.ResourceScopeKeys(resources.DeploymentExtension),
sac.ClusterScopeKeys("a-wrong-cluster")))
sac.ClusterScopeKeys(testconsts.Cluster3)))

ctxWithWriteAccess :=
sac.WithGlobalAccessScopeChecker(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/stackrox/rox/pkg/dackbox/concurrency"
"github.com/stackrox/rox/pkg/env"
"github.com/stackrox/rox/pkg/fixtures"
"github.com/stackrox/rox/pkg/fixtures/fixtureconsts"
"github.com/stackrox/rox/pkg/nodes/converter"
"github.com/stackrox/rox/pkg/postgres/pgtest"
"github.com/stackrox/rox/pkg/sac"
Expand Down Expand Up @@ -106,7 +107,7 @@ func (suite *NodePostgresDataStoreTestSuite) TearDownSuite() {
}

func (suite *NodePostgresDataStoreTestSuite) TestBasicOps() {
node := getTestNodeForPostgres("id1", "name1")
node := getTestNodeForPostgres(fixtureconsts.Node1, "name1")
allowAllCtx := sac.WithAllAccess(context.Background())

// Upsert.
Expand All @@ -126,10 +127,10 @@ func (suite *NodePostgresDataStoreTestSuite) TestBasicOps() {
suite.EqualValues(expectedNode, storedNode)

// Exists tests.
exists, err = suite.datastore.Exists(allowAllCtx, "id1")
exists, err = suite.datastore.Exists(allowAllCtx, fixtureconsts.Node1)
suite.NoError(err)
suite.True(exists)
exists, err = suite.datastore.Exists(allowAllCtx, "id2")
exists, err = suite.datastore.Exists(allowAllCtx, fixtureconsts.Node2)
suite.NoError(err)
suite.False(exists)

Expand All @@ -146,13 +147,13 @@ func (suite *NodePostgresDataStoreTestSuite) TestBasicOps() {
suite.Equal(expectedNode, storedNode)

newNode := node.Clone()
newNode.Id = "id2"
newNode.Id = fixtureconsts.Node2

// Upsert new node.
suite.NoError(suite.datastore.UpsertNode(allowAllCtx, newNode))

// Exists test.
exists, err = suite.datastore.Exists(allowAllCtx, "id2")
exists, err = suite.datastore.Exists(allowAllCtx, fixtureconsts.Node2)
suite.NoError(err)
suite.True(exists)

Expand All @@ -170,27 +171,27 @@ func (suite *NodePostgresDataStoreTestSuite) TestBasicOps() {
suite.Equal(2, count)

// Get batch.
nodes, err := suite.datastore.GetNodesBatch(allowAllCtx, []string{"id1", "id2"})
nodes, err := suite.datastore.GetNodesBatch(allowAllCtx, []string{fixtureconsts.Node1, fixtureconsts.Node2})
suite.NoError(err)
suite.Len(nodes, 2)
suite.ElementsMatch([]*storage.Node{expectedNode, newExpectedNode}, nodes)

// Delete both nodes.
suite.mockRisk.EXPECT().RemoveRisk(gomock.Any(), "id1", storage.RiskSubjectType_NODE).Return(nil)
suite.mockRisk.EXPECT().RemoveRisk(gomock.Any(), "id2", storage.RiskSubjectType_NODE).Return(nil)
suite.NoError(suite.datastore.DeleteNodes(allowAllCtx, "id1", "id2"))
suite.mockRisk.EXPECT().RemoveRisk(gomock.Any(), fixtureconsts.Node1, storage.RiskSubjectType_NODE).Return(nil)
suite.mockRisk.EXPECT().RemoveRisk(gomock.Any(), fixtureconsts.Node2, storage.RiskSubjectType_NODE).Return(nil)
suite.NoError(suite.datastore.DeleteNodes(allowAllCtx, fixtureconsts.Node1, fixtureconsts.Node2))

// Exists tests.
exists, err = suite.datastore.Exists(allowAllCtx, "id1")
exists, err = suite.datastore.Exists(allowAllCtx, fixtureconsts.Node1)
suite.NoError(err)
suite.False(exists)
exists, err = suite.datastore.Exists(allowAllCtx, "id2")
exists, err = suite.datastore.Exists(allowAllCtx, fixtureconsts.Node2)
suite.NoError(err)
suite.False(exists)
}

func (suite *NodePostgresDataStoreTestSuite) TestBasicSearch() {
node := getTestNodeForPostgres("id1", "name1")
node := getTestNodeForPostgres(fixtureconsts.Node1, "name1")
allowAllCtx := sac.WithAllAccess(context.Background())

// Basic unscoped search.
Expand Down Expand Up @@ -230,7 +231,7 @@ func (suite *NodePostgresDataStoreTestSuite) TestBasicSearch() {
suite.Equal(expectedNode, nodes[0])

// Upsert new node.
newNode := getTestNodeForPostgres("id2", "name2")
newNode := getTestNodeForPostgres(fixtureconsts.Node2, "name2")
newNode.GetScan().Components = append(newNode.GetScan().GetComponents(), &storage.EmbeddedNodeScanComponent{
Name: "comp3",
Version: "ver1",
Expand Down Expand Up @@ -282,7 +283,7 @@ func (suite *NodePostgresDataStoreTestSuite) TestSearchByVuln() {
results, err = suite.datastore.Search(scopedCtx, pkgSearch.EmptyQuery())
suite.NoError(err)
suite.Len(results, 1)
suite.Equal("id2", results[0].ID)
suite.Equal(fixtureconsts.Node2, results[0].ID)

scopedCtx = scoped.Context(ctx, scoped.Scope{
ID: cve.ID("cve4", "ubuntu"),
Expand Down Expand Up @@ -339,7 +340,7 @@ func (suite *NodePostgresDataStoreTestSuite) TestSearchByComponent() {
results, err = suite.datastore.Search(scopedCtx, pkgSearch.EmptyQuery())
suite.NoError(err)
suite.Len(results, 1)
suite.Equal("id2", results[0].ID)
suite.Equal(fixtureconsts.Node2, results[0].ID)

scopedCtx = scoped.Context(ctx, scoped.Scope{
ID: scancomponent.ComponentID("comp4", "ver1", "ubuntu"),
Expand Down Expand Up @@ -430,13 +431,13 @@ func (suite *NodePostgresDataStoreTestSuite) TestSortByComponent() {
}

func (suite *NodePostgresDataStoreTestSuite) upsertTestNodes(ctx context.Context) {
node := getTestNodeForPostgres("id1", "name1")
node := getTestNodeForPostgres(fixtureconsts.Node1, "name1")

// Upsert node.
suite.NoError(suite.datastore.UpsertNode(ctx, node))

// Upsert new node.
newNode := getTestNodeForPostgres("id2", "name2")
newNode := getTestNodeForPostgres(fixtureconsts.Node2, "name2")
newNode.GetScan().Components = append(newNode.GetScan().GetComponents(), &storage.EmbeddedNodeScanComponent{
Name: "comp3",
Version: "ver1",
Expand All @@ -452,9 +453,9 @@ func (suite *NodePostgresDataStoreTestSuite) upsertTestNodes(ctx context.Context
}

func (suite *NodePostgresDataStoreTestSuite) deleteTestNodes(ctx context.Context) {
suite.mockRisk.EXPECT().RemoveRisk(gomock.Any(), "id1", storage.RiskSubjectType_NODE).Return(nil)
suite.mockRisk.EXPECT().RemoveRisk(gomock.Any(), "id2", storage.RiskSubjectType_NODE).Return(nil)
suite.NoError(suite.datastore.DeleteNodes(ctx, "id1", "id2"))
suite.mockRisk.EXPECT().RemoveRisk(gomock.Any(), fixtureconsts.Node1, storage.RiskSubjectType_NODE).Return(nil)
suite.mockRisk.EXPECT().RemoveRisk(gomock.Any(), fixtureconsts.Node2, storage.RiskSubjectType_NODE).Return(nil)
suite.NoError(suite.datastore.DeleteNodes(ctx, fixtureconsts.Node1, fixtureconsts.Node2))
}

func (suite *NodePostgresDataStoreTestSuite) TestOrphanedNodeTreeDeletion() {
Expand Down Expand Up @@ -503,7 +504,7 @@ func (suite *NodePostgresDataStoreTestSuite) TestOrphanedNodeTreeDeletion() {
suite.ElementsMatch(cveIDsSet.AsSlice(), pkgSearch.ResultsToIDs(results))

testNode2 := testNode.Clone()
testNode2.Id = "2"
testNode2.Id = fixtureconsts.Node2
suite.NoError(suite.datastore.UpsertNode(ctx, testNode2))
storedNode, found, err = suite.datastore.GetNode(ctx, testNode2.GetId())
suite.NoError(err)
Expand Down
Loading