github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/command/hook_ui_test.go (about) 1 package command 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/mitchellh/cli" 11 "github.com/mitchellh/colorstring" 12 ) 13 14 func TestUiHookPreApply_periodicTimer(t *testing.T) { 15 ui := &cli.MockUi{ 16 InputReader: bytes.NewReader([]byte{}), 17 ErrorWriter: bytes.NewBuffer([]byte{}), 18 OutputWriter: bytes.NewBuffer([]byte{}), 19 } 20 h := &UiHook{ 21 Colorize: &colorstring.Colorize{ 22 Colors: colorstring.DefaultColors, 23 Disable: true, 24 Reset: true, 25 }, 26 Ui: ui, 27 PeriodicUiTimer: 1 * time.Second, 28 } 29 h.init() 30 h.resources = map[string]uiResourceState{ 31 "data.aws_availability_zones.available": uiResourceState{ 32 Op: uiResourceDestroy, 33 Start: time.Now(), 34 }, 35 } 36 37 n := &terraform.InstanceInfo{ 38 Id: "data.aws_availability_zones.available", 39 ModulePath: []string{"root"}, 40 Type: "aws_availability_zones", 41 } 42 43 s := &terraform.InstanceState{ 44 ID: "2017-03-05 10:56:59.298784526 +0000 UTC", 45 Attributes: map[string]string{ 46 "id": "2017-03-05 10:56:59.298784526 +0000 UTC", 47 "names.#": "4", 48 "names.0": "us-east-1a", 49 "names.1": "us-east-1b", 50 "names.2": "us-east-1c", 51 "names.3": "us-east-1d", 52 }, 53 } 54 d := &terraform.InstanceDiff{ 55 Destroy: true, 56 } 57 58 action, err := h.PreApply(n, s, d) 59 if err != nil { 60 t.Fatal(err) 61 } 62 if action != terraform.HookActionContinue { 63 t.Fatalf("Expected hook to continue, given: %#v", action) 64 } 65 66 time.Sleep(3100 * time.Millisecond) 67 68 expectedOutput := `data.aws_availability_zones.available: Destroying... (ID: 2017-03-0...0000 UTC) 69 data.aws_availability_zones.available: Still destroying... (ID: 2017-03-0...0000 UTC, 1s elapsed) 70 data.aws_availability_zones.available: Still destroying... (ID: 2017-03-0...0000 UTC, 2s elapsed) 71 data.aws_availability_zones.available: Still destroying... (ID: 2017-03-0...0000 UTC, 3s elapsed) 72 ` 73 output := ui.OutputWriter.String() 74 if output != expectedOutput { 75 t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output) 76 } 77 78 expectedErrOutput := "" 79 errOutput := ui.ErrorWriter.String() 80 if errOutput != expectedErrOutput { 81 t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput) 82 } 83 } 84 85 func TestUiHookPreApply_destroy(t *testing.T) { 86 ui := &cli.MockUi{ 87 InputReader: bytes.NewReader([]byte{}), 88 ErrorWriter: bytes.NewBuffer([]byte{}), 89 OutputWriter: bytes.NewBuffer([]byte{}), 90 } 91 h := &UiHook{ 92 Colorize: &colorstring.Colorize{ 93 Colors: colorstring.DefaultColors, 94 Disable: true, 95 Reset: true, 96 }, 97 Ui: ui, 98 } 99 h.init() 100 h.resources = map[string]uiResourceState{ 101 "data.aws_availability_zones.available": uiResourceState{ 102 Op: uiResourceDestroy, 103 Start: time.Now(), 104 }, 105 } 106 107 n := &terraform.InstanceInfo{ 108 Id: "data.aws_availability_zones.available", 109 ModulePath: []string{"root"}, 110 Type: "aws_availability_zones", 111 } 112 113 s := &terraform.InstanceState{ 114 ID: "2017-03-05 10:56:59.298784526 +0000 UTC", 115 Attributes: map[string]string{ 116 "id": "2017-03-05 10:56:59.298784526 +0000 UTC", 117 "names.#": "4", 118 "names.0": "us-east-1a", 119 "names.1": "us-east-1b", 120 "names.2": "us-east-1c", 121 "names.3": "us-east-1d", 122 }, 123 } 124 d := &terraform.InstanceDiff{ 125 Destroy: true, 126 } 127 128 action, err := h.PreApply(n, s, d) 129 if err != nil { 130 t.Fatal(err) 131 } 132 if action != terraform.HookActionContinue { 133 t.Fatalf("Expected hook to continue, given: %#v", action) 134 } 135 136 expectedOutput := "data.aws_availability_zones.available: Destroying... (ID: 2017-03-0...0000 UTC)\n" 137 output := ui.OutputWriter.String() 138 if output != expectedOutput { 139 t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output) 140 } 141 142 expectedErrOutput := "" 143 errOutput := ui.ErrorWriter.String() 144 if errOutput != expectedErrOutput { 145 t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput) 146 } 147 } 148 149 func TestUiHookPostApply_emptyState(t *testing.T) { 150 ui := &cli.MockUi{ 151 InputReader: bytes.NewReader([]byte{}), 152 ErrorWriter: bytes.NewBuffer([]byte{}), 153 OutputWriter: bytes.NewBuffer([]byte{}), 154 } 155 h := &UiHook{ 156 Colorize: &colorstring.Colorize{ 157 Colors: colorstring.DefaultColors, 158 Disable: true, 159 Reset: true, 160 }, 161 Ui: ui, 162 } 163 h.init() 164 h.resources = map[string]uiResourceState{ 165 "data.google_compute_zones.available": uiResourceState{ 166 Op: uiResourceDestroy, 167 Start: time.Now(), 168 }, 169 } 170 171 n := &terraform.InstanceInfo{ 172 Id: "data.google_compute_zones.available", 173 ModulePath: []string{"root"}, 174 Type: "google_compute_zones", 175 } 176 action, err := h.PostApply(n, nil, nil) 177 if err != nil { 178 t.Fatal(err) 179 } 180 if action != terraform.HookActionContinue { 181 t.Fatalf("Expected hook to continue, given: %#v", action) 182 } 183 184 expectedOutput := "data.google_compute_zones.available: Destruction complete\n" 185 output := ui.OutputWriter.String() 186 if output != expectedOutput { 187 t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output) 188 } 189 190 expectedErrOutput := "" 191 errOutput := ui.ErrorWriter.String() 192 if errOutput != expectedErrOutput { 193 t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput) 194 } 195 } 196 197 func TestTruncateId(t *testing.T) { 198 testCases := []struct { 199 Input string 200 Expected string 201 MaxLen int 202 }{ 203 { 204 Input: "Hello world", 205 Expected: "H...d", 206 MaxLen: 3, 207 }, 208 { 209 Input: "Hello world", 210 Expected: "H...d", 211 MaxLen: 5, 212 }, 213 { 214 Input: "Hello world", 215 Expected: "He...d", 216 MaxLen: 6, 217 }, 218 { 219 Input: "Hello world", 220 Expected: "He...ld", 221 MaxLen: 7, 222 }, 223 { 224 Input: "Hello world", 225 Expected: "Hel...ld", 226 MaxLen: 8, 227 }, 228 { 229 Input: "Hello world", 230 Expected: "Hel...rld", 231 MaxLen: 9, 232 }, 233 { 234 Input: "Hello world", 235 Expected: "Hell...rld", 236 MaxLen: 10, 237 }, 238 { 239 Input: "Hello world", 240 Expected: "Hello world", 241 MaxLen: 11, 242 }, 243 { 244 Input: "Hello world", 245 Expected: "Hello world", 246 MaxLen: 12, 247 }, 248 } 249 for i, tc := range testCases { 250 testName := fmt.Sprintf("%d", i) 251 t.Run(testName, func(t *testing.T) { 252 out := truncateId(tc.Input, tc.MaxLen) 253 if out != tc.Expected { 254 t.Fatalf("Expected %q to be shortened to %d as %q (given: %q)", 255 tc.Input, tc.MaxLen, tc.Expected, out) 256 } 257 }) 258 } 259 }