1 package entrypoint
2
3 import (
4 "bytes"
5 "io/ioutil"
6 "net/http"
7 "os"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11
12 "github.com/datawire/dlib/dlog"
13 )
14
15 var sanitizeExternalSnapshotTests = []struct {
16 testName string
17 rawJSON string
18 hasEdgeStackSidecar bool
19 edgeStackSidecarResponse int
20 expectedSanitizedJSON string
21 }{
22 {
23 testName: "no AmbassadorMeta",
24 rawJSON: `{"AmbassadorMeta":null,"Kubernetes":null,"Consul":null,"Deltas":null,"Invalid":null}`,
25 hasEdgeStackSidecar: true,
26 edgeStackSidecarResponse: 200,
27 expectedSanitizedJSON: `{"AmbassadorMeta":null,"Kubernetes":null,"Consul":null,"Deltas":null,"Invalid":null}`,
28 },
29 {
30 testName: "AmbassadorMeta with sidecar",
31 rawJSON: `{"AmbassadorMeta":{"cluster_id":"","ambassador_id":"","ambassador_version":"","kube_version":""}}`,
32 hasEdgeStackSidecar: true,
33 edgeStackSidecarResponse: 200,
34 expectedSanitizedJSON: `{"AmbassadorMeta":{"cluster_id":"","ambassador_id":"","ambassador_version":"","kube_version":"","sidecar":{"contains":"raw sidecar process-info content"}},"Kubernetes":null,"Consul":null,"Deltas":null,"Invalid":null}`,
35 },
36 {
37 testName: "AmbassadorMeta with bad sidecar response",
38 rawJSON: `{"AmbassadorMeta":{"cluster_id":"","ambassador_id":"","ambassador_version":"","kube_version":""}}`,
39 hasEdgeStackSidecar: true,
40 edgeStackSidecarResponse: 500,
41 expectedSanitizedJSON: `{"AmbassadorMeta":{"cluster_id":"","ambassador_id":"","ambassador_version":"","kube_version":"","sidecar":null},"Kubernetes":null,"Consul":null,"Deltas":null,"Invalid":null}`,
42 },
43 {
44 testName: "AmbassadorMeta without any sidecar",
45 rawJSON: `{"AmbassadorMeta":{"cluster_id":"","ambassador_id":"","ambassador_version":"","kube_version":""}}`,
46 hasEdgeStackSidecar: false,
47 expectedSanitizedJSON: `{"AmbassadorMeta":{"cluster_id":"","ambassador_id":"","ambassador_version":"","kube_version":"","sidecar":null},"Kubernetes":null,"Consul":null,"Deltas":null,"Invalid":null}`,
48 },
49 }
50
51 func TestSanitizeExternalSnapshot(t *testing.T) {
52 const isEdgeStackEnvironmentVariable = "EDGE_STACK"
53 const rawSidecarProcessInfoResponse = `{"contains":"raw sidecar process-info content"}`
54
55 for _, sanitizeExternalSnapshotTest := range sanitizeExternalSnapshotTests {
56 t.Run(sanitizeExternalSnapshotTest.testName, func(innerT *testing.T) {
57 defer os.Unsetenv(isEdgeStackEnvironmentVariable)
58 if sanitizeExternalSnapshotTest.hasEdgeStackSidecar {
59 os.Setenv(isEdgeStackEnvironmentVariable, "true")
60 }
61
62 client := newHTTPTestClient(func(req *http.Request) *http.Response {
63 assert.Equal(t, "http://localhost:8500/process-info/", req.URL.String())
64 return &http.Response{
65 StatusCode: sanitizeExternalSnapshotTest.edgeStackSidecarResponse,
66 Body: ioutil.NopCloser(bytes.NewBufferString(rawSidecarProcessInfoResponse)),
67 }
68 })
69
70 ctx := dlog.NewTestContext(t, false)
71 snapshot, err := sanitizeExternalSnapshot(ctx, []byte(sanitizeExternalSnapshotTest.rawJSON), client)
72
73 assert.Nil(innerT, err)
74 assert.Equal(innerT, sanitizeExternalSnapshotTest.expectedSanitizedJSON, string(snapshot))
75 })
76 }
77 }
78
79 type roundTripFunc func(req *http.Request) *http.Response
80
81 func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
82 return f(req), nil
83 }
84
85 func newHTTPTestClient(mockHandler roundTripFunc) *http.Client {
86 return &http.Client{
87 Transport: mockHandler,
88 }
89 }
90
View as plain text