Skip to content

Commit

Permalink
Add more unit tests (#1970)
Browse files Browse the repository at this point in the history
  • Loading branch information
hariso authored Nov 12, 2024
1 parent 5769934 commit 5c42c30
Show file tree
Hide file tree
Showing 5 changed files with 927 additions and 16 deletions.
20 changes: 10 additions & 10 deletions pkg/plugin/connector/builtin/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ var DefaultBuiltinConnectors = map[string]sdk.Connector{
"github.com/conduitio/conduit-connector-s3": s3.Connector,
}

type Registry struct {
logger log.CtxLogger

connectors map[string]sdk.Connector
// plugins stores plugin blueprints in a 2D map, first key is the plugin
// name, the second key is the plugin version
plugins map[string]map[string]blueprint
service *connutils.SchemaService
}

type blueprint struct {
fullName plugin.FullName
specification pconnector.Specification
Expand Down Expand Up @@ -89,6 +79,16 @@ func newDispenserFactory(conn sdk.Connector) dispenserFactory {
}
}

type Registry struct {
logger log.CtxLogger

connectors map[string]sdk.Connector
// plugins stores plugin blueprints in a 2D map, first key is the plugin
// name, the second key is the plugin version
plugins map[string]map[string]blueprint
service *connutils.SchemaService
}

func NewRegistry(logger log.CtxLogger, connectors map[string]sdk.Connector, service *connutils.SchemaService) *Registry {
logger = logger.WithComponentFromType(Registry{})
// The built-in plugins use Conduit's own schema service
Expand Down
110 changes: 110 additions & 0 deletions pkg/plugin/connector/builtin/registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builtin

import (
"context"
"testing"

"github.com/conduitio/conduit-connector-protocol/pconnector"
"github.com/conduitio/conduit/pkg/foundation/cerrors"
"github.com/conduitio/conduit/pkg/foundation/log"
"github.com/conduitio/conduit/pkg/plugin"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/matryer/is"
)

func TestRegistry_InitList(t *testing.T) {
is := is.New(t)
ctx := context.Background()

underTest := NewRegistry(log.Nop(), DefaultBuiltinConnectors, nil)
underTest.Init(ctx)

specs := underTest.List()

is.Equal(len(DefaultBuiltinConnectors), len(specs))
for _, gotSpec := range specs {
wantSpec := DefaultBuiltinConnectors["github.com/conduitio/conduit-connector-"+gotSpec.Name].NewSpecification()
is.Equal(
"",
cmp.Diff(
pconnector.Specification(wantSpec),
gotSpec,
cmpopts.IgnoreFields(pconnector.Specification{}, "SourceParams", "DestinationParams"),
),
)
}
}

func TestRegistry_NewDispenser_PluginNotFound(t *testing.T) {
testCases := []struct {
name string
pluginName string
wantErr bool
}{
{
name: "non-existing plugin",
pluginName: "builtin:foobar",
wantErr: true,
},
{
name: "plugin exists, version doesn't",
pluginName: "builtin:[email protected]",
wantErr: true,
},
{
name: "existing plugin, no builtin prefix, no version",
pluginName: "file",
wantErr: false,
},
{
name: "existing plugin, with builtin prefix, no version",
pluginName: "builtin:file",
wantErr: false,
},
{
name: "existing plugin, with builtin prefix, with version",
pluginName: func() string {
v := DefaultBuiltinConnectors["github.com/conduitio/conduit-connector-file"].
NewSpecification().
Version
return "builtin:file@" + v
}(),
wantErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
is := is.New(t)
ctx := context.Background()

underTest := NewRegistry(log.Nop(), DefaultBuiltinConnectors, nil)
underTest.Init(ctx)

dispenser, err := underTest.NewDispenser(log.Nop(), plugin.FullName(tc.pluginName), pconnector.PluginConfig{})
if tc.wantErr {
is.True(cerrors.Is(err, plugin.ErrPluginNotFound))
is.True(dispenser == nil)
return
}

is.NoErr(err)
is.True(dispenser != nil)
})
}
}
Loading

0 comments on commit 5c42c30

Please sign in to comment.