Skip to content

Commit

Permalink
fix: apply pvc & upgrade image test added in long running
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Jan 27, 2025
1 parent 6297ac8 commit 73d0d9b
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 67 deletions.
67 changes: 67 additions & 0 deletions e2e/longrunning/image_upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package longrunning

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/resource"
)

func TestImageUpgrade(t *testing.T) {
const (
instanceName = "image-upgrade"
firstImage = "alpine:3.20.3"
secondImage = "nginx:latest"
volumeSize = "1Gi"
volumePath = "/tmp/test-id"
fileContent = "hello world"
filePath = volumePath + "/test-id.txt"
)

ctx := context.Background()

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

err = ins1.Storage().AddVolume(volumePath, resource.MustParse(volumeSize))
require.NoError(t, err)

require.NoError(t, ins1.Execution().Start(ctx))

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

_, err = ins1.Execution().ExecuteCommand(ctx, "echo", fileContent, ">", filePath)
require.NoError(t, err)

t.Logf("Waiting for 5 seconds to simulate a long running process")
time.Sleep(5 * time.Second)

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

err = ins2.Storage().AddVolume(volumePath, resource.MustParse(volumeSize))
require.NoError(t, err)

require.NoError(t, ins2.Execution().Start(ctx))

// To upgrade the image, first the instance must be stopped
require.NoError(t, ins2.Execution().Stop(ctx))

// Now we can upgrade the image
require.NoError(t, ins2.Build().SetImage(ctx, secondImage))
require.NoError(t, ins2.Build().Commit(ctx))
require.NoError(t, ins2.Execution().Start(ctx))

// Test if the alpine image is replaced with the nginx image successfully
out, err := ins2.Execution().ExecuteCommand(ctx, "cat", "/etc/nginx/nginx.conf")
require.NoError(t, err)
require.NotEmpty(t, out)

// Test if the volume is persisted across the image upgrade
out, err = ins2.Execution().ExecuteCommand(ctx, "cat", filePath)
require.NoError(t, err)
require.Contains(t, out, fileContent)
}
45 changes: 2 additions & 43 deletions e2e/longrunning/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import (
"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 * 5
)

func TestSimple(t *testing.T) {
Expand All @@ -24,7 +20,7 @@ func TestSimple(t *testing.T) {

ctx := context.Background()

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

Expand All @@ -36,47 +32,10 @@ func TestSimple(t *testing.T) {
t.Logf("Waiting for 5 seconds to simulate a long running process")
time.Sleep(5 * time.Second)

ins2, err := createInstance(ctx, instanceName, testScope)
ins2, err := createInstanceAndStart(ctx, instanceName, testScope, alpineImage)
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, SkipCleanup: true}
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
}
59 changes: 59 additions & 0 deletions e2e/longrunning/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package longrunning

import (
"context"
"time"

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

const (
testTimeout = time.Minute * 5
)

func createInstance(ctx context.Context, name, testScope string, image string) (*instance.Instance, error) {
knOpts := knuu.Options{Timeout: testTimeout, SkipCleanup: true}
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, image); 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
}

return ins, nil
}

func createInstanceAndStart(ctx context.Context, name, testScope string, image string) (*instance.Instance, error) {
ins, err := createInstance(ctx, name, testScope, image)
if err != nil {
return nil, err
}

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

return ins, nil
}
8 changes: 5 additions & 3 deletions pkg/instance/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,12 @@ func (s *storage) addFileToInstance(srcPath, dest, chown string) error {

file := s.instance.K8sClient.NewFile(srcPath, dest, chown, permission)

// TODO: remove this
fmt.Printf("\nfile: %#v\n", file)

s.files = append(s.files, file)
s.instance.Logger.WithFields(logrus.Fields{
"file": file,
"instance": s.instance.name,
}).Debug("added file to instance")

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
defaultClusterDomain = "cluster.local"

// FieldManager is the field manager to use for the Kubernetes client
fieldManager = "knuu"
FieldManager = "knuu"
)

type Client struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (c *Client) DeployPod(ctx context.Context, podConfig PodConfig, init bool)
pod := c.preparePod(podConfig, init)
return c.clientset.CoreV1().Pods(c.namespace).
Apply(ctx, pod, metav1.ApplyOptions{
FieldManager: fieldManager,
FieldManager: FieldManager,
})
}
func (c *Client) NewVolume(path string, size resource.Quantity, owner int64) *Volume {
Expand Down
33 changes: 15 additions & 18 deletions pkg/k8s/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/applyconfigurations/core/v1"
)

// CreatePersistentVolumeClaim deploys a PersistentVolumeClaim if it does not exist.
Expand All @@ -29,29 +30,25 @@ func (c *Client) CreatePersistentVolumeClaim(
return err
}

pvc := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: c.namespace,
Name: name,
Labels: labels,
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{
v1.ReadWriteOnce,
},
Resources: v1.VolumeResourceRequirements{
Requests: v1.ResourceList{
pvc := corev1.PersistentVolumeClaim(name, c.namespace).
WithLabels(labels).
WithSpec(corev1.PersistentVolumeClaimSpec().
WithAccessModes(v1.ReadWriteOnce).
WithResources(corev1.VolumeResourceRequirements().
WithRequests(v1.ResourceList{
v1.ResourceStorage: size,
},
},
},
}
}),
),
)

if _, err := c.clientset.CoreV1().PersistentVolumeClaims(c.namespace).Create(ctx, pvc, metav1.CreateOptions{}); err != nil {
_, err := c.clientset.CoreV1().PersistentVolumeClaims(c.namespace).Apply(ctx, pvc, metav1.ApplyOptions{
FieldManager: FieldManager,
})
if err != nil {
return ErrCreatingPersistentVolumeClaim.WithParams(name).Wrap(err)
}

c.logger.WithField("name", name).Debug("PersistentVolumeClaim created")
c.logger.WithField("name", name).Debug("PersistentVolumeClaim applied")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Client) CreateReplicaSet(ctx context.Context, rsConfig ReplicaSetConfig
}

return c.clientset.AppsV1().ReplicaSets(c.namespace).
Apply(ctx, newRs, metav1.ApplyOptions{FieldManager: fieldManager})
Apply(ctx, newRs, metav1.ApplyOptions{FieldManager: FieldManager})
}

func (c *Client) ReplaceReplicaSetWithGracePeriod(ctx context.Context, ReplicaSetConfig ReplicaSetConfig, gracePeriod *int64) (*appv1.ReplicaSet, error) {
Expand Down

0 comments on commit 73d0d9b

Please sign in to comment.