diff --git a/tests/provider.go b/tests/provider.go new file mode 100644 index 00000000..52a77546 --- /dev/null +++ b/tests/provider.go @@ -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(), + ) +} diff --git a/tests/provider_suite_test.go b/tests/provider_suite_test.go index 650802d3..fd458a5b 100644 --- a/tests/provider_suite_test.go +++ b/tests/provider_suite_test.go @@ -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()) +}) diff --git a/tests/provider_test.go b/tests/provider_test.go index 353c43cd..dee8bcd6 100644 --- a/tests/provider_test.go +++ b/tests/provider_test.go @@ -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") } diff --git a/tests/provisioner.go b/tests/provisioner.go index 748ea3e6..7e7846dc 100644 --- a/tests/provisioner.go +++ b/tests/provisioner.go @@ -2,6 +2,7 @@ package tests import ( "context" + "errors" "fmt" "io" "os" @@ -9,6 +10,9 @@ import ( "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" ) @@ -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{ @@ -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)), + }, + }) +} diff --git a/tests/tee_test.go b/tests/tee_test.go new file mode 100644 index 00000000..eb3556ef --- /dev/null +++ b/tests/tee_test.go @@ -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)")) + }) +})