Skip to content

Commit

Permalink
Merge pull request #280 from clcollins/fix_backplane_config
Browse files Browse the repository at this point in the history
Fix error setting backplane config env if unset
  • Loading branch information
openshift-merge-bot[bot] authored May 14, 2024
2 parents 35bf5c2 + a10e555 commit 461b36b
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pkg/backplane/backplane.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package backplane

import (
"fmt"
"os"

"github.com/openshift/ocm-container/pkg/engine"
)

const (
backplaneConfig = ".config/backplane/config.json"
backplaneConfigDest = "/root/.config/backplane/config.json"
backplaneConfig = ".config/backplane/config.json"
backplaneConfigDest = "/root/.config/backplane/config.json"
backplaneConfigMountOpts = "rw"
)

var osStat = os.Stat

type Config struct {
Env map[string]string
Mounts []engine.VolumeMount
Expand All @@ -25,17 +27,18 @@ func New(home string) (*Config, error) {
b = home + "/" + backplaneConfig
}

_, err := os.Stat(b)
_, err := osStat(b)
if err != nil {
return c, fmt.Errorf("error: problem reading backplane config: %v: %v", b, err)
return c, err
}

c.Env = make(map[string]string)
c.Env["BACKPLANE_CONFIG"] = backplaneConfig
c.Env["BACKPLANE_CONFIG"] = backplaneConfigDest // This will ALWAYS be the same inside the container, since we mount it

c.Mounts = append(c.Mounts, engine.VolumeMount{
Source: b,
Destination: backplaneConfigDest,
MountOptions: "rw",
MountOptions: backplaneConfigMountOpts,
})

return c, nil
Expand Down
128 changes: 128 additions & 0 deletions pkg/backplane/backplane_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package backplane

import (
"errors"
"io/fs"
"os"
"testing"

"github.com/openshift/ocm-container/pkg/engine"
)

func TestNew(t *testing.T) {
testCases := []struct {
name string
homeInput string
backplaneEnvInput string
expected *Config
errExpected error
}{
{
name: "BACKPLANE_CONFIG environment variable is set",
homeInput: "/home/user",
backplaneEnvInput: "/home/user/.config/backplane/prod-config.json",
expected: &Config{
Env: map[string]string{
"BACKPLANE_CONFIG": backplaneConfigDest,
},
Mounts: []engine.VolumeMount{
{
Source: "/home/user/.config/backplane/prod-config.json",
Destination: backplaneConfigDest,
MountOptions: backplaneConfigMountOpts,
},
},
},
errExpected: nil,
},
{
name: "BACKPLANE_CONFIG environment variable is not set",
homeInput: "/home/user",
backplaneEnvInput: "",
expected: &Config{
Env: map[string]string{
"BACKPLANE_CONFIG": backplaneConfigDest,
},
Mounts: []engine.VolumeMount{
{
Source: "/home/user/.config/backplane/config.json",
Destination: backplaneConfigDest,
MountOptions: backplaneConfigMountOpts,
},
},
},
errExpected: nil,
},
{
name: "BACKPLANE_CONFIG environment variable is set but file does not exist",
homeInput: "/home/user",
backplaneEnvInput: "/home/user/.config/backplane/nonexistent-config.json",
expected: &Config{},
errExpected: &fs.PathError{
Op: "stat",
Path: "/home/user/.config/backplane/nonexistent-config.json",
Err: errors.New("no such file or directory"),
},
},
{
name: "BACKPLANE_CONFIG environment variable and HOME do not match (should not matter)",
homeInput: "/home/some-other-user",
backplaneEnvInput: "/home/user/.config/backplane/prod-config.json",
expected: &Config{
Env: map[string]string{
"BACKPLANE_CONFIG": backplaneConfigDest,
},
Mounts: []engine.VolumeMount{
{
Source: "/home/user/.config/backplane/prod-config.json",
Destination: backplaneConfigDest,
MountOptions: backplaneConfigMountOpts,
},
},
},
errExpected: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
stat := osStat
defer func() { osStat = stat }()

osStat = func(name string) (os.FileInfo, error) {
if tc.name == "BACKPLANE_CONFIG environment variable is set but file does not exist" {
return nil, errors.New("someError")
}
return nil, nil
}

err := os.Setenv("BACKPLANE_CONFIG", tc.backplaneEnvInput)
if err != nil {
t.Errorf("Test Setup Failed: error setting environment variable: %v", err)
}

config, err := New(tc.homeInput)

// Assert that the error is not nil if we expect an error
if err == nil && tc.errExpected != nil {
t.Errorf("Expected: %v (%T), got %v (%T)", tc.errExpected, tc.errExpected, err, err)
}

// Assert that the BACKPLANE_CONFIG environment variable is set correctly
if config.Env["BACKPLANE_CONFIG"] != tc.expected.Env["BACKPLANE_CONFIG"] {
t.Errorf("Expected BACKPLANE_CONFIG to be %v, got %v", tc.expected.Env["BACKPLANE_CONFIG"], config.Env["BACKPLANE_CONFIG"])
}

if tc.errExpected == nil {
if len(config.Mounts) != len(tc.expected.Mounts) {
t.Errorf("Expected one mount, got %v", len(config.Mounts))
} else {
mount := config.Mounts[0]
if mount.Source != tc.expected.Mounts[0].Source || mount.Destination != tc.expected.Mounts[0].Destination || mount.MountOptions != tc.expected.Mounts[0].MountOptions {
t.Errorf("Expected mount values %v, got %v", tc.expected.Mounts, mount)
}
}
}
})
}
}

0 comments on commit 461b36b

Please sign in to comment.