channel_config/
lib.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
// 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 omaha_client::protocol::Cohort;
use serde::{Deserialize, Serialize};
use std::io;

/// Wrapper for deserializing repository configs to the on-disk JSON format.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ChannelConfigs {
    pub default_channel: Option<String>,
    #[serde(rename = "channels")]
    pub known_channels: Vec<ChannelConfig>,
}

impl ChannelConfigs {
    pub fn validate(&self) -> Result<(), io::Error> {
        let names: Vec<&str> = self.known_channels.iter().map(|c| c.name.as_str()).collect();
        if !names.iter().all(|n| Cohort::validate_name(n)) {
            return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid channel name"));
        }
        if let Some(default) = &self.default_channel {
            if !names.contains(&default.as_str()) {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "default channel not a known channel",
                ));
            }
        }
        Ok(())
    }

    pub fn get_default_channel(&self) -> Option<ChannelConfig> {
        self.default_channel.as_ref().and_then(|default| self.get_channel(default))
    }

    pub fn get_channel(&self, name: &str) -> Option<ChannelConfig> {
        self.known_channels.iter().find(|channel_config| channel_config.name == name).cloned()
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ChannelConfig {
    pub name: String,
    pub repo: String,
    pub appid: Option<String>,
    pub check_interval_secs: Option<u64>,
}

impl ChannelConfig {
    pub fn new_for_test(name: &str) -> Self {
        testing::ChannelConfigBuilder::new(name, name.to_owned() + "-repo").build()
    }

    pub fn with_appid_for_test(name: &str, appid: &str) -> Self {
        testing::ChannelConfigBuilder::new(name, name.to_owned() + "-repo").appid(appid).build()
    }
}

pub mod testing {
    use super::*;
    #[derive(Debug, Default)]
    pub struct ChannelConfigBuilder {
        name: String,
        repo: String,
        appid: Option<String>,
        check_interval_secs: Option<u64>,
    }

    impl ChannelConfigBuilder {
        pub fn new(name: impl Into<String>, repo: impl Into<String>) -> Self {
            ChannelConfigBuilder {
                name: name.into(),
                repo: repo.into(),
                ..ChannelConfigBuilder::default()
            }
        }

        pub fn appid(mut self, appid: impl Into<String>) -> Self {
            self.appid = Some(appid.into());
            self
        }

        pub fn check_interval_secs(mut self, check_interval_secs: u64) -> Self {
            self.check_interval_secs = Some(check_interval_secs);
            self
        }

        pub fn build(self) -> ChannelConfig {
            ChannelConfig {
                name: self.name,
                repo: self.repo,
                appid: self.appid,
                check_interval_secs: self.check_interval_secs,
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_channel_configs_get_default() {
        let configs = ChannelConfigs {
            default_channel: Some("default_channel".to_string()),
            known_channels: vec![
                ChannelConfig::new_for_test("some_channel"),
                ChannelConfig::new_for_test("default_channel"),
                ChannelConfig::new_for_test("other"),
            ],
        };
        assert_eq!(configs.get_default_channel().unwrap(), configs.known_channels[1]);
    }

    #[test]
    fn test_channel_configs_get_default_none() {
        let configs = ChannelConfigs {
            default_channel: None,
            known_channels: vec![ChannelConfig::new_for_test("some_channel")],
        };
        assert_eq!(configs.get_default_channel(), None);
    }

    #[test]
    fn test_channel_configs_get_channel() {
        let configs = ChannelConfigs {
            default_channel: Some("default_channel".to_string()),
            known_channels: vec![
                ChannelConfig::new_for_test("some_channel"),
                ChannelConfig::new_for_test("default_channel"),
                ChannelConfig::new_for_test("other"),
            ],
        };
        assert_eq!(configs.get_channel("other").unwrap(), configs.known_channels[2]);
    }

    #[test]
    fn test_channel_configs_get_channel_missing() {
        let configs = ChannelConfigs {
            default_channel: Some("default_channel".to_string()),
            known_channels: vec![
                ChannelConfig::new_for_test("some_channel"),
                ChannelConfig::new_for_test("default_channel"),
                ChannelConfig::new_for_test("other"),
            ],
        };
        assert_eq!(configs.get_channel("missing"), None);
    }

    #[test]
    fn test_channel_cfg_builder_app_id() {
        let config = testing::ChannelConfigBuilder::new("name", "repo").appid("appid").build();
        assert_eq!("name", config.name);
        assert_eq!("repo", config.repo);
        assert_eq!(Some("appid".to_owned()), config.appid);
        assert_eq!(None, config.check_interval_secs);
    }

    #[test]
    fn test_channel_cfg_builder_check_interval() {
        let config = testing::ChannelConfigBuilder::new("name", "repo")
            .appid("appid")
            .check_interval_secs(3600)
            .build();
        assert_eq!("name", config.name);
        assert_eq!("repo", config.repo);
        assert_eq!(Some("appid".to_owned()), config.appid);
        assert_eq!(Some(3600), config.check_interval_secs);
    }
}