settings/storage_migrations/
v1653667208_light_migration.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::light::types::LightInfo;
use crate::migration::{FileGenerator, Migration, MigrationError};
use anyhow::{anyhow, Context};
use fidl::endpoints::create_proxy;
use fidl::persist;
use fidl_fuchsia_io::FileProxy;
use fidl_fuchsia_settings::LightGroup as LightGroupFidl;
use fidl_fuchsia_settings_storage::LightGroups;
use fidl_fuchsia_stash::{StoreProxy, Value};

use settings_storage::fidl_storage::FidlStorageConvertible;

const LIGHT_KEY: &str = "settings_light_info";

/// Migrates Light settings data from stash and stores it in persistent fidl-serialized files.
pub(crate) struct V1653667208LightMigration(pub(crate) StoreProxy);

#[async_trait::async_trait(?Send)]
impl Migration for V1653667208LightMigration {
    fn id(&self) -> u64 {
        1653667208
    }

    async fn migrate(&self, file_generator: FileGenerator) -> Result<(), MigrationError> {
        let (stash_proxy, server_end) = create_proxy();
        self.0.create_accessor(true, server_end).expect("failed to create accessor for stash");
        let value = stash_proxy.get_value(LIGHT_KEY).await.context("failed to call get_value")?;
        let str_json = match value {
            None => return Err(MigrationError::NoData),
            Some(value) => {
                if let Value::Stringval(str_json) = *value {
                    str_json
                } else {
                    return Err(MigrationError::Unrecoverable(anyhow!("data in incorrect format")));
                }
            }
        };
        let light_data = serde_json::from_str::<LightInfo>(&str_json)
            .context("failed to deserialize light data")?;

        let light_groups = LightGroups {
            groups: light_data
                .light_groups
                .into_values()
                .map(LightGroupFidl::from)
                .collect::<Vec<_>>(),
        };
        let file: FileProxy = file_generator
            .new_file(<LightInfo as FidlStorageConvertible>::KEY)
            .await
            .map_err(|file_error| match file_error {
                fuchsia_fs::node::OpenError::OpenError(status)
                    if status == zx::Status::NO_SPACE =>
                {
                    MigrationError::DiskFull
                }
                _ => Err::<(), _>(file_error)
                    .context("unable to open new_file: {:?}")
                    .unwrap_err()
                    .into(),
            })?;
        let encoded = persist(&light_groups).context("failed to serialize new fidl format")?;
        let _ = file.write(&encoded).await.context("file to call write")?.map_err(|e| {
            let status = zx::Status::from_raw(e);
            if status == zx::Status::NO_SPACE {
                MigrationError::DiskFull
            } else {
                MigrationError::Unrecoverable(anyhow!("failed to write migration: {:?}", e))
            }
        })?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage_migrations::tests::open_tempdir;
    use assert_matches::assert_matches;
    use fidl_fuchsia_settings::{LightGroup, LightState, LightType, LightValue};
    use fidl_fuchsia_stash::{StoreAccessorRequest, StoreMarker, StoreRequest};
    use fidl_fuchsia_ui_types::ColorRgb;
    use fuchsia_async as fasync;
    use futures::StreamExt;

    // Ensure that failing to get data from stash (i.e. not out of space or not found) results in
    // an unrecoverable error.
    #[fuchsia::test]
    async fn v1653667208_light_migration_error_getting_value() {
        let (store_proxy, server_end) = create_proxy::<StoreMarker>();
        let mut request_stream = server_end.into_stream();
        let task = fasync::Task::local(async move {
            let mut tasks = vec![];
            while let Some(Ok(request)) = request_stream.next().await {
                if let StoreRequest::CreateAccessor { accessor_request, .. } = request {
                    let mut request_stream = accessor_request.into_stream();
                    tasks.push(fasync::Task::local(async move {
                        while let Some(Ok(request)) = request_stream.next().await {
                            if let StoreAccessorRequest::GetValue { responder, .. } = request {
                                // Just ignore the responder so the client receives an error.
                                drop(responder);
                            } else {
                                panic!("unexpected request: {request:?}");
                            }
                        }
                    }))
                }
            }
            for task in tasks {
                task.await
            }
        });

        let migration = V1653667208LightMigration(store_proxy);
        let fs = tempfile::tempdir().expect("failed to create tempdir");
        let directory = open_tempdir(&fs);
        let file_generator = FileGenerator::new(0, migration.id(), Clone::clone(&directory));
        let result = migration.migrate(file_generator).await;
        assert_matches!(result, Err(MigrationError::Unrecoverable(_)));
        let err = result.unwrap_err();
        assert!(format!("{err:?}").contains("failed to call get_value"));

        drop(migration);

        task.await;
    }

    // Ensure we see that no data is available when the data is not in stash.
    #[fuchsia::test]
    async fn v1653667208_light_migration_no_data() {
        let (store_proxy, server_end) = create_proxy::<StoreMarker>();
        let mut request_stream = server_end.into_stream();
        let task = fasync::Task::local(async move {
            let mut tasks = vec![];
            while let Some(Ok(request)) = request_stream.next().await {
                if let StoreRequest::CreateAccessor { accessor_request, .. } = request {
                    let mut request_stream = accessor_request.into_stream();
                    tasks.push(fasync::Task::local(async move {
                        while let Some(Ok(request)) = request_stream.next().await {
                            if let StoreAccessorRequest::GetValue { responder, .. } = request {
                                responder.send(None).expect("should be able to send response");
                            } else {
                                panic!("unexpected request: {request:?}");
                            }
                        }
                    }))
                }
            }
            for task in tasks {
                task.await
            }
        });

        let migration = V1653667208LightMigration(store_proxy);
        let fs = tempfile::tempdir().expect("failed to create tempdir");
        let directory = open_tempdir(&fs);
        let file_generator = FileGenerator::new(0, migration.id(), Clone::clone(&directory));
        assert_matches!(migration.migrate(file_generator).await, Err(MigrationError::NoData));

        drop(migration);

        task.await;
    }

    // Ensure we can properly migrate the original json data in stash over to persistent fidl.
    #[fuchsia::test]
    async fn v1653667208_light_migration_test() {
        let (store_proxy, server_end) = create_proxy::<StoreMarker>();
        let mut request_stream = server_end.into_stream();
        let task = fasync::Task::local(async move {
            let mut tasks = vec![];
            while let Some(Ok(request)) = request_stream.next().await {
                if let StoreRequest::CreateAccessor { read_only, accessor_request, .. } = request {
                    assert!(read_only);
                    let mut request_stream = accessor_request.into_stream();
                    tasks.push(fasync::Task::local(async move {
                        while let Some(Ok(request)) = request_stream.next().await {
                            if let StoreAccessorRequest::GetValue { key, responder } = request {
                                assert_eq!(key, LIGHT_KEY);
                                responder
                                    .send(Some(Value::Stringval(
                                        r#"{
                                            "light_groups":{
                                                "abc":{
                                                    "name":"abc",
                                                    "enabled":false,
                                                    "light_type":"Brightness",
                                                    "lights":[{
                                                        "value": {
                                                            "Brightness": 0.5
                                                        }
                                                    }],
                                                    "hardware_index":[],
                                                    "disable_conditions":[]
                                                },
                                                "def":{
                                                    "name":"def",
                                                    "enabled":true,
                                                    "light_type":"Rgb",
                                                    "lights":[{
                                                        "value": {
                                                            "Rgb": {
                                                                "red": 1.0,
                                                                "green": 0.5,
                                                                "blue": 0.0
                                                            }
                                                        }
                                                    }],
                                                    "hardware_index":[1, 2, 3],
                                                    "disable_conditions":[]
                                                },
                                                "ghi":{
                                                    "name":"ghi",
                                                    "enabled":false,
                                                    "light_type":"Simple",
                                                    "lights":[{
                                                        "value": {
                                                            "Simple": true
                                                        }
                                                    }],
                                                    "hardware_index":[1],
                                                    "disable_conditions":[]
                                                },
                                                "jkl":{
                                                    "name":"jkl",
                                                    "enabled":false,
                                                    "light_type":"Simple",
                                                    "lights":[{
                                                        "value": null
                                                    }],
                                                    "hardware_index":[1],
                                                    "disable_conditions":["MicSwitch"]
                                                }
                                            }
                                        }"#
                                        .to_owned(),
                                    )))
                                    .expect("should be able to respond");
                            } else {
                                panic!("unexpected request: {request:?}");
                            }
                        }
                    }))
                }
            }
            for task in tasks {
                task.await
            }
        });

        let migration = V1653667208LightMigration(store_proxy);
        let fs = tempfile::tempdir().expect("failed to create tempdir");
        let directory = open_tempdir(&fs);
        let file_generator = FileGenerator::new(0, migration.id(), Clone::clone(&directory));
        assert_matches!(migration.migrate(file_generator).await, Ok(()));

        let file = fuchsia_fs::directory::open_file(
            &directory,
            "light_info_1653667208.pfidl",
            fuchsia_fs::PERM_READABLE,
        )
        .await
        .expect("file should exist");
        let LightGroups { groups } = fuchsia_fs::file::read_fidl::<LightGroups>(&file)
            .await
            .expect("should be able to read and deserialize light groups");
        assert_eq!(groups.len(), 4);
        assert!(groups.contains(&LightGroup {
            name: Some("abc".to_owned()),
            enabled: Some(false),
            type_: Some(LightType::Brightness),
            lights: Some(vec![LightState {
                value: Some(LightValue::Brightness(0.5)),
                ..Default::default()
            }]),
            ..Default::default()
        }));
        assert!(groups.contains(&LightGroup {
            name: Some("def".to_owned()),
            enabled: Some(true),
            type_: Some(LightType::Rgb),
            lights: Some(vec![LightState {
                value: Some(LightValue::Color(ColorRgb { red: 1.0, green: 0.5, blue: 0.0 })),
                ..Default::default()
            }]),
            ..Default::default()
        }));
        assert!(groups.contains(&LightGroup {
            name: Some("ghi".to_owned()),
            enabled: Some(false),
            type_: Some(LightType::Simple),
            lights: Some(vec![LightState {
                value: Some(LightValue::On(true)),
                ..Default::default()
            }]),
            ..Default::default()
        }));
        assert!(groups.contains(&LightGroup {
            name: Some("jkl".to_owned()),
            enabled: Some(false),
            type_: Some(LightType::Simple),
            lights: Some(vec![LightState { value: None, ..Default::default() }]),
            ..Default::default()
        }));

        drop(migration);

        task.await;
    }
}