Skip to content

Commit

Permalink
fix: throw error if resource type is not known (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Rusch (cluetec GmbH) authored Nov 10, 2023
1 parent a8257ce commit cb0d32a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
6 changes: 5 additions & 1 deletion internal/destination/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package destination

import (
"errors"
"github.com/cluetec/lifeboat/internal/config"
"github.com/cluetec/lifeboat/internal/destination/filesystem"
"io"
Expand All @@ -29,6 +30,7 @@ type Destination struct {

func New(c config.DestinationConfig) (*Destination, error) {
d := Destination{}

if c.Type == filesystem.Type {
filesystemConfig, err := filesystem.NewConfig(c.ResourceConfig)
if err != nil {
Expand All @@ -43,7 +45,9 @@ func New(c config.DestinationConfig) (*Destination, error) {
slog.Error("error while initializing writer interface for filesystem destination", "error", err)
return nil, err
}

return &d, nil
}

return &d, nil
return nil, errors.New("destination type not known")
}
54 changes: 54 additions & 0 deletions internal/destination/destination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 cluetec GmbH
*
* 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 destination

import (
"github.com/cluetec/lifeboat/internal/config"
"reflect"
"testing"
)

func TestNew(t *testing.T) {
type args struct {
c config.DestinationConfig
}
tests := []struct {
name string
args args
want *Destination
wantErr bool
}{
{
name: "Throw error if type is not known",
args: args{c: config.DestinationConfig{Type: "not-known-type"}},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.c)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() got = %v, want %v", got, tt.want)
}
})
}
}
5 changes: 4 additions & 1 deletion internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package source

import (
"errors"
"github.com/cluetec/lifeboat/internal/config"
"github.com/cluetec/lifeboat/internal/source/filesystem"
"io"
Expand All @@ -29,6 +30,7 @@ type Source struct {

func New(c config.SourceConfig) (*Source, error) {
s := Source{}

if c.Type == filesystem.Type {
filesystemConfig, err := filesystem.NewConfig(c.ResourceConfig)
if err != nil {
Expand All @@ -43,7 +45,8 @@ func New(c config.SourceConfig) (*Source, error) {
slog.Error("error while initializing reader interface for filesystem source", "error", err)
return nil, err
}
return &s, nil
}

return &s, nil
return nil, errors.New("source type not known")
}
54 changes: 54 additions & 0 deletions internal/source/source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 cluetec GmbH
*
* 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 source

import (
"github.com/cluetec/lifeboat/internal/config"
"reflect"
"testing"
)

func TestNew(t *testing.T) {
type args struct {
c config.SourceConfig
}
tests := []struct {
name string
args args
want *Source
wantErr bool
}{
{
name: "Throw error if type is not known",
args: args{c: config.SourceConfig{Type: "not-known-type"}},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.c)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit cb0d32a

Please sign in to comment.