Skip to content

Commit

Permalink
Add ZipRunner and InteractiveRunner with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mraron committed Feb 19, 2024
1 parent 79618b7 commit aab87d7
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 216 deletions.
File renamed without changes.
3 changes: 1 addition & 2 deletions pkg/language/isolate_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package language_test

import (
"github.com/mraron/njudge/pkg/internal/testutils"
"github.com/mraron/njudge/pkg/language"
"github.com/mraron/njudge/pkg/language/internal/testutils"

"github.com/mraron/njudge/pkg/language/langs/cpp"
_ "github.com/mraron/njudge/pkg/language/langs/csharp"
_ "github.com/mraron/njudge/pkg/language/langs/cython3"
Expand Down
2 changes: 1 addition & 1 deletion pkg/language/langs/cpp/cpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cpp_test
import (
"bytes"
"context"
"github.com/mraron/njudge/pkg/language/internal/testutils"
"github.com/mraron/njudge/pkg/internal/testutils"
"github.com/mraron/njudge/pkg/language/langs/cpp"
"github.com/mraron/njudge/pkg/language/memory"
"github.com/mraron/njudge/pkg/language/sandbox"
Expand Down
20 changes: 8 additions & 12 deletions pkg/language/langs/zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,28 @@ import (
"github.com/mraron/njudge/pkg/language"
)

type zip struct{}
type Zip struct{}

func (zip) ID() string {
func (Zip) ID() string {
return "zip"
}

func (zip) DisplayName() string {
func (Zip) DisplayName() string {
return "ZIP archívum"
}

func (zip) DefaultFilename() string {
func (Zip) DefaultFilename() string {
return "main.zip"
}

func (zip) Compile(ctx context.Context, s sandbox.Sandbox, f sandbox.File, stderr io.Writer, extras []sandbox.File) (*sandbox.File, error) {
return nil, nil
func (Zip) Compile(_ context.Context, _ sandbox.Sandbox, f sandbox.File, _ io.Writer, _ []sandbox.File) (*sandbox.File, error) {
return &f, nil
}

func (zip) Run(ctx context.Context, s sandbox.Sandbox, binary sandbox.File, stdin io.Reader, stdout io.Writer, tl time.Duration, ml memory.Amount) (*sandbox.Status, error) {
func (Zip) Run(_ context.Context, _ sandbox.Sandbox, _ sandbox.File, _ io.Reader, _ io.Writer, _ time.Duration, _ memory.Amount) (*sandbox.Status, error) {
return &sandbox.Status{}, nil
}

func (zip) Test(sandbox.Sandbox) error {
return nil
}

func init() {
language.DefaultStore.Register("zip", zip{})
language.DefaultStore.Register("zip", Zip{})
}
185 changes: 10 additions & 175 deletions pkg/language/sandbox/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ type Dummy struct {

type DummyOption func(*Dummy) error

func DummyWithLogger(logger *slog.Logger) DummyOption {
return func(dummy *Dummy) error {
dummy.Logger = logger
return nil
}
}

func NewDummy(opts ...DummyOption) (*Dummy, error) {
dummyID += 1
res := &Dummy{
Expand All @@ -55,7 +62,7 @@ func (d *Dummy) Id() string {
return strconv.Itoa(d.ID)
}

func (d *Dummy) Init(ctx context.Context) error {
func (d *Dummy) Init(_ context.Context) error {
var err error
if d.Dir, err = os.MkdirTemp("", DummyPattern); err != nil {
return err
Expand Down Expand Up @@ -115,189 +122,17 @@ func (d *Dummy) Run(ctx context.Context, config RunConfig, command string, comma
st.Verdict = VerdictTL
} else if strings.HasPrefix(err.Error(), "exit status") || strings.HasPrefix(err.Error(), "signal:") { // TODO
st.Verdict = VerdictRE
st.ExitCode = err.(*exec.ExitError).ExitCode()
}
st.ExitCode = err.(*exec.ExitError).ExitCode()
}

return &st, nil
}

func (d *Dummy) Cleanup(ctx context.Context) error {
func (d *Dummy) Cleanup(_ context.Context) error {
d.Logger.Info("cleanup dummy sandbox", "dir", d.Dir)
d.inited = false
d.OsFS = OsFS{}
return os.RemoveAll(d.Dir)

}

/*
func (s Dummy) Id() string {
return s.tmpdir
}
func (s *Dummy) Init(logger *log.Logger) error {
var err error
if s.tmpdir, err = os.MkdirTemp("", "dummysandbox"); err != nil {
return err
}
s.workingDir = s.tmpdir
s.logger = logger
return nil
}
func (s Dummy) Pwd() string {
return s.tmpdir
}
func (s *Dummy) CreateFilePopulated(name string, r io.Reader) error {
filename := filepath.Join(s.tmpdir, name)
s.logger.Print("Creating file ", filename)
f, err := os.Create(filename)
if err != nil {
s.logger.Print("Error occurred while creating file ", err)
return err
}
if _, err := io.Copy(f, r); err != nil {
s.logger.Print("Error occurred while populating it with its content: ", err)
f.Close()
return err
}
return f.Close()
}
func (s *Dummy) Create(name string) (io.WriteCloser, error) {
return os.Create(filepath.Join(s.Pwd(), name))
}
func (s Dummy) Open(name string) (fs.File, error) {
return os.Open(filepath.Join(s.Pwd(), name))
}
func (s Dummy) MakeExecutable(name string) error {
filename := filepath.Join(s.Pwd(), name)
err := os.Chmod(filename, 0777)
s.logger.Print("Making executable: ", filename, " error: ", err)
return err
}
func (s *Dummy) SetMaxProcesses(i int) Sandbox {
return s
}
func (s *Dummy) Env() Sandbox {
s.env = os.Environ()
return s
}
func (s *Dummy) SetEnv(env string) Sandbox {
s.env = append(s.env, env+"="+os.Getenv(env))
return s
}
func (s *Dummy) AddArg(string) Sandbox {
return s
}
func (s *Dummy) TimeLimit(tl time.Duration) Sandbox {
s.tl = tl
return s
}
func (s *Dummy) MemoryLimit(int) Sandbox {
return s
}
func (s *Dummy) Stdin(reader io.Reader) Sandbox {
s.stdin = reader
return s
}
func (s *Dummy) Stderr(writer io.Writer) Sandbox {
s.stderr = writer
return s
}
func (s *Dummy) Stdout(writer io.Writer) Sandbox {
s.stdout = writer
return s
}
func (s *Dummy) MapDir(x string, y string, i []string, b bool) Sandbox {
return s
}
func (s *Dummy) WorkingDirectory(dir string) Sandbox {
s.workingDir = dir
return s
}
func (s *Dummy) Verbose() Sandbox {
return s
}
func (s *Dummy) Run(prg string, needStatus bool) (Status, error) {
cmd := exec.Command("bash", "-c", prg)
cmd.Stdin = s.stdin
cmd.Stdout = s.stdout
cmd.Stderr = s.stderr
cmd.Dir = s.workingDir
cmd.Env = append(s.env, "PATH="+os.Getenv("PATH")+":"+s.tmpdir)
var (
st Status
errKill, errWait error
finish = make(chan bool, 1)
wg sync.WaitGroup
)
st.Verdict = VerdictOK
start := time.NewTimer(s.tl)
if err := cmd.Start(); err != nil {
st.Verdict = VerdictXX
return st, err
}
defer start.Stop()
wg.Add(1)
go func() {
defer wg.Done()
errWait = cmd.Wait()
finish <- true
}()
select {
case <-start.C:
st.Verdict = VerdictTL
if errKill = cmd.Process.Kill(); errKill != nil {
st.Verdict = VerdictXX
}
case <-finish:
}
wg.Wait()
if errWait != nil && (strings.HasPrefix(errWait.Error(), "exit status") || strings.HasPrefix(errWait.Error(), "signal:")) {
if st.Verdict == VerdictOK {
st.Verdict = VerdictRE
}
errWait = nil
}
if errWait != nil {
return st, errWait
}
return st, errKill
}
func (s *Dummy) Cleanup() error {
return os.RemoveAll(s.tmpdir)
}
*/
2 changes: 1 addition & 1 deletion pkg/language/sandbox/isolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sandbox_test
import (
"context"
"flag"
"github.com/mraron/njudge/pkg/language/internal/testutils"
"github.com/mraron/njudge/pkg/internal/testutils"
"github.com/mraron/njudge/pkg/language/sandbox"
"github.com/stretchr/testify/assert"
"log/slog"
Expand Down
Loading

0 comments on commit aab87d7

Please sign in to comment.