package swapcfg import ( "context" _ "embed" "fmt" "os" "testing" "path/filepath" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gotest.tools/v3/fs" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1ien "edge-infra.dev/pkg/sds/ien/k8s/apis/v1" "edge-infra.dev/pkg/sds/ien/k8s/controllers/nodeagent/config" "edge-infra.dev/test/f2" ) var f f2.Framework var ( testPlugin = Plugin{} hostname = "test-ienode" kind = "IENode" apiVersion = "dsds.edge.ncr.com/v1" swapConfigFile = "/zynstra/config/swapcfg.yaml" typeMeta = metav1.TypeMeta{APIVersion: apiVersion, Kind: kind} swapEnabled = true testSwapConfigFile = `swap: enabled: false ` expectedSwapConfigFile = `swap: enabled: true ` 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}} ) func TestMain(m *testing.M) { f = f2.New(context.Background(), f2.WithExtensions()). Setup(). Teardown() os.Exit(f.Run(m)) } func TestReconcile(t *testing.T) { dir := fs.NewDir(t, "test-dir") defer dir.Remove() err := os.MkdirAll(filepath.Join(dir.Path(), "zynstra/config"), 0755) assert.NoError(t, err) NonexistentFileFeature := f2.NewFeature("swapcfg plugin without existing file"). Test("reconcile plugin without existing file", func(ctx f2.Context, t *testing.T) f2.Context { path := dir.Path() _, err := testPlugin.Reconcile(ctx, &testIENode, config.NewConfig(nil, nil, nil, config.Flags{ NodeRootPath: &path, })) assert.NoError(t, err) content, err := os.ReadFile(filepath.Join(dir.Path(), swapConfigFile)) assert.NoError(t, err) fmt.Println(string(content)) assert.Equal(t, expectedSwapConfigFile, string(content)) return ctx }).Feature() ExistentFileFeature := f2.NewFeature("swapcfg plugin with existing file"). Setup("Create swapcfg", func(ctx f2.Context, t *testing.T) f2.Context { swapCfgContents := []byte(testSwapConfigFile) err := os.WriteFile(filepath.Join(dir.Path(), "zynstra/config/swapcfg.yaml"), swapCfgContents, 0644) require.NoError(t, err) return ctx }). Test("reconile plugin with existing file", func(ctx f2.Context, t *testing.T) f2.Context { path := dir.Path() _, err := testPlugin.Reconcile(ctx, &testIENode, config.NewConfig(nil, nil, nil, config.Flags{ NodeRootPath: &path, })) assert.NoError(t, err) content, err := os.ReadFile(filepath.Join(dir.Path(), swapConfigFile)) assert.NoError(t, err) assert.Equal(t, expectedSwapConfigFile, string(content)) return ctx }).Feature() f.Test(t, NonexistentFileFeature, ExistentFileFeature) }