github.com/annwntech/go-micro/v2@v2.9.5/config/source/env/env_test.go (about)

     1  package env
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/annwntech/go-micro/v2/config/source"
    10  )
    11  
    12  func TestEnv_Read(t *testing.T) {
    13  	expected := map[string]map[string]string{
    14  		"database": {
    15  			"host":       "localhost",
    16  			"password":   "password",
    17  			"datasource": "user:password@tcp(localhost:port)/db?charset=utf8mb4&parseTime=True&loc=Local",
    18  		},
    19  	}
    20  
    21  	os.Setenv("DATABASE_HOST", "localhost")
    22  	os.Setenv("DATABASE_PASSWORD", "password")
    23  	os.Setenv("DATABASE_DATASOURCE", "user:password@tcp(localhost:port)/db?charset=utf8mb4&parseTime=True&loc=Local")
    24  
    25  	source := NewSource()
    26  	c, err := source.Read()
    27  	if err != nil {
    28  		t.Error(err)
    29  	}
    30  
    31  	var actual map[string]interface{}
    32  	if err := json.Unmarshal(c.Data, &actual); err != nil {
    33  		t.Error(err)
    34  	}
    35  
    36  	actualDB := actual["database"].(map[string]interface{})
    37  
    38  	for k, v := range expected["database"] {
    39  		a := actualDB[k]
    40  
    41  		if a != v {
    42  			t.Errorf("expected %v got %v", v, a)
    43  		}
    44  	}
    45  }
    46  
    47  func TestEnvvar_Prefixes(t *testing.T) {
    48  	os.Setenv("APP_DATABASE_HOST", "localhost")
    49  	os.Setenv("APP_DATABASE_PASSWORD", "password")
    50  	os.Setenv("VAULT_ADDR", "vault:1337")
    51  	os.Setenv("MICRO_REGISTRY", "mdns")
    52  
    53  	var prefixtests = []struct {
    54  		prefixOpts   []source.Option
    55  		expectedKeys []string
    56  	}{
    57  		{[]source.Option{WithPrefix("APP", "MICRO")}, []string{"app", "micro"}},
    58  		{[]source.Option{WithPrefix("MICRO"), WithStrippedPrefix("APP")}, []string{"database", "micro"}},
    59  		{[]source.Option{WithPrefix("MICRO"), WithStrippedPrefix("APP")}, []string{"database", "micro"}},
    60  	}
    61  
    62  	for _, pt := range prefixtests {
    63  		source := NewSource(pt.prefixOpts...)
    64  
    65  		c, err := source.Read()
    66  		if err != nil {
    67  			t.Error(err)
    68  		}
    69  
    70  		var actual map[string]interface{}
    71  		if err := json.Unmarshal(c.Data, &actual); err != nil {
    72  			t.Error(err)
    73  		}
    74  
    75  		// assert other prefixes ignored
    76  		if l := len(actual); l != len(pt.expectedKeys) {
    77  			t.Errorf("expected %v top keys, got %v", len(pt.expectedKeys), l)
    78  		}
    79  
    80  		for _, k := range pt.expectedKeys {
    81  			if !containsKey(actual, k) {
    82  				t.Errorf("expected key %v, not found", k)
    83  			}
    84  		}
    85  	}
    86  }
    87  
    88  func TestEnvvar_WatchNextNoOpsUntilStop(t *testing.T) {
    89  	src := NewSource(WithStrippedPrefix("GOMICRO_"))
    90  	w, err := src.Watch()
    91  	if err != nil {
    92  		t.Error(err)
    93  	}
    94  
    95  	go func() {
    96  		time.Sleep(50 * time.Millisecond)
    97  		w.Stop()
    98  	}()
    99  
   100  	if _, err := w.Next(); err != source.ErrWatcherStopped {
   101  		t.Errorf("expected watcher stopped error, got %v", err)
   102  	}
   103  }
   104  
   105  func containsKey(m map[string]interface{}, s string) bool {
   106  	for k := range m {
   107  		if k == s {
   108  			return true
   109  		}
   110  	}
   111  	return false
   112  }