1 package swapcfg
2
3 import (
4 "context"
5 _ "embed"
6 "fmt"
7 "os"
8 "testing"
9
10 "path/filepath"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 "gotest.tools/v3/fs"
15
16 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
18 v1ien "edge-infra.dev/pkg/sds/ien/k8s/apis/v1"
19 "edge-infra.dev/pkg/sds/ien/k8s/controllers/nodeagent/config"
20 "edge-infra.dev/test/f2"
21 )
22
23 var f f2.Framework
24
25 var (
26 testPlugin = Plugin{}
27
28 hostname = "test-ienode"
29 kind = "IENode"
30 apiVersion = "dsds.edge.ncr.com/v1"
31 swapConfigFile = "/zynstra/config/swapcfg.yaml"
32 typeMeta = metav1.TypeMeta{APIVersion: apiVersion, Kind: kind}
33 swapEnabled = true
34 testSwapConfigFile = `swap:
35 enabled: false
36 `
37 expectedSwapConfigFile = `swap:
38 enabled: true
39 `
40 testIENode = v1ien.IENode{TypeMeta: typeMeta, ObjectMeta: metav1.ObjectMeta{Name: hostname, UID: "1"}, Spec: v1ien.IENodeSpec{Role: v1ien.ControlPlane, Lane: "1", Class: v1ien.Server, SwapEnabled: &swapEnabled}}
41 )
42
43 func TestMain(m *testing.M) {
44 f = f2.New(context.Background(), f2.WithExtensions()).
45 Setup().
46 Teardown()
47 os.Exit(f.Run(m))
48 }
49
50 func TestReconcile(t *testing.T) {
51 dir := fs.NewDir(t, "test-dir")
52 defer dir.Remove()
53
54 err := os.MkdirAll(filepath.Join(dir.Path(), "zynstra/config"), 0755)
55 assert.NoError(t, err)
56
57 NonexistentFileFeature := f2.NewFeature("swapcfg plugin without existing file").
58 Test("reconcile plugin without existing file", func(ctx f2.Context, t *testing.T) f2.Context {
59 path := dir.Path()
60 _, err := testPlugin.Reconcile(ctx, &testIENode, config.NewConfig(nil, nil, nil, config.Flags{
61 NodeRootPath: &path,
62 }))
63 assert.NoError(t, err)
64 content, err := os.ReadFile(filepath.Join(dir.Path(), swapConfigFile))
65 assert.NoError(t, err)
66 fmt.Println(string(content))
67 assert.Equal(t, expectedSwapConfigFile, string(content))
68 return ctx
69 }).Feature()
70
71 ExistentFileFeature := f2.NewFeature("swapcfg plugin with existing file").
72 Setup("Create swapcfg", func(ctx f2.Context, t *testing.T) f2.Context {
73 swapCfgContents := []byte(testSwapConfigFile)
74 err := os.WriteFile(filepath.Join(dir.Path(), "zynstra/config/swapcfg.yaml"), swapCfgContents, 0644)
75 require.NoError(t, err)
76 return ctx
77 }).
78 Test("reconile plugin with existing file", func(ctx f2.Context, t *testing.T) f2.Context {
79 path := dir.Path()
80 _, err := testPlugin.Reconcile(ctx, &testIENode, config.NewConfig(nil, nil, nil, config.Flags{
81 NodeRootPath: &path,
82 }))
83 assert.NoError(t, err)
84 content, err := os.ReadFile(filepath.Join(dir.Path(), swapConfigFile))
85 assert.NoError(t, err)
86 assert.Equal(t, expectedSwapConfigFile, string(content))
87 return ctx
88 }).Feature()
89
90 f.Test(t, NonexistentFileFeature, ExistentFileFeature)
91 }
92
View as plain text