github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/test/config_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package test
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  	"fmt"
    10  	"reflect"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestConfig(t *testing.T) {
    17  	TrySuite(t, testConfig, retryCount)
    18  }
    19  
    20  func testConfig(t *T) {
    21  	t.Parallel()
    22  	serv := NewServer(t, WithLogin())
    23  	defer serv.Close()
    24  	if err := serv.Run(); err != nil {
    25  		return
    26  	}
    27  
    28  	cmd := serv.Command()
    29  
    30  	t.T().Run("Test non existing record", func(tee *testing.T) {
    31  		if err := Try("Calling micro config read", t, func() ([]byte, error) {
    32  			outp, err := cmd.Exec("config", "get", "somekey")
    33  			if err == nil {
    34  				return outp, errors.New("config gete should fail")
    35  			}
    36  			if !strings.Contains(string(outp), "Not found") {
    37  				return outp, fmt.Errorf("Output should be 'Not found\n', got %v", string(outp))
    38  			}
    39  			return outp, nil
    40  		}, 5*time.Second); err != nil {
    41  			return
    42  		}
    43  	})
    44  
    45  	t.T().Run("Test no dot get set delete", func(tee *testing.T) {
    46  		// This needs to be retried to the the "error listing rules"
    47  		// error log output that happens when the auth service is not yet available.
    48  
    49  		if err := Try("Calling micro config set", t, func() ([]byte, error) {
    50  			outp, err := cmd.Exec("config", "set", "somekey", "val1")
    51  			if err != nil {
    52  				return outp, err
    53  			}
    54  			if string(outp) != "" {
    55  				return outp, fmt.Errorf("Expected no output, got: %v", string(outp))
    56  			}
    57  			return outp, err
    58  		}, 5*time.Second); err != nil {
    59  			return
    60  		}
    61  
    62  		if err := Try("micro config get somekey", t, func() ([]byte, error) {
    63  			outp, err := cmd.Exec("config", "get", "somekey")
    64  			if err != nil {
    65  				return outp, err
    66  			}
    67  			if string(outp) != "val1\n" {
    68  				return outp, errors.New("Expected 'val1\n'")
    69  			}
    70  			return outp, err
    71  		}, 8*time.Second); err != nil {
    72  			return
    73  		}
    74  
    75  		outp, err := cmd.Exec("config", "del", "somekey")
    76  		if err != nil {
    77  			t.Fatalf(string(outp))
    78  			return
    79  		}
    80  		if string(outp) != "" {
    81  			t.Fatalf("Expected '', got: '%v'", string(outp))
    82  			return
    83  		}
    84  
    85  		if err := Try("micro config get somekey", t, func() ([]byte, error) {
    86  			outp, err := cmd.Exec("config", "get", "somekey")
    87  			if err == nil {
    88  				return outp, errors.New("getting somekey should fail")
    89  			}
    90  			if !strings.Contains(string(outp), "Not found") {
    91  				return outp, errors.New("Expected 'Not found'")
    92  			}
    93  			return outp, nil
    94  		}, 8*time.Second); err != nil {
    95  			return
    96  		}
    97  
    98  		t.T().Run("Test dot notation", func(tee *testing.T) {
    99  			outp, err = cmd.Exec("config", "set", "someotherkey.subkey", "otherval1")
   100  			if err != nil {
   101  				t.Fatal(err)
   102  				return
   103  			}
   104  			if string(outp) != "" {
   105  				t.Fatalf("Expected no output, got: %v", string(outp))
   106  				return
   107  			}
   108  
   109  			if err := Try("micro config get someotherkey.subkey", t, func() ([]byte, error) {
   110  				outp, err := cmd.Exec("config", "get", "someotherkey.subkey")
   111  				if err != nil {
   112  					return outp, err
   113  				}
   114  				if string(outp) != "otherval1\n" {
   115  					return outp, errors.New("Expected 'otherval1\n'")
   116  				}
   117  				return outp, err
   118  			}, 8*time.Second); err != nil {
   119  				return
   120  			}
   121  		})
   122  	})
   123  
   124  	t.T().Run("Test basic merge", func(tee *testing.T) {
   125  		outp, err := cmd.Exec("config", "set", "mergekey", `{"a":1}`)
   126  		if err != nil {
   127  			t.Fatal(err)
   128  			return
   129  		}
   130  
   131  		if string(outp) != "" {
   132  			t.Fatalf("Expected no output, got: %v", string(outp))
   133  			return
   134  		}
   135  
   136  		outp, err = cmd.Exec("config", "set", "mergekey", `{"b":2}`)
   137  		if err != nil {
   138  			t.Fatal(err)
   139  			return
   140  		}
   141  		if string(outp) != "" {
   142  			t.Fatalf("Expected no output, got: %v", string(outp))
   143  			return
   144  		}
   145  
   146  		outp, err = cmd.Exec("config", "get", "mergekey")
   147  		if err != nil {
   148  			t.Fatal(err)
   149  			return
   150  		}
   151  		var m map[string]interface{}
   152  		err = json.Unmarshal(outp, &m)
   153  		if err != nil {
   154  			t.Fatal(err)
   155  			return
   156  		}
   157  		expected := map[string]interface{}{
   158  			"a": float64(1),
   159  			"b": float64(2),
   160  		}
   161  		if !reflect.DeepEqual(m, expected) {
   162  			t.Fatalf("Output is %v, expected: %v", m, expected)
   163  			return
   164  		}
   165  	})
   166  
   167  	t.T().Run("Test config escape", func(tee *testing.T) {
   168  		outp, err := cmd.Exec("config", "set", "jsonescape", `"Value with <> signs"`)
   169  		if err != nil {
   170  			t.Fatal(err)
   171  			return
   172  		}
   173  		if string(outp) != "" {
   174  			t.Fatalf("Expected no output, got: %v", string(outp))
   175  			return
   176  		}
   177  		outp, err = cmd.Exec("config", "get", "jsonescape")
   178  		if err != nil {
   179  			t.Fatal(err)
   180  			return
   181  		}
   182  
   183  		if strings.TrimSpace(string(outp)) != "Value with <> signs" {
   184  			t.Fatalf("Expected 'Value with <> signs', got: '%v'", string(outp))
   185  			return
   186  		}
   187  	})
   188  
   189  	t.T().Run("Test complex merge", func(tee *testing.T) {
   190  		outp, err := cmd.Exec("config", "set", "complexmerge", `{"a":1,"b":{"b1":2},"c":3}`)
   191  		if err != nil {
   192  			t.Fatal(err)
   193  			return
   194  		}
   195  		if string(outp) != "" {
   196  			t.Fatalf("Expected no output, got: %v", string(outp))
   197  			return
   198  		}
   199  		outp, err = cmd.Exec("config", "set", "complexmerge", `{"d":4,"b":{"b2":2.2}}`)
   200  		if err != nil {
   201  			t.Fatal(err)
   202  			return
   203  		}
   204  
   205  		expected := map[string]interface{}{
   206  			"a": float64(1),
   207  			"b": map[string]interface{}{
   208  				"b1": float64(2),
   209  				"b2": float64(2.2),
   210  			},
   211  			"c": float64(3),
   212  			"d": float64(4),
   213  		}
   214  		outp, err = cmd.Exec("config", "get", "complexmerge")
   215  		if err != nil {
   216  			t.Fatal(err)
   217  			return
   218  		}
   219  		result := map[string]interface{}{}
   220  		err = json.Unmarshal(outp, &result)
   221  		if err != nil {
   222  			t.Fatal(err)
   223  			return
   224  		}
   225  		if !reflect.DeepEqual(result, expected) {
   226  			t.Fatalf("Expected %v is different from result %v", expected, result)
   227  			return
   228  		}
   229  	})
   230  
   231  	t.T().Run("Test plain old type being overwritten by map", func(tee *testing.T) {
   232  		outp, err := cmd.Exec("config", "set", "pod", "hi")
   233  		if err != nil {
   234  			t.Fatal(err)
   235  			return
   236  		}
   237  		if string(outp) != "" {
   238  			t.Fatalf("Expected no output, got: %v", string(outp))
   239  			return
   240  		}
   241  		outp, err = cmd.Exec("config", "set", "pod", `{"a":1}`)
   242  		if err != nil {
   243  			t.Fatal(err)
   244  			return
   245  		}
   246  
   247  		expected := map[string]interface{}{
   248  			"a": float64(1),
   249  		}
   250  		outp, err = cmd.Exec("config", "get", "pod")
   251  		if err != nil {
   252  			t.Fatal(err)
   253  			return
   254  		}
   255  		result := map[string]interface{}{}
   256  		err = json.Unmarshal(outp, &result)
   257  		if err != nil {
   258  			t.Fatal(err)
   259  			return
   260  		}
   261  		if !reflect.DeepEqual(result, expected) {
   262  			t.Fatalf("Expected %v is different from result %v", expected, result)
   263  			return
   264  		}
   265  	})
   266  
   267  	t.T().Run("Test plain old type being overwritten by pod", func(tee *testing.T) {
   268  		outp, err := cmd.Exec("config", "set", "pod", "hi")
   269  		if err != nil {
   270  			t.Fatal(err)
   271  			return
   272  		}
   273  		if string(outp) != "" {
   274  			t.Fatalf("Expected no output, got: %v", string(outp))
   275  			return
   276  		}
   277  		outp, err = cmd.Exec("config", "set", "pod", "hello")
   278  		if err != nil {
   279  			tee.Fatal(err)
   280  			return
   281  		}
   282  		outp, err = cmd.Exec("config", "get", "pod")
   283  		if err != nil {
   284  			t.Fatal(err)
   285  			return
   286  		}
   287  		expected := "hello"
   288  		if !reflect.DeepEqual(strings.TrimSpace(string(outp)), expected) {
   289  			tee.Fatalf("Expected %v is different from result %v", expected, strings.TrimSpace(string(outp)))
   290  			return
   291  		}
   292  	})
   293  }
   294  
   295  func TestConfigReadFromService(t *testing.T) {
   296  	TrySuite(t, testConfigReadFromService, retryCount)
   297  }
   298  
   299  func testConfigReadFromService(t *T) {
   300  	t.Parallel()
   301  	serv := NewServer(t, WithLogin())
   302  	defer serv.Close()
   303  	if err := serv.Run(); err != nil {
   304  		return
   305  	}
   306  
   307  	cmd := serv.Command()
   308  
   309  	// This needs to be retried to the the "error listing rules"
   310  	// error log output that happens when the auth service is not yet available.
   311  	if err := Try("Calling micro config set", t, func() ([]byte, error) {
   312  		outp, err := cmd.Exec("config", "set", "key.subkey", "val1")
   313  		if err != nil {
   314  			return outp, err
   315  		}
   316  		if string(outp) != "" {
   317  			return outp, fmt.Errorf("Expected no output, got: %v", string(outp))
   318  		}
   319  		outp, err = cmd.Exec("config", "set", "--secret", "key.subkey1", "42")
   320  		if err != nil {
   321  			return outp, err
   322  		}
   323  		if string(outp) != "" {
   324  			return outp, fmt.Errorf("Expected no output, got: %v", string(outp))
   325  		}
   326  		// Testing JSON escape of "<" etc chars.
   327  		outp, err = cmd.Exec("config", "set", "--secret", "key.subkey2", "\"Micro Team <support@m3o.com>\"")
   328  		if err != nil {
   329  			return outp, err
   330  		}
   331  		if string(outp) != "" {
   332  			return outp, fmt.Errorf("Expected no output, got: %v", string(outp))
   333  		}
   334  		// Setting an other key for `val.String` test
   335  		outp, err = cmd.Exec("config", "set", "key.subkey3", "\"Micro Test <test@m3o.com>\"")
   336  		if err != nil {
   337  			return outp, err
   338  		}
   339  		if string(outp) != "" {
   340  			return outp, fmt.Errorf("Expected no output, got: %v", string(outp))
   341  		}
   342  		return outp, err
   343  	}, 5*time.Second); err != nil {
   344  		return
   345  	}
   346  
   347  	// check the value set correctly
   348  	if err := Try("Calling micro config get", t, func() ([]byte, error) {
   349  		outp, err := cmd.Exec("config", "get", "key")
   350  		if err != nil {
   351  			return outp, err
   352  		}
   353  		if !strings.Contains(string(outp), "val1") {
   354  			return outp, fmt.Errorf("Expected output to contain val1, got: %v", string(outp))
   355  		}
   356  
   357  		return outp, err
   358  	}, 5*time.Second); err != nil {
   359  		return
   360  	}
   361  
   362  	outp, err := cmd.Exec("run", "./services/test/conf")
   363  	if err != nil {
   364  		t.Fatalf("micro run failure, output: %v", string(outp))
   365  		return
   366  	}
   367  
   368  	if err := Try("Try logs read", t, func() ([]byte, error) {
   369  		outp, err := cmd.Exec("logs", "conf")
   370  		if err != nil {
   371  			return outp, err
   372  		}
   373  
   374  		if !strings.Contains(string(outp), "val1") {
   375  			return outp, fmt.Errorf("Expected val1 in output, got: %v", string(outp))
   376  		}
   377  		if !strings.Contains(string(outp), "42") {
   378  			return outp, fmt.Errorf("Expected output to contain 42, got: %v", string(outp))
   379  		}
   380  		if !strings.Contains(string(outp), "Micro Team <support@m3o.com>") {
   381  			return outp, fmt.Errorf("Expected output to contain \"Micro Team <support@m3o.com>\", got: %v", string(outp))
   382  		}
   383  		if !strings.Contains(string(outp), "Micro Test <test@m3o.com>") {
   384  			return outp, fmt.Errorf("Expected output to contain \"Micro Test <test@m3o.com>\", got: %v", string(outp))
   385  		}
   386  		if !strings.Contains(string(outp), "Default Hello") {
   387  			return outp, fmt.Errorf("Expected output to contain \"Default Hello\", got: %v", string(outp))
   388  		}
   389  		return outp, err
   390  	}, 60*time.Second); err != nil {
   391  		return
   392  	}
   393  }