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

     1  //go:build kind
     2  // +build kind
     3  
     4  package test
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"math/rand"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func init() {
    19  	testFilter = []string{
    20  		"TestRunGithubSource",
    21  		"TestStore",
    22  		// @todo Reactivate this once source to running works in kind
    23  		// "TestStoreImpl",
    24  		"TestCorruptedTokenLogin",
    25  		"TestRunPrivateSource",
    26  		"TestEventsStream",
    27  		"TestRPC",
    28  	}
    29  	maxTimeMultiplier = 3
    30  	isParallel = false // in theory should work in parallel
    31  	newSrv = newK8sServer
    32  	retryCount = 1
    33  }
    34  
    35  func newK8sServer(t *T, fname string, opts ...Option) Server {
    36  	options := Options{
    37  		Namespace: strings.ToLower(fname),
    38  		Login:     false,
    39  	}
    40  	for _, o := range opts {
    41  		o(&options)
    42  	}
    43  
    44  	portnum := rand.Intn(maxPort-minPort) + minPort
    45  	configFile := configFile(fname)
    46  
    47  	var cmd *exec.Cmd
    48  	if v := os.Getenv("IN_HELM_TEST"); len(v) > 0 {
    49  		cmd = exec.Command("kubectl", "port-forward", "--namespace", "micro", "svc/proxy", fmt.Sprintf("%d:443", portnum))
    50  	} else {
    51  		cmd = exec.Command("kubectl", "port-forward", "--namespace", "default", "svc/micro-proxy", fmt.Sprintf("%d:8081", portnum))
    52  	}
    53  
    54  	s := &testK8sServer{ServerBase{
    55  		dir:       filepath.Dir(configFile),
    56  		config:    configFile,
    57  		t:         t,
    58  		env:       options.Namespace,
    59  		proxyPort: portnum,
    60  		opts:      options,
    61  		cmd:       cmd,
    62  	}}
    63  	s.namespace = s.env
    64  
    65  	return s
    66  }
    67  
    68  type testK8sServer struct {
    69  	ServerBase
    70  }
    71  
    72  func (s *testK8sServer) Run() error {
    73  	if err := s.ServerBase.Run(); err != nil {
    74  		return err
    75  	}
    76  
    77  	ChangeNamespace(s.Command(), s.Env(), "micro")
    78  
    79  	// login to admin account
    80  	if err := Login(s, s.t, "admin", "micro"); err != nil {
    81  		s.t.Fatalf("Error logging in %s", err)
    82  		return err
    83  	}
    84  
    85  	if err := Try("Calling micro server", s.t, func() ([]byte, error) {
    86  		outp, err := s.Command().Exec("services")
    87  		if !strings.Contains(string(outp), "runtime") ||
    88  			!strings.Contains(string(outp), "registry") ||
    89  			!strings.Contains(string(outp), "broker") ||
    90  			!strings.Contains(string(outp), "config") ||
    91  			!strings.Contains(string(outp), "proxy") ||
    92  			!strings.Contains(string(outp), "auth") ||
    93  			!strings.Contains(string(outp), "store") {
    94  			return outp, errors.New("Not ready")
    95  		}
    96  
    97  		return outp, err
    98  	}, 60*time.Second); err != nil {
    99  		return err
   100  	}
   101  
   102  	// generate account in new namespace
   103  	// ignore errors because it is not an idempotent call
   104  	s.Command().Exec("auth", "create", "account", "--secret", "micro", "--namespace", s.Env(), "--scopes", "admin", "admin")
   105  
   106  	// switch to the namespace
   107  	ChangeNamespace(s.Command(), s.Env(), s.Env())
   108  
   109  	// login to the admin account which is generated for each namespace
   110  	if s.opts.Login {
   111  		Login(s, s.t, "admin", "micro")
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  func (s *testK8sServer) Close() {
   118  	s.ServerBase.Close()
   119  	// kill the port forward
   120  	s.cmd.Process.Kill()
   121  }
   122  
   123  func TestDeleteOwnAccount(t *testing.T) {
   124  	TrySuite(t, testDeleteOwnAccount, retryCount)
   125  }
   126  
   127  func testDeleteOwnAccount(t *T) {
   128  	t.Parallel()
   129  	serv := NewServer(t, WithLogin())
   130  	defer serv.Close()
   131  	if err := serv.Run(); err != nil {
   132  		return
   133  	}
   134  
   135  	cmd := serv.Command()
   136  	outp, err := cmd.Exec("auth", "delete", "account", "admin")
   137  	if err == nil {
   138  		t.Fatal(string(outp))
   139  	}
   140  }