|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/internal/commands" |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/internal/config" |
| 9 | + smock "github.com/UpCloudLtd/upcloud-cli/internal/mock" |
| 10 | + "github.com/UpCloudLtd/upcloud-cli/internal/output" |
| 11 | + internal "github.com/UpCloudLtd/upcloud-cli/internal/service" |
| 12 | + |
| 13 | + "github.com/UpCloudLtd/upcloud-go-api/v4/upcloud" |
| 14 | + "github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request" |
| 15 | + "github.com/gemalto/flume" |
| 16 | + "github.com/jedib0t/go-pretty/v6/text" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func TestListServers(t *testing.T) { |
| 21 | + text.DisableColors() |
| 22 | + |
| 23 | + uuid := "server-list-test-server-uuid" |
| 24 | + servers := upcloud.Servers{ |
| 25 | + Servers: []upcloud.Server{ |
| 26 | + { |
| 27 | + Hostname: "server-list-test-server", |
| 28 | + UUID: uuid, |
| 29 | + Plan: "1xCPU-1GB", |
| 30 | + Zone: "pl-waw1", |
| 31 | + State: "started", |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | + serverNetworks := upcloud.Networking{ |
| 36 | + Interfaces: upcloud.ServerInterfaceSlice{ |
| 37 | + { |
| 38 | + IPAddresses: upcloud.IPAddressSlice{ |
| 39 | + { |
| 40 | + Access: "utility", |
| 41 | + Address: "10.0.100.1", |
| 42 | + Floating: upcloud.False, |
| 43 | + }, |
| 44 | + }, |
| 45 | + Type: "utility", |
| 46 | + }, |
| 47 | + { |
| 48 | + IPAddresses: upcloud.IPAddressSlice{ |
| 49 | + { |
| 50 | + Access: "private", |
| 51 | + Address: "10.0.99.2", |
| 52 | + Floating: upcloud.False, |
| 53 | + }, |
| 54 | + }, |
| 55 | + Type: "private", |
| 56 | + }, |
| 57 | + { |
| 58 | + IPAddresses: upcloud.IPAddressSlice{ |
| 59 | + { |
| 60 | + Access: "public", |
| 61 | + Address: "10.0.98.3", |
| 62 | + Floating: upcloud.False, |
| 63 | + }, |
| 64 | + { |
| 65 | + Access: "public", |
| 66 | + Address: "10.0.97.4", |
| 67 | + Floating: upcloud.True, |
| 68 | + }, |
| 69 | + }, |
| 70 | + Type: "public", |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + ipaddressesTitle := "IP addresses" |
| 76 | + |
| 77 | + for _, test := range []struct { |
| 78 | + name string |
| 79 | + args []string |
| 80 | + outputContains []string |
| 81 | + outputNotContains []string |
| 82 | + }{ |
| 83 | + { |
| 84 | + name: "No args", |
| 85 | + args: []string{}, |
| 86 | + outputNotContains: []string{ |
| 87 | + ipaddressesTitle, |
| 88 | + "10.0.98.3", |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "Show all IP Addresses", |
| 93 | + args: []string{"--show-ip-addresses"}, |
| 94 | + outputContains: []string{ |
| 95 | + ipaddressesTitle, |
| 96 | + "public: 10.0.97.4 (f)", |
| 97 | + "public: 10.0.98.3", |
| 98 | + "private: 10.0.99.2", |
| 99 | + "utility: 10.0.100.1", |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "Show public Addresses", |
| 104 | + args: []string{"--show-ip-addresses=public"}, |
| 105 | + outputContains: []string{ |
| 106 | + ipaddressesTitle, |
| 107 | + "public: 10.0.97.4 (f)", |
| 108 | + "public: 10.0.98.3", |
| 109 | + }, |
| 110 | + outputNotContains: []string{ |
| 111 | + "private: 10.0.99.2", |
| 112 | + "utility: 10.0.100.1", |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "Show private IP Addresses", |
| 117 | + args: []string{"--show-ip-addresses=private"}, |
| 118 | + outputContains: []string{ |
| 119 | + ipaddressesTitle, |
| 120 | + "private: 10.0.99.2", |
| 121 | + }, |
| 122 | + outputNotContains: []string{ |
| 123 | + "public: 10.0.97.4 (f)", |
| 124 | + "public: 10.0.98.3", |
| 125 | + "utility: 10.0.100.1", |
| 126 | + }, |
| 127 | + }, |
| 128 | + } { |
| 129 | + t.Run(test.name, func(t *testing.T) { |
| 130 | + conf := config.New() |
| 131 | + conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman) |
| 132 | + |
| 133 | + testCmd := ListCommand() |
| 134 | + mService := new(smock.Service) |
| 135 | + conf.Service = internal.Wrapper{Service: mService} |
| 136 | + |
| 137 | + mService.On("GetServers").Return(&servers, nil) |
| 138 | + mService.On("GetServerNetworks", &request.GetServerNetworksRequest{ServerUUID: uuid}).Return(&serverNetworks, nil) |
| 139 | + |
| 140 | + c := commands.BuildCommand(testCmd, nil, conf) |
| 141 | + err := c.Cobra().Flags().Parse(test.args) |
| 142 | + assert.NoError(t, err) |
| 143 | + |
| 144 | + res, err := c.(commands.NoArgumentCommand).ExecuteWithoutArguments(commands.NewExecutor(conf, mService, flume.New("test"))) |
| 145 | + assert.NoError(t, err) |
| 146 | + |
| 147 | + buf := bytes.NewBuffer(nil) |
| 148 | + err = output.Render(buf, conf, res) |
| 149 | + assert.NoError(t, err) |
| 150 | + str := buf.String() |
| 151 | + |
| 152 | + for _, contains := range test.outputContains { |
| 153 | + assert.Contains(t, str, contains) |
| 154 | + } |
| 155 | + |
| 156 | + for _, notContains := range test.outputNotContains { |
| 157 | + assert.NotContains(t, str, notContains) |
| 158 | + } |
| 159 | + }) |
| 160 | + } |
| 161 | +} |
0 commit comments