Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more unit tests #1970

Merged
merged 7 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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