github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/cli/command/formatter/service_test.go (about)

     1  package formatter
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/api/types/swarm"
    10  	"github.com/docker/docker/pkg/testutil/assert"
    11  )
    12  
    13  func TestServiceContextWrite(t *testing.T) {
    14  	cases := []struct {
    15  		context  Context
    16  		expected string
    17  	}{
    18  		// Errors
    19  		{
    20  			Context{Format: "{{InvalidFunction}}"},
    21  			`Template parsing error: template: :1: function "InvalidFunction" not defined
    22  `,
    23  		},
    24  		{
    25  			Context{Format: "{{nil}}"},
    26  			`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
    27  `,
    28  		},
    29  		// Table format
    30  		{
    31  			Context{Format: NewServiceListFormat("table", false)},
    32  			`ID                  NAME                MODE                REPLICAS            IMAGE
    33  id_baz              baz                 global              2/4                 
    34  id_bar              bar                 replicated          2/4                 
    35  `,
    36  		},
    37  		{
    38  			Context{Format: NewServiceListFormat("table", true)},
    39  			`id_baz
    40  id_bar
    41  `,
    42  		},
    43  		{
    44  			Context{Format: NewServiceListFormat("table {{.Name}}", false)},
    45  			`NAME
    46  baz
    47  bar
    48  `,
    49  		},
    50  		{
    51  			Context{Format: NewServiceListFormat("table {{.Name}}", true)},
    52  			`NAME
    53  baz
    54  bar
    55  `,
    56  		},
    57  		// Raw Format
    58  		{
    59  			Context{Format: NewServiceListFormat("raw", false)},
    60  			`id: id_baz
    61  name: baz
    62  mode: global
    63  replicas: 2/4
    64  image: 
    65  
    66  id: id_bar
    67  name: bar
    68  mode: replicated
    69  replicas: 2/4
    70  image: 
    71  
    72  `,
    73  		},
    74  		{
    75  			Context{Format: NewServiceListFormat("raw", true)},
    76  			`id: id_baz
    77  id: id_bar
    78  `,
    79  		},
    80  		// Custom Format
    81  		{
    82  			Context{Format: NewServiceListFormat("{{.Name}}", false)},
    83  			`baz
    84  bar
    85  `,
    86  		},
    87  	}
    88  
    89  	for _, testcase := range cases {
    90  		services := []swarm.Service{
    91  			{ID: "id_baz", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "baz"}}},
    92  			{ID: "id_bar", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "bar"}}},
    93  		}
    94  		info := map[string]ServiceListInfo{
    95  			"id_baz": {
    96  				Mode:     "global",
    97  				Replicas: "2/4",
    98  			},
    99  			"id_bar": {
   100  				Mode:     "replicated",
   101  				Replicas: "2/4",
   102  			},
   103  		}
   104  		out := bytes.NewBufferString("")
   105  		testcase.context.Output = out
   106  		err := ServiceListWrite(testcase.context, services, info)
   107  		if err != nil {
   108  			assert.Error(t, err, testcase.expected)
   109  		} else {
   110  			assert.Equal(t, out.String(), testcase.expected)
   111  		}
   112  	}
   113  }
   114  
   115  func TestServiceContextWriteJSON(t *testing.T) {
   116  	services := []swarm.Service{
   117  		{ID: "id_baz", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "baz"}}},
   118  		{ID: "id_bar", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "bar"}}},
   119  	}
   120  	info := map[string]ServiceListInfo{
   121  		"id_baz": {
   122  			Mode:     "global",
   123  			Replicas: "2/4",
   124  		},
   125  		"id_bar": {
   126  			Mode:     "replicated",
   127  			Replicas: "2/4",
   128  		},
   129  	}
   130  	expectedJSONs := []map[string]interface{}{
   131  		{"ID": "id_baz", "Name": "baz", "Mode": "global", "Replicas": "2/4", "Image": ""},
   132  		{"ID": "id_bar", "Name": "bar", "Mode": "replicated", "Replicas": "2/4", "Image": ""},
   133  	}
   134  
   135  	out := bytes.NewBufferString("")
   136  	err := ServiceListWrite(Context{Format: "{{json .}}", Output: out}, services, info)
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   141  		t.Logf("Output: line %d: %s", i, line)
   142  		var m map[string]interface{}
   143  		if err := json.Unmarshal([]byte(line), &m); err != nil {
   144  			t.Fatal(err)
   145  		}
   146  		assert.DeepEqual(t, m, expectedJSONs[i])
   147  	}
   148  }
   149  func TestServiceContextWriteJSONField(t *testing.T) {
   150  	services := []swarm.Service{
   151  		{ID: "id_baz", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "baz"}}},
   152  		{ID: "id_bar", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "bar"}}},
   153  	}
   154  	info := map[string]ServiceListInfo{
   155  		"id_baz": {
   156  			Mode:     "global",
   157  			Replicas: "2/4",
   158  		},
   159  		"id_bar": {
   160  			Mode:     "replicated",
   161  			Replicas: "2/4",
   162  		},
   163  	}
   164  	out := bytes.NewBufferString("")
   165  	err := ServiceListWrite(Context{Format: "{{json .Name}}", Output: out}, services, info)
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   170  		t.Logf("Output: line %d: %s", i, line)
   171  		var s string
   172  		if err := json.Unmarshal([]byte(line), &s); err != nil {
   173  			t.Fatal(err)
   174  		}
   175  		assert.Equal(t, s, services[i].Spec.Name)
   176  	}
   177  }