|
| 1 | +package command_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "net" |
| 8 | + "testing" |
| 9 | + |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + "github.com/warehouse-13/safety" |
| 12 | + "github.com/weaveworks-liquidmetal/flintlock/api/services/microvm/v1alpha1" |
| 13 | + "google.golang.org/grpc" |
| 14 | + |
| 15 | + "github.com/warehouse-13/hammertime/pkg/client" |
| 16 | + "github.com/warehouse-13/hammertime/pkg/command" |
| 17 | + "github.com/warehouse-13/hammertime/pkg/config" |
| 18 | + "github.com/warehouse-13/hammertime/pkg/dialler" |
| 19 | + "github.com/warehouse-13/hammertime/pkg/utils" |
| 20 | +) |
| 21 | + |
| 22 | +func Test_CRUD_noBasicAuth_noTLS(t *testing.T) { |
| 23 | + g := NewWithT(t) |
| 24 | + |
| 25 | + fakeserver := safety.New() |
| 26 | + dialer := fakeserver.StartBuf("") |
| 27 | + |
| 28 | + t.Cleanup(func() { |
| 29 | + fakeserver.Stop() |
| 30 | + }) |
| 31 | + |
| 32 | + cfg := &config.Config{ |
| 33 | + ClientConfig: config.ClientConfig{ |
| 34 | + ClientBuilderFunc: cl(dialer), |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + buf := &bytes.Buffer{} |
| 39 | + w := utils.NewWriter(buf) |
| 40 | + |
| 41 | + g.Expect(command.CreateFn(w, cfg)).To(Succeed()) |
| 42 | + |
| 43 | + out := &v1alpha1.CreateMicroVMResponse{} |
| 44 | + g.Expect(json.Unmarshal(buf.Bytes(), out)).To(Succeed()) |
| 45 | + |
| 46 | + cfg.UUID = *out.Microvm.Spec.Uid |
| 47 | + |
| 48 | + g.Expect(command.GetFn(w, cfg)).To(Succeed()) |
| 49 | + g.Expect(command.ListFn(w, cfg)).To(Succeed()) |
| 50 | + g.Expect(command.DeleteFn(w, cfg)).To(Succeed()) |
| 51 | +} |
| 52 | + |
| 53 | +func Test_CRUD_basicAuth_noTLS(t *testing.T) { |
| 54 | + g := NewWithT(t) |
| 55 | + |
| 56 | + basicAuthToken := "secret" |
| 57 | + |
| 58 | + fakeserver := safety.New() |
| 59 | + dialer := fakeserver.StartBuf(basicAuthToken) |
| 60 | + |
| 61 | + t.Cleanup(func() { |
| 62 | + fakeserver.Stop() |
| 63 | + }) |
| 64 | + |
| 65 | + cfg := &config.Config{ |
| 66 | + ClientConfig: config.ClientConfig{ |
| 67 | + ClientBuilderFunc: cl(dialer), |
| 68 | + }, |
| 69 | + Token: basicAuthToken, |
| 70 | + } |
| 71 | + |
| 72 | + buf := &bytes.Buffer{} |
| 73 | + w := utils.NewWriter(buf) |
| 74 | + |
| 75 | + g.Expect(command.CreateFn(w, cfg)).To(Succeed()) |
| 76 | + |
| 77 | + out := &v1alpha1.CreateMicroVMResponse{} |
| 78 | + g.Expect(json.Unmarshal(buf.Bytes(), out)).To(Succeed()) |
| 79 | + |
| 80 | + cfg.UUID = *out.Microvm.Spec.Uid |
| 81 | + |
| 82 | + g.Expect(command.GetFn(w, cfg)).To(Succeed()) |
| 83 | + g.Expect(command.ListFn(w, cfg)).To(Succeed()) |
| 84 | + g.Expect(command.DeleteFn(w, cfg)).To(Succeed()) |
| 85 | +} |
| 86 | + |
| 87 | +func Test_basicAuth_failsWithNoClientToken(t *testing.T) { |
| 88 | + g := NewWithT(t) |
| 89 | + |
| 90 | + basicAuthToken := "secret" |
| 91 | + |
| 92 | + fakeserver := safety.New() |
| 93 | + dialer := fakeserver.StartBuf(basicAuthToken) |
| 94 | + |
| 95 | + t.Cleanup(func() { |
| 96 | + fakeserver.Stop() |
| 97 | + }) |
| 98 | + |
| 99 | + cfg := &config.Config{ |
| 100 | + ClientConfig: config.ClientConfig{ |
| 101 | + ClientBuilderFunc: cl(dialer), |
| 102 | + }, |
| 103 | + Token: "", |
| 104 | + } |
| 105 | + |
| 106 | + buf := &bytes.Buffer{} |
| 107 | + w := utils.NewWriter(buf) |
| 108 | + |
| 109 | + g.Expect(command.CreateFn(w, cfg)).To(MatchError(ContainSubstring("unauthenticated"))) |
| 110 | +} |
| 111 | + |
| 112 | +func cl(dialer func(context.Context, string) (net.Conn, error)) func(string, string) (client.FlintlockClient, error) { |
| 113 | + return func(string, token string) (client.FlintlockClient, error) { |
| 114 | + opt := []grpc.DialOption{grpc.WithContextDialer(dialer)} |
| 115 | + conn, err := dialler.New("bufnet", token, opt) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + return &client.Client{ |
| 121 | + MicroVMClient: v1alpha1.NewMicroVMClient(conn), |
| 122 | + Conn: conn, |
| 123 | + }, nil |
| 124 | + } |
| 125 | +} |
0 commit comments