Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simple long running test #598

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions e2e/basic/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"strings"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
corev1 "k8s.io/client-go/applyconfigurations/core/v1"

"github.com/celestiaorg/knuu/e2e"
)
Expand All @@ -23,16 +23,16 @@ func (s *Suite) TestProbe() {
err = web.Storage().AddFile(resourcesHTML+"/index.html", e2e.NginxHTMLPath+"/index.html", "0:0")
s.Require().NoError(err)

livenessProbe := v1.Probe{
ProbeHandler: v1.ProbeHandler{
HTTPGet: &v1.HTTPGetAction{
Path: "/",
Port: intstr.IntOrString{Type: intstr.Int, IntVal: e2e.NginxPort},
},
},
InitialDelaySeconds: 10,
}
s.Require().NoError(web.Monitoring().SetLivenessProbe(&livenessProbe))
err = web.Monitoring().SetLivenessProbe(
corev1.Probe().
WithHTTPGet(corev1.HTTPGetAction().
WithPath("/").
WithPort(intstr.FromInt(e2e.NginxPort)),
).
WithInitialDelaySeconds(10),
)
s.Require().NoError(err)

s.Require().NoError(web.Build().Commit(ctx))
s.Require().NoError(web.Execution().Start(ctx))

Expand Down
81 changes: 81 additions & 0 deletions e2e/longrunning/simple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package longrunning

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/celestiaorg/knuu/pkg/instance"
"github.com/celestiaorg/knuu/pkg/knuu"
)

const (
alpineImage = "alpine:3.20.3"
testTimeout = time.Minute * 15
)

func TestSimple(t *testing.T) {
const (
instanceName = "simple-id"
fileContent = "identifier:12345"
)

ctx := context.Background()

ins1, err := createInstance(ctx, instanceName, "")
require.NoError(t, err)
testScope := ins1.Scope

t.Logf("Scope: %s", testScope)

_, err = ins1.Execution().ExecuteCommand(ctx, "echo", fileContent, ">", "/tmp/test-id")
require.NoError(t, err)

time.Sleep(1 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Replace arbitrary sleep with proper readiness check

Using time.Sleep is not reliable for testing. Consider implementing a proper readiness check or using a probe.

- _, err = ins1.Execution().ExecuteCommand(ctx, "echo", fileContent, ">", "/tmp/test-id")
- require.NoError(t, err)
- 
- time.Sleep(1 * time.Second)
+ _, err = ins1.Execution().ExecuteCommand(ctx, "echo", fileContent, ">", "/tmp/test-id")
+ require.NoError(t, err)
+ 
+ // Wait for file to be ready
+ err = waitForFile(ctx, ins1, "/tmp/test-id")
+ require.NoError(t, err)

Consider adding this helper function:

func waitForFile(ctx context.Context, ins *instance.Instance, path string) error {
    for i := 0; i < 10; i++ {
        _, err := ins.Execution().ExecuteCommand(ctx, "test", "-f", path)
        if err == nil {
            return nil
        }
        select {
        case <-ctx.Done():
            return ctx.Err()
        case <-time.After(100 * time.Millisecond):
        }
    }
    return fmt.Errorf("file %s not ready", path)
}


ins2, err := createInstance(ctx, instanceName, testScope)
require.NoError(t, err)

out, err := ins2.Execution().ExecuteCommand(ctx, "cat", "/tmp/test-id")
require.NoError(t, err)
require.Contains(t, out, fileContent)
}

func createInstance(ctx context.Context, name, testScope string) (*instance.Instance, error) {
knOpts := knuu.Options{Timeout: testTimeout}
if testScope != "" {
knOpts.Scope = testScope
}

kn, err := knuu.New(ctx, knOpts)
if err != nil {
return nil, err
}

kn.HandleStopSignal(ctx)

ins, err := kn.NewInstance(name)
if err != nil {
return nil, err
}

if err := ins.Build().SetImage(ctx, alpineImage); err != nil {
return nil, err
}

if err := ins.Build().Commit(ctx); err != nil {
return nil, err
}

if err := ins.Build().SetStartCommand("sleep", "infinity"); err != nil {
return nil, err
}

if err := ins.Execution().Start(ctx); err != nil {
return nil, err
}

return ins, nil
}
17 changes: 8 additions & 9 deletions e2e/system/start_callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"sync"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
corev1 "k8s.io/client-go/applyconfigurations/core/v1"

"github.com/celestiaorg/knuu/e2e"
)
Expand All @@ -23,14 +23,13 @@ func (s *Suite) TestStartWithCallback() {

// This probe is used to make sure the instance will not be ready for a second so the
// second execute command must fail and the first one with callback must succeed
err := target.Monitoring().SetReadinessProbe(&corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/",
Port: intstr.FromInt(e2e.NginxPort),
},
},
})
err := target.Monitoring().SetReadinessProbe(
corev1.Probe().
WithHTTPGet(corev1.HTTPGetAction().
WithPath("/").
WithPort(intstr.FromInt(e2e.NginxPort)),
),
)
s.Require().NoError(err)

err = target.Build().SetStartCommand([]string{"sleep", sleepTimeBeforeReady, "&&", e2e.NginxCommand}...)
Expand Down
30 changes: 0 additions & 30 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.22.5

require (
github.com/celestiaorg/bittwister v0.0.0-20231213180407-65cdbaf5b8c7
github.com/docker/docker v26.1.4+incompatible
github.com/google/uuid v1.6.0
github.com/minio/minio-go/v7 v7.0.74
github.com/sirupsen/logrus v1.9.3
Expand All @@ -17,23 +16,15 @@ require (
)

require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/acroca/go-symbols v0.1.1 // indirect
github.com/cilium/ebpf v0.12.3 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/go-connections v0.4.1-0.20210727194412-58542c764a11 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/frankban/quicktest v1.14.6 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand All @@ -49,56 +40,35 @@ require (
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/karrick/godirwalk v1.17.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/nsf/gocode v0.0.0-20230322162601-b672b49f3818 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/ramya-rao-a/go-outline v0.0.0-20210608161538-9736a4bde949 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/uudashr/gopkgs v1.3.2 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/term v0.26.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.27.0 // indirect
golang.org/x/tools/cmd/guru v0.1.1-deprecated // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
Loading
Loading