package archied import ( "context" "errors" "io" "strings" "testing" "github.com/containerd/errdefs" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/workflow" ) type fakeTasks struct { task *workflow.Task err error } func (f fakeTasks) TaskByID(context.Context, int64) (*workflow.Task, error) { return f.task, f.err } func TestRunViewOpen(t *testing.T) { running := &workflow.Task{ID: 7, Status: workflow.StatusRunning, Attempt: 2} cases := []struct { name string tasks fakeTasks attempt int64 follow error code codes.Code reason string }{ {"running attempt", fakeTasks{task: running}, 2, nil, codes.OK, ""}, {"unknown task", fakeTasks{}, 2, nil, codes.NotFound, "unknown"}, {"finished task", fakeTasks{task: &workflow.Task{ID: 7, Status: workflow.StatusParked, Attempt: 2}}, 2, nil, codes.NotFound, "not running"}, {"earlier attempt", fakeTasks{task: running}, 1, nil, codes.NotFound, "attempt 1 is not running"}, {"container gone", fakeTasks{task: running}, 2, errdefs.ErrNotFound, codes.NotFound, "no running sandbox"}, {"store down", fakeTasks{err: errors.New("dial")}, 2, nil, codes.Unavailable, "unavailable"}, {"other org", fakeTasks{task: &workflow.Task{ID: 7, Org: "other", Status: workflow.StatusRunning, Attempt: 2}}, 2, nil, codes.NotFound, "unknown"}, {"docker down", fakeTasks{task: running}, 2, errors.New("connection refused"), codes.Unavailable, "unavailable"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { v := &runView{tasks: tc.tasks, follow: func(context.Context, int64, int64) (io.ReadCloser, error) { if tc.follow != nil { return nil, tc.follow } return io.NopCloser(strings.NewReader("")), nil }} rc, err := v.open(org.WithOrg(t.Context(), org.DefaultOrgID), &pb.RunTarget{TaskId: 7, Attempt: tc.attempt}) if got := status.Code(err); got != tc.code { t.Fatalf("code = %v (%v), want %v", got, err, tc.code) } if err != nil && !strings.Contains(err.Error(), tc.reason) { t.Fatalf("reason %q does not state %q", err, tc.reason) } if rc != nil { _ = rc.Close() } }) } } type harnessOpenStream struct { pb.HarnessService_OpenServer ctx context.Context //nolint:containedctx // gRPC streams expose their context through Context(). target *pb.OpenTarget } func (s harnessOpenStream) Context() context.Context { return s.ctx } func (s harnessOpenStream) Recv() (*pb.OpenRequest, error) { return &pb.OpenRequest{Frame: &pb.OpenRequest_Target{Target: s.target}}, nil } func TestHarnessSessionCallerOrg(t *testing.T) { for _, tt := range []struct { name string caller org.OrgID target string }{ {"tenant cannot open another tenant", "acme", "victim"}, {"system caller cannot name another tenant", org.DefaultOrgID, "victim"}, {"unattributed caller cannot name a tenant", "", "victim"}, } { t.Run(tt.name, func(t *testing.T) { stream := harnessOpenStream{ctx: org.WithOrg(t.Context(), tt.caller), target: &pb.OpenTarget{Org: tt.target, Target: &pb.OpenTarget_Setup{Setup: &pb.SetupTarget{Profile: "kit"}}}} if err := (&harnessServer{}).Open(stream); status.Code(err) != codes.PermissionDenied { t.Fatalf("error %v, want PermissionDenied", err) } }) } }