|
| 1 | +//go:build e2e || template |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2026 The Dapr Authors |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package standalone_test |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "net" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | +) |
| 28 | + |
| 29 | +// TestMain installs Dapr once for the entire test binary, removing the |
| 30 | +// need for every test to call cmdUninstall/ensureDaprInstallation. |
| 31 | +// Tests that need to test the install/uninstall lifecycle itself must |
| 32 | +// reinstall Dapr in their t.Cleanup so subsequent tests still work. |
| 33 | +func TestMain(m *testing.M) { |
| 34 | + // Start from a clean slate. |
| 35 | + cmdUninstall() |
| 36 | + |
| 37 | + if err := installDapr(); err != nil { |
| 38 | + fmt.Fprintf(os.Stderr, "TestMain: failed to install Dapr: %v\n", err) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + |
| 42 | + code := m.Run() |
| 43 | + |
| 44 | + cmdUninstall() |
| 45 | + os.Exit(code) |
| 46 | +} |
| 47 | + |
| 48 | +// installDapr performs a Dapr init for the test binary. This mirrors |
| 49 | +// ensureDaprInstallation but does not require a *testing.T. |
| 50 | +func installDapr() error { |
| 51 | + daprRuntimeVersion, ok := os.LookupEnv("DAPR_RUNTIME_PINNED_VERSION") |
| 52 | + if !ok { |
| 53 | + return fmt.Errorf("env var DAPR_RUNTIME_PINNED_VERSION not set") |
| 54 | + } |
| 55 | + daprDashboardVersion, ok := os.LookupEnv("DAPR_DASHBOARD_PINNED_VERSION") |
| 56 | + if !ok { |
| 57 | + return fmt.Errorf("env var DAPR_DASHBOARD_PINNED_VERSION not set") |
| 58 | + } |
| 59 | + |
| 60 | + if !isSlimMode() { |
| 61 | + if err := waitForPortsFreeDirect(60*time.Second, 58080, 58081, 50005); err != nil { |
| 62 | + return fmt.Errorf("waiting for container ports: %w", err) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + args := []string{ |
| 67 | + "--runtime-version", daprRuntimeVersion, |
| 68 | + "--dashboard-version", daprDashboardVersion, |
| 69 | + } |
| 70 | + output, err := cmdInit(args...) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("dapr init: %s: %w", output, err) |
| 73 | + } |
| 74 | + |
| 75 | + if isSlimMode() { |
| 76 | + homeDir, err := os.UserHomeDir() |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("getting home dir: %w", err) |
| 79 | + } |
| 80 | + if err := createSlimComponents(filepath.Join(homeDir, ".dapr", "components")); err != nil { |
| 81 | + return fmt.Errorf("creating slim components: %w", err) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// waitForPortsFreeDirect is a non-test variant of waitForPortsFree for |
| 89 | +// use in TestMain where *testing.T is not available. |
| 90 | +func waitForPortsFreeDirect(timeout time.Duration, ports ...int) error { |
| 91 | + deadline := time.Now().Add(timeout) |
| 92 | + for time.Now().Before(deadline) { |
| 93 | + allFree := true |
| 94 | + for _, port := range ports { |
| 95 | + ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port)) |
| 96 | + if err != nil { |
| 97 | + allFree = false |
| 98 | + break |
| 99 | + } |
| 100 | + ln.Close() |
| 101 | + } |
| 102 | + if allFree { |
| 103 | + return nil |
| 104 | + } |
| 105 | + time.Sleep(time.Second) |
| 106 | + } |
| 107 | + return fmt.Errorf("ports %v not free within %v", ports, timeout) |
| 108 | +} |
0 commit comments