added k8s stub adapter for execution environment
This commit is contained in:
@ -1,42 +0,0 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"github.com/openHPI/poseidon/pkg/dto"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/sirupsen/logrus/hooks/test"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mockHTTPStatusHandler(status int) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(status)
|
||||
})
|
||||
}
|
||||
|
||||
type MainTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
}
|
||||
|
||||
func TestMainTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MainTestSuite))
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestHTTPMiddlewareDebugsWhenStatusOK() {
|
||||
var hook *test.Hook
|
||||
log, hook = test.NewNullLogger()
|
||||
InitializeLogging(logrus.DebugLevel.String(), dto.FormatterText)
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
if err != nil {
|
||||
s.Fail(err.Error())
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
HTTPLoggingMiddleware(mockHTTPStatusHandler(200)).ServeHTTP(recorder, request)
|
||||
|
||||
s.Equal(1, len(hook.Entries))
|
||||
s.Equal(logrus.DebugLevel, hook.LastEntry().Level)
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package nullio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type responseWriterStub struct {
|
||||
bytes.Buffer
|
||||
header http.Header
|
||||
}
|
||||
|
||||
func (r *responseWriterStub) Header() http.Header {
|
||||
return r.header
|
||||
}
|
||||
func (r *responseWriterStub) WriteHeader(_ int) {
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestContentLengthWriter_Write() {
|
||||
header := http.Header(make(map[string][]string))
|
||||
buf := &responseWriterStub{header: header}
|
||||
writer := &ContentLengthWriter{Target: buf}
|
||||
part1 := []byte("-rw-rw-r-- 1 kali ka")
|
||||
contentLength := "42"
|
||||
part2 := []byte("li " + contentLength + " 1660763446 flag\nFL")
|
||||
part3 := []byte("AG")
|
||||
|
||||
count, err := writer.Write(part1)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(len(part1), count)
|
||||
s.Empty(buf.String())
|
||||
s.Equal("", header.Get("Content-Length"))
|
||||
|
||||
count, err = writer.Write(part2)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(len(part2), count)
|
||||
s.Equal("FL", buf.String())
|
||||
s.Equal(contentLength, header.Get("Content-Length"))
|
||||
|
||||
count, err = writer.Write(part3)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(len(part3), count)
|
||||
s.Equal("FLAG", buf.String())
|
||||
s.Equal(contentLength, header.Get("Content-Length"))
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
package nullio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLs2JsonTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(Ls2JsonTestSuite))
|
||||
}
|
||||
|
||||
type Ls2JsonTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
buf *bytes.Buffer
|
||||
writer *Ls2JsonWriter
|
||||
}
|
||||
|
||||
func (s *Ls2JsonTestSuite) SetupTest() {
|
||||
s.MemoryLeakTestSuite.SetupTest()
|
||||
s.buf = &bytes.Buffer{}
|
||||
s.writer = &Ls2JsonWriter{Target: s.buf, Ctx: context.Background()}
|
||||
}
|
||||
|
||||
func (s *Ls2JsonTestSuite) TestLs2JsonWriter_WriteCreationAndClose() {
|
||||
count, err := s.writer.Write([]byte(""))
|
||||
s.Zero(count)
|
||||
s.NoError(err)
|
||||
|
||||
s.Equal("{\"files\": [", s.buf.String())
|
||||
|
||||
s.writer.Close()
|
||||
s.Equal("{\"files\": []}", s.buf.String())
|
||||
}
|
||||
|
||||
func (s *Ls2JsonTestSuite) TestLs2JsonWriter_WriteFile() {
|
||||
input := "total 0\n-rw-rw-r-- 1 kali kali 0 1660763446 flag\n"
|
||||
count, err := s.writer.Write([]byte(input))
|
||||
s.Equal(len(input), count)
|
||||
s.NoError(err)
|
||||
s.writer.Close()
|
||||
|
||||
s.Equal("{\"files\": [{\"name\":\"flag\",\"entryType\":\"-\",\"size\":0,\"modificationTime\":1660763446"+
|
||||
",\"permissions\":\"rw-rw-r--\",\"owner\":\"kali\",\"group\":\"kali\"}]}",
|
||||
s.buf.String())
|
||||
}
|
||||
|
||||
func (s *Ls2JsonTestSuite) TestLs2JsonWriter_WriteRecursive() {
|
||||
input := ".:\ntotal 4\ndrwxrwxr-x 2 kali kali 4096 1660764411 dir\n" +
|
||||
"-rw-rw-r-- 1 kali kali 0 1660763446 flag\n" +
|
||||
"\n./dir:\ntotal 4\n-rw-rw-r-- 1 kali kali 3 1660764366 another.txt\n"
|
||||
count, err := s.writer.Write([]byte(input))
|
||||
s.Equal(len(input), count)
|
||||
s.NoError(err)
|
||||
s.writer.Close()
|
||||
|
||||
s.Equal("{\"files\": ["+
|
||||
"{\"name\":\"./dir\",\"entryType\":\"d\",\"size\":4096,\"modificationTime\":1660764411,"+
|
||||
"\"permissions\":\"rwxrwxr-x\",\"owner\":\"kali\",\"group\":\"kali\"},"+
|
||||
"{\"name\":\"./flag\",\"entryType\":\"-\",\"size\":0,\"modificationTime\":1660763446,"+
|
||||
"\"permissions\":\"rw-rw-r--\",\"owner\":\"kali\",\"group\":\"kali\"},"+
|
||||
"{\"name\":\"./dir/another.txt\",\"entryType\":\"-\",\"size\":3,\"modificationTime\":1660764366,"+
|
||||
"\"permissions\":\"rw-rw-r--\",\"owner\":\"kali\",\"group\":\"kali\"}"+
|
||||
"]}",
|
||||
s.buf.String())
|
||||
}
|
||||
|
||||
func (s *Ls2JsonTestSuite) TestLs2JsonWriter_WriteRemaining() {
|
||||
input1 := "total 4\n-rw-rw-r-- 1 kali kali 3 1660764366 an.txt\n-rw-rw-r-- 1 kal"
|
||||
_, err := s.writer.Write([]byte(input1))
|
||||
s.NoError(err)
|
||||
s.Equal("{\"files\": [{\"name\":\"an.txt\",\"entryType\":\"-\",\"size\":3,\"modificationTime\":1660764366,"+
|
||||
"\"permissions\":\"rw-rw-r--\",\"owner\":\"kali\",\"group\":\"kali\"}", s.buf.String())
|
||||
|
||||
input2 := "i kali 0 1660763446 flag\n"
|
||||
_, err = s.writer.Write([]byte(input2))
|
||||
s.NoError(err)
|
||||
s.writer.Close()
|
||||
s.Equal("{\"files\": [{\"name\":\"an.txt\",\"entryType\":\"-\",\"size\":3,\"modificationTime\":1660764366,"+
|
||||
"\"permissions\":\"rw-rw-r--\",\"owner\":\"kali\",\"group\":\"kali\"},"+
|
||||
"{\"name\":\"flag\",\"entryType\":\"-\",\"size\":0,\"modificationTime\":1660763446,"+
|
||||
"\"permissions\":\"rw-rw-r--\",\"owner\":\"kali\",\"group\":\"kali\"}]}", s.buf.String())
|
||||
}
|
||||
|
||||
func (s *Ls2JsonTestSuite) TestLs2JsonWriter_WriteLink() {
|
||||
input1 := "total 4\nlrw-rw-r-- 1 kali kali 3 1660764366 another.txt -> /bin/bash\n"
|
||||
_, err := s.writer.Write([]byte(input1))
|
||||
s.NoError(err)
|
||||
s.writer.Close()
|
||||
s.Equal("{\"files\": [{\"name\":\"another.txt\",\"entryType\":\"l\",\"linkTarget\":\"/bin/bash\",\"size\":3,"+
|
||||
"\"modificationTime\":1660764366,\"permissions\":\"rw-rw-r--\",\"owner\":\"kali\",\"group\":\"kali\"}]}",
|
||||
s.buf.String())
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
package nullio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MainTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
}
|
||||
|
||||
func TestMainTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MainTestSuite))
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestReader_Read() {
|
||||
read := func(reader io.Reader, ret chan<- bool) {
|
||||
p := make([]byte, 0, 5)
|
||||
_, err := reader.Read(p)
|
||||
s.ErrorIs(io.EOF, err)
|
||||
close(ret)
|
||||
}
|
||||
|
||||
s.Run("WithContext_DoesNotReturnImmediately", func() {
|
||||
readingContext, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
readerReturned := make(chan bool)
|
||||
go read(&Reader{readingContext}, readerReturned)
|
||||
|
||||
select {
|
||||
case <-readerReturned:
|
||||
s.Fail("The reader returned before the timeout was reached")
|
||||
case <-time.After(tests.ShortTimeout):
|
||||
}
|
||||
})
|
||||
|
||||
s.Run("WithoutContext_DoesReturnImmediately", func() {
|
||||
readerReturned := make(chan bool)
|
||||
go read(&Reader{}, readerReturned)
|
||||
|
||||
select {
|
||||
case <-readerReturned:
|
||||
case <-time.After(tests.ShortTimeout):
|
||||
s.Fail("The reader returned before the timeout was reached")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestReadWriterWritesEverything() {
|
||||
readWriter := &ReadWriter{}
|
||||
p := []byte{1, 2, 3}
|
||||
n, err := readWriter.Write(p)
|
||||
s.NoError(err)
|
||||
s.Equal(len(p), n)
|
||||
}
|
@ -1,197 +0,0 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/influxdata/influxdb-client-go/v2/api/write"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRunnerPoolTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(ObjectPoolTestSuite))
|
||||
}
|
||||
|
||||
type ObjectPoolTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
objectStorage *localStorage[any]
|
||||
object int
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) SetupTest() {
|
||||
s.MemoryLeakTestSuite.SetupTest()
|
||||
s.objectStorage = NewLocalStorage[any]()
|
||||
s.object = 42
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestAddedObjectCanBeRetrieved() {
|
||||
s.objectStorage.Add("my_id", s.object)
|
||||
retrievedRunner, ok := s.objectStorage.Get("my_id")
|
||||
s.True(ok, "A saved object should be retrievable")
|
||||
s.Equal(s.object, retrievedRunner)
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestLocalStorage_List() {
|
||||
s.objectStorage.Add("my_id", s.object)
|
||||
s.objectStorage.Add("my_id 2", 21)
|
||||
retrievedRunners := s.objectStorage.List()
|
||||
s.Len(retrievedRunners, 2)
|
||||
s.Contains(retrievedRunners, s.object)
|
||||
s.Contains(retrievedRunners, 21)
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestObjectWithSameIdOverwritesOldOne() {
|
||||
otherObject := 21
|
||||
// assure object is actually different
|
||||
s.NotEqual(s.object, otherObject)
|
||||
|
||||
s.objectStorage.Add("my_id", s.object)
|
||||
s.objectStorage.Add("my_id", otherObject)
|
||||
retrievedObject, _ := s.objectStorage.Get("my_id")
|
||||
s.NotEqual(s.object, retrievedObject)
|
||||
s.Equal(otherObject, retrievedObject)
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestDeletedObjectsAreNotAccessible() {
|
||||
s.objectStorage.Add("my_id", s.object)
|
||||
s.objectStorage.Delete("my_id")
|
||||
retrievedObject, ok := s.objectStorage.Get("my_id")
|
||||
s.Nil(retrievedObject)
|
||||
s.False(ok, "A deleted object should not be accessible")
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestSampleReturnsObjectWhenOneIsAvailable() {
|
||||
s.objectStorage.Add("my_id", s.object)
|
||||
sampledObject, ok := s.objectStorage.Sample()
|
||||
s.NotNil(sampledObject)
|
||||
s.True(ok)
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestSampleReturnsFalseWhenNoneIsAvailable() {
|
||||
sampledObject, ok := s.objectStorage.Sample()
|
||||
s.Nil(sampledObject)
|
||||
s.False(ok)
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestSampleRemovesObjectFromPool() {
|
||||
s.objectStorage.Add("my_id", s.object)
|
||||
_, _ = s.objectStorage.Sample()
|
||||
_, ok := s.objectStorage.Get("my_id")
|
||||
s.False(ok)
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestLenOfEmptyPoolIsZero() {
|
||||
s.Equal(uint(0), s.objectStorage.Length())
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) TestLenChangesOnStoreContentChange() {
|
||||
s.Run("len increases when object is added", func() {
|
||||
s.objectStorage.Add("my_id_1", s.object)
|
||||
s.Equal(uint(1), s.objectStorage.Length())
|
||||
})
|
||||
|
||||
s.Run("len does not increase when object with same id is added", func() {
|
||||
s.objectStorage.Add("my_id_1", s.object)
|
||||
s.Equal(uint(1), s.objectStorage.Length())
|
||||
})
|
||||
|
||||
s.Run("len increases again when different object is added", func() {
|
||||
anotherObject := 21
|
||||
s.objectStorage.Add("my_id_2", anotherObject)
|
||||
s.Equal(uint(2), s.objectStorage.Length())
|
||||
})
|
||||
|
||||
s.Run("len decreases when object is deleted", func() {
|
||||
s.objectStorage.Delete("my_id_1")
|
||||
s.Equal(uint(1), s.objectStorage.Length())
|
||||
})
|
||||
|
||||
s.Run("len decreases when object is sampled", func() {
|
||||
_, _ = s.objectStorage.Sample()
|
||||
s.Equal(uint(0), s.objectStorage.Length())
|
||||
})
|
||||
}
|
||||
|
||||
type MainTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
}
|
||||
|
||||
func TestMainTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MainTestSuite))
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestNewMonitoredLocalStorage_Callback() {
|
||||
callbackCalls := 0
|
||||
callbackAdditions := 0
|
||||
callbackDeletions := 0
|
||||
os := NewMonitoredLocalStorage[string]("testMeasurement", func(p *write.Point, o string, eventType EventType) {
|
||||
callbackCalls++
|
||||
if eventType == Deletion {
|
||||
callbackDeletions++
|
||||
} else if eventType == Creation {
|
||||
callbackAdditions++
|
||||
}
|
||||
}, 0, context.Background())
|
||||
|
||||
assertCallbackCounts := func(test func(), totalCalls, additions, deletions int) {
|
||||
beforeTotal := callbackCalls
|
||||
beforeAdditions := callbackAdditions
|
||||
beforeDeletions := callbackDeletions
|
||||
test()
|
||||
s.Equal(beforeTotal+totalCalls, callbackCalls)
|
||||
s.Equal(beforeAdditions+additions, callbackAdditions)
|
||||
s.Equal(beforeDeletions+deletions, callbackDeletions)
|
||||
}
|
||||
|
||||
s.Run("Add", func() {
|
||||
assertCallbackCounts(func() {
|
||||
os.Add("id 1", "object 1")
|
||||
}, 1, 1, 0)
|
||||
})
|
||||
|
||||
s.Run("Delete", func() {
|
||||
assertCallbackCounts(func() {
|
||||
os.Delete("id 1")
|
||||
}, 1, 0, 1)
|
||||
})
|
||||
|
||||
s.Run("List", func() {
|
||||
assertCallbackCounts(func() {
|
||||
os.List()
|
||||
}, 0, 0, 0)
|
||||
})
|
||||
|
||||
s.Run("Pop", func() {
|
||||
os.Add("id 1", "object 1")
|
||||
|
||||
assertCallbackCounts(func() {
|
||||
o, ok := os.Pop("id 1")
|
||||
s.True(ok)
|
||||
s.Equal("object 1", o)
|
||||
}, 1, 0, 1)
|
||||
})
|
||||
|
||||
s.Run("Purge", func() {
|
||||
os.Add("id 1", "object 1")
|
||||
os.Add("id 2", "object 2")
|
||||
|
||||
assertCallbackCounts(func() {
|
||||
os.Purge()
|
||||
}, 2, 0, 2)
|
||||
})
|
||||
}
|
||||
func (s *MainTestSuite) TestNewMonitoredLocalStorage_Periodically() {
|
||||
callbackCalls := 0
|
||||
NewMonitoredLocalStorage[string]("testMeasurement", func(p *write.Point, o string, eventType EventType) {
|
||||
callbackCalls++
|
||||
s.Equal(Periodically, eventType)
|
||||
}, 2*tests.ShortTimeout, s.TestCtx)
|
||||
|
||||
<-time.After(tests.ShortTimeout)
|
||||
s.Equal(0, callbackCalls)
|
||||
<-time.After(2 * tests.ShortTimeout)
|
||||
s.Equal(1, callbackCalls)
|
||||
<-time.After(2 * tests.ShortTimeout)
|
||||
s.Equal(2, callbackCalls)
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/openHPI/poseidon/pkg/dto"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MainTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
}
|
||||
|
||||
func TestMainTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MainTestSuite))
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestMergeContext_Deadline() {
|
||||
ctxWithoutDeadline := context.Background()
|
||||
earlyDeadline := time.Now().Add(time.Second)
|
||||
ctxWithEarlyDeadline, cancel := context.WithDeadline(context.Background(), earlyDeadline)
|
||||
defer cancel()
|
||||
ctxWithLateDeadline, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Hour))
|
||||
defer cancel()
|
||||
|
||||
ctx := NewMergeContext([]context.Context{ctxWithoutDeadline, ctxWithEarlyDeadline, ctxWithLateDeadline})
|
||||
deadline, ok := ctx.Deadline()
|
||||
|
||||
s.True(ok)
|
||||
s.Equal(earlyDeadline, deadline, "The ealiest deadline is returned")
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestMergeContext_Done() {
|
||||
ctxWithoutDeadline := context.Background()
|
||||
ctxWithEarlyDeadline, cancel := context.WithTimeout(context.Background(), 2*tests.ShortTimeout)
|
||||
defer cancel()
|
||||
ctxWithLateDeadline, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
|
||||
ctx := NewMergeContext([]context.Context{ctxWithoutDeadline, ctxWithEarlyDeadline, ctxWithLateDeadline})
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
s.Fail("mergeContext is done before any of its parents")
|
||||
return
|
||||
case <-time.After(tests.ShortTimeout):
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(3 * tests.ShortTimeout):
|
||||
s.Fail("mergeContext is not done after the earliest of its parents")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestMergeContext_Err() {
|
||||
ctxWithoutDeadline := context.Background()
|
||||
ctxCancelled, cancel := context.WithCancel(context.Background())
|
||||
ctx := NewMergeContext([]context.Context{ctxWithoutDeadline, ctxCancelled})
|
||||
|
||||
s.NoError(ctx.Err())
|
||||
cancel()
|
||||
s.Error(ctx.Err())
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestMergeContext_Value() {
|
||||
ctxWithAValue := context.WithValue(context.Background(), dto.ContextKey("keyA"), "valueA")
|
||||
ctxWithAnotherValue := context.WithValue(context.Background(), dto.ContextKey("keyB"), "valueB")
|
||||
ctx := NewMergeContext([]context.Context{ctxWithAValue, ctxWithAnotherValue})
|
||||
|
||||
s.Equal("valueA", ctx.Value(dto.ContextKey("keyA")))
|
||||
s.Equal("valueB", ctx.Value(dto.ContextKey("keyB")))
|
||||
s.Nil(ctx.Value("keyC"))
|
||||
}
|
Reference in New Issue
Block a user