Skip to content

Commit

Permalink
Refactor test suite (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnstoppableMango authored Jul 28, 2024
1 parent fcebae7 commit a25013c
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 82 deletions.
16 changes: 16 additions & 0 deletions tests/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tests

import (
"github.com/blang/semver"
"github.com/pulumi/pulumi-go-provider/integration"

baremetal "github.com/unmango/pulumi-baremetal/provider"
)

func NewIntegrationProvider() integration.Server {
return integration.NewServer(
baremetal.Name,
semver.MustParse("1.0.0"),
baremetal.Provider(),
)
}
21 changes: 21 additions & 0 deletions tests/provider_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package tests

import (
"context"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var (
provisioner testProvisioner
)

func TestProvider(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Provider Suite")
}

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

err = prov.Start(ctx)
Expect(err).NotTo(HaveOccurred())
provisioner = prov
})

var _ = AfterSuite(func(ctx context.Context) {
By("stopping the provisioner")
err := provisioner.Stop(ctx)
Expect(err).NotTo(HaveOccurred())
})
83 changes: 8 additions & 75 deletions tests/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,22 @@
package tests

import (
"context"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/blang/semver"
p "github.com/pulumi/pulumi-go-provider"
"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"
)

var _ = Describe("Provider", Ordered, func() {
var server integration.Server
var provisioner *testProvisioner

BeforeAll(func(ctx context.Context) {
By("creating a provisioner")
prov, err := NewTestProvisioner(ctx, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())

err = prov.Start(ctx)
Expect(err).NotTo(HaveOccurred())
provisioner = prov

By("creating a provider server")
server = integration.NewServer(
baremetal.Name,
semver.MustParse("1.0.0"),
baremetal.Provider(),
)
})

BeforeEach(func(ctx context.Context) {
ip, err := provisioner.ct.ContainerIP(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(ip).NotTo(BeEmpty())
port := provisioner.port.Int()

By("configuring the provider")
err = server.Configure(p.ConfigureRequest{
Args: resource.PropertyMap{
"address": resource.NewStringProperty(ip),
"port": resource.NewNumberProperty(float64(port)),
},
})
Expect(err).NotTo(HaveOccurred())
})

It("should create a tee", func() {
stdin := "Test stdin"
By("generating expected data")

By("creating the resource")
response, err := server.Create(p.CreateRequest{
Urn: urn("Tee"),
Properties: resource.PropertyMap{
"stdin": resource.NewStringProperty(stdin),
"create": resource.NewObjectProperty(resource.PropertyMap{
"files": resource.NewArrayProperty([]resource.PropertyValue{
resource.NewStringProperty("test"),
}),
}),
},
Preview: false,
})

Expect(err).NotTo(HaveOccurred())
Expect(response).NotTo(BeNil())
Expect(response.Properties["stdout"].V).To(Equal("op: OP_CREATE, cmd: COMMAND_TEE, args: []string{\"test\"}, flags: map[string]*baremetalv1alpha1.Flag(nil)"))
})
var _ = Describe("Provider", Ordered, func() {})

AfterAll(func(ctx context.Context) {
By("stopping the provisioner")
err := provisioner.Stop(ctx)
Expect(err).NotTo(HaveOccurred())
})
})
func urn(typ string, mods ...string) resource.URN {
if len(mods) == 0 {
mods = []string{"index"}
}

// urn is a helper function to build an urn for running integration tests.
func urn(typ string) resource.URN {
tok := strings.Join(append(mods, typ), ":")
return resource.NewURN("stack", "proj", "",
tokens.Type("test:cmd:"+typ), "name")
tokens.Type("test:"+tok), "name")
}
38 changes: 31 additions & 7 deletions tests/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package tests

import (
"context"
"errors"
"fmt"
"io"
"os"
"path"
"time"

"github.com/docker/go-connections/nat"
p "github.com/pulumi/pulumi-go-provider"
"github.com/pulumi/pulumi-go-provider/integration"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
tc "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand All @@ -23,15 +27,15 @@ type testProvisioner struct {
port nat.Port
}

func NewTestProvisioner(ctx context.Context, logger io.Writer) (*testProvisioner, error) {
func NewTestProvisioner(ctx context.Context, logger io.Writer) (testProvisioner, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
return testProvisioner{}, err
}

port, err := nat.NewPort(defaultProtocol, "6969")
if err != nil {
return nil, err
return testProvisioner{}, err
}

container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
Expand All @@ -52,20 +56,40 @@ func NewTestProvisioner(ctx context.Context, logger io.Writer) (*testProvisioner
},
})
if err != nil {
return nil, err
return testProvisioner{}, err
}

return &testProvisioner{
return testProvisioner{
ct: container,
port: port,
}, nil
}

func (p *testProvisioner) Start(ctx context.Context) error {
func (p testProvisioner) Start(ctx context.Context) error {
return p.ct.Start(ctx)
}

func (p *testProvisioner) Stop(ctx context.Context) error {
func (p testProvisioner) Stop(ctx context.Context) error {
timeout := time.Duration(10 * time.Second)
return p.ct.Stop(ctx, &timeout)
}

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

if ip == "" {
return errors.New("container returned empty ip")
}

port := prov.port.Int()

return server.Configure(p.ConfigureRequest{
Args: resource.PropertyMap{
"address": resource.NewStringProperty(ip),
"port": resource.NewNumberProperty(float64(port)),
},
})
}
48 changes: 48 additions & 0 deletions tests/tee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tests

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

p "github.com/pulumi/pulumi-go-provider"
"github.com/pulumi/pulumi-go-provider/integration"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
)

var _ = Describe("Tee", Ordered, func() {
var server integration.Server

BeforeEach(func(ctx context.Context) {
By("creating a provider server")
server = NewIntegrationProvider()

By("configuring the provider")
err := provisioner.ConfigureProvider(ctx, server)
Expect(err).NotTo(HaveOccurred())
})

It("should create a tee", func() {
stdin := "Test stdin"
By("generating expected data")

By("creating the resource")
response, err := server.Create(p.CreateRequest{
Urn: urn("Tee", "cmd"),
Properties: resource.PropertyMap{
"stdin": resource.NewStringProperty(stdin),
"create": resource.NewObjectProperty(resource.PropertyMap{
"files": resource.NewArrayProperty([]resource.PropertyValue{
resource.NewStringProperty("test"),
}),
}),
},
Preview: false,
})

Expect(err).NotTo(HaveOccurred())
Expect(response).NotTo(BeNil())
Expect(response.Properties["stdout"].V).To(Equal("op: OP_CREATE, cmd: COMMAND_TEE, args: []string{\"test\"}, flags: map[string]*baremetalv1alpha1.Flag(nil)"))
})
})

0 comments on commit a25013c

Please sign in to comment.