Skip to content

Commit

Permalink
Merge pull request #977 from traPtitech/feat/builder-mock
Browse files Browse the repository at this point in the history
Builder mockの作成
  • Loading branch information
pirosiki197 authored Dec 3, 2024
2 parents e204980 + 6cf657a commit 32cb9f3
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 33 deletions.
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type BuilderConfig struct {
Controller grpc.ControllerServiceClientConfig `mapstructure:"controller" yaml:"controller"`
Priority int `mapstructure:"priority" yaml:"priority"`
StepTimeout string `mapstructure:"stepTimeout" yaml:"stepTimeout"`
Mock bool `mapstructure:"mock" yaml:"mock"`
}

type ControllerConfig struct {
Expand Down
22 changes: 22 additions & 0 deletions cmd/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/backend/dockerimpl"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/backend/k8simpl"
ubuilder "github.com/traPtitech/neoshowcase/pkg/usecase/builder"
buildermock "github.com/traPtitech/neoshowcase/pkg/usecase/builder/mock"
)

func NewAuthDev(c Config) (component, error) {
Expand All @@ -28,9 +30,29 @@ func NewAuthDev(c Config) (component, error) {
}

func NewBuilder(c Config) (component, error) {
if c.Components.Builder.Mock {
return newMockBuilder(c)
} else {
return newBuilder(c)
}
}

func newBuilder(c Config) (component, error) {
wire.Build(
providers,
wire.FieldsOf(new(BuilderConfig), "Buildpack"),
wire.Bind(new(ubuilder.Service), new(*ubuilder.ServiceImpl)),
wire.Bind(new(component), new(*builder.Server)),
wire.Struct(new(builder.Server), "*"),
)
return nil, nil
}

func newMockBuilder(c Config) (component, error) {
wire.Build(
providers,
buildermock.NewBuilderServiceMock,
wire.Bind(new(ubuilder.Service), new(*buildermock.BuilderServiceMock)),
wire.Bind(new(component), new(*builder.Server)),
wire.Struct(new(builder.Server), "*"),
)
Expand Down
33 changes: 30 additions & 3 deletions cmd/wire_gen.go

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

12 changes: 6 additions & 6 deletions pkg/usecase/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/traPtitech/neoshowcase/pkg/util/cli"
)

func (s *builderService) startBuild(req *domain.StartBuildRequest) error {
func (s *ServiceImpl) startBuild(req *domain.StartBuildRequest) error {
s.statusLock.Lock()
defer s.statusLock.Unlock()

Expand Down Expand Up @@ -67,7 +67,7 @@ type buildStep struct {
fn func(ctx context.Context) error
}

func (s *builderService) buildSteps(st *state) ([]buildStep, error) {
func (s *ServiceImpl) buildSteps(st *state) ([]buildStep, error) {
var steps []buildStep

steps = append(steps, buildStep{"Repository Clone", func(ctx context.Context) error {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (s *builderService) buildSteps(st *state) ([]buildStep, error) {
return steps, nil
}

func (s *builderService) process(ctx context.Context, st *state) domain.BuildStatus {
func (s *ServiceImpl) process(ctx context.Context, st *state) domain.BuildStatus {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go s.buildPingLoop(ctx, st.build.ID)
Expand Down Expand Up @@ -195,7 +195,7 @@ func (s *builderService) process(ctx context.Context, st *state) domain.BuildSta
return domain.BuildStatusSucceeded
}

func (s *builderService) buildPingLoop(ctx context.Context, buildID string) {
func (s *ServiceImpl) buildPingLoop(ctx context.Context, buildID string) {
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
for {
Expand All @@ -211,7 +211,7 @@ func (s *builderService) buildPingLoop(ctx context.Context, buildID string) {
}
}

func (s *builderService) finalize(ctx context.Context, st *state, status domain.BuildStatus) {
func (s *ServiceImpl) finalize(ctx context.Context, st *state, status domain.BuildStatus) {
err := s.client.SaveBuildLog(ctx, st.build.ID, st.logWriter.buf.Bytes())
if err != nil {
log.Errorf("failed to save build log: %+v", err)
Expand All @@ -231,7 +231,7 @@ func (s *builderService) finalize(ctx context.Context, st *state, status domain.
}
}

func (s *builderService) fetchImageSize(ctx context.Context, st *state) (int64, error) {
func (s *ServiceImpl) fetchImageSize(ctx context.Context, st *state) (int64, error) {
r := s.imageConfig.NewRegistry()
ref, err := ref.New(s.destImage(st.app, st.build))
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/usecase/builder/build_buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func dockerignoreExists(dir string) bool {
return err == nil && !info.IsDir()
}

func (s *builderService) authSessions() []session.Attachable {
func (s *ServiceImpl) authSessions() []session.Attachable {
if s.imageConfig.Registry.Username == "" && s.imageConfig.Registry.Password == "" {
return nil
}
Expand All @@ -107,7 +107,7 @@ func (s *builderService) authSessions() []session.Attachable {
}, nil)}
}

func (s *builderService) solveDockerfile(
func (s *ServiceImpl) solveDockerfile(
ctx context.Context,
dest string,
contextDir string,
Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *builderService) solveDockerfile(
return err
}

func (s *builderService) buildRuntimeCmd(
func (s *ServiceImpl) buildRuntimeCmd(
ctx context.Context,
st *state,
ch chan *buildkit.SolveStatus,
Expand Down Expand Up @@ -222,7 +222,7 @@ func (s *builderService) buildRuntimeCmd(
)
}

func (s *builderService) buildRuntimeDockerfile(
func (s *ServiceImpl) buildRuntimeDockerfile(
ctx context.Context,
st *state,
ch chan *buildkit.SolveStatus,
Expand All @@ -240,7 +240,7 @@ func (s *builderService) buildRuntimeDockerfile(
)
}

func (s *builderService) buildStaticCmd(
func (s *ServiceImpl) buildStaticCmd(
ctx context.Context,
st *state,
ch chan *buildkit.SolveStatus,
Expand Down Expand Up @@ -302,7 +302,7 @@ func (s *builderService) buildStaticCmd(
)
}

func (s *builderService) buildStaticDockerfile(
func (s *ServiceImpl) buildStaticDockerfile(
ctx context.Context,
st *state,
ch chan *buildkit.SolveStatus,
Expand Down
4 changes: 2 additions & 2 deletions pkg/usecase/builder/build_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/traPtitech/neoshowcase/pkg/domain"
)

func (s *builderService) buildRuntimeBuildpack(
func (s *ServiceImpl) buildRuntimeBuildpack(
ctx context.Context,
st *state,
bc *domain.BuildConfigRuntimeBuildpack,
Expand All @@ -20,7 +20,7 @@ func (s *builderService) buildRuntimeBuildpack(
return err
}

func (s *builderService) buildStaticBuildpackPack(
func (s *ServiceImpl) buildStaticBuildpackPack(
ctx context.Context,
st *state,
bc *domain.BuildConfigStaticBuildpack,
Expand Down
2 changes: 1 addition & 1 deletion pkg/usecase/builder/build_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/traPtitech/neoshowcase/pkg/domain"
)

func (s *builderService) cloneRepository(ctx context.Context, st *state) error {
func (s *ServiceImpl) cloneRepository(ctx context.Context, st *state) error {
repo, err := git.PlainInit(st.repositoryTempDir, false)
if err != nil {
return errors.Wrap(err, "failed to init repository")
Expand Down
2 changes: 1 addition & 1 deletion pkg/usecase/builder/build_save_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/traPtitech/neoshowcase/pkg/domain"
)

func (s *builderService) saveArtifact(ctx context.Context, st *state) error {
func (s *ServiceImpl) saveArtifact(ctx context.Context, st *state) error {
// Open artifact
filename := st.artifactTempFile.Name()
stat, err := os.Stat(filename)
Expand Down
4 changes: 2 additions & 2 deletions pkg/usecase/builder/build_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/tonistiigi/fsutil"
)

func (s *builderService) buildStaticExtract(
func (s *ServiceImpl) buildStaticExtract(
ctx context.Context,
st *state,
ch chan *buildkit.SolveStatus,
Expand Down Expand Up @@ -46,7 +46,7 @@ func (s *builderService) buildStaticExtract(
return err
}

func (s *builderService) buildStaticCleanup(
func (s *ServiceImpl) buildStaticCleanup(
ctx context.Context,
st *state,
) error {
Expand Down
61 changes: 61 additions & 0 deletions pkg/usecase/builder/mock/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package mock

import (
"context"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb"
"github.com/traPtitech/neoshowcase/pkg/util/retry"
)

type BuilderServiceMock struct {
client domain.ControllerBuilderServiceClient
response chan *pb.BuilderResponse

close func()
}

func NewBuilderServiceMock(client domain.ControllerBuilderServiceClient) *BuilderServiceMock {
return &BuilderServiceMock{
client: client,
}
}

func (bs *BuilderServiceMock) Start(_ context.Context) error {
ctx, cancel := context.WithCancel(context.Background())
bs.close = func() {
cancel()
}
bs.response = make(chan *pb.BuilderResponse)
retry.Do(ctx, func(ctx context.Context) error {
return bs.client.ConnectBuilder(ctx, bs.onRequest, bs.response)
}, "connect to controller")
return nil
}

func (bs *BuilderServiceMock) Shutdown(ctx context.Context) error {
bs.close()
return nil
}

func (bs *BuilderServiceMock) onRequest(req *pb.BuilderRequest) {
// always cancel build
switch req.Type {
case pb.BuilderRequest_START_BUILD:
b := req.Body.(*pb.BuilderRequest_StartBuild).StartBuild
bs.response <- &pb.BuilderResponse{
Type: pb.BuilderResponse_BUILD_SETTLED,
Body: &pb.BuilderResponse_Settled{
Settled: &pb.BuildSettled{
BuildId: b.Build.Id,
Status: pb.BuildStatus_CANCELLED,
},
},
}
bs.client.SaveBuildLog(context.Background(), b.Build.Id, []byte("Build skipped by admin"))
case pb.BuilderRequest_CANCEL_BUILD:
// no-op
default:
panic("unexpected request type")
}
}
Loading

0 comments on commit 32cb9f3

Please sign in to comment.