Skip to content

Commit

Permalink
Merge branch 'main' into selectors_delete_by_profile
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjb authored Jul 19, 2024
2 parents 5a5cb54 + dccc11a commit b0c0f21
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/controlplane/handlers_evalstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *Server) ListEvaluationHistory(
}

// convert data set to proto
data, err := fromEvaluationHistoryRow(result.Data)
data, err := fromEvaluationHistoryRows(result.Data)
if err != nil {
return nil, err
}
Expand All @@ -138,7 +138,7 @@ func (s *Server) ListEvaluationHistory(
return resp, nil
}

func fromEvaluationHistoryRow(
func fromEvaluationHistoryRows(
rows []db.ListEvaluationHistoryRow,
) ([]*minderv1.EvaluationHistory, error) {
res := []*minderv1.EvaluationHistory{}
Expand Down
198 changes: 198 additions & 0 deletions internal/controlplane/handlers_evalstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -149,6 +150,26 @@ func TestDBEntityToEntity(t *testing.T) {
input: db.EntitiesBuildEnvironment,
output: minderv1.Entity_ENTITY_BUILD_ENVIRONMENTS,
},
{
name: "release",
input: db.EntitiesRelease,
output: minderv1.Entity_ENTITY_RELEASE,
},
{
name: "pipeline run",
input: db.EntitiesPipelineRun,
output: minderv1.Entity_ENTITY_PIPELINE_RUN,
},
{
name: "task run",
input: db.EntitiesTaskRun,
output: minderv1.Entity_ENTITY_TASK_RUN,
},
{
name: "build",
input: db.EntitiesBuild,
output: minderv1.Entity_ENTITY_BUILD,
},
{
name: "default",
input: db.Entities("whatever"),
Expand Down Expand Up @@ -317,3 +338,180 @@ func TestGetEntityName(t *testing.T) {
})
}
}

func TestFromEvaluationHistoryRows(t *testing.T) {
t.Parallel()

now := time.Now().UTC()
uuid1 := uuid.MustParse("00000000-0000-0000-0000-000000000001")
entityid1 := uuid.MustParse("00000000-0000-0000-0000-000000000011")
uuid2 := uuid.MustParse("00000000-0000-0000-0000-000000000002")
entityid2 := uuid.MustParse("00000000-0000-0000-0000-000000000022")

tests := []struct {
name string
rows []db.ListEvaluationHistoryRow
checkf func(*testing.T, db.ListEvaluationHistoryRow, *minderv1.EvaluationHistory)
err bool
}{
{
name: "empty",
rows: []db.ListEvaluationHistoryRow{},
},
{
name: "happy path",
rows: []db.ListEvaluationHistoryRow{
{
EvaluationID: uuid1,
EvaluatedAt: now,
EntityType: db.EntitiesRepository,
EntityID: entityid1,
RepoOwner: nullStr("stacklok"),
RepoName: nullStr("minder"),
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
ProfileName: "profile_name",
},
},
},
{
name: "order preserved",
rows: []db.ListEvaluationHistoryRow{
{
EvaluationID: uuid1,
EvaluatedAt: now,
EntityType: db.EntitiesRepository,
EntityID: entityid1,
RepoOwner: nullStr("stacklok"),
RepoName: nullStr("minder"),
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
ProfileName: "profile_name",
},
{
EvaluationID: uuid2,
EvaluatedAt: now,
EntityType: db.EntitiesRepository,
EntityID: entityid2,
RepoOwner: nullStr("stacklok"),
RepoName: nullStr("frizbee"),
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
ProfileName: "profile_name",
},
},
},
{
name: "optional alert",
rows: []db.ListEvaluationHistoryRow{
{
EvaluationID: uuid1,
EvaluatedAt: now,
EntityType: db.EntitiesRepository,
EntityID: entityid1,
RepoOwner: nullStr("stacklok"),
RepoName: nullStr("minder"),
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
ProfileName: "profile_name",
AlertStatus: nullAlertStatusOK(),
AlertDetails: nullStr("alert details"),
},
},
},
{
name: "optional remediation",
rows: []db.ListEvaluationHistoryRow{
{
EvaluationID: uuid1,
EvaluatedAt: now,
EntityType: db.EntitiesRepository,
EntityID: entityid1,
RepoOwner: nullStr("stacklok"),
RepoName: nullStr("minder"),
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
ProfileName: "profile_name",
RemediationStatus: nullRemediationStatusTypesSuccess(),
RemediationDetails: nullStr("remediation details"),
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
res, err := fromEvaluationHistoryRows(tt.rows)

if tt.err {
require.Error(t, err)
require.Equal(t, nil, res)
return
}

require.NoError(t, err)
require.Equal(t, len(tt.rows), len(res))
for i := 0; i < len(tt.rows); i++ {
row := tt.rows[i]
item := res[i]
require.Equal(t, row.EvaluatedAt, item.EvaluatedAt.AsTime())
require.Equal(t, row.EntityID.String(), item.Entity.Id)
require.Equal(t, dbEntityToEntity(row.EntityType), item.Entity.Type)
require.Equal(t, row.RuleType, item.Rule.RuleType)
require.Equal(t, row.RuleName, item.Rule.Name)
require.Equal(t, row.ProfileName, item.Rule.Profile)

require.Equal(t, row.AlertStatus.Valid, item.Alert != nil)
if row.AlertStatus.Valid {
require.Equal(t,
string(row.AlertStatus.AlertStatusTypes),
item.Alert.Status,
)
require.Equal(t,
row.AlertDetails.String,
item.Alert.Details,
)
}

require.Equal(t, row.RemediationStatus.Valid, item.Remediation != nil)
if row.RemediationStatus.Valid {
require.Equal(t,
string(row.RemediationStatus.RemediationStatusTypes),
item.Remediation.Status,
)
require.Equal(t,
string(row.RemediationDetails.String),
item.Remediation.Details,
)
}
}
})
}
}

func nullStr(s string) sql.NullString {
return sql.NullString{
String: s,
Valid: true,
}
}

func nullAlertStatusOK() db.NullAlertStatusTypes {
return db.NullAlertStatusTypes{
AlertStatusTypes: db.AlertStatusTypesOn,
Valid: true,
}
}

func nullRemediationStatusTypesSuccess() db.NullRemediationStatusTypes {
return db.NullRemediationStatusTypes{
RemediationStatusTypes: db.RemediationStatusTypesSuccess,
Valid: true,
}
}
4 changes: 3 additions & 1 deletion internal/db/profile_selector_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func (s *ProfileSelector) Scan(value interface{}) error {
selector = strings.TrimSuffix(selector, "\"")
s.Selector = selector

s.Comment = parts[4]
comment := strings.TrimPrefix(parts[4], "\"")
comment = strings.TrimSuffix(comment, "\"")
s.Comment = comment

return nil
}
2 changes: 1 addition & 1 deletion internal/db/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestProfileListWithSelectors(t *testing.T) {

noSelectors := createRandomProfile(t, randomEntities.proj.ID, []string{})
oneSelectorProfile := createRandomProfile(t, randomEntities.proj.ID, []string{})
oneSel := createRepoSelector(t, oneSelectorProfile.ID, "one_selector1", "one_comment1")
oneSel := createRepoSelector(t, oneSelectorProfile.ID, "one_selector1", "multi word comment")

multiSelectorProfile := createRandomProfile(t, randomEntities.proj.ID, []string{})
mulitSel1 := createRepoSelector(t, multiSelectorProfile.ID, "multi_selector1", "multi_comment1")
Expand Down

0 comments on commit b0c0f21

Please sign in to comment.