Relation add and remove commands improvements. #6334

Merged
merged 6 commits into from Sep 28, 2016

Conversation

Projects
None yet
4 participants
Member

anastasiamac commented Sep 27, 2016

This PR:

  1. changes relation commands to have api-getter functions;
  2. ensures related cmd package are unit tests by testing command behavior with mock API;
  3. adds related full-stack tests to featuretests package to ensure that all connections are made as expected.

I have consciously removed some tests like relation validation as those belong in the package where validation actually happens - see apiserver/application. And, indeed, we have the tests that cover all possible permutations there.

I have a couple comments but mostly it looks very good.

cmd/juju/application/addrelation_test.go
- c.Assert(err, gc.ErrorMatches, t.err)
- }
+func (s *AddRelationSuite) TestAddRelationFail(c *gc.C) {
+ msg := "fail add relation"
@jameinel

jameinel Sep 27, 2016

Owner

I guess the message doesn't matter too much but this doesn't read like proper English

@anastasiamac

anastasiamac Sep 27, 2016

Member

Well, it is proper in a sense that it says that it's failed add/remove relation but I have re-phrased to be more obvious ;)
Let me know if it reads better and helps to understand the test.

+ return common.OperationBlockedError("TestRemoveRelationBlocked")
+ }
+ err := s.runRemoveRelation(c, "application1", "application2")
+ coretesting.AssertOperationWasBlocked(c, err, ".*TestRemoveRelationBlocked.*")
@jameinel

jameinel Sep 27, 2016

Owner

I feel like this might be missing a check of whether the error is something that is actually helpful for the end user. Thoughts?

@anastasiamac

anastasiamac Sep 27, 2016

Member

We do not return anything user-friendly to console. We log blocked operations. I wonder if it's right though... If it's not, I am tempted to address it in a separate PR as it will involve change in behavior of all blocked commands.

I have added a check to ensure that the log gets correct message with currently existing behavior.

axw approved these changes Sep 28, 2016

LGTM with a bunch of small things. Thanks!

cmd/juju/application/addrelation.go
@@ -34,28 +44,26 @@ func (c *addRelationCommand) Info() *cmd.Info {
}
func (c *addRelationCommand) Init(args []string) error {
+ // Must have only 2 arguments
@axw

axw Sep 28, 2016

Member

this is evident from the line directly below, can we please do without this?

cmd/juju/application/addrelation.go
if len(args) != 2 {
return errors.Errorf("a relation must involve two applications")
}
+ // None of the arguments can be empty.
+ if args[0] == "" || args[1] == "" {
+ return errors.Errorf("a relation must involve two applications")
@axw

axw Sep 28, 2016

Member

This error message isn't great. It's not that a value wasn't specified, but it was empty. You really have to be trying to do that. I feel it would be more helpful to validate according to the names package, i.e. check that each arg matches

fmt.Sprintf("%s(?::%s)?", names.ApplicationSnippet, names.RelationSnippet)
@anastasiamac

anastasiamac Sep 28, 2016

Member

Considering that this PR is about improving tests, I'll remove the validation that I have added for command arguments. All validation of application/endpoints happens at state as there are several permutations to consider and I do not want to hold up this PR from landing.

I'll adjust tests accordingly - just check that the correct number of arguments passed into the command.

cmd/juju/application/addrelation_test.go
+ addRelationFunc: func(endpoints ...string) (*params.AddRelationResults, error) {
+ // At the moment, cmd implementation ignores the return values,
+ // so nil is an acceptable return for testing purposes.
+ return nil, nil
@axw

axw Sep 28, 2016

Member

if you add a testing.Stub to the suite, and return .NextErr() here, you can get away without redefining s.mockAPI in the error test below, and you can use the .CheckCall method to check the args passed in the success case

- ch = testcharms.Repo.CharmArchivePath(s.CharmsPath, "logging")
- err = runDeploy(c, ch, "lg", "--series", "quantal")
+func (s *AddRelationSuite) TestAddRelationSuccess(c *gc.C) {
+ err := s.runAddRelation(c, "application1", "application2")
c.Assert(err, jc.ErrorIsNil)
@axw

axw Sep 28, 2016

Member

please check what arguments were passed through to the API here

cmd/juju/application/removerelation.go
@@ -61,25 +71,22 @@ func (c *removeRelationCommand) Init(args []string) error {
if len(args) != 2 {
return errors.Errorf("a relation must involve two applications")
}
+ // None of the arguments can be empty.
@axw

axw Sep 28, 2016

Member

same comment applies from the other file

@anastasiamac

anastasiamac Sep 28, 2016

Member

reverted as per add-relation command

+}
+
+func (s *RemoveRelationSuite) TestRemoveRelationSuccess(c *gc.C) {
+ err := s.runRemoveRelation(c, "application1", "application2")
@axw

axw Sep 28, 2016

Member

again, please check args

featuretests/cmd_juju_relation_test.go
+ }
+}
+
+func run(c *gc.C, command, expectedError string, args ...string) {
@axw

axw Sep 28, 2016

Member

I think this is a bit too generic to live in this file. Move to package_test.go, or make a suite method?

@axw

axw Sep 28, 2016

Member

I think the calls of run would be more readable if you had a separate function for the error case.

@anastasiamac

anastasiamac Sep 28, 2016

Member

Good idea \o/ I'll move this to the package file and will return error so that individual tests can check for failures if expected.

featuretests/package_test.go
+ // return an error if we attempt to run two commands in the
+ // same test.
+ loggo.ResetWriters()
+ ctx, err := cmd.DefaultContext()
@axw

axw Sep 28, 2016

Member

can we use coretesting.Context() instead? then you can use coretesting.Stderr(ctx) where you're currently asserting bytes.Buffer

@axw

axw Sep 28, 2016

Member

can the existing runJujuCommand be dropped, and this used instead?

@anastasiamac

anastasiamac Sep 28, 2016

Member

Yes, the plan is that runJujuCommand will be dropped and this one will be used as a package-wide standard. I did not want to change all tests to the new version in this PR to minimise the diff. I'll do so in a follow up though.

Owner

jameinel commented Sep 28, 2016

I'm a bit confused by the github review pane. It still has 2 comments from Andrew, but only 2 out of all the comments made. Maybe because the change you made to address the comments happens below his actual comment, rather than to the line he commented on?

Anyway, LGTM.

Member

anastasiamac commented Sep 28, 2016

$$merge$$

Contributor

jujubot commented Sep 28, 2016

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju

Contributor

jujubot commented Sep 28, 2016

Build failed: Tests failed
build url: http://juju-ci.vapour.ws:8080/job/github-merge-juju/9349

Member

anastasiamac commented Sep 28, 2016

Intermittent :)
s.AgentSuite.TearDownTest(c)
c:/users/admini~1/appdata/local/temp/tmpkuozyb/gogo/src/github.com/juju/juju/provider/dummy/environs.go:326:
c.Assert(err, jc.ErrorIsNil)
... value _errors.Err = &errors.Err{message:"", cause:(_errors.errorString)(0xc082bfc470), previous:(*errors.Err)(0xc082a26410), file:"github.com/juju/retry/retry.go", line:187} ("inst.Dial() failed: no reachable servers")
$$merge$$

Contributor

jujubot commented Sep 28, 2016

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju

@jujubot jujubot merged commit e3e316f into juju:master Sep 28, 2016

@anastasiamac anastasiamac deleted the anastasiamac:cmd-juju-application-jujuconnsuite branch Aug 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment