Skip to content

Commit

Permalink
Show the secrets and configs count in service detail
Browse files Browse the repository at this point in the history
  • Loading branch information
moncho committed Jul 25, 2018
1 parent 51297e6 commit b15be50
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
13 changes: 11 additions & 2 deletions appui/swarm/service_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package swarm

import (
"bytes"
"strconv"
"strings"
"time"

Expand All @@ -27,15 +28,16 @@ func NewServiceInfoWidget(swarmClient docker.SwarmAPI, service *swarm.Service, y
w := &ServiceInfoWidget{}
name, _ := swarmClient.ResolveService(service.ID)
w.serviceName = name
di := drytermui.NewParFromMarkupText(appui.DryTheme, serviceInfo(swarmClient, name, service))
info := serviceInfo(swarmClient, name, service)
di := drytermui.NewParFromMarkupText(appui.DryTheme, info)
di.BorderTop = false
di.BorderBottom = true
di.BorderLeft = false
di.BorderRight = false
di.BorderFg = termui.Attribute(appui.DryTheme.Footer)
di.BorderBg = termui.Attribute(appui.DryTheme.Bg)

di.Height = 5
di.Height = 6
di.Bg = termui.Attribute(appui.DryTheme.Bg)
di.TextBgColor = termui.Attribute(appui.DryTheme.Bg)
di.Display = false
Expand Down Expand Up @@ -81,6 +83,13 @@ func serviceInfo(swarmClient docker.SwarmAPI, name string, service *swarm.Servic
ui.Blue("Networks:"), ui.Yellow(dryFormatter.FormatSwarmNetworks(service.Spec.TaskTemplate.Networks)),
ui.Blue("Ports:"), ui.Yellow(dryFormatter.FormatPorts(service.Spec.EndpointSpec.Ports)),
},
{
ui.Blue("Configs:"), ui.Yellow(
strconv.Itoa(
len(service.Spec.TaskTemplate.ContainerSpec.Configs))),
ui.Blue("Secrets:"), ui.Yellow(strconv.Itoa(
len(service.Spec.TaskTemplate.ContainerSpec.Secrets))),
},
}

table = tablewriter.NewWriter(buffer)
Expand Down
51 changes: 38 additions & 13 deletions appui/swarm/service_info_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package swarm

import (
"flag"
"io/ioutil"
"path/filepath"
"testing"

"github.com/docker/docker/api/types/swarm"
"github.com/moncho/dry/docker"
"github.com/moncho/dry/mocks"
)

var expectedServiceInfo = ` <blue>Service Name:</> <yellow>serviceName</> <blue>Image:</> <yellow>bla</>
<blue>Service Mode:</> <yellow></> <blue>Labels:</> <yellow></> <blue>Created at:</> <yellow>01 Jan 01 00:00 UTC</>
<blue>Replicas:</> <yellow></> <blue>Constraints:</> <yellow>constraint,magic</> <blue>Updated at:</> <yellow>01 Jan 01 00:00 UTC</>
<blue>Networks:</> <yellow></> <blue>Ports:</> <yellow></>
`
var update = flag.Bool("update", false, "update .golden files")

var testService = &swarm.Service{
Spec: swarm.ServiceSpec{
TaskTemplate: swarm.TaskSpec{
Expand Down Expand Up @@ -43,15 +44,39 @@ func TestServiceInfo(t *testing.T) {
}
}

func TestSwarmDockerInfoContent(t *testing.T) {
daemon := &mocks.SwarmDockerDaemon{}
info := serviceInfo(daemon, "serviceName", testService)

if info == "" {
t.Error("Docker info is empty")
func Test_serviceInfo(t *testing.T) {
type args struct {
swarmClient docker.SwarmAPI
name string
service *swarm.Service
}
if info != expectedServiceInfo {
t.Errorf("Service info output does not match. Expected: \n'%q'\n, got: \n'%q'", expectedServiceInfo, info)
tests := []struct {
name string
args args
}{
{
"service_info",
args{
&mocks.SwarmDockerDaemon{},
"serviceName",
testService,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

got := serviceInfo(tt.args.swarmClient, tt.args.name, tt.args.service)

golden := filepath.Join("testdata", tt.name+".golden")
if *update {
ioutil.WriteFile(golden, []byte(got), 0644)
}
expected, _ := ioutil.ReadFile(golden)

if got != string(expected) {
t.Errorf("serviceInfo() = %v, want %v", got, expected)
}
})
}
}
5 changes: 5 additions & 0 deletions appui/swarm/testdata/service_info.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<blue>Service Name:</> <yellow>serviceName</> <blue>Image:</> <yellow>bla</>
<blue>Service Mode:</> <yellow></> <blue>Labels:</> <yellow></> <blue>Created at:</> <yellow>01 Jan 01 00:00 UTC</>
<blue>Replicas:</> <yellow></> <blue>Constraints:</> <yellow>constraint,magic</> <blue>Updated at:</> <yellow>01 Jan 01 00:00 UTC</>
<blue>Networks:</> <yellow></> <blue>Ports:</> <yellow></>
<blue>Configs:</> <yellow>0</> <blue>Secrets:</> <yellow>0</>

0 comments on commit b15be50

Please sign in to comment.