Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to evaluation log entries conversion. #3941

Merged
merged 1 commit into from
Jul 19, 2024
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
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,
}
}