-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from vladimirvivien/starlark-impl-parity
Starlark - Implement Configuration Functions Impl
- Loading branch information
Showing
9 changed files
with
563 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
// addDefaultCrashdConf initalizes a Starlark Dict with default | ||
// crashd_config configuration data | ||
func addDefaultCrashdConf(thread *starlark.Thread) error { | ||
args := []starlark.Tuple{ | ||
starlark.Tuple{starlark.String("gid"), starlark.String(getGid())}, | ||
starlark.Tuple{starlark.String("uid"), starlark.String(getUid())}, | ||
starlark.Tuple{starlark.String("workdir"), starlark.String(defaults.workdir)}, | ||
starlark.Tuple{starlark.String("output_path"), starlark.String(defaults.outPath)}, | ||
} | ||
|
||
_, err := crashdConfigFn(thread, nil, nil, args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// crashConfig is built-in starlark function that wraps the kwargs into a dictionary value. | ||
// The result is also added to the thread for other built-in to access. | ||
func crashdConfigFn(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var dictionary *starlark.Dict | ||
if kwargs != nil { | ||
dict, err := tupleSliceToDict(kwargs) | ||
if err != nil { | ||
return starlark.None, err | ||
} | ||
dictionary = dict | ||
} | ||
|
||
// save dict to be used as default | ||
thread.SetLocal(identifiers.crashdCfg, dictionary) | ||
|
||
return dictionary, nil | ||
} |
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,133 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"go.starlark.net/starlark" | ||
) | ||
|
||
func TestCrashdConfigNew(t *testing.T) { | ||
e := New() | ||
if e.thread == nil { | ||
t.Error("thread is nil") | ||
} | ||
cfg := e.thread.Local(identifiers.crashdCfg) | ||
if cfg == nil { | ||
t.Error("crashd_config dict not found in thread") | ||
} | ||
} | ||
|
||
func TestCrashdConfigFunc(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
script string | ||
eval func(t *testing.T, script string) | ||
}{ | ||
{ | ||
name: "crash_config saved in thread", | ||
script: `crashd_config(foo="fooval", bar="barval")`, | ||
eval: func(t *testing.T, script string) { | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
data := exe.thread.Local(identifiers.crashdCfg) | ||
if data == nil { | ||
t.Fatal("crashd_config not saved in thread local") | ||
} | ||
cfg, ok := data.(*starlark.Dict) | ||
if !ok { | ||
t.Fatalf("unexpected type for thread local key configs.crashd: %T", data) | ||
} | ||
if cfg.Len() != 2 { | ||
t.Fatalf("unexpected item count in configs.crashd: %d", cfg.Len()) | ||
} | ||
val, found, err := cfg.Get(starlark.String("foo")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !found { | ||
t.Fatalf("key 'foo' not found in configs.crashd") | ||
} | ||
if trimQuotes(val.String()) != "fooval" { | ||
t.Fatalf("unexpected value for key 'foo': %s", val.String()) | ||
} | ||
}, | ||
}, | ||
|
||
{ | ||
name: "crash_config returned value", | ||
script: `cfg = crashd_config(foo="fooval", bar="barval")`, | ||
eval: func(t *testing.T, script string) { | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
data := exe.result["cfg"] | ||
if data == nil { | ||
t.Fatal("crashd_config function not returning value") | ||
} | ||
cfg, ok := data.(*starlark.Dict) | ||
if !ok { | ||
t.Fatalf("unexpected type for thread local key configs.crashd: %T", data) | ||
} | ||
if cfg.Len() != 2 { | ||
t.Fatalf("unexpected item count in configs.crashd: %d", cfg.Len()) | ||
} | ||
val, found, err := cfg.Get(starlark.String("foo")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !found { | ||
t.Fatalf("key 'foo' not found in configs.crashd") | ||
} | ||
if trimQuotes(val.String()) != "fooval" { | ||
t.Fatalf("unexpected value for key %s in configs.crashd", val.String()) | ||
} | ||
}, | ||
}, | ||
|
||
{ | ||
name: "crash_config default", | ||
script: `one = 1`, | ||
eval: func(t *testing.T, script string) { | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
data := exe.thread.Local(identifiers.crashdCfg) | ||
if data == nil { | ||
t.Fatal("default crashd_config not saved in thread local") | ||
} | ||
|
||
cfg, ok := data.(*starlark.Dict) | ||
if !ok { | ||
t.Fatalf("unexpected type for thread local key crashd_config: %T", data) | ||
} | ||
if cfg.Len() != 4 { | ||
t.Fatalf("unexpected item count in configs.crashd: %d", cfg.Len()) | ||
} | ||
val, found, err := cfg.Get(starlark.String("uid")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !found { | ||
t.Fatalf("key 'foo' not found in configs.crashd") | ||
} | ||
if trimQuotes(val.String()) != getUid() { | ||
t.Fatalf("unexpected value for key %s in configs.crashd", val.String()) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.eval(t, test.script) | ||
}) | ||
} | ||
} |
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,45 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
// addDefaultSshConf initalizes a Starlark Dict with default | ||
// ssh_config configuration data | ||
func addDefaultSSHConf(thread *starlark.Thread) error { | ||
args := []starlark.Tuple{ | ||
starlark.Tuple{starlark.String("username"), starlark.String(getUsername())}, | ||
starlark.Tuple{starlark.String("private_key_path"), starlark.String(defaults.pkPath)}, | ||
starlark.Tuple{starlark.String("conn_retries"), starlark.MakeInt(defaults.connRetries)}, | ||
starlark.Tuple{starlark.String("conn_timeout"), starlark.MakeInt(defaults.connTimeout)}, | ||
} | ||
|
||
_, err := sshConfigFn(thread, nil, nil, args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// sshConfigFn is the backing built-in function for the `ssh_config` configuration function. | ||
// It creates and returns a dictionary from collected configs (as kwargs) | ||
// It also saves the dict into the thread as the last known ssh config to be used as default. | ||
func sshConfigFn(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var dictionary *starlark.Dict | ||
if kwargs != nil { | ||
dict, err := tupleSliceToDict(kwargs) | ||
if err != nil { | ||
return starlark.None, err | ||
} | ||
dictionary = dict | ||
} | ||
|
||
// save to be used as default when needed | ||
thread.SetLocal(identifiers.sshCfg, dictionary) | ||
|
||
return dictionary, nil | ||
} |
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,134 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"go.starlark.net/starlark" | ||
) | ||
|
||
func TestSSHConfigNew(t *testing.T) { | ||
e := New() | ||
if e.thread == nil { | ||
t.Error("thread is nil") | ||
} | ||
cfg := e.thread.Local(identifiers.sshCfg) | ||
if cfg == nil { | ||
t.Error("ssh_config dict not found in thread") | ||
} | ||
} | ||
|
||
func TestSSHConfigFunc(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
script string | ||
eval func(t *testing.T, script string) | ||
}{ | ||
{ | ||
name: "ssh_config saved in thread", | ||
script: `ssh_config(username="uname", private_key_path="path")`, | ||
eval: func(t *testing.T, script string) { | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
data := exe.thread.Local(identifiers.sshCfg) | ||
if data == nil { | ||
t.Fatal("ssh_config not saved in thread local") | ||
} | ||
cfg, ok := data.(*starlark.Dict) | ||
if !ok { | ||
t.Fatalf("unexpected type for thread local key ssh_config: %T", data) | ||
} | ||
if cfg.Len() != 2 { | ||
t.Fatalf("unexpected item count in ssh_config: %d", cfg.Len()) | ||
} | ||
val, found, err := cfg.Get(starlark.String("username")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !found { | ||
t.Fatalf("key 'username' not found in ssh_config") | ||
} | ||
if trimQuotes(val.String()) != "uname" { | ||
t.Fatalf("unexpected value for key 'foo': %s", val.String()) | ||
} | ||
}, | ||
}, | ||
|
||
{ | ||
name: "ssh_config returned value", | ||
script: `cfg = ssh_config(username="uname", private_key_path="path")`, | ||
eval: func(t *testing.T, script string) { | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
data := exe.result["cfg"] | ||
if data == nil { | ||
t.Fatal("ssh_config function not returning value") | ||
} | ||
cfg, ok := data.(*starlark.Dict) | ||
if !ok { | ||
t.Fatalf("unexpected type for thread local key ssh_config: %T", data) | ||
} | ||
if cfg.Len() != 2 { | ||
t.Fatalf("unexpected item count in ssh_config: %d", cfg.Len()) | ||
} | ||
val, found, err := cfg.Get(starlark.String("private_key_path")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !found { | ||
t.Fatalf("key 'private_key_path' not found in ssh_config") | ||
} | ||
if trimQuotes(val.String()) != "path" { | ||
t.Fatalf("unexpected value for key %s in ssh_config", val.String()) | ||
} | ||
}, | ||
}, | ||
|
||
{ | ||
name: "crash_config default", | ||
script: `one = 1`, | ||
eval: func(t *testing.T, script string) { | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
data := exe.thread.Local(identifiers.sshCfg) | ||
if data == nil { | ||
t.Fatal("default ssh_config not saved in thread local") | ||
} | ||
|
||
cfg, ok := data.(*starlark.Dict) | ||
if !ok { | ||
t.Fatalf("unexpected type for thread local key ssh_config: %T", data) | ||
} | ||
if cfg.Len() != 4 { | ||
t.Fatalf("unexpected item count in ssh_config: %d", cfg.Len()) | ||
} | ||
val, found, err := cfg.Get(starlark.String("conn_retries")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !found { | ||
t.Fatalf("key 'conn_retries' not found in ssh_config") | ||
} | ||
retries := val.(starlark.Int) | ||
if retries.BigInt().Int64() != int64(10) { | ||
t.Fatalf("unexpected value for key %s in configs.crashd", val.String()) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.eval(t, test.script) | ||
}) | ||
} | ||
} |
Oops, something went wrong.