risingwave_license/
cpu.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Tests below only work in debug mode.
16#[cfg(debug_assertions)]
17#[cfg(test)]
18mod tests {
19    use expect_test::expect;
20    use thiserror_ext::AsReport as _;
21
22    use crate::{Feature, LicenseKey, LicenseManager, TEST_PAID_LICENSE_KEY_CONTENT};
23
24    fn do_test(key: &str, cpu_core_count: usize, expect: expect_test::Expect) {
25        let manager = LicenseManager::new();
26        manager.refresh(LicenseKey(key));
27        manager.update_cpu_core_count(cpu_core_count);
28
29        match Feature::TestPaid.check_available_with(&manager) {
30            Ok(_) => expect.assert_eq("ok"),
31            Err(error) => expect.assert_eq(&error.to_report_string()),
32        }
33    }
34
35    #[test]
36    fn test_no_limit() {
37        do_test(TEST_PAID_LICENSE_KEY_CONTENT, 114514, expect!["ok"]);
38    }
39
40    #[test]
41    fn test_invalid_license_key() {
42        const KEY: &str = "invalid";
43
44        do_test(
45            KEY,
46            0,
47            expect![
48                "feature TestPaid is not available due to license error: invalid license key: InvalidToken"
49            ],
50        );
51        do_test(
52            KEY,
53            114514,
54            expect![
55                "feature TestPaid is not available due to license error: invalid license key: InvalidToken"
56            ],
57        );
58    }
59
60    #[test]
61    fn test_limit() {
62        const KEY: &str = "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.\
63          eyJzdWIiOiJwYWlkLXRlc3QtMzIiLCJpc3MiOiJ0ZXN0LnJpc2luZ3dhdmUuY29tIiwidGllciI6InBhaWQiLCJleHAiOjIxNTA0OTU5OTksImlhdCI6MTczNzYxMjQ5NSwiY3B1X2NvcmVfbGltaXQiOjMyfQ.\
64          SQpX2Dmon5Mb04VUbHyxsU7owJhcdLZHqUefxAXBwG5AqgKdpfS0XUePW5E4D-EfxtH_cWJiD4QDFsfdRUz88g_n_KvfNUObMW7NV5TUoRs_ImtS4ySugExNX3JzJi71QqgI8kugStQ7uOR9kZ_C-cCc_IG2CwwEmhhW1Ij0vX7qjhG5JNMit_bhxPY7Rh27ppgPTqWxJFTTsw-9B7O5WR_yIlaDjxVzk0ALm_j6DPB249gG3dkeK0rP0AK_ip2cK6iQdy8Cge7ATD6yUh4c_aR6GILDF6-vyB7QdWU6DdQS4KhdkPNWoe_Z9psotcXQJ7NhQ39hk8tdLzmTfGDDBA";
65
66        do_test(KEY, 31, expect!["ok"]);
67        do_test(KEY, 32, expect!["ok"]);
68        do_test(
69            KEY,
70            33,
71            expect![
72                "feature TestPaid is not available due to license error: the license key is currently not effective because the CPU core in the cluster (33) exceeds the maximum allowed by the license key (32); consider removing some nodes or acquiring a new license key with a higher limit"
73            ],
74        );
75    }
76}