github.com/splunk/qbec@v0.15.2/vm/internal/ds/factory/lazy_test.go (about)

     1  /*
     2     Copyright 2021 Splunk Inc.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package factory
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/splunk/qbec/vm/datasource"
    24  	"github.com/splunk/qbec/vm/internal/ds"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  type delegate struct {
    30  	initErr      error
    31  	resolveError error
    32  	closeError   error
    33  }
    34  
    35  func (d *delegate) Init(_ datasource.ConfigProvider) error {
    36  	return d.initErr
    37  }
    38  
    39  func (d *delegate) Name() string {
    40  	return "me"
    41  }
    42  
    43  func (d *delegate) Resolve(path string) (string, error) {
    44  	return fmt.Sprintf("resolved:%s", path), d.resolveError
    45  }
    46  
    47  func (d *delegate) Close() error {
    48  	return d.closeError
    49  }
    50  
    51  var _ ds.DataSourceWithLifecycle = &delegate{}
    52  
    53  var cp = func(name string) (string, error) {
    54  	return "", nil
    55  }
    56  
    57  func TestLazySuccess(t *testing.T) {
    58  	d := &delegate{}
    59  	l := makeLazy(d)
    60  	assert.Equal(t, "me", l.Name())
    61  	err := l.Init(cp)
    62  	require.NoError(t, err)
    63  	x, err := l.Resolve("/foo")
    64  	require.NoError(t, err)
    65  	assert.Equal(t, "resolved:/foo", x)
    66  }
    67  
    68  func TestLazyPropagatesInitError(t *testing.T) {
    69  	d := &delegate{initErr: fmt.Errorf("init error")}
    70  	l := makeLazy(d)
    71  	err := l.Init(cp)
    72  	require.NoError(t, err)
    73  	_, err = l.Resolve("/foo")
    74  	require.Error(t, err)
    75  	assert.Contains(t, err.Error(), "init error")
    76  }
    77  
    78  func TestLazyPropagatesResolveError(t *testing.T) {
    79  	d := &delegate{resolveError: fmt.Errorf("resolve error")}
    80  	l := makeLazy(d)
    81  	err := l.Init(cp)
    82  	require.NoError(t, err)
    83  	_, err = l.Resolve("/foo")
    84  	require.Error(t, err)
    85  	assert.Contains(t, err.Error(), "resolve error")
    86  }
    87  
    88  func TestLazyPropagatesCloseError(t *testing.T) {
    89  	d := &delegate{closeError: fmt.Errorf("close error")}
    90  	l := makeLazy(d)
    91  	err := l.Init(cp)
    92  	require.NoError(t, err)
    93  	_, err = l.Resolve("/foo")
    94  	require.NoError(t, err)
    95  	err = l.Close()
    96  	require.Error(t, err)
    97  	assert.Contains(t, err.Error(), "close error")
    98  }