Skip to content

Commit

Permalink
Happy path implementation (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnstoppableMango authored Jul 29, 2024
1 parent a25013c commit b291cc9
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 114 deletions.
108 changes: 59 additions & 49 deletions gen/go/unmango/baremetal/v1alpha1/command.pb.go

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

1 change: 1 addition & 0 deletions proto/unmango/baremetal/v1alpha1/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ message CommandRequest {
Command command = 2;
repeated string args = 3;
map<string, Flag> flags = 4;
string stdin = 5;
}

message CommandResponse {
Expand Down
1 change: 1 addition & 0 deletions provider/cmd/provisioner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var rootCmd = &cobra.Command{
return err
}

slog.SetDefault(log)
log.Info("serving", "network", network, "address", address, "verbose", verbose)
return provisioner.Serve(lis)
},
Expand Down
1 change: 1 addition & 0 deletions provider/pkg/provider/cmd/tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (state *TeeState) create(ctx context.Context, input TeeArgs) error {
Command: pb.Command_COMMAND_TEE,
Args: input.Create.Files,
Flags: map[string]*pb.Flag{},
Stdin: input.Stdin,
})
if err != nil {
return err
Expand Down
41 changes: 37 additions & 4 deletions provider/pkg/provisioner/cmd/service.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,59 @@
package cmd

import (
"bytes"
"context"
"fmt"
"log/slog"
"os/exec"
"strings"

pb "github.com/unmango/pulumi-baremetal/gen/go/unmango/baremetal/v1alpha1"
)

type service struct {
pb.UnimplementedCommandServiceServer
log *slog.Logger
}

func NewServer() pb.CommandServiceServer {
return &service{}
return &service{log: slog.Default()}
}

// Command implements baremetalv1alpha1.CommandServiceServer.
func (c *service) Command(ctx context.Context, req *pb.CommandRequest) (*pb.CommandResponse, error) {
switch req.Command {
log := c.log.With("op", req.Op)
bin, err := getBin(req.Command)
if err != nil {
return nil, err
}

cmd := exec.CommandContext(ctx, bin, req.Args...)
log = log.With("bin", bin, "args", req.Args)

stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdin = strings.NewReader(req.Stdin)
cmd.Stdout = stdout
cmd.Stderr = stderr

log.Info("executing command")
err = cmd.Run()
if err != nil {
log.Error("command failed", "err", err)
}

return &pb.CommandResponse{
Stdout: stdout.String(),
Stderr: stderr.String(),
}, nil
}

func getBin(cmd pb.Command) (string, error) {
switch cmd {
case pb.Command_COMMAND_TEE:
return tee(ctx, req)
return "tee", nil
}

return nil, fmt.Errorf("unrecognized command: %s", req.Command)
return "", fmt.Errorf("unrecognized command: %s", cmd)
}
18 changes: 0 additions & 18 deletions provider/pkg/provisioner/cmd/tee.go

This file was deleted.

2 changes: 1 addition & 1 deletion tests/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewLogger(w io.Writer) tc.Logging {

// Accept implements testcontainers.LogConsumer.
func (w *writerConsumer) Accept(log tc.Log) {
_, _ = w.Write(log.Content)
_, _ = w.Write(append(log.Content, '\n'))
}

// Printf implements testcontainers.Logging.
Expand Down
14 changes: 14 additions & 0 deletions tests/provider.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package tests

import (
"strings"

"github.com/blang/semver"
"github.com/pulumi/pulumi-go-provider/integration"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"

baremetal "github.com/unmango/pulumi-baremetal/provider"
)
Expand All @@ -14,3 +18,13 @@ func NewIntegrationProvider() integration.Server {
baremetal.Provider(),
)
}

func urn(typ string, mods ...string) resource.URN {
if len(mods) == 0 {
mods = []string{"index"}
}

tok := strings.Join(append(mods, typ), ":")
return resource.NewURN("stack", "proj", "",
tokens.Type("test:"+tok), "name")
}
4 changes: 3 additions & 1 deletion tests/provider_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"context"
"os"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -19,9 +20,10 @@ func TestProvider(t *testing.T) {

var _ = BeforeSuite(func(ctx context.Context) {
By("creating a provisioner")
prov, err := NewTestProvisioner(ctx, GinkgoWriter)
prov, err := NewTestProvisioner(ctx, os.Stdout)
Expect(err).NotTo(HaveOccurred())

By("starting the provisioner")
err = prov.Start(ctx)
Expect(err).NotTo(HaveOccurred())
provisioner = prov
Expand Down
22 changes: 0 additions & 22 deletions tests/provider_test.go

This file was deleted.

19 changes: 19 additions & 0 deletions tests/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func NewTestProvisioner(ctx context.Context, logger io.Writer) (testProvisioner,
Cmd: []string{
"--network", defaultProtocol,
"--address", fmt.Sprintf("%s:%d", "0.0.0.0", port.Int()),
"--verbose",
},
ExposedPorts: []string{port.Port()},
WaitingFor: wait.ForListeningPort(port),
Expand Down Expand Up @@ -74,6 +75,24 @@ func (p testProvisioner) Stop(ctx context.Context) error {
return p.ct.Stop(ctx, &timeout)
}

func (p testProvisioner) Exec(ctx context.Context, args ...string) error {
code, output, err := p.ct.Exec(ctx, args)
if err != nil {
return err
}

out, err := io.ReadAll(output)
if err != nil {
return err
}

if code != 0 {
return fmt.Errorf("unexpected return code: %d, output: %s", code, out)
}

return nil
}

func (prov testProvisioner) ConfigureProvider(ctx context.Context, server integration.Server) error {
ip, err := prov.ct.ContainerIP(ctx)
if err != nil {
Expand Down
Loading

0 comments on commit b291cc9

Please sign in to comment.