-
-
Notifications
You must be signed in to change notification settings - Fork 347
[wip] cli: Add info command #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,6 @@ | ||
| # minimal example to make integrationtest pass | ||
| $root = getenv("MGMT_TEST_ROOT") | ||
|
|
||
| file "${root}/pass" { | ||
| content => "not empty", | ||
| } |
This file contains hidden or 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,112 @@ | ||
| // Mgmt | ||
| // Copyright (C) 2013-2018+ James Shubin and the project contributors | ||
| // Written by James Shubin <james@shubin.ca> and the project contributors | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "os/exec" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| errwrap "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| const ( | ||
| stringExistingInHelp = "--version, -v print the version" | ||
| ) | ||
|
|
||
| func TestHelp(t *testing.T) { | ||
| out, err := exec.Command(mgmt).Output() | ||
| if err != nil { | ||
| t.Fatalf("Error: %v", err) | ||
| } | ||
| if !strings.Contains(string(out), stringExistingInHelp) { | ||
| t.Logf("Command output: %s", string(out)) | ||
| t.Fatal("Expected output not found") | ||
| } | ||
| } | ||
|
|
||
| // TestSmoketest makes sure the most basic functionality works. | ||
| // If this test fails assumptions made by the rest of the testsuite are invalid. | ||
| func TestSmoketest(t *testing.T) { | ||
| // create an mgmt test environment and ensure cleanup/debug logging on failure/exit | ||
| m := Instance{} | ||
| defer m.Cleanup(t) | ||
|
|
||
| // run mgmt to convergence | ||
| m.Run(t) | ||
|
|
||
| // verify output contains what is expected from a converging and finished run | ||
| m.Finished(t, true) | ||
| } | ||
|
|
||
| // TestSimple applies a simple mcl file and tests the result. | ||
| // If this test fails assumptions made by the rest of the testsuite are invalid. | ||
| func TestSimple(t *testing.T) { | ||
| // create an mgmt test environment and ensure cleanup/debug logging on failure/exit | ||
| m := Instance{} | ||
| defer m.Cleanup(t) | ||
|
|
||
| // apply the configration from lang/simple.mcl | ||
| m.RunLang(t, "lang/simple.mcl") | ||
|
|
||
| // verify output contains what is expected from a converging and finished run | ||
| m.Finished(t, true) | ||
|
|
||
| // verify if a non-empty `pass` file is created in the working directory | ||
| m.Pass(t) | ||
| } | ||
|
|
||
| // TestDeploy checks if background running and deployment works. | ||
| // If this test fails assumptions made by the rest of the testsuite are invalid. | ||
| func TestDeploy(t *testing.T) { | ||
| // create an mgmt test environment and ensure cleanup/debug logging on failure/exit | ||
| m := Instance{} | ||
| defer m.Cleanup(t) | ||
|
|
||
| // start a mgmt instance running in the background | ||
| m.RunBackground(t) | ||
|
|
||
| // wait until server is up and running | ||
| m.WaitUntilIdle(t) | ||
|
|
||
| // expect mgmt to listen on default client and server url | ||
| if _, err := http.Get("http://127.0.0.1:2379"); err != nil { | ||
| t.Fatal("default client url is not reachable over tcp") | ||
| } | ||
| if _, err := http.Get("http://127.0.0.1:2380"); err != nil { | ||
| t.Fatal("default server url is not reachable over tcp") | ||
| } | ||
|
|
||
| // deploy lang file to the just started instance | ||
| out, err := m.DeployLangFile(nil, "lang/simple.mcl") | ||
| if err != nil { | ||
| t.Fatal(errwrap.Wrapf(err, "deploy command failed, output: %s", out)) | ||
| } | ||
| // wait for deploy to come to a rest | ||
| m.WaitUntilConverged(t) | ||
|
|
||
| // stop the running instance | ||
| m.StopBackground(t) | ||
|
|
||
| // verify output contains what is expected from a converged and cleanly finished run | ||
| m.Finished(t, false) | ||
|
|
||
| // verify if a non-empty `pass` file is created in the working directory | ||
| m.Pass(t) | ||
| } |
This file contains hidden or 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,74 @@ | ||
| // Mgmt | ||
| // Copyright (C) 2013-2018+ James Shubin and the project contributors | ||
| // Written by James Shubin <james@shubin.ca> and the project contributors | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| var mgmt string | ||
|
|
||
| // TestMain ensures a temporary environment is created (and cleaned afterward) to perform tests in | ||
| func TestMain(m *testing.M) { | ||
| // get absolute path for mgmt binary from testenvironment | ||
| mgmt = os.Getenv("MGMT") | ||
| // fallback to assumption based on current directory if path is not provided | ||
| if mgmt == "" { | ||
| path, err := filepath.Abs("../mgmt") | ||
| if err != nil { | ||
| log.Printf("failed to get absolute mgmt path") | ||
| os.Exit(1) | ||
| } | ||
| mgmt = path | ||
| } | ||
| if _, err := os.Stat(mgmt); os.IsNotExist(err) { | ||
| log.Printf("mgmt executable %s does not exist", mgmt) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // move to clean/stateless directory before running tests | ||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| log.Printf("failed to get current directory") | ||
| os.Exit(1) | ||
| } | ||
| tmpdir, err := ioutil.TempDir("", "mgmt-integrationtest") | ||
| if err != nil { | ||
| log.Printf("failed to create test working directory") | ||
| os.Exit(1) | ||
| } | ||
| if err := os.Chdir(tmpdir); err != nil { | ||
| log.Printf("failed to enter test working directory") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // run all the tests | ||
| os.Exit(m.Run()) | ||
|
|
||
| // and back to where we started | ||
| os.Chdir(cwd) | ||
|
|
||
| if err := os.RemoveAll(tmpdir); err != nil { | ||
| log.Printf("failed to remove working directory") | ||
| os.Exit(1) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,127 @@ | ||
| // Mgmt | ||
| // Copyright (C) 2013-2018+ James Shubin and the project contributors | ||
| // Written by James Shubin <james@shubin.ca> and the project contributors | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package lib | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "os" | ||
| "reflect" | ||
| "sort" | ||
| "strings" | ||
| "text/tabwriter" | ||
|
|
||
| "github.com/purpleidea/mgmt/lang/funcs" | ||
| "github.com/purpleidea/mgmt/lang/interfaces" | ||
| "github.com/purpleidea/mgmt/resources" | ||
| "github.com/urfave/cli" | ||
| ) | ||
|
|
||
| const ( | ||
| twMinWidth = 0 | ||
| twTabWidth = 8 | ||
| twPadding = 2 // ensure columns have at least a space between them | ||
| twPadChar = ' ' // using a tab here creates 'jumpy' columns on output | ||
| twFlags = 0 | ||
| ) | ||
|
|
||
| // info wraps infocmd to produce output to stdout | ||
| func info(c *cli.Context) error { | ||
| output, err := infoCmd(c) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| w := tabwriter.NewWriter(os.Stdout, twMinWidth, twTabWidth, twPadding, twPadChar, twFlags) | ||
| fmt.Fprint(w, output.String()) | ||
| w.Flush() | ||
| return nil | ||
| } | ||
|
|
||
| // infoCmd takes the cli context and returns the requested output | ||
| func infoCmd(c *cli.Context) (bytes.Buffer, error) { | ||
| var out bytes.Buffer | ||
| var names []string | ||
| descriptions := make(map[string]string) | ||
|
|
||
| switch c.Command.Name { | ||
| case "resources": | ||
| for _, name := range resources.RegisteredResourcesNames() { | ||
| names = append(names, name) | ||
|
|
||
| descriptions[name] = "" | ||
|
|
||
| res, err := resources.Lookup(name) | ||
| if err != nil { | ||
| continue | ||
| } | ||
|
|
||
| s := reflect.ValueOf(res).Elem() | ||
| typeOfT := s.Type() | ||
|
|
||
| var fields []string | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use, improve, or add to one of the functions in if anything wouldn't we want the info sub commands to be separate functions? |
||
| for i := 0; i < s.NumField(); i++ { | ||
| field := typeOfT.Field(i) | ||
| // skip unexported fields | ||
| if field.PkgPath != "" { | ||
| continue | ||
| } | ||
| fieldname := field.Name | ||
| if fieldname == "BaseRes" { | ||
| continue | ||
| } | ||
| f := s.Field(i) | ||
| fieldtype := f.Type() | ||
| fields = append(fields, fmt.Sprintf("%s (%s)", strings.ToLower(fieldname), fieldtype)) | ||
| } | ||
| descriptions[name] = strings.Join(fields, ", ") | ||
|
|
||
| } | ||
| case "functions": | ||
| for name := range funcs.RegisteredFuncs { | ||
| // skip internal functions (history, operations, etc) | ||
| if strings.HasPrefix(name, "_") { | ||
| continue | ||
| } | ||
| names = append(names, name) | ||
| descriptions[name] = "" | ||
| fn, err := funcs.Lookup(name) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| if _, ok := fn.(interfaces.PolyFunc); !ok { | ||
| // TODO: skip for now, needs Build before Info | ||
| continue | ||
| } | ||
|
|
||
| // set function signature as description | ||
| descriptions[name] = strings.Replace(fn.Info().Sig.String(), "func", name, 1) | ||
| } | ||
| default: | ||
| return out, fmt.Errorf("invalid command") | ||
| } | ||
|
|
||
| sort.Strings(names) | ||
| for _, name := range names { | ||
| if c.Bool("type") { | ||
| fmt.Fprintf(&out, "%s\t%s\n", name, descriptions[name]) | ||
| } else { | ||
| fmt.Fprintln(&out, name) | ||
| } | ||
| } | ||
| return out, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://golang.org/doc/effective_go.html#mixed-caps
probably these could be public if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i know, i'm following the names set by the tabwriter package, let me know if you prefer proper go names instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh gosh. Another inconsistency in their own style guide? Gah. Sorry. Do the right thing. If golangv2 fixes these things, we'll be ahead of the curve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed