-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): Add test cases for shell and home manager (#163)
* chore: Add more test cases Signed-off-by: Ce Gao <cegao@tensorchord.ai> * fix: Fix Signed-off-by: Ce Gao <cegao@tensorchord.ai>
- Loading branch information
Showing
10 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package jupyter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/tensorchord/envd/pkg/lang/ir" | ||
) | ||
|
||
func TestGenerateCommand(t *testing.T) { | ||
testcases := []struct { | ||
graph ir.Graph | ||
dir string | ||
expected []string | ||
}{ | ||
{ | ||
graph: ir.Graph{ | ||
JupyterConfig: &ir.JupyterConfig{ | ||
Password: "", | ||
Port: 8888, | ||
}, | ||
}, | ||
dir: "test", | ||
expected: []string{ | ||
"jupyter", "notebook", "--allow-root", | ||
"--ip", "0.0.0.0", "--notebook-dir", "test", | ||
"--NotebookApp.password", "''", "--NotebookApp.token", "''", | ||
"--port", "8888", | ||
}, | ||
}, | ||
{ | ||
graph: ir.Graph{ | ||
JupyterConfig: &ir.JupyterConfig{ | ||
Password: "test", | ||
Port: 8888, | ||
}, | ||
}, | ||
dir: "test", | ||
expected: []string{ | ||
"jupyter", "notebook", "--allow-root", | ||
"--ip", "0.0.0.0", "--notebook-dir", "test", | ||
"--NotebookApp.password", "test", "--NotebookApp.token", "''", | ||
"--port", "8888", | ||
}, | ||
}, | ||
{ | ||
graph: ir.Graph{}, | ||
dir: "test", | ||
expected: []string{}, | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
actual := GenerateCommand(tc.graph, tc.dir) | ||
if !equal(actual, tc.expected) { | ||
t.Errorf("failed to generate the command: expected %v, got %v", tc.expected, actual) | ||
} | ||
} | ||
} | ||
|
||
// Equal tells whether a and b contain the same elements. | ||
// A nil argument is equivalent to an empty slice. | ||
func equal(a, b []string) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
for i, v := range a { | ||
if v != b[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package home | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestManager(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Home Manager Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package home | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/adrg/xdg" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/tensorchord/envd/pkg/util/fileutil" | ||
) | ||
|
||
var _ = Describe("home manager", func() { | ||
BeforeEach(func() { | ||
// Cleanup the home cache. | ||
Expect(Initialize()).NotTo(HaveOccurred()) | ||
m := GetManager() | ||
Expect(fileutil.RemoveAll(m.(*generalManager).cacheStatusFile)).NotTo(HaveOccurred()) | ||
}) | ||
AfterEach(func() { | ||
// Cleanup the home cache. | ||
Expect(Initialize()).NotTo(HaveOccurred()) | ||
m := GetManager() | ||
Expect(fileutil.RemoveAll(m.(*generalManager).cacheStatusFile)).NotTo(HaveOccurred()) | ||
}) | ||
When("initialized", func() { | ||
It("should initialized successfully", func() { | ||
Expect(Initialize()).NotTo(HaveOccurred()) | ||
m := GetManager() | ||
Expect(m.CacheDir()).To(Equal(filepath.Join(xdg.CacheHome, "envd"))) | ||
Expect(m.ConfigFile()).To(Equal(filepath.Join(xdg.ConfigHome, "envd/config.envd"))) | ||
}) | ||
It("should return the cache status", func() { | ||
Expect(Initialize()).NotTo(HaveOccurred()) | ||
m := GetManager() | ||
Expect(m.Cached("test")).To(BeFalse()) | ||
Expect(m.MarkCache("test", true)).To(Succeed()) | ||
Expect(m.Cached("test")).To(BeTrue()) | ||
// Restart the init process, the cache should be persistent. | ||
Expect(Initialize()).NotTo(HaveOccurred()) | ||
m = GetManager() | ||
Expect(m.Cached("test")).To(BeTrue()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package shell | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestZSH(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "ZSH Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package shell | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/tensorchord/envd/pkg/home" | ||
"github.com/tensorchord/envd/pkg/util/fileutil" | ||
) | ||
|
||
var _ = Describe("zsh manager", func() { | ||
zshManager := NewManager() | ||
BeforeEach(func() { | ||
Expect(home.Initialize()).NotTo(HaveOccurred()) | ||
}) | ||
AfterEach(func() { | ||
// Cleanup the home cache. | ||
Expect(home.Initialize()).NotTo(HaveOccurred()) | ||
m := home.GetManager() | ||
Expect(fileutil.RemoveAll(m.CacheDir())).NotTo(HaveOccurred()) | ||
}) | ||
When("cached", func() { | ||
It("should skip", func() { | ||
err := home.GetManager().MarkCache(cacheKey, true) | ||
Expect(err).NotTo(HaveOccurred()) | ||
cached, err := zshManager.DownloadOrCache() | ||
Expect(cached).To(BeTrue()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
When("not cached", func() { | ||
It("should download", func() { | ||
err := home.GetManager().MarkCache(cacheKey, false) | ||
Expect(err).NotTo(HaveOccurred()) | ||
cached, err := zshManager.DownloadOrCache() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(cached).To(BeFalse()) | ||
exists, err := fileutil.DirExists(filepath.Join(home.GetManager().CacheDir(), "oh-my-zsh")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(exists).To(BeTrue()) | ||
}) | ||
}) | ||
}) |