|
| 1 | +package updateengine_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + dbus "github.com/godbus/dbus/v5" |
| 9 | + |
| 10 | + "github.com/kinvolk/flatcar-linux-update-operator/pkg/updateengine" |
| 11 | +) |
| 12 | + |
| 13 | +//nolint:paralleltest // Test uses environment variables, which are global. |
| 14 | +func Test_Connecting_to_non_existing_system_bus_fails(t *testing.T) { |
| 15 | + if err := os.Setenv("DBUS_SYSTEM_BUS_ADDRESS", "foo"); err != nil { |
| 16 | + t.Fatalf("Setting systemd bus address: %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + if _, err := updateengine.New(); err == nil { |
| 20 | + t.Fatalf("Creating client should fail when unable to connect to system bus") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +//nolint:funlen,tparallel // Test uses environment variables, which are global. |
| 25 | +func Test_Emitted_status_parses(t *testing.T) { |
| 26 | + var ( |
| 27 | + lastCheckedTime int64 = 10 |
| 28 | + progress float64 = 20 |
| 29 | + currentOperation = updateengine.UpdateStatusVerifying |
| 30 | + newVersion = "1.2.3" |
| 31 | + newSize int64 = 30 |
| 32 | + ) |
| 33 | + |
| 34 | + withMockGetStatus(t, func(message dbus.Message) (int64, float64, string, string, int64, *dbus.Error) { |
| 35 | + return lastCheckedTime, progress, currentOperation, newVersion, newSize, nil |
| 36 | + }) |
| 37 | + |
| 38 | + c, err := updateengine.New() |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("Creating client should succeed, got: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + stop := make(chan struct{}) |
| 44 | + ch := make(chan updateengine.Status, 1) |
| 45 | + |
| 46 | + go c.ReceiveStatuses(ch, stop) |
| 47 | + |
| 48 | + status := <-ch |
| 49 | + |
| 50 | + t.Run("first_value_as_last_checked_time", func(t *testing.T) { |
| 51 | + t.Parallel() |
| 52 | + |
| 53 | + if status.LastCheckedTime != lastCheckedTime { |
| 54 | + t.Errorf("Expected %v, got %v", lastCheckedTime, status.LastCheckedTime) |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("second_value_as_progress", func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + if status.Progress != progress { |
| 62 | + t.Errorf("Expected %v, got %v", progress, status.Progress) |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("third_value_as_current_operation", func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + |
| 69 | + if status.CurrentOperation != currentOperation { |
| 70 | + t.Errorf("Expected %q, got %q", currentOperation, status.CurrentOperation) |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("forth_value_as_new_version", func(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + |
| 77 | + if status.NewVersion != newVersion { |
| 78 | + t.Errorf("Expected %q, got %q", newVersion, status.NewVersion) |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("fifth_value_as_new_size", func(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + |
| 85 | + if status.NewSize != newSize { |
| 86 | + t.Errorf("Expected %v, got %v", newSize, status.NewSize) |
| 87 | + } |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +const ( |
| 92 | + testDbusSocketEnv = "FLUO_TEST_DBUS_SOCKET" |
| 93 | +) |
| 94 | + |
| 95 | +func testSystemConnection(t *testing.T) *dbus.Conn { |
| 96 | + t.Helper() |
| 97 | + |
| 98 | + socket := os.Getenv(testDbusSocketEnv) |
| 99 | + if socket == "" { |
| 100 | + t.Skipf("%q environment variable empty", testDbusSocketEnv) |
| 101 | + } |
| 102 | + |
| 103 | + if err := os.Setenv("DBUS_SYSTEM_BUS_ADDRESS", fmt.Sprintf("unix:path=%s", socket)); err != nil { |
| 104 | + t.Fatalf("Setting systemd bus address: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + conn, err := dbus.SystemBus() |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("Opening private connection to system bus: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + return conn |
| 113 | +} |
| 114 | + |
| 115 | +func withMockGetStatus(t *testing.T, fn interface{}) { |
| 116 | + t.Helper() |
| 117 | + |
| 118 | + conn := testSystemConnection(t) |
| 119 | + |
| 120 | + if _, err := conn.RequestName("com.coreos.update1", 0); err != nil { |
| 121 | + t.Fatalf("Requesting name: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + tbl := map[string]interface{}{ |
| 125 | + "GetStatus": fn, |
| 126 | + } |
| 127 | + |
| 128 | + if err := conn.ExportMethodTable(tbl, "/com/coreos/update1", "com.coreos.update1.Manager"); err != nil { |
| 129 | + t.Fatalf("Exporting method table: %v", err) |
| 130 | + } |
| 131 | +} |
0 commit comments