-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtarget_release.rs
187 lines (171 loc) · 6.11 KB
/
target_release.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Get/set the target release via the external API.
use anyhow::Result;
use camino::Utf8Path;
use camino_tempfile::Utf8TempDir;
use chrono::Utc;
use clap::Parser as _;
use dropshot::test_util::{ClientTestContext, LogContext};
use http::StatusCode;
use http::method::Method;
use nexus_config::UpdatesConfig;
use nexus_test_utils::http_testing::AuthnMode;
use nexus_test_utils::http_testing::{NexusRequest, RequestBuilder};
use nexus_test_utils::load_test_config;
use nexus_test_utils::test_setup_with_config;
use nexus_types::external_api::params::SetTargetReleaseParams;
use nexus_types::external_api::views::{TargetRelease, TargetReleaseSource};
use omicron_common::api::external::TufRepoInsertResponse;
use omicron_sled_agent::sim;
use semver::Version;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_set_target_release() -> Result<()> {
let mut config = load_test_config();
config.pkg.updates = Some(UpdatesConfig {
// XXX: This is currently not used by the update system, but
// trusted_root will become meaningful in the future.
trusted_root: "does-not-exist.json".into(),
});
let ctx = test_setup_with_config::<omicron_nexus::Server>(
"test_update_uninitialized",
&mut config,
sim::SimMode::Explicit,
None,
0,
)
.await;
let client = &ctx.external_client;
let logctx = LogContext::new("get_set_target_release", &config.pkg.log);
// There should always be a target release.
let target_release: TargetRelease =
NexusRequest::object_get(client, "/v1/system/update/target-release")
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.unwrap()
.parsed_body()
.unwrap();
assert_eq!(target_release.generation, 1);
assert!(target_release.time_requested < Utc::now());
assert_eq!(target_release.release_source, TargetReleaseSource::Unspecified);
// Attempting to set an invalid system version should fail.
let system_version = Version::new(0, 0, 0);
NexusRequest::object_put(
client,
"/v1/system/update/target-release",
Some(&SetTargetReleaseParams { system_version }),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect_err("invalid TUF repo");
let temp = Utf8TempDir::new().unwrap();
// Adding a fake (tufaceous) repo and then setting it as the
// target release should succeed.
{
let before = Utc::now();
let system_version = Version::new(1, 0, 0);
let path = temp.path().join("repo-1.0.0.zip");
tufaceous::Args::try_parse_from([
"tufaceous",
"assemble",
"../update-common/manifests/fake.toml",
path.as_str(),
])
.expect("can't parse tufaceous args")
.exec(&logctx.log)
.await
.expect("can't assemble TUF repo");
assert_eq!(
system_version,
upload_tuf_repo(client, &path).await?.recorded.repo.system_version
);
let target_release =
set_target_release(client, system_version.clone()).await?;
let after = Utc::now();
assert_eq!(target_release.generation, 2);
assert!(target_release.time_requested >= before);
assert!(target_release.time_requested <= after);
assert_eq!(
target_release.release_source,
TargetReleaseSource::SystemVersion { version: system_version },
);
}
// Adding a repo with non-semver artifact versions should be ok, too.
{
let before = Utc::now();
let system_version = Version::new(2, 0, 0);
let path = temp.path().join("repo-2.0.0.zip");
tufaceous::Args::try_parse_from([
"tufaceous",
"assemble",
"../update-common/manifests/fake-non-semver.toml",
"--allow-non-semver",
path.as_str(),
])
.expect("can't parse tufaceous args")
.exec(&logctx.log)
.await
.expect("can't assemble TUF repo");
assert_eq!(
system_version,
upload_tuf_repo(client, &path).await?.recorded.repo.system_version
);
let target_release =
set_target_release(client, system_version.clone()).await?;
let after = Utc::now();
assert_eq!(target_release.generation, 3);
assert!(target_release.time_requested >= before);
assert!(target_release.time_requested <= after);
assert_eq!(
target_release.release_source,
TargetReleaseSource::SystemVersion { version: system_version },
);
}
// Attempting to downgrade to an earlier system version (2.0.0 → 1.0.0)
// should not be allowed.
set_target_release(client, Version::new(1, 0, 0))
.await
.expect_err("shouldn't be able to downgrade system");
ctx.teardown().await;
logctx.cleanup_successful();
Ok(())
}
async fn upload_tuf_repo(
client: &ClientTestContext,
path: &Utf8Path,
) -> Result<TufRepoInsertResponse> {
NexusRequest::new(
RequestBuilder::new(
client,
http::Method::PUT,
"/v1/system/update/repository?file_name=/tmp/foo.zip",
)
.body_file(Some(path))
.expect_status(Some(StatusCode::OK)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.map(|response| response.parsed_body().unwrap())
}
async fn set_target_release(
client: &ClientTestContext,
system_version: Version,
) -> Result<TargetRelease> {
NexusRequest::new(
RequestBuilder::new(
client,
Method::PUT,
"/v1/system/update/target-release",
)
.body(Some(&SetTargetReleaseParams { system_version }))
.expect_status(Some(StatusCode::CREATED)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.map(|response| response.parsed_body().unwrap())
}