Skip to content

Commit

Permalink
test: capture stderr for tests (#607)
Browse files Browse the repository at this point in the history
In the future this will be useful for testing "-o=json" on resource
creation, because the actual JSON and status messages will be printed to
stdout/stderr respectively.
  • Loading branch information
phm07 committed Nov 9, 2023
1 parent d671ab2 commit a77e553
Show file tree
Hide file tree
Showing 85 changed files with 137 additions and 125 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/all/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestListAll(t *testing.T) {
},
}, nil)

out, err := fx.Run(cmd, []string{})
out, _, err := fx.Run(cmd, []string{})

expOut := `SERVERS
---
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestListAllPaidJSON(t *testing.T) {
AllWithOpts(gomock.Any(), hcloud.VolumeListOpts{}).
Return([]*hcloud.Volume{}, nil)

out, err := fx.Run(cmd, []string{"--paid", "-o=json"})
out, _, err := fx.Run(cmd, []string{"--paid", "-o=json"})

expOut := "{\"floating_ips\":[],\"images\":[{\"id\":114690387,\"status\":\"available\",\"type\":\"system\"," +
"\"name\":\"debian-12\",\"description\":\"Debian 12\",\"image_size\":0,\"disk_size\":5,\"created\":" +
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCreateManaged(t *testing.T) {
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), &hcloud.Action{ID: 321})

out, err := fx.Run(cmd, []string{"--name", "test", "--type", "managed", "--domain", "example.com"})
out, _, err := fx.Run(cmd, []string{"--name", "test", "--type", "managed", "--domain", "example.com"})

expOut := "Certificate 123 created\n"

Expand Down Expand Up @@ -72,7 +72,7 @@ func TestCreateUploaded(t *testing.T) {
Type: hcloud.CertificateTypeUploaded,
}, nil, nil)

out, err := fx.Run(cmd, []string{"--name", "test", "--key-file", "testdata/key.pem", "--cert-file", "testdata/cert.pem"})
out, _, err := fx.Run(cmd, []string{"--name", "test", "--key-file", "testdata/key.pem", "--cert-file", "testdata/cert.pem"})

expOut := "Certificate 123 created\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/certificate/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDelete(t *testing.T) {
Delete(gomock.Any(), cert).
Return(nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := "certificate test deleted\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/certificate/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestDescribe(t *testing.T) {
LoadBalancerName(int64(123)).
Return("test")

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := fmt.Sprintf(`ID: 123
Name: test
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/certificate/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestLabelAdd(t *testing.T) {
},
})

out, err := fx.Run(cmd, []string{"123", "key=value"})
out, _, err := fx.Run(cmd, []string{"123", "key=value"})

expOut := "Label key added to certificate 123\n"

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestLabelRemove(t *testing.T) {
Labels: make(map[string]string),
})

out, err := fx.Run(cmd, []string{"123", "key"})
out, _, err := fx.Run(cmd, []string{"123", "key"})

expOut := "Label key removed from certificate 123\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/certificate/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestList(t *testing.T) {
},
}, nil)

out, err := fx.Run(cmd, []string{})
out, _, err := fx.Run(cmd, []string{})

expOut := `ID NAME TYPE DOMAIN NAMES NOT VALID AFTER AGE
123 test managed example.com Wed Aug 20 12:00:00 UTC 2036 20m
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/certificate/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestUpdateName(t *testing.T) {
Name: "new-name",
})

out, err := fx.Run(cmd, []string{"123", "--name", "new-name"})
out, _, err := fx.Run(cmd, []string{"123", "--name", "new-name"})

expOut := "certificate 123 updated\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/datacenter/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestDescribe(t *testing.T) {
Description: "Falkenstein 1 virtual DC 14",
}, nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := `ID: 4
Name: fsn1-dc14
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/datacenter/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestList(t *testing.T) {
},
}, nil)

out, err := fx.Run(cmd, []string{})
out, _, err := fx.Run(cmd, []string{})

expOut := `ID NAME DESCRIPTION LOCATION
4 fsn1-dc14 Falkenstein 1 virtual DC 14 fsn1
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCreate(t *testing.T) {
fx.ActionWaiter.EXPECT().
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 321}})

out, err := fx.Run(cmd, []string{"--name", "test"})
out, _, err := fx.Run(cmd, []string{"--name", "test"})

expOut := "Firewall 123 created\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDelete(t *testing.T) {
Delete(gomock.Any(), firewall).
Return(nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := "firewall test deleted\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestDescribe(t *testing.T) {
ServerName(int64(321)).
Return("myServer")

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := fmt.Sprintf(`ID: 123
Name: test
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/firewall/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestLabelAdd(t *testing.T) {
},
})

out, err := fx.Run(cmd, []string{"123", "key=value"})
out, _, err := fx.Run(cmd, []string{"123", "key=value"})

expOut := "Label key added to firewall 123\n"

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestLabelRemove(t *testing.T) {
Labels: make(map[string]string),
})

out, err := fx.Run(cmd, []string{"123", "key"})
out, _, err := fx.Run(cmd, []string{"123", "key"})

expOut := "Label key removed from firewall 123\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestList(t *testing.T) {
},
}, nil)

out, err := fx.Run(cmd, []string{})
out, _, err := fx.Run(cmd, []string{})

expOut := `ID NAME RULES COUNT APPLIED TO COUNT
123 test 5 Rules 2 Servers | 0 Label Selectors
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestUpdateName(t *testing.T) {
Name: "new-name",
})

out, err := fx.Run(cmd, []string{"123", "--name", "new-name"})
out, _, err := fx.Run(cmd, []string{"123", "--name", "new-name"})

expOut := "Firewall 123 updated\n"

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/floatingip/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestCreate(t *testing.T) {
Action: nil,
}, nil, nil)

out, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1"})
out, _, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1"})

expOut := `Floating IP 123 created
IPv4: 192.168.2.1
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestCreateProtection(t *testing.T) {
Return(&hcloud.Action{ID: 333}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 333}).Return(nil)

out, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1", "--enable-protection", "delete"})
out, _, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1", "--enable-protection", "delete"})

expOut := `Floating IP 123 created
Resource protection enabled for floating IP 123
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDelete(t *testing.T) {
Delete(gomock.Any(), floatingIP).
Return(nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := "Floating IP test deleted\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestDescribe(t *testing.T) {
ServerName(int64(321)).
Return("myServer")

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := fmt.Sprintf(`ID: 123
Type: ipv4
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/floatingip/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestLabelAdd(t *testing.T) {
},
})

out, err := fx.Run(cmd, []string{"123", "key=value"})
out, _, err := fx.Run(cmd, []string{"123", "key=value"})

expOut := "Label key added to Floating IP 123\n"

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestLabelRemove(t *testing.T) {
Labels: make(map[string]string),
})

out, err := fx.Run(cmd, []string{"123", "key"})
out, _, err := fx.Run(cmd, []string{"123", "key"})

expOut := "Label key removed from Floating IP 123\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestList(t *testing.T) {
},
}, nil)

out, err := fx.Run(cmd, []string{})
out, _, err := fx.Run(cmd, []string{})

expOut := `ID TYPE NAME DESCRIPTION IP HOME SERVER DNS AGE
123 ipv4 test - 192.168.2.1 fsn1 - - 10m
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/floatingip/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestUpdateName(t *testing.T) {
Name: "new-name",
})

out, err := fx.Run(cmd, []string{"123", "--name", "new-name"})
out, _, err := fx.Run(cmd, []string{"123", "--name", "new-name"})

expOut := "Floating IP 123 updated\n"

Expand All @@ -55,7 +55,7 @@ func TestUpdateDescription(t *testing.T) {
Description: "new-description",
})

out, err := fx.Run(cmd, []string{"123", "--description", "new-description"})
out, _, err := fx.Run(cmd, []string{"123", "--description", "new-description"})

expOut := "Floating IP 123 updated\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/image/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDelete(t *testing.T) {
Delete(gomock.Any(), image).
Return(nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := "image test deleted\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/image/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestDescribe(t *testing.T) {
GetForArchitecture(gomock.Any(), "test", hcloud.ArchitectureX86).
Return(img, nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := fmt.Sprintf(`ID: 123
Type: system
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/image/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestLabelAdd(t *testing.T) {
},
})

out, err := fx.Run(cmd, []string{"123", "key=value"})
out, _, err := fx.Run(cmd, []string{"123", "key=value"})

expOut := "Label key added to image 123\n"

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestLabelRemove(t *testing.T) {
Labels: make(map[string]string),
})

out, err := fx.Run(cmd, []string{"123", "key"})
out, _, err := fx.Run(cmd, []string{"123", "key"})

expOut := "Label key removed from image 123\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/image/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestList(t *testing.T) {
},
}, nil)

out, err := fx.Run(cmd, []string{})
out, _, err := fx.Run(cmd, []string{})

expOut := `ID TYPE NAME DESCRIPTION ARCHITECTURE IMAGE SIZE DISK SIZE CREATED DEPRECATED
123 system test - x86 20.00 GB 15 GB Wed Aug 20 12:00:00 UTC 2036 -
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/image/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestUpdateDescription(t *testing.T) {
Description: hcloud.Ptr("new-description"),
})

out, err := fx.Run(cmd, []string{"123", "--description", "new-description"})
out, _, err := fx.Run(cmd, []string{"123", "--description", "new-description"})

expOut := "Image 123 updated\n"

Expand All @@ -56,7 +56,7 @@ func TestUpdateType(t *testing.T) {
Type: hcloud.ImageTypeSnapshot,
})

out, err := fx.Run(cmd, []string{"123", "--type", "snapshot"})
out, _, err := fx.Run(cmd, []string{"123", "--type", "snapshot"})

expOut := "Image 123 updated\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/iso/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestDescribe(t *testing.T) {
Architecture: hcloud.Ptr(hcloud.ArchitectureX86),
}, nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := `ID: 123
Name: test
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/iso/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestList(t *testing.T) {
},
}, nil)

out, err := fx.Run(cmd, []string{})
out, _, err := fx.Run(cmd, []string{})

expOut := `ID NAME DESCRIPTION TYPE ARCHITECTURE
123 test Test ISO public x86
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/loadbalancer/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestCreate(t *testing.T) {
},
}, nil, nil)

out, err := fx.Run(cmd, []string{"--name", "myLoadBalancer", "--type", "lb11", "--location", "fsn1"})
out, _, err := fx.Run(cmd, []string{"--name", "myLoadBalancer", "--type", "lb11", "--location", "fsn1"})

expOut := `Load Balancer 123 created
IPv4: 192.168.2.1
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestCreateProtection(t *testing.T) {
Return(&hcloud.Action{ID: 333}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 333}).Return(nil)

out, err := fx.Run(cmd, []string{"--name", "myLoadBalancer", "--type", "lb11", "--location", "fsn1", "--enable-protection", "delete"})
out, _, err := fx.Run(cmd, []string{"--name", "myLoadBalancer", "--type", "lb11", "--location", "fsn1", "--enable-protection", "delete"})

expOut := `Load Balancer 123 created
Resource protection enabled for Load Balancer 123
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDelete(t *testing.T) {
Delete(gomock.Any(), loadBalancer).
Return(nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := "Load Balancer test deleted\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestDescribe(t *testing.T) {
Get(gomock.Any(), "test").
Return(lb, nil, nil)

out, err := fx.Run(cmd, []string{"test"})
out, _, err := fx.Run(cmd, []string{"test"})

expOut := fmt.Sprintf(`ID: 123
Name: test
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/loadbalancer/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestLabelAdd(t *testing.T) {
},
})

out, err := fx.Run(cmd, []string{"123", "key=value"})
out, _, err := fx.Run(cmd, []string{"123", "key=value"})

expOut := "Label key added to Load Balancer 123\n"

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestLabelRemove(t *testing.T) {
Labels: make(map[string]string),
})

out, err := fx.Run(cmd, []string{"123", "key"})
out, _, err := fx.Run(cmd, []string{"123", "key"})

expOut := "Label key removed from Load Balancer 123\n"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestUpdateName(t *testing.T) {
Name: "new-name",
})

out, err := fx.Run(cmd, []string{"123", "--name", "new-name"})
out, _, err := fx.Run(cmd, []string{"123", "--name", "new-name"})

expOut := "Load Balancer 123 updated\n"

Expand Down
Loading

0 comments on commit a77e553

Please sign in to comment.