Skip to content

Commit

Permalink
change error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmurugappan committed Jun 24, 2021
1 parent 53f333c commit 28cea22
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 41 deletions.
10 changes: 7 additions & 3 deletions pkg/kn/commands/source/apiserver/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ func TestSinkNotFoundError(t *testing.T) {
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default")
apiServerClient := v1.NewMockKnAPIServerSourceClient(t)
errorMsg := "cannot create ApiServerSource 'testsource' in namespace 'default' because: services.serving.knative.dev \"testsvc\" not found"
argMissingMsg := "requires the name of the source to create as single argument"
_, err := executeAPIServerSourceCommand(apiServerClient, dynamicClient, "create", "--resource", "Event:v1:key1=value1", "--service-account", "testsa", "--sink", "ksvc:testsvc", "--mode", "Reference")
assert.Error(t, err, argMissingMsg)
out, err := executeAPIServerSourceCommand(apiServerClient, dynamicClient, "create", "testsource", "--resource", "Event:v1:key1=value1", "--service-account", "testsa", "--sink", "ksvc:testsvc", "--mode", "Reference")
assert.Error(t, err, errorMsg)
assert.Assert(t, util.ContainsAll(out, errorMsg, "Usage"))
Expand All @@ -61,3 +58,10 @@ func TestNoSinkError(t *testing.T) {
_, err := executeAPIServerSourceCommand(apiServerClient, nil, "create", "testsource", "--resource", "Event:v1", "--service-account", "testsa", "--mode", "Reference")
assert.ErrorContains(t, err, "required flag(s)", "sink", "not set")
}

func TestApiServerCreateArgsMissing(t *testing.T) {
apiServerClient := v1.NewMockKnAPIServerSourceClient(t)
out, err := executeAPIServerSourceCommand(apiServerClient, nil, "create", "--sink", "ksvc:testsvc", "--resource", "Event:v1")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}
7 changes: 3 additions & 4 deletions pkg/kn/commands/source/apiserver/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func TestDeleteWithError(t *testing.T) {

func TestDeleteWithNoArgError(t *testing.T) {
apiServerClient := v1.NewMockKnAPIServerSourceClient(t, "mynamespace")
argMissingMsg := "requires the name of the source as single argument"

_, err := executeAPIServerSourceCommand(apiServerClient, nil, "delete")
assert.Error(t, err, argMissingMsg)
out, err := executeAPIServerSourceCommand(apiServerClient, nil, "delete")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}
6 changes: 3 additions & 3 deletions pkg/kn/commands/source/apiserver/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func TestDescribeWithSinkURI(t *testing.T) {

func TestDescribeNoArgError(t *testing.T) {
apiServerClient := v1.NewMockKnAPIServerSourceClient(t, "mynamespace")
argMissingMsg := "'kn source apiserver describe' requires name of the source as single argument"

_, err := executeAPIServerSourceCommand(apiServerClient, nil, "describe")
assert.Error(t, err, argMissingMsg)
out, err := executeAPIServerSourceCommand(apiServerClient, nil, "describe")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}
11 changes: 6 additions & 5 deletions pkg/kn/commands/source/apiserver/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func TestApiServerSourceUpdateDeletionTimestampNotNil(t *testing.T) {

func TestApiServerUpdateErrorForNoArgs(t *testing.T) {
apiServerClient := v1.NewMockKnAPIServerSourceClient(t)
argMissingMsg := "requires the name of the source as single argument"
_, err := executeAPIServerSourceCommand(apiServerClient, nil, "update")
assert.Error(t, err, argMissingMsg)
out, err := executeAPIServerSourceCommand(apiServerClient, nil, "update")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}

func TestApiServerUpdateSinkMissingError(t *testing.T) {
Expand All @@ -87,6 +87,7 @@ func TestApiServerUpdateSinkMissingError(t *testing.T) {
sinkMissingMsg := "services.serving.knative.dev \"svc3\" not found"
apiServerRecorder.UpdateAPIServerSource("", errors.New(sinkMissingMsg))

_, err := executeAPIServerSourceCommand(apiServerClient, dynamicClient, "update", "testsource", "--service-account", "testsa2", "--sink", "ksvc:svc3", "--ce-override", "bla-", "--ce-override", "foo=baz")
assert.Error(t, err, sinkMissingMsg)
out, err := executeAPIServerSourceCommand(apiServerClient, dynamicClient, "update", "testsource", "--service-account", "testsa2", "--sink", "ksvc:svc3", "--ce-override", "bla-", "--ce-override", "foo=baz")
assert.ErrorContains(t, err, "not found")
assert.Assert(t, util.ContainsAll(out, "services.serving.knative.dev", "svc3", "not found"))
}
5 changes: 3 additions & 2 deletions pkg/kn/commands/source/container/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestContainerCreatePSError(t *testing.T) {
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", testsvc)
containerClient := v1.NewMockKnContainerSourceClient(t)

_, err := executeContainerSourceCommand(containerClient, dynamicClient, "create", "testsource", "--sink", "ksvc:testsvc", "--image", "docker.io/test/testimg", "--mount", "123456")
assert.Error(t, err, "cannot create ContainerSource 'testsource' in namespace 'default' because: Invalid --mount: argument requires a value that contains the \"=\" character; got \"123456\"")
out, err := executeContainerSourceCommand(containerClient, dynamicClient, "create", "testsource", "--sink", "ksvc:testsvc", "--image", "docker.io/test/testimg", "--mount", "123456")
assert.ErrorContains(t, err, "cannot create ContainerSource")
assert.Assert(t, util.ContainsAll(out, "cannot create ContainerSource", "Invalid --mount"))
}
6 changes: 3 additions & 3 deletions pkg/kn/commands/source/container/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestDeleteWithError(t *testing.T) {

func TestContainerDeleteErrorForNoArgs(t *testing.T) {
containerClient := v1.NewMockKnContainerSourceClient(t, "mynamespace")
argMissingMsg := "requires the name of the source as single argument"
_, err := executeContainerSourceCommand(containerClient, nil, "delete")
assert.Error(t, err, argMissingMsg)
out, err := executeContainerSourceCommand(containerClient, nil, "delete")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}
6 changes: 3 additions & 3 deletions pkg/kn/commands/source/container/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestSimpleDescribe(t *testing.T) {

func TestContainerDescribeErrorForNoArgs(t *testing.T) {
containerClient := v1.NewMockKnContainerSourceClient(t, "mynamespace")
argMissingMsg := "'kn source container describe' requires name of the source as single argument"
_, err := executeContainerSourceCommand(containerClient, nil, "describe")
assert.Error(t, err, argMissingMsg)
out, err := executeContainerSourceCommand(containerClient, nil, "describe")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}
16 changes: 9 additions & 7 deletions pkg/kn/commands/source/container/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func TestContainerSourceUpdateSinkError(t *testing.T) {

func TestContainerUpdateErrorForNoArgs(t *testing.T) {
containerClient := v1.NewMockKnContainerSourceClient(t, "mynamespace")
argMissingMsg := "requires the name of the source as single argument"
_, err := executeContainerSourceCommand(containerClient, nil, "update")
assert.Error(t, err, argMissingMsg)
out, err := executeContainerSourceCommand(containerClient, nil, "update")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}

func TestContainerUpdateDeletionTimestampNotNil(t *testing.T) {
Expand All @@ -76,8 +76,9 @@ func TestContainerUpdateDeletionTimestampNotNil(t *testing.T) {
containerRecorder := containerClient.Recorder()
containerRecorder.GetContainerSource("testsource", present, nil)

_, err := executeContainerSourceCommand(containerClient, nil, "update", "testsource")
assert.Error(t, err, "can't update container source testsource because it has been marked for deletion")
out, err := executeContainerSourceCommand(containerClient, nil, "update", "testsource")
assert.ErrorContains(t, err, "can't update container source")
assert.Assert(t, util.ContainsAll(out, "can't update container source", "marked for deletion"))
}

func TestContainerUpdatePSError(t *testing.T) {
Expand All @@ -87,6 +88,7 @@ func TestContainerUpdatePSError(t *testing.T) {
present := createContainerSource("testsource", "docker.io/test/testimg", createSinkv1("svc1", "default"), nil, nil, nil)
containerRecorder.GetContainerSource("testsource", present, nil)

_, err := executeContainerSourceCommand(containerClient, nil, "update", "testsource", "--mount", "123456")
assert.Error(t, err, "cannot update ContainerSource 'testsource' in namespace 'default' because: Invalid --mount: argument requires a value that contains the \"=\" character; got \"123456\"")
out, err := executeContainerSourceCommand(containerClient, nil, "update", "testsource", "--mount", "123456")
assert.ErrorContains(t, err, "cannot update ContainerSource")
assert.Assert(t, util.ContainsAll(out, "cannot update ContainerSource", "Invalid --mount"))
}
6 changes: 3 additions & 3 deletions pkg/kn/commands/source/ping/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestDeleteWithError(t *testing.T) {

func TestPingDeleteErrorForNoArgs(t *testing.T) {
pingClient := clientsourcesv1beta2.NewMockKnPingSourceClient(t, "mynamespace")
argMissingMsg := "'requires the name of the Ping source to delete as single argument"
_, err := executePingSourceCommand(pingClient, nil, "delete")
assert.Error(t, err, argMissingMsg)
out, err := executePingSourceCommand(pingClient, nil, "delete")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}
6 changes: 3 additions & 3 deletions pkg/kn/commands/source/ping/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func TestDescribeError(t *testing.T) {

func TestPingDescribeErrorForNoArgs(t *testing.T) {
pingClient := clientv1beta2.NewMockKnPingSourceClient(t, "mynamespace")
argMissingMsg := "'kn source ping describe' requires name of the source as single argument"
_, err := executePingSourceCommand(pingClient, nil, "describe")
assert.Error(t, err, argMissingMsg)
out, err := executePingSourceCommand(pingClient, nil, "describe")
assert.ErrorContains(t, err, "single argument")
assert.Assert(t, util.ContainsAll(out, "requires", "single argument"))
}

func getPingSourceSinkURI() *sourcesv1beta.PingSource {
Expand Down
11 changes: 6 additions & 5 deletions pkg/kn/commands/source/ping/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func TestPingUpdateDeletionTimestampNotNil(t *testing.T) {

func TestPingUpdateErrorForNoArgs(t *testing.T) {
pingClient := sourcesv1beta2.NewMockKnPingSourceClient(t, "mynamespace")
argMissingMsg := "name of Ping source required"
_, err := executePingSourceCommand(pingClient, nil, "update")
assert.Error(t, err, argMissingMsg)
out, err := executePingSourceCommand(pingClient, nil, "update")
assert.ErrorContains(t, err, "required")
assert.Assert(t, util.ContainsAll(out, "Ping", "name", "required"))
}

func TestPingUpdateNoSinkError(t *testing.T) {
Expand All @@ -102,6 +102,7 @@ func TestPingUpdateNoSinkError(t *testing.T) {

pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "mysvc", nil), nil)

_, err := executePingSourceCommand(pingClient, dynamicClient, "update", "testsource", "--sink", "ksvc1")
assert.Error(t, err, "services.serving.knative.dev \"ksvc1\" not found")
out, err := executePingSourceCommand(pingClient, dynamicClient, "update", "testsource", "--sink", "ksvc1")
assert.ErrorContains(t, err, "not found")
assert.Assert(t, util.ContainsAll(out, "services.serving.knative.dev", "not found", "ksvc1"))
}

0 comments on commit 28cea22

Please sign in to comment.