Skip to content

Commit

Permalink
feat: update style to match github (#697)
Browse files Browse the repository at this point in the history
* feat: update style to match github

* fix: tests
  • Loading branch information
Richard87 authored Oct 29, 2024
1 parent 287bdab commit ca99914
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 99 deletions.
45 changes: 5 additions & 40 deletions api/buildstatus/build_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestGetBuildStatus(t *testing.T) {
expected := []byte("badge")

fakeBuildStatus.EXPECT().
GetBadge(gomock.Any(), gomock.Any()).
GetBadge(gomock.Any(), gomock.Any(), gomock.Any()).
Return(expected, nil).
Times(1)

Expand All @@ -125,16 +125,7 @@ func TestGetBuildStatus(t *testing.T) {

fakeBuildStatus := mock.NewMockPipelineBadge(ctrl)

var actualCondition v1.RadixJobCondition
var actualPipeline v1.RadixPipelineType

fakeBuildStatus.EXPECT().
GetBadge(gomock.Any(), gomock.Any()).
DoAndReturn(func(c v1.RadixJobCondition, p v1.RadixPipelineType) ([]byte, error) {
actualCondition = c
actualPipeline = p
return nil, nil
})
fakeBuildStatus.EXPECT().GetBadge(gomock.Any(), v1.JobRunning, v1.BuildDeploy).Return(nil, nil).Times(1)

mockValidator := authnmock.NewMockValidatorInterface(gomock.NewController(t))
mockValidator.EXPECT().ValidateToken(gomock.Any(), gomock.Any()).AnyTimes().Return(controllertest.NewTestPrincipal(true), nil)
Expand All @@ -144,8 +135,6 @@ func TestGetBuildStatus(t *testing.T) {
response := <-responseChannel

assert.Equal(t, response.Result().StatusCode, 200)
assert.Equal(t, v1.JobRunning, actualCondition)
assert.Equal(t, v1.BuildDeploy, actualPipeline)
})

t.Run("deploy in master - JobRunning", func(t *testing.T) {
Expand All @@ -154,17 +143,7 @@ func TestGetBuildStatus(t *testing.T) {
defer ctrl.Finish()

fakeBuildStatus := mock.NewMockPipelineBadge(ctrl)

var actualCondition v1.RadixJobCondition
var actualPipeline v1.RadixPipelineType

fakeBuildStatus.EXPECT().
GetBadge(gomock.Any(), gomock.Any()).
DoAndReturn(func(c v1.RadixJobCondition, p v1.RadixPipelineType) ([]byte, error) {
actualCondition = c
actualPipeline = p
return nil, nil
})
fakeBuildStatus.EXPECT().GetBadge(gomock.Any(), v1.JobSucceeded, v1.Deploy).Return(nil, nil).Times(1)

mockValidator := authnmock.NewMockValidatorInterface(gomock.NewController(t))
mockValidator.EXPECT().ValidateToken(gomock.Any(), gomock.Any()).AnyTimes().Return(controllertest.NewTestPrincipal(true), nil)
Expand All @@ -174,8 +153,6 @@ func TestGetBuildStatus(t *testing.T) {
response := <-responseChannel

assert.Equal(t, response.Result().StatusCode, 200)
assert.Equal(t, v1.JobSucceeded, actualCondition)
assert.Equal(t, v1.Deploy, actualPipeline)
})

t.Run("promote in master - JobFailed", func(t *testing.T) {
Expand All @@ -184,17 +161,7 @@ func TestGetBuildStatus(t *testing.T) {
defer ctrl.Finish()

fakeBuildStatus := mock.NewMockPipelineBadge(ctrl)

var actualCondition v1.RadixJobCondition
var actualPipeline v1.RadixPipelineType

fakeBuildStatus.EXPECT().
GetBadge(gomock.Any(), gomock.Any()).
DoAndReturn(func(c v1.RadixJobCondition, p v1.RadixPipelineType) ([]byte, error) {
actualCondition = c
actualPipeline = p
return nil, nil
})
fakeBuildStatus.EXPECT().GetBadge(gomock.Any(), v1.JobFailed, v1.Promote).Return(nil, nil).Times(1)

mockValidator := authnmock.NewMockValidatorInterface(gomock.NewController(t))
mockValidator.EXPECT().ValidateToken(gomock.Any(), gomock.Any()).AnyTimes().Return(controllertest.NewTestPrincipal(true), nil)
Expand All @@ -204,8 +171,6 @@ func TestGetBuildStatus(t *testing.T) {
response := <-responseChannel

assert.Equal(t, response.Result().StatusCode, 200)
assert.Equal(t, v1.JobFailed, actualCondition)
assert.Equal(t, v1.Promote, actualPipeline)
})

t.Run("return status 500", func(t *testing.T) {
Expand All @@ -216,7 +181,7 @@ func TestGetBuildStatus(t *testing.T) {
fakeBuildStatus := mock.NewMockPipelineBadge(ctrl)

fakeBuildStatus.EXPECT().
GetBadge(gomock.Any(), gomock.Any()).
GetBadge(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, errors.New("error")).
Times(1)

Expand Down
2 changes: 1 addition & 1 deletion api/buildstatus/buildstatus_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (handler BuildStatusHandler) GetBuildStatusForApplication(ctx context.Conte
buildCondition = latestPipelineJob.Status.Condition
}

output, err = handler.pipelineBadge.GetBadge(buildCondition, v1.RadixPipelineType(pipeline))
output, err = handler.pipelineBadge.GetBadge(ctx, buildCondition, v1.RadixPipelineType(pipeline))
if err != nil {
return nil, err
}
Expand Down
86 changes: 48 additions & 38 deletions api/buildstatus/models/badges/build-status.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 10 additions & 6 deletions api/buildstatus/models/buildstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package models

import (
"bytes"
"context"
_ "embed"
"errors"
"html/template"
"strings"

v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/rs/zerolog/log"
)

// embed https://golang.org/pkg/embed/ - For embedding a single file, a variable of type []byte or string is often best
Expand All @@ -33,7 +35,7 @@ const (
)

type PipelineBadge interface {
GetBadge(condition v1.RadixJobCondition, pipeline v1.RadixPipelineType) ([]byte, error)
GetBadge(ctx context.Context, condition v1.RadixJobCondition, pipeline v1.RadixPipelineType) ([]byte, error)
}

func NewPipelineBadge() PipelineBadge {
Expand All @@ -54,16 +56,16 @@ type pipelineBadge struct {
badgeTemplate string
}

func (rbs *pipelineBadge) GetBadge(condition v1.RadixJobCondition, pipeline v1.RadixPipelineType) ([]byte, error) {
return rbs.getBadge(condition, pipeline)
func (rbs *pipelineBadge) GetBadge(ctx context.Context, condition v1.RadixJobCondition, pipeline v1.RadixPipelineType) ([]byte, error) {
return rbs.getBadge(ctx, condition, pipeline)
}

func (rbs *pipelineBadge) getBadge(condition v1.RadixJobCondition, pipeline v1.RadixPipelineType) ([]byte, error) {
func (rbs *pipelineBadge) getBadge(ctx context.Context, condition v1.RadixJobCondition, pipeline v1.RadixPipelineType) ([]byte, error) {
operation := translatePipeline(pipeline)
status := translateCondition(condition)
color := getColor(condition)
operationWidth := calculateWidth(10, operation)
statusWidth := calculateWidth(10, status)
operationWidth := calculateWidth(6, operation)
statusWidth := calculateWidth(6, status)
badgeData := pipelineBadgeData{
Operation: operation,
OperationWidth: operationWidth,
Expand All @@ -72,6 +74,8 @@ func (rbs *pipelineBadge) getBadge(condition v1.RadixJobCondition, pipeline v1.R
StatusWidth: statusWidth,
}

log.Ctx(ctx).Trace().Interface("badge", badgeData).Msg("Rendering badge")

funcMap := template.FuncMap{"sum": TemplateSum}
svgTemplate := template.New("status-badge.svg").Funcs(funcMap)
_, err := svgTemplate.Parse(rbs.badgeTemplate)
Expand Down
21 changes: 11 additions & 10 deletions api/buildstatus/models/buildstatus_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"context"
"testing"

v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
Expand All @@ -14,71 +15,71 @@ func Test_PipelineBadgeBuilder(t *testing.T) {
t.Run("failed condition", func(t *testing.T) {
t.Parallel()
expected := "-Failing"
actual, err := badgeBuilder.GetBadge(v1.JobFailed, v1.RadixPipelineType(""))
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobFailed, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("queued condition", func(t *testing.T) {
t.Parallel()
expected := "-Pending"
actual, err := badgeBuilder.GetBadge(v1.JobQueued, v1.RadixPipelineType(""))
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobQueued, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("running condition", func(t *testing.T) {
t.Parallel()
expected := "-Running"
actual, err := badgeBuilder.GetBadge(v1.JobRunning, v1.RadixPipelineType(""))
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobRunning, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("stopped condition", func(t *testing.T) {
t.Parallel()
expected := "-Stopped"
actual, err := badgeBuilder.GetBadge(v1.JobStopped, v1.RadixPipelineType(""))
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobStopped, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("succeeded condition", func(t *testing.T) {
t.Parallel()
expected := "-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.RadixPipelineType(""))
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobSucceeded, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("waiting condition", func(t *testing.T) {
t.Parallel()
expected := "-Pending"
actual, err := badgeBuilder.GetBadge(v1.JobWaiting, v1.RadixPipelineType(""))
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobWaiting, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})

t.Run("build-deploy type", func(t *testing.T) {
t.Parallel()
expected := "build-deploy-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.BuildDeploy)
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobSucceeded, v1.BuildDeploy)
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("promote type", func(t *testing.T) {
t.Parallel()
expected := "promote-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.Promote)
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobSucceeded, v1.Promote)
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("deploy type", func(t *testing.T) {
t.Parallel()
expected := "deploy-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.Deploy)
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobSucceeded, v1.Deploy)
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("unhandled type", func(t *testing.T) {
t.Parallel()
expected := "-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.RadixPipelineType("unhandled"))
actual, err := badgeBuilder.GetBadge(context.Background(), v1.JobSucceeded, v1.RadixPipelineType("unhandled"))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
Expand Down
9 changes: 5 additions & 4 deletions api/test/mock/buildstatus_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ca99914

Please sign in to comment.