mirror of https://github.com/knative/func.git
feat: client effective runtimes list (#490)
* feat: repositories accessor * feat: repository and templates client api - Templates management api - Repositories management api expansion * fix: nil pointer reference on generate * src: test temp directory name consistency and comment improvements * feat: runtimes accessor
This commit is contained in:
parent
ae638c349c
commit
e0aad6f936
24
client.go
24
client.go
|
|
@ -686,6 +686,30 @@ func (c *Client) Emit(ctx context.Context, endpoint string) error {
|
||||||
return c.emitter.Emit(ctx, endpoint)
|
return c.emitter.Emit(ctx, endpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Runtimes available in totality.
|
||||||
|
// Not all repository/template combinations necessarily exist,
|
||||||
|
// and further validation is performed when a template+runtime is chosen.
|
||||||
|
// from a given repository. This is the global list of all available.
|
||||||
|
// Returned list is unique and sorted.
|
||||||
|
func (c *Client) Runtimes() ([]string, error) {
|
||||||
|
runtimes := newSortedSet()
|
||||||
|
|
||||||
|
// Gather all runtimes from all repositories
|
||||||
|
// into a uniqueness map
|
||||||
|
repositories, err := c.Repositories.All()
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
for _, repo := range repositories {
|
||||||
|
for _, runtime := range repo.Runtimes {
|
||||||
|
runtimes.Add(runtime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a unique, sorted list of runtimes
|
||||||
|
return runtimes.Items(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// sorted set of strings.
|
// sorted set of strings.
|
||||||
//
|
//
|
||||||
// write-optimized and suitable only for fairly small values of N.
|
// write-optimized and suitable only for fairly small values of N.
|
||||||
|
|
|
||||||
|
|
@ -831,6 +831,50 @@ func TestWithConfiguredBuildpacks(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestRuntimes ensures that the total set of runtimes are returned.
|
||||||
|
func TestRuntimes(t *testing.T) {
|
||||||
|
// TODO: test when a specific repo override is indicated
|
||||||
|
// (remote repo which takes precidence over embedded and extended)
|
||||||
|
|
||||||
|
client := fn.New(fn.WithRepositories("testdata/repositories"))
|
||||||
|
|
||||||
|
runtimes, err := client.Runtimes()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Runtimes from `./templates` + `./testdata/repositories`
|
||||||
|
// Should be unique and sorted.
|
||||||
|
//
|
||||||
|
// Note that hard-coding the runtimes list here does add future maintenance
|
||||||
|
// (test will fail requiring updates when either the builtin set of or test
|
||||||
|
// set change), but the simplicity and straightforwardness of this
|
||||||
|
// requirement seems to outweigh the complexity of calculating the list for
|
||||||
|
// testing, which effectively just recreates the logic within the client.
|
||||||
|
// Additionally, this list has the benefit of creating a more understandable
|
||||||
|
// test (a primary goal of course being human communication of libray intent).
|
||||||
|
// If this is an incorrect assumption, we would need to calculate this
|
||||||
|
// slice from the contents of ./templates & ./testdata/repositories, taking
|
||||||
|
// into acount future repository manifests.
|
||||||
|
expected := []string{
|
||||||
|
"customRuntime",
|
||||||
|
"go",
|
||||||
|
"node",
|
||||||
|
"python",
|
||||||
|
"quarkus",
|
||||||
|
"rust",
|
||||||
|
"springboot",
|
||||||
|
"test",
|
||||||
|
"typescript",
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(runtimes, expected) {
|
||||||
|
t.Logf("expected: %v", expected)
|
||||||
|
t.Logf("received: %v", runtimes)
|
||||||
|
t.Fatal("Runtimes not as expected.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Helpers ----
|
// Helpers ----
|
||||||
|
|
||||||
// USING: Make specified dir. Return deferrable cleanup fn.
|
// USING: Make specified dir. Return deferrable cleanup fn.
|
||||||
|
|
|
||||||
|
|
@ -98,10 +98,10 @@ func (r *Repositories) Add(name, uri string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename a repository
|
// Rename a repository
|
||||||
func (r *Repositories) Rename(old string, new string) error {
|
func (r *Repositories) Rename(from, to string) error {
|
||||||
oldPath := filepath.Join(r.Path, old)
|
a := filepath.Join(r.Path, from)
|
||||||
newPath := filepath.Join(r.Path, new)
|
b := filepath.Join(r.Path, to)
|
||||||
return os.Rename(oldPath, newPath)
|
return os.Rename(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove a repository of the given name from the repositories.
|
// Remove a repository of the given name from the repositories.
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ func TestRepositoriesGet(t *testing.T) {
|
||||||
|
|
||||||
// valid should have expected name
|
// valid should have expected name
|
||||||
if repo.Name != "customProvider" {
|
if repo.Name != "customProvider" {
|
||||||
t.Fatalf("expected 'customProvider' as repository name, got: %v", repo.Name)
|
t.Fatalf("Expected 'customProvider', got: %v", repo.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue