...

Source file src/golang.conradwood.net/tests/ctx2/ctx2.go

Documentation: golang.conradwood.net/tests/ctx2

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"golang.conradwood.net/apis/common"
    11  	ge "golang.conradwood.net/apis/getestservice"
    12  	"golang.conradwood.net/go-easyops/authremote"
    13  	"golang.conradwood.net/go-easyops/cmdline"
    14  	"golang.conradwood.net/go-easyops/ctx"
    15  	gctx "golang.conradwood.net/go-easyops/ctx"
    16  	"golang.conradwood.net/go-easyops/ctx/shared"
    17  	"golang.conradwood.net/go-easyops/errors"
    18  	"golang.conradwood.net/go-easyops/server"
    19  	"golang.conradwood.net/go-easyops/utils"
    20  	"google.golang.org/grpc"
    21  )
    22  
    23  const (
    24  	ACTION_CALL_YOURSELF      = 1
    25  	ACTION_SEND_ACCESS_DENIED = 2
    26  )
    27  
    28  var (
    29  	TEST_SERVICE_IDS = []string{"33", "31", "22", "29", "27", "35", "57", "39"}
    30  )
    31  
    32  func main() {
    33  	flag.Parse()
    34  	server.SetHealth(common.Health_READY)
    35  	sd := server.NewServerDef()
    36  	sd.SetPort(3006)
    37  	sd.SetOnStartupCallback(run_tests)
    38  	sd.SetRegister(server.Register(
    39  		func(g *grpc.Server) error {
    40  			ge.RegisterCtx2TestServer(g, &geServer{})
    41  			return nil
    42  		},
    43  	))
    44  	err := server.ServerStartup(sd)
    45  	utils.Bail("Unable to start server", err)
    46  }
    47  func run_tests() {
    48  	fmt.Printf("Starting tests...\n")
    49  	ctx := authremote.Context()
    50  	//trr := &ge.TriggerRPCRequest{Action: ACTION_SEND_ACCESS_DENIED}
    51  	trr := &ge.TriggerRPCRequest{Action: ACTION_CALL_YOURSELF}
    52  	_, err := ge.GetCtx2TestClient().TriggerRPC(ctx, trr)
    53  	utils.Bail("test failed", err)
    54  	fmt.Printf("Tests completed\n")
    55  }
    56  
    57  type geServer struct {
    58  }
    59  
    60  func (ges *geServer) TriggerRPC(ctx context.Context, req *ge.TriggerRPCRequest) (*common.Void, error) {
    61  	cmdline.SetDebugContext()
    62  	fmt.Printf("------------------------------- In TRIGGERRPC %d --------------------\n", req.Counter)
    63  	fmt.Printf("LocalState: %s\n", shared.LocalState2string(gctx.GetLocalState(ctx)))
    64  	if req.Action == ACTION_SEND_ACCESS_DENIED {
    65  		return nil, errors.AccessDenied(ctx, "told to return access denied")
    66  	}
    67  	if req.Action == ACTION_CALL_YOURSELF {
    68  		if req.Counter == 0 {
    69  			ctx = buildContext(req.Counter)
    70  		}
    71  		assert_correct_ctx(ctx, req)
    72  		if req.Counter < 5 {
    73  			req.Counter++
    74  			fmt.Printf("------------------------------- Calling TRIGGERRPC %d --------------------\n", req.Counter)
    75  			return ge.GetCtx2TestClient().TriggerRPC(ctx, req)
    76  		}
    77  	} else {
    78  		return nil, errors.Errorf("Invalid action \"%d\"", req.Action)
    79  	}
    80  	return &common.Void{}, nil
    81  }
    82  func assert_correct_ctx(ctx context.Context, req *ge.TriggerRPCRequest) {
    83  	ls := gctx.GetLocalState(ctx)
    84  	if ls == nil {
    85  		fail(ctx, req, "No localstate at all")
    86  	}
    87  	svc := ls.CallingService()
    88  	if svc == nil {
    89  		fail(ctx, req, "no calling service")
    90  	}
    91  	svc = ls.CreatorService()
    92  	if svc == nil {
    93  		fail(ctx, req, "no creatorservice")
    94  	}
    95  	if ls.SudoUser() == nil {
    96  		fail(ctx, req, "no sudouser")
    97  	}
    98  	if req.Counter == 1 {
    99  	}
   100  }
   101  func fail(ctx context.Context, req *ge.TriggerRPCRequest, format string, args ...interface{}) {
   102  	fmt.Printf("****** FAILED\n")
   103  	s := fmt.Sprintf(format, args...)
   104  	fmt.Printf("[counter=%d] %s\n", req.Counter, s)
   105  	os.Exit(10)
   106  }
   107  
   108  func buildContext(ct uint32) context.Context {
   109  	cb := ctx.NewContextBuilder()
   110  	cb.WithTimeout(time.Duration(3) * time.Second)
   111  	//	cb.WithSession(f.session)
   112  	u, err := authremote.GetSignedUserByID(authremote.Context(), "1")
   113  	utils.Bail("failed to get testuserid", err)
   114  	if u == nil {
   115  		panic("no user to build context with")
   116  	}
   117  	_, s := authremote.GetLocalUsers()
   118  	if s == nil {
   119  		panic("no service user. forgot -token option?")
   120  	}
   121  	sx, err := authremote.GetSignedUserByID(authremote.Context(), TEST_SERVICE_IDS[ct])
   122  	utils.Bail("failed to get user by id", err)
   123  	cb.WithUser(u)
   124  	cb.WithSudoUser(u)
   125  	cb.WithCreatorService(s)
   126  	cb.WithCallingService(sx)
   127  	cb.WithDebug()
   128  	cb.WithTrace()
   129  	cb.WithRequestID("foo-requestid")
   130  	cb.EnableExperiment("debug_context1")
   131  	cb.EnableExperiment("debug_context2")
   132  
   133  	res_ctx := cb.ContextWithAutoCancel()
   134  	fmt.Printf("Created new context\n")
   135  
   136  	return res_ctx
   137  }
   138  

View as plain text