Skip to content

Commit

Permalink
Add unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimansk committed Oct 14, 2021
1 parent 65471ce commit b3d718d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
4 changes: 1 addition & 3 deletions pkg/kn/commands/domain/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ func describe(w io.Writer, domainMapping *v1alpha1.DomainMapping, printDetails b
commands.WriteMetadata(dw, &domainMapping.ObjectMeta, printDetails)
dw.WriteLine()
dw.WriteAttribute("URL", domainMapping.Status.URL.String())
dw.WriteLine()
ref := dw.WriteAttribute("Reference", "")
ref.WriteAttribute("APIVersion", domainMapping.Spec.Ref.APIVersion)
ref.WriteAttribute("Kind", domainMapping.Spec.Ref.Kind)
ref.WriteAttribute("Name", domainMapping.Spec.Ref.Name)
if domainMapping.Namespace != domainMapping.Spec.Ref.Namespace {
ref.WriteAttribute("Namespace", domainMapping.Spec.Ref.Namespace)
}
if err := ref.Flush(); err != nil {
return err
}
dw.WriteLine()
commands.WriteConditions(dw, domainMapping.Status.Conditions, printDetails)
if err := dw.Flush(); err != nil {
Expand Down
50 changes: 46 additions & 4 deletions pkg/kn/commands/domain/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,27 @@ func TestDomainMappingDescribe(t *testing.T) {
lineCounter++
}
}
assert.Equal(t, lineCounter, 2)
assert.Equal(t, lineCounter, 3)

servingRecorder.Validate()
}

func TestDomainMappingDescribeDiffNamespace(t *testing.T) {
client := v1alpha1.NewMockKnServiceClient(t)

servingRecorder := client.Recorder()
servingRecorder.GetDomainMapping("foo.bar", getDomainMapping("otherNS"), nil)

out, err := executeDomainCommand(client, nil, "describe", "foo.bar")
assert.NilError(t, err)
assert.Assert(t, cmp.Regexp("Name:\\s+foo.bar", out))
assert.Assert(t, cmp.Regexp("Namespace:\\s+default", out))
assert.Assert(t, util.ContainsAll(out, "URL:", "http://foo.bar"))
assert.Assert(t, cmp.Regexp("Reference:", out))
assert.Assert(t, cmp.Regexp("Kind:\\s+Service", out))
assert.Assert(t, cmp.Regexp("Name:\\s+foo", out))
assert.Assert(t, cmp.Regexp("Namespace:\\s+otherNS", out))
assert.Assert(t, util.ContainsAll(out, "Conditions:", "Ready"))

servingRecorder.Validate()
}
Expand All @@ -71,6 +91,24 @@ func TestDomainMappingDescribeError(t *testing.T) {
servingRecorder.Validate()
}

func TestDomainMappingDescribeNameError(t *testing.T) {
client := v1alpha1.NewMockKnServiceClient(t)

servingRecorder := client.Recorder()

_, err := executeDomainCommand(client, nil, "describe")
assert.Assert(t, err != nil)
assert.Assert(t, util.ContainsAll(err.Error(), "name", "single", "argument"))

servingRecorder.Validate()

_, err = executeDomainCommand(client, nil, "describe", "foo", "bar")
assert.Assert(t, err != nil)
assert.Assert(t, util.ContainsAll(err.Error(), "name", "single", "argument"))

servingRecorder.Validate()
}

func TestDomainMappingDescribeURL(t *testing.T) {
client := v1alpha1.NewMockKnServiceClient(t)

Expand All @@ -97,11 +135,15 @@ func TestDomainMappingDescribeYAML(t *testing.T) {
servingRecorder.Validate()
}

func getDomainMapping() *servingv1alpha1.DomainMapping {
dm := createDomainMapping("foo.bar", createServiceRef("foo", "default"), "")
func getDomainMapping(ns ...string) *servingv1alpha1.DomainMapping {
serviceNamespace := "default"
if len(ns) == 1 {
serviceNamespace = ns[0]
}
dm := createDomainMapping("foo.bar", createServiceRef("foo", serviceNamespace), "")
dm.TypeMeta = v1.TypeMeta{
Kind: "DomainMapping",
APIVersion: "serving.knative.dev/v1alpha1",
APIVersion: servingv1alpha1.SchemeGroupVersion.String(),
}
dm.Status = servingv1alpha1.DomainMappingStatus{
Status: duckv1.Status{
Expand Down

0 comments on commit b3d718d

Please sign in to comment.