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

     1  //go:build integration
     2  // +build integration
     3  
     4  package test
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  // Test for making sure config and store values across namespaces
    15  // are correctly isolated
    16  func TestNamespaceConfigIsolation(t *testing.T) {
    17  	TrySuite(t, testNamespaceConfigIsolation, retryCount)
    18  }
    19  
    20  func testNamespaceConfigIsolation(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  	testNamespaceConfigIsolationSuite(serv, t)
    29  }
    30  
    31  func testNamespaceConfigIsolationSuite(serv Server, t *T) {
    32  	cmd := serv.Command()
    33  
    34  	if err := Try("Calling micro config set", t, func() ([]byte, error) {
    35  		outp, err := cmd.Exec("config", "set", "somekey", "val1")
    36  		if err != nil {
    37  			return outp, err
    38  		}
    39  		if string(outp) != "" {
    40  			return outp, fmt.Errorf("Expected no output, got: %v", string(outp))
    41  		}
    42  		return outp, err
    43  	}, 5*time.Second); err != nil {
    44  		return
    45  	}
    46  
    47  	if err := Try("micro config get somekey", t, func() ([]byte, error) {
    48  		outp, err := cmd.Exec("config", "get", "somekey")
    49  		if err != nil {
    50  			return outp, err
    51  		}
    52  		if string(outp) != "val1\n" {
    53  			return outp, errors.New("Expected 'val1\n'")
    54  		}
    55  		return outp, err
    56  	}, 8*time.Second); err != nil {
    57  		return
    58  	}
    59  
    60  	outp, err := cmd.Exec("auth", "create", "account", "--secret", "micro", "--scopes", "admin", "--namespace", "random", "admin")
    61  	if err != nil {
    62  		t.Fatal(string(outp), err)
    63  		return
    64  	}
    65  	currNamespace, _ := cmd.Exec("user", "namespace")
    66  	if err := ChangeNamespace(cmd, serv.Env(), "random"); err != nil {
    67  		t.Fatalf("Error changing namespace %s", err)
    68  	}
    69  
    70  	if err := Login(serv, t, "admin", "micro"); err != nil {
    71  		return
    72  	}
    73  
    74  	if err := Try("reading 'somekey' should not be found with this account", t, func() ([]byte, error) {
    75  		outp, err := cmd.Exec("config", "get", "somekey")
    76  		if err == nil {
    77  			return outp, errors.New("getting somekey should fail")
    78  		}
    79  		if !strings.Contains(string(outp), "Not found") {
    80  			return outp, errors.New("Expected 'Not found\n'")
    81  		}
    82  		return outp, nil
    83  	}, 8*time.Second); err != nil {
    84  		return
    85  	}
    86  
    87  	// Log back to original namespace and see if value is already there
    88  	if err := ChangeNamespace(cmd, serv.Env(), string(currNamespace)); err != nil {
    89  		t.Fatalf("Error changing namespace %s", err)
    90  	}
    91  
    92  	if err := Login(serv, t, "admin", "micro"); err != nil {
    93  		return
    94  	}
    95  
    96  	if err := Try("micro config get somekey", t, func() ([]byte, error) {
    97  		outp, err := cmd.Exec("config", "get", "somekey")
    98  		if err != nil {
    99  			return outp, err
   100  		}
   101  		if string(outp) != "val1\n" {
   102  			return outp, errors.New("Expected 'val1\n'")
   103  		}
   104  		return outp, err
   105  	}, 8*time.Second); err != nil {
   106  		return
   107  	}
   108  }