|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/docker/cli/cli-plugins/hooks" |
| 25 | + "gotest.tools/v3/assert" |
| 26 | +) |
| 27 | + |
| 28 | +func TestHandleHook_NoArgs(t *testing.T) { |
| 29 | + var buf bytes.Buffer |
| 30 | + err := handleHook(nil, &buf) |
| 31 | + assert.NilError(t, err) |
| 32 | + assert.Equal(t, buf.String(), "") |
| 33 | +} |
| 34 | + |
| 35 | +func TestHandleHook_InvalidJSON(t *testing.T) { |
| 36 | + var buf bytes.Buffer |
| 37 | + err := handleHook([]string{"not json"}, &buf) |
| 38 | + assert.NilError(t, err) |
| 39 | + assert.Equal(t, buf.String(), "") |
| 40 | +} |
| 41 | + |
| 42 | +func TestHandleHook_UnknownCommand(t *testing.T) { |
| 43 | + data := marshalHookData(t, hooks.Request{ |
| 44 | + RootCmd: "compose push", |
| 45 | + }) |
| 46 | + var buf bytes.Buffer |
| 47 | + err := handleHook([]string{data}, &buf) |
| 48 | + assert.NilError(t, err) |
| 49 | + assert.Equal(t, buf.String(), "") |
| 50 | +} |
| 51 | + |
| 52 | +func TestHandleHook_LogsCommand(t *testing.T) { |
| 53 | + tests := []struct { |
| 54 | + rootCmd string |
| 55 | + wantHint string |
| 56 | + }{ |
| 57 | + {rootCmd: "compose logs", wantHint: composeLogsHint}, |
| 58 | + {rootCmd: "logs", wantHint: dockerLogsHint}, |
| 59 | + } |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.rootCmd, func(t *testing.T) { |
| 62 | + data := marshalHookData(t, hooks.Request{ |
| 63 | + RootCmd: tt.rootCmd, |
| 64 | + }) |
| 65 | + var buf bytes.Buffer |
| 66 | + err := handleHook([]string{data}, &buf) |
| 67 | + assert.NilError(t, err) |
| 68 | + |
| 69 | + msg := unmarshalResponse(t, buf.Bytes()) |
| 70 | + assert.Equal(t, msg.Type, hooks.NextSteps) |
| 71 | + assert.Equal(t, msg.Template, tt.wantHint) |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestHandleHook_ComposeUpDetached(t *testing.T) { |
| 77 | + tests := []struct { |
| 78 | + name string |
| 79 | + flags map[string]string |
| 80 | + wantHint bool |
| 81 | + }{ |
| 82 | + { |
| 83 | + name: "with --detach flag", |
| 84 | + flags: map[string]string{"detach": ""}, |
| 85 | + wantHint: true, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "with -d flag", |
| 89 | + flags: map[string]string{"d": ""}, |
| 90 | + wantHint: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "without detach flag", |
| 94 | + flags: map[string]string{"build": ""}, |
| 95 | + wantHint: false, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "no flags", |
| 99 | + flags: map[string]string{}, |
| 100 | + wantHint: false, |
| 101 | + }, |
| 102 | + } |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + data := marshalHookData(t, hooks.Request{ |
| 106 | + RootCmd: "compose up", |
| 107 | + Flags: tt.flags, |
| 108 | + }) |
| 109 | + var buf bytes.Buffer |
| 110 | + err := handleHook([]string{data}, &buf) |
| 111 | + assert.NilError(t, err) |
| 112 | + |
| 113 | + if tt.wantHint { |
| 114 | + msg := unmarshalResponse(t, buf.Bytes()) |
| 115 | + assert.Equal(t, msg.Template, composeLogsHint) |
| 116 | + } else { |
| 117 | + assert.Equal(t, buf.String(), "") |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func marshalHookData(t *testing.T, data hooks.Request) string { |
| 124 | + t.Helper() |
| 125 | + b, err := json.Marshal(data) |
| 126 | + assert.NilError(t, err) |
| 127 | + return string(b) |
| 128 | +} |
| 129 | + |
| 130 | +func unmarshalResponse(t *testing.T, data []byte) hooks.Response { |
| 131 | + t.Helper() |
| 132 | + var msg hooks.Response |
| 133 | + err := json.Unmarshal(data, &msg) |
| 134 | + assert.NilError(t, err) |
| 135 | + return msg |
| 136 | +} |
0 commit comments