client/pkg/serving
Roland Huß 71c39e822d feature(testing): Introduce a mock implementation for KnClient (#306)
* feature(testing): Introduce a Mock implementation for KnClient

Commands must only use the `KnClient` API and their unit tests should also
only use a mock implementation for this interface.

This commit introduces such a Mock implementation and works like in
the example below for creating a simple service in a synchronous
way

```
// New mock client
	client := knclient.NewMockKnClient(t)

	// Recording:
	r := client.Recorder()
	// Check for existing service --> no
	r.GetService("foo", nil, errors.NewNotFound(v1alpha1.Resource("service"), "foo"))
	// Create service (don't validate given service --> "Any()" arg is allowed)
	r.CreateService(knclient.Any(), nil)
	// Wait for service to become ready
	r.WaitForService("foo", knclient.Any(), nil)
	// Get for showing the URL
	r.GetService("foo", getServiceWithUrl("foo", "http://foo.example.com"), nil)

	// Testing:
	output, err := executeCommand(client, "create", "foo", "--image", "gcr.io/foo/bar:baz")
	assert.NilError(t, err)
	assert.Assert(t, util.ContainsAll(output, "created", "foo", "http://foo.example.com", "Waiting"))

	// Validate that all recorded API methods have been called
	r.Validate()
```

Such tests have three phases:

* A recording phase where the mock client is prepared. In this phase a
  recorder is called with the expected arguments and the return values
  it can return. The arguments can be also functions with
  signature `func (t *testing.T, actual interface{}, expected interface{})`
  and will be called to verify a given argument. Such a function should
  `t.Fail()` on its own if the validation fails.
  A convenient `Any()` method is added to allow no validation on an argument
  (see example below).
  Method can be called multiple times, but the order needs to reflect
  the actual calling order
* A playback phase where the test executed which in turn calls out to the
  Mocks
* A validation phase to check the expected output. The recorder can be
  also validated to verify that all recorded mock calls has been
  used during the test.

  See `service_create_mock_test.go` for a full example.

* chore: Test the mock client

* chore: Minor fixes

* chore: Cosmetic fixes
2019-07-30 17:51:42 -07:00
..
v1alpha1 feature(testing): Introduce a mock implementation for KnClient (#306) 2019-07-30 17:51:42 -07:00
config_changes.go Validate scale and container concurrency options when updating configuration resource (#279) 2019-07-25 02:57:35 -07:00
config_changes_test.go Validate scale and container concurrency options when updating configuration resource (#279) 2019-07-25 02:57:35 -07:00
revision_template.go Validate scale and container concurrency options when updating configuration resource (#279) 2019-07-25 02:57:35 -07:00
schema_handling.go refactor(serving): KnClient interface for single point of cluster access (#134) 2019-07-08 10:08:34 -07:00
schema_handling_test.go refactor(serving): KnClient interface for single point of cluster access (#134) 2019-07-08 10:08:34 -07:00
service.go Rename accessors to be less like methods that get from the server (#275) 2019-07-18 23:19:28 -07:00