github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/config/config_test.go (about)

     1  // Copyright 2023 Northern.tech AS
     2  //
     3  //	Licensed under the Apache License, Version 2.0 (the "License");
     4  //	you may not use this file except in compliance with the License.
     5  //	You may obtain a copy of the License at
     6  //
     7  //	    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //	Unless required by applicable law or agreed to in writing, software
    10  //	distributed under the License is distributed on an "AS IS" BASIS,
    11  //	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //	See the License for the specific language governing permissions and
    13  //	limitations under the License.
    14  package config
    15  
    16  import (
    17  	"errors"
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  type MockConfigReader struct{}
    23  
    24  func (m *MockConfigReader) Get(key string) interface{}                      { return nil }
    25  func (m *MockConfigReader) GetBool(key string) bool                         { return true }
    26  func (m *MockConfigReader) GetFloat64(key string) float64                   { return 1.1 }
    27  func (m *MockConfigReader) GetInt(key string) int                           { return 1 }
    28  func (m *MockConfigReader) GetString(key string) string                     { return "some string" }
    29  func (m *MockConfigReader) GetStringMap(key string) map[string]interface{}  { return nil }
    30  func (m *MockConfigReader) GetStringMapString(key string) map[string]string { return nil }
    31  func (m *MockConfigReader) GetStringSlice(key string) []string              { return []string{} }
    32  func (m *MockConfigReader) GetTime(key string) time.Time                    { return time.Now() }
    33  func (m *MockConfigReader) GetDuration(key string) time.Duration            { return time.Second }
    34  func (m *MockConfigReader) IsSet(key string) bool                           { return true }
    35  
    36  type MockConfigWriter struct {
    37  	vals map[string]interface{}
    38  }
    39  
    40  func (m *MockConfigWriter) SetDefault(key string, val interface{}) {
    41  	m.vals[key] = val
    42  }
    43  
    44  func (m *MockConfigWriter) Set(key string, val interface{}) {
    45  	m.vals[key] = val
    46  }
    47  
    48  func NewMockWriter() *MockConfigWriter {
    49  	return &MockConfigWriter{
    50  		make(map[string]interface{}),
    51  	}
    52  }
    53  
    54  func TestValidateConfig(t *testing.T) {
    55  
    56  	err := errors.New("test error")
    57  
    58  	testList := []struct {
    59  		out        error
    60  		c          Reader
    61  		validators []Validator
    62  	}{
    63  		{nil, &MockConfigReader{}, []Validator{}},
    64  		{err, &MockConfigReader{}, []Validator{func(c Reader) error { return err }}},
    65  	}
    66  
    67  	for _, test := range testList {
    68  		if ValidateConfig(test.c, test.validators...) != test.out {
    69  			t.FailNow()
    70  		}
    71  	}
    72  }
    73  
    74  func TestSetDefaultConfigs(t *testing.T) {
    75  	defaults := []Default{
    76  		{"foo", "bar"},
    77  		{"baz", 1},
    78  	}
    79  
    80  	c := NewMockWriter()
    81  
    82  	SetDefaults(c, defaults)
    83  
    84  	val_foo, ok := c.vals["foo"]
    85  	if ok != true || val_foo != "bar" {
    86  		t.FailNow()
    87  	}
    88  
    89  	val_baz, ok := c.vals["baz"]
    90  	if ok != true || val_baz != 1 {
    91  		t.FailNow()
    92  	}
    93  }
    94  
    95  func TestFromConfigFile(t *testing.T) {
    96  	if err := FromConfigFile("testdata/config-empty.yaml", []Default{}); err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	err := FromConfigFile("", nil)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	err = FromConfigFile("non-existing-file.yaml", nil)
   106  	if err == nil {
   107  		t.FailNow()
   108  	}
   109  }