-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #977 from traPtitech/feat/builder-mock
Builder mockの作成
- Loading branch information
Showing
11 changed files
with
145 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Oops, something went wrong.