(t *testing.T)
| 82 | } |
| 83 | |
| 84 | func TestAddonDownloadStdout(t *testing.T) { |
| 85 | oldStdout := os.Stdout |
| 86 | r, w, _ := os.Pipe() |
| 87 | os.Stdout = w |
| 88 | defer func() { |
| 89 | os.Stdout = oldStdout |
| 90 | }() |
| 91 | |
| 92 | t.Setenv("CLOUDQUERY_API_KEY", "testkey") |
| 93 | |
| 94 | wantCalls := map[string]int{ |
| 95 | "GET /teams": 1, |
| 96 | "GET /teams/test_team/addons/cloudquery/visualization/test/versions/v1.2.3/assets": 1, |
| 97 | "GET /assets/cloudquery/addon_visualization/test/v1.2.3/cloudquery_visualization_test_v1.2.3.zip": 1, |
| 98 | } |
| 99 | |
| 100 | payload := []byte("payload to stdout") |
| 101 | |
| 102 | s := sha256.New() |
| 103 | s.Write(payload) |
| 104 | payloadChecksum := hex.EncodeToString(s.Sum(nil)) |
| 105 | |
| 106 | gotCalls := map[string]int{} |
| 107 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 108 | if r.Method != http.MethodGet { |
| 109 | t.Fatalf("unexpected method: %s %s", r.Method, r.URL.Path) |
| 110 | } |
| 111 | w.Header().Set("Content-Type", "application/json") |
| 112 | gotCalls[r.Method+" "+r.URL.Path]++ |
| 113 | switch r.URL.Path { |
| 114 | case "/teams/test_team/addons/cloudquery/visualization/test/versions/v1.2.3/assets": |
| 115 | checkAuthHeader(t, r) |
| 116 | w.WriteHeader(http.StatusOK) |
| 117 | b, _ := json.Marshal(map[string]string{ |
| 118 | "checksum": payloadChecksum, |
| 119 | "location": "http://" + r.Host + "/assets/cloudquery/addon_visualization/test/v1.2.3/cloudquery_visualization_test_v1.2.3.zip", |
| 120 | }) |
| 121 | _, err := w.Write(b) |
| 122 | require.NoError(t, err) |
| 123 | case "/assets/cloudquery/addon_visualization/test/v1.2.3/cloudquery_visualization_test_v1.2.3.zip": |
| 124 | w.Header().Set("Content-Type", "application/octet-stream") |
| 125 | w.WriteHeader(http.StatusOK) |
| 126 | _, err := w.Write(payload) |
| 127 | require.NoError(t, err) |
| 128 | case "/teams": |
| 129 | w.WriteHeader(http.StatusOK) |
| 130 | _, err := w.Write([]byte(`{"items":[{"name":"test_team","displayName":"Test Team"}]}`)) |
| 131 | require.NoError(t, err) |
| 132 | } |
| 133 | })) |
| 134 | defer ts.Close() |
| 135 | |
| 136 | cmd := NewCmdRoot() |
| 137 | t.Setenv(envAPIURL, ts.URL) |
| 138 | args := append([]string{"addon", "download", "cloudquery/visualization/test@v1.2.3", "-t", "-"}, testCommandArgs(t)...) |
| 139 | cmd.SetArgs(args) |
| 140 | err := cmd.Execute() |
| 141 | if err != nil { |
nothing calls this directly
no test coverage detected